Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e5551f0d1eb1a0a632cd69196739dd62e1cce3dc (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
/*****************************************************************************
 * 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
 *	Saadia Dhouib (CEA LIST) - Implementation of loading diagrams from template files  (.uml, .di , .notation)
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.wizards.template;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.core.extension.commands.CreationCommandDescriptor;

/**
 * The Class ModelTemplatesContentProvider.
 */
public class ModelTemplatesContentProvider implements IStructuredContentProvider {

	/** The Constant EXTENSION_POINT_ID. */
	private static final String EXTENSION_POINT_ID = "org.eclipse.papyrus.uml.diagram.wizards.templates"; //$NON-NLS-1$

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

	/** The Constant ATTRIBUTE_FILE. */
	private static final String ATTRIBUTE_UML_FILE = "file"; //$NON-NLS-1$
	
	/** The Constant ATTRIBUTE_NOTATION_FILE. */
	private static final String ATTRIBUTE_NOTATION_FILE = "notation_file"; //$NON-NLS-1$
	
	/** The Constant ATTRIBUTE_DI_FILE. */
	private static final String ATTRIBUTE_DI_FILE = "di_file"; //$NON-NLS-1$

	/** The Constant ATTRIBUTE_LANGUAGE. */
	private static final String ATTRIBUTE_LANGUAGE = "language"; //$NON-NLS-1$
	
	/** The my template descriptions. */
	private ModelTemplateDescription[] myTemplateDescriptions;

	/**
	 * Dispose.
	 *
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	public void dispose() {
		// TODO Auto-generated method stub

	}

	/**
	 * Gets the templates description.
	 *
	 * @return the templates description
	 */
	private ModelTemplateDescription[] getTemplatesDescription() {
		if (myTemplateDescriptions == null) {
			List<ModelTemplateDescription> templates = new ArrayList<ModelTemplateDescription>();

			IExtensionRegistry registry = Platform.getExtensionRegistry();
			IExtension[] extensions = registry.getExtensionPoint(EXTENSION_POINT_ID).getExtensions();

			for(IExtension extension : extensions) {
				templates.addAll(processExtension(extension));
			}
			myTemplateDescriptions = templates.toArray(new ModelTemplateDescription[templates.size()]);
		}
		return myTemplateDescriptions;
	}

	/**
	 * Process extension.
	 *
	 * @param extension the extension
	 * @return the collection
	 */
	private Collection<ModelTemplateDescription> processExtension(IExtension extension) {
		List<ModelTemplateDescription> templates = new ArrayList<ModelTemplateDescription>();
		for(IConfigurationElement configElement : extension.getConfigurationElements()) {
			ModelTemplateDescription template = new ModelTemplateDescription(configElement.getAttribute(ATTRIBUTE_NAME), extension.getContributor().getName(), configElement.getAttribute(ATTRIBUTE_UML_FILE),configElement.getAttribute(ATTRIBUTE_NOTATION_FILE),configElement.getAttribute(ATTRIBUTE_DI_FILE));
			template.setLanguage(configElement.getAttribute(ATTRIBUTE_LANGUAGE));
			templates.add(template);
		}
		return templates;
	}

	/**
	 * Gets the elements.
	 *
	 * @param inputElement the input element
	 * @return the elements
	 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
	 */
	public Object[] getElements(Object inputElement) {
		
		if(inputElement instanceof Object[]) {		
			List<ModelTemplateDescription> result = new ArrayList<ModelTemplateDescription>();			
			
			for (Object next: (Object[])inputElement) {
				if (next instanceof String) {
					String diagramCategory = (String)next;
					for(ModelTemplateDescription template : getTemplatesDescription()) {						
						if(diagramCategory == null || diagramCategory.equals(template.getLanguage())) {
							result.add(template);
						}
					}
				}
			}
			return result.toArray();
		}
		
		return new Object[0];
	}

	/**
	 * Input changed.
	 *
	 * @param viewer the viewer
	 * @param oldInput the old input
	 * @param newInput the new input
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		if(viewer instanceof TableViewer) {
			((TableViewer)viewer).add(getElements(null));
		}
	}

}

Back to the top