Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e8410e3f6e16eaaa74429de8eff40219dee50aa (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 * 
 * 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:
 *  Tatiana Fesenko (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.wizards.category;

import static org.eclipse.papyrus.uml.diagram.wizards.Activator.log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.infra.core.extension.BadClassNameException;
import org.eclipse.papyrus.infra.core.extension.ExtensionUtils;
import org.eclipse.papyrus.infra.core.extension.commands.IModelCreationCommand;
import org.eclipse.papyrus.uml.diagram.wizards.Messages;
import org.eclipse.ui.plugin.AbstractUIPlugin;

/**
 * The Class DiagramCategoryRegistry reads available DiagramCategories from plugin descriptors.
 */
public class DiagramCategoryRegistry extends ExtensionUtils {

	/** The Constant CATEGORY_EXTENSION_POINT_NAME. */
	private static final String CATEGORY_EXTENSION_POINT_NAME = "org.eclipse.papyrus.infra.core.papyrusDiagram"; //$NON-NLS-1$

	/** ID of the editor extension (schema filename). */
	public static final String CATEGORY_ELEMENT_NAME = "diagramCategory"; //$NON-NLS-1$

	/** The Constant CATEGORY_ID. */
	private static final String CATEGORY_ID = "id"; //$NON-NLS-1$

	/** The Constant CATEGORY_LABEL. */
	private static final String CATEGORY_LABEL = "label"; //$NON-NLS-1$

	/** The Constant CATEGORY_DESCRIPTION. */
	private static final String CATEGORY_DESCRIPTION = "description"; //$NON-NLS-1$

	/** The Constant CATEGORY_ICON. */
	private static final String CATEGORY_ICON = "icon"; //$NON-NLS-1$

	/** The Constant CATEGORY_CLASS. */
	private static final String CATEGORY_CLASS = "class"; //$NON-NLS-1$

	/** The Constant CATEGORY_FILE_EXTENSION_PREFIX. */
	private static final String CATEGORY_FILE_EXTENSION_PREFIX = "extensionPrefix"; //$NON-NLS-1$

	/** The diagram categories. */
	private static Map<String, DiagramCategoryDescriptor> diagramCategories;

	/** The our instance. */
	private static DiagramCategoryRegistry ourInstance;

	/**
	 * Instantiates a new diagram category registry.
	 */
	private DiagramCategoryRegistry() {
	}

	/**
	 * Gets the single instance of DiagramCategoryRegistry.
	 * 
	 * @return single instance of DiagramCategoryRegistry
	 */
	public static DiagramCategoryRegistry getInstance() {
		if(ourInstance == null) {
			ourInstance = new DiagramCategoryRegistry();
		}
		return ourInstance;
	}

	/**
	 * Gets the diagram categories.
	 * 
	 * @return the diagram categories
	 */
	public List<DiagramCategoryDescriptor> getDiagramCategories() {
		if(diagramCategories == null) {
			diagramCategories = buildDiagramCategories();
		}
		return new ArrayList<DiagramCategoryDescriptor>(diagramCategories.values());
	}

	/**
	 * Gets the diagram category map.
	 *
	 * @return the diagram category map
	 */
	public Map<String, DiagramCategoryDescriptor> getDiagramCategoryMap() {
		if(diagramCategories == null) {
			diagramCategories = buildDiagramCategories();
		}
		return diagramCategories;
	}

	/**
	 * Builds the diagram categories.
	 * 
	 * @return the hash map
	 */
	public Map<String, DiagramCategoryDescriptor> buildDiagramCategories() {
		Map<String, DiagramCategoryDescriptor> result = new HashMap<String, DiagramCategoryDescriptor>();

		IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(CATEGORY_EXTENSION_POINT_NAME);
		for(IExtension extension : extensionPoint.getExtensions()) {
			for(IConfigurationElement confElement : extension.getConfigurationElements()) {
				if(CATEGORY_ELEMENT_NAME.equals(confElement.getName())) {
					DiagramCategoryDescriptor diagramCategoryDescriptor;
					try {
						diagramCategoryDescriptor = buildCategoryDescriptor(confElement);
						result.put(diagramCategoryDescriptor.getId(), diagramCategoryDescriptor);
					} catch (BadClassNameException e) {
						log.error(Messages.DiagramCategoryRegistry_bad_class_name_of_category, e);
					}
				}
			}
		}
		return result;
	}

	/**
	 * Builds the category descriptor.
	 *
	 * @param confElement the conf element
	 * @return the diagram category descriptor
	 * @throws BadClassNameException the bad class name exception
	 */
	protected DiagramCategoryDescriptor buildCategoryDescriptor(IConfigurationElement confElement) throws BadClassNameException {
		Class<IModelCreationCommand> commandClazz = (Class<IModelCreationCommand>)parseClass(confElement, CATEGORY_CLASS, CATEGORY_ELEMENT_NAME);
		DiagramCategoryDescriptor diagramCategoryDescriptor = new DiagramCategoryDescriptor(confElement.getAttribute(CATEGORY_ID), confElement.getAttribute(CATEGORY_LABEL), commandClazz);
		diagramCategoryDescriptor.setDescription(confElement.getAttribute(CATEGORY_DESCRIPTION));
		diagramCategoryDescriptor.setExtensionPrefix(confElement.getAttribute(CATEGORY_FILE_EXTENSION_PREFIX));
		String iconPath = confElement.getAttribute(CATEGORY_ICON);
		if(iconPath != null) {
			diagramCategoryDescriptor.setIcon(AbstractUIPlugin.imageDescriptorFromPlugin(confElement.getNamespaceIdentifier(), iconPath));
		}
		return diagramCategoryDescriptor;
	}

}

Back to the top