| /******************************************************************************* |
| * Copyright (c) 2009, 2017 xored software, Inc. and others. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * xored software, Inc. - initial API and Implementation (Alex Panchenko) |
| *******************************************************************************/ |
| package org.eclipse.dltk.console.ui.internal; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.List; |
| |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IConfigurationElement; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.ui.console.IConsoleConstants; |
| |
| /** |
| * @since 2.0 |
| */ |
| public class ConsoleViewManager { |
| |
| public static class Descriptor { |
| private final String viewId; |
| private final int priority; |
| |
| public Descriptor(String viewId, int priority) { |
| this.viewId = viewId; |
| this.priority = priority; |
| } |
| |
| public Descriptor(IConfigurationElement element) { |
| this.viewId = element.getAttribute("viewId"); //$NON-NLS-1$ |
| this.priority = parseInt(element.getAttribute("priority")); //$NON-NLS-1$ |
| } |
| |
| protected static int parseInt(String value) { |
| try { |
| return Integer.parseInt(value); |
| } catch (NumberFormatException e) { |
| return 0; |
| } |
| } |
| |
| public String getViewId() { |
| return viewId; |
| } |
| } |
| |
| /** |
| * @param extensionPoint |
| * @param elementType |
| */ |
| public ConsoleViewManager(String extensionPoint) { |
| extensions = new ArrayList<>(5); |
| extensions.add(new Descriptor(IConsoleConstants.ID_CONSOLE_VIEW, 0)); |
| for (final IConfigurationElement element : Platform.getExtensionRegistry() |
| .getConfigurationElementsFor(extensionPoint)) { |
| final Descriptor descriptor = new Descriptor(element); |
| if (isValidDescriptor(descriptor)) { |
| extensions.add(descriptor); |
| } |
| } |
| Collections.sort(extensions, (arg0, arg1) -> arg1.priority - arg0.priority); |
| } |
| |
| // Contains list of descriptors. |
| private List<Descriptor> extensions; |
| |
| /** |
| * Return array of descriptors. If there are no contributed instances the |
| * empty array is returned. |
| * |
| * @param natureId |
| * @return |
| * @throws CoreException |
| */ |
| public Descriptor[] getDescriptors() { |
| return extensions.toArray(new Descriptor[extensions.size()]); |
| } |
| |
| public Descriptor getFirst() { |
| return extensions.get(0); |
| } |
| |
| protected boolean isValidDescriptor(Descriptor descriptor) { |
| return descriptor != null; |
| } |
| |
| } |