Skip to main content
summaryrefslogtreecommitdiffstats
blob: d350c75d454a149da1a4eeb7f06ce4e2518d7cd6 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*****************************************************************************
 * Copyright (c) 2009 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:
 *    Patrick Tessier patrick.tessier@cea.fr - initial API and implementation
 *    Vincent Lorenzo vincent.lorenzo@cea.fr 
 *******************************************************************************/
package org.eclipse.papyrus.diagram.profile.custom.ui.dialog;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.papyrus.diagram.common.Activator;
import org.eclipse.papyrus.diagram.profile.custom.messages.Messages;
import org.eclipse.papyrus.diagram.profile.part.UMLDiagramEditorPlugin;
import org.eclipse.papyrus.profile.ui.dialogs.AlphabeticalViewerSorter;
import org.eclipse.papyrus.profile.ui.dialogs.ChooseSetAssistedDialog;
import org.eclipse.papyrus.profile.ui.dialogs.IChooseDialog;
import org.eclipse.papyrus.umlutils.ElementUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Profile;

/**
 * 
 * This dialog allows to choose the metaclass to import in the profile.
 * This dialog doesn't allow remove imported metaclass.
 * This dialog doesn't show the metaclasses which are already imported in the profile
 * 
 */
public class ChooseSetMetaclassDialog extends ChooseSetAssistedDialog implements IChooseDialog {


	/**
	 * parent profile (top Profile).
	 */
	protected Profile profile;

	/**
	 * @param parentShell
	 * @param profile
	 */
	public ChooseSetMetaclassDialog(Shell parentShell, Profile profile) {
		this(parentShell, profile, true);
	}

	/**
	 * 
	 * 
	 * @param parentShell
	 * @param profile
	 */
	public ChooseSetMetaclassDialog(Shell parentShell, Profile prof, boolean retrieveImported) {
		super(parentShell, Messages.ChooseSetMetaclassDialog_Metaclasses, Messages.ChooseSetMetaclassDialog_ImportedMetaclasses);


		//this.profile = profile;
		/**
		 * for the moment, all the metaclasses must be imported to the root profile
		 */
		EObject container = prof.eContainer();
		EObject oldContainer = prof;
		while(container != null) {
			oldContainer = container;
			container = container.eContainer();
		}

		this.profile = (Profile)oldContainer;
		selectedElementList = new ElementListWithComparator(new ClassElementComparator());
		//		redrawedMetaclasses = new ArrayList<Object>();
		labelProvider = new MetaclassLabelProvider();
		decoratedContentProposalProvider = new MetaclassContentProposalProvider();

		Iterator<?> metaclasses = ElementUtil.getPossibleMetaclasses(this.profile).iterator();
		while(metaclasses.hasNext()) {
			possibleElementList.addElement(metaclasses.next());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cea.papyrus.core.ui.dialogs.ChooseSetAssistedDialog#createDialogArea(org.eclipse.swt.
	 * widgets.Composite)
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Control composite = super.createDialogArea(parent);

		// Add 2 columns to the table area
		// possibleElementsTable.setLinesVisible(true);
		possibleElementsTable.setHeaderVisible(true);

		// 1st column with image/checkboxes - NOTE: The SWT.CENTER has no effect!!
		TableColumn column = new TableColumn(possibleElementsTable, SWT.CENTER, 0);
		column.setText(Messages.ChooseSetMetaclassDialog_Metaclass);
		column.setWidth(150);
		column.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				possibleElementsTableViewer.setSorter(new AlphabeticalViewerSorter(0));
			}
		});

		// 2nd column with task Description
		column = new TableColumn(possibleElementsTable, SWT.LEFT, 1);
		column.setText(Messages.ChooseSetMetaclassDialog_Information);
		column.setWidth(165);
		// Add listener to column so tasks are sorted by description when clicked
		column.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				possibleElementsTableViewer.setSorter(new AlphabeticalViewerSorter(1));
			}
		});

		// set sorter to the possible element table viewer
		possibleElementsTableViewer.setSorter(new AlphabeticalViewerSorter(0));

		return composite;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cea.papyrus.core.ui.dialogs.ChooseSetAssistedDialog#runAddElement(java.lang.String)
	 */
	/**
	 * 
	 * 
	 * @param name
	 */
	@Override
	protected void runAddElement(String name) {
		// find the class in the list
		Class theClass = null;
		Iterator<?> it = possibleElementList.getElements().iterator();
		while(it.hasNext()) {
			Class current = (Class)it.next();
			if(name.equalsIgnoreCase(current.getName()) || name.equalsIgnoreCase(current.getQualifiedName())) {
				theClass = current;
			}
		}
		if(theClass != null) {
			runActionAdd(theClass);
		}
	}


	/**
	 * adds the element to the list of selected elements and remove it from the
	 * list of possible elements.
	 * 
	 * @param element
	 *        the element to add to the selected elements list
	 */
	@Override
	public void runActionAdd(Object element) {
		super.runActionAdd(element);
		//update the OK button status
		updateOKButtonStatus();
	}


	/**
	 * adds the element to the list of possible elements and remove it from the
	 * list of selected elements.
	 * 
	 * @param element
	 *        the element to remove from the selected elements list
	 */
	@Override
	protected void runActionRemove(Object element) {
		super.runActionRemove(element);
		//update the OK button status
		updateOKButtonStatus();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cea.papyrus.core.ui.dialogs.ChooseSetAssistedDialog#isSelectableElement(java.lang.String)
	 */
	/**
	 * 
	 * 
	 * @param text
	 * 
	 * @return
	 */
	@Override
	protected boolean isSelectableElement(String text) {
		// iterate through all possibilities and return true if text corresponds
		Iterator<?> it = possibleElementList.getElements().iterator();
		while(it.hasNext()) {
			Class element = (Class)it.next();
			if(text.equalsIgnoreCase(element.getName()) || text.equalsIgnoreCase(element.getQualifiedName())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.profile.ui.dialogs.ChooseSetAssistedDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		//init the status of the OKButton to false;
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	/**
	 * Update the status of the OK Button
	 */
	protected void updateOKButtonStatus() {
		Button OKButton = this.getButton(IDialogConstants.OK_ID);
		if(OKButton != null) {
			OKButton.setEnabled(!selectedElementList.getElements().isEmpty());
		}
	}

	/**
	 * 
	 */
	public class MetaclassContentProposalProvider extends DecoratedContentProposalProvider {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.cea.papyrus.core.ui.dialogs.ChooseSetAssistedDialog.DecoratedContentProposalProvider
		 * #getProposals(java.lang.String, int)
		 */
		/**
		 * 
		 * 
		 * @param contents
		 * @param position
		 * 
		 * @return
		 */
		@Override
		public DecoratedContentProposal[] getProposals(String contents, int position) {
			ArrayList<DecoratedContentProposal> proposals = new ArrayList<DecoratedContentProposal>();

			if(possibleElementList != null) {
				Iterator<?> it = possibleElementList.getElements().iterator();
				while(it.hasNext()) {
					final NamedElement element = (NamedElement)it.next();
					final String simpleName = element.getName();

					if(position < simpleName.length() && contents.substring(0, position).equalsIgnoreCase(simpleName.substring(0, position))) {
						proposals.add(new DecoratedContentProposal(element, labelProvider));
					}
				}
			}

			Collections.sort(proposals);
			return proposals.toArray(new DecoratedContentProposal[proposals.size()]);
		}
	}

	/**
	 * Label provider for metaclasses.
	 * 
	 * @author Remi Schnekenburger
	 *         adapted code by Vincent Lorenzo vincent.lorenzo@cea.fr
	 */
	protected class MetaclassLabelProvider extends LabelProvider implements ITableLabelProvider {


		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
		 */
		private static final String ID = "org.eclipse.uml2.uml.edit"; //$NON-NLS-1$

		private static final String PATH = "/icons/full/obj16/"; //$NON-NLS-1$

		private static final String LOCAL_PATH = "/icons/obj16/"; //$NON-NLS-1$

		private static final String EXTENSION = ".gif"; //$NON-NLS-1$

		private static final String EMPTY_IMAGE = "Empty" + EXTENSION; //$NON-NLS-1$

		//One of the metaclasses without image!
		private static final String DUMMY_IMAGE = "dummy" + EXTENSION; //$NON-NLS-1$

		private static final String EMPTY_IMAGE_PATH = "../../../../../../../../../icons/obj16/Empty.gif"; //$NON-NLS-1$



		/**
		 * 
		 * 
		 * @param element
		 * 
		 * @return the researched image or a blank image if the first doesn't exist
		 */
		@Override
		public Image getImage(Object element) {

			Image image = null;
			String name = getText(element);

			if(element instanceof Class) {
				image = Activator.getPluginIconImage(ID, PATH + name + EXTENSION);
			}

			/**
			 * Some elements doesn't have associated image. For example the UML Element Action doesn't have it!
			 * the returned image is always the same when there isn't image.
			 */
			//if the returned image is the same that with a dummy image : 
			if(image.equals(Activator.getPluginIconImage(ID, PATH + DUMMY_IMAGE + EXTENSION))) {
				//we search the image with the name EMPTY
				image = Activator.getPluginIconImage(UMLDiagramEditorPlugin.ID, LOCAL_PATH + EMPTY_IMAGE);
				//the first time, this image doesn't exist, so we create it!
				if(image.equals(Activator.getPluginIconImage(ID, PATH + DUMMY_IMAGE + EXTENSION))) {
					ImageRegistry registry = Activator.getDefault().getImageRegistry();
					if(registry.getDescriptor(UMLDiagramEditorPlugin.ID + PATH + EMPTY_IMAGE) == null) {
						try {
							ImageDescriptor imDesc = ImageDescriptor.createFromFile(getClass(), EMPTY_IMAGE_PATH);
							String key = UMLDiagramEditorPlugin.ID + LOCAL_PATH + EMPTY_IMAGE;
							registry.put(key, imDesc);
							image = registry.get(key);
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				}


			}


			return image;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
		 */
		/**
		 * 
		 * 
		 * @param element
		 * 
		 * @return
		 */
		@Override
		public String getText(Object element) {
			if(!(element instanceof Class)) {
				return "not a class"; //$NON-NLS-1$
			} else {
				return ((Class)element).getName();
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
		 */
		/**
		 * 
		 * 
		 * @param element
		 * @param columnIndex
		 * 
		 * @return
		 */
		public Image getColumnImage(Object element, int columnIndex) {
			if(columnIndex == 0) {
				return getImage(element);
			}
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
		 */
		/**
		 * 
		 * 
		 * @param element
		 * @param columnIndex
		 * 
		 * @return
		 */
		public String getColumnText(Object element, int columnIndex) {
			if(columnIndex == 0) {
				return getText(element);
			} else if(columnIndex == 1) {
				return ((Class)element).getQualifiedName();
			}
			return "not the right column index"; //$NON-NLS-1$
		}
	}

	/**
	 * Label provider for stereotypes that modifies {@link MetaclassLabelProvider#getText(Object)}.
	 * It returns the qualified name of the Metaclass instead of the simple name.
	 * 
	 * @author Remi Schnekenburger
	 */
	protected class MetaclassQualifiedLabelProvider extends MetaclassLabelProvider {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.cea.papyrus.core.ui.dialogs.ChooseSetStereotypeDialog.StereotypeLabelProvider#getText
		 * (java.lang.Object)
		 */
		@Override
		public String getText(Object element) {
			if(!(element instanceof Class)) {
				return "not a class"; //$NON-NLS-1$
			} else {
				return ((Class)element).getQualifiedName();
			}
		}
	}

}

Back to the top