Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ed9d0d47259d5f85ed67ce89daf949d07f2c3665 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*****************************************************************************
 * Copyright (c) 2011, 2013 CEA LIST and others.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA LIST) - Consolidate all hyperlink helper contributions into one tab
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.hyperlink.util;

import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.hyperlink.Activator;
import org.eclipse.papyrus.infra.hyperlink.helper.AbstractHyperLinkHelper;
import org.eclipse.papyrus.infra.hyperlink.messages.Messages;
import org.eclipse.papyrus.infra.hyperlink.ui.AbstractHyperLinkTab;
import org.eclipse.papyrus.infra.hyperlink.ui.HyperLinkTab;

/**
 *
 * This class creates all the tabs for the HyperLink Shell
 *
 */
public class HyperLinkTabsRegistrationUtil {

	/**
	 * The id to get the registered hyperlink tab
	 */
	public static final String HYPERLINK_TAB_REGISTRATION_ID = "org.eclipse.papyrus.infra.hyperlink.tab.registration"; //$NON-NLS-1$

	/**
	 * The 3 parameters for of the tab extension point
	 */
	public static final String TAB = "tab"; //$NON-NLS-1$

	public static final String POSITION = "position"; //$NON-NLS-1$

	public static final String TAB_ID = "tabId"; //$NON-NLS-1$

	/** The ID of the tab that shows helper-based contributions. */
	private static final String GENERIC_TAB_ID = "org.eclipse.papyrus.infra.hyperlink.helpers"; //$NON-NLS-1$

	/** the instance of HyperLinkTabsRegistrationUtil */
	public static final HyperLinkTabsRegistrationUtil INSTANCE = new HyperLinkTabsRegistrationUtil();

	/**
	 *
	 * Constructor.
	 *
	 */
	private HyperLinkTabsRegistrationUtil() {
		// nothing to do
	}

	/**
	 *
	 * @return
	 *         the tabs for the HyperLink Shell, sorted by position
	 */
	public Collection<AbstractHyperLinkTab> getAllHyperLinkTab() {
		// should not be a global variable, because we should create the tabs each time!
		final Map<Integer, AbstractHyperLinkTab> allTabs;
		allTabs = new TreeMap<Integer, AbstractHyperLinkTab>();
		IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(HYPERLINK_TAB_REGISTRATION_ID);
		for (IConfigurationElement e : config) {
			AbstractHyperLinkTab tab = null;
			try {
				final Object h = e.createExecutableExtension(TAB);
				if (h instanceof AbstractHyperLinkTab) {
					tab = (AbstractHyperLinkTab) h;
					final String o = e.getAttribute(POSITION);
					final Integer position = Integer.valueOf(o);
					final String tabId = e.getAttribute(TAB_ID);
					tab.setTabId(tabId);
					allTabs.put(position, tab);
				} else {
					Activator.log.info(NLS.bind(Messages.HyperLinkTabRegistrationUtil_NotAnInstanceOf, new Object[] { h, AbstractHyperLinkTab.class }));
				}
			} catch (NumberFormatException ex) {
				Activator.log.error(NLS.bind(Messages.HyperLinkTabRegistrationUtil_tabWillBeIgnored, tab), ex);
			} catch (CoreException ex) {
				Activator.log.error(Messages.HyperLinkTabRegistrationUtil_ICantCreateTheTab, ex);
			}
		}

		// we create the tab for the helpers
		Map<Integer, AbstractHyperLinkHelper> helpers = HyperLinkHelpersRegistrationUtil.INSTANCE.getHelperWithPosition();
		if (!helpers.isEmpty()) {
			// use the least position requested by the helpers as the tab position
			allTabs.put(helpers.keySet().iterator().next(), new HyperLinkTab(GENERIC_TAB_ID, helpers.values()));
		}

		return allTabs.values();
	}
}

Back to the top