Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9e84c33dafd9d9f0f32438e414a86fb2c5ab465b (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
/*****************************************************************************
 * Copyright (c) 2013 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:
 *  Ansgar Radermacher  ansgar.radermacher@cea.fr  
 *
 *****************************************************************************/

package org.eclipse.papyrus.qompass.designer.core.dialogs;


import java.util.Arrays;

import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.UniqueEList;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.papyrus.FCM.InteractionComponent;
import org.eclipse.papyrus.qompass.designer.core.Description;
import org.eclipse.papyrus.qompass.designer.core.Log;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.papyrus.qompass.designer.core.Utils;
import org.eclipse.papyrus.qompass.designer.core.templates.ConnectorBinding;
import org.eclipse.papyrus.qompass.designer.core.transformations.TransformationException;
import org.eclipse.papyrus.qompass.designer.core.transformations.TransformationRTException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.AbstractElementListSelectionDialog;
import org.eclipse.ui.dialogs.FilteredList;
import org.eclipse.ui.dialogs.SelectionStatusDialog;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Feature;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.util.UMLUtil;

/**
 * Select a connector type and implementation (group)
 * TODO: show information about the used connector [usage, implem properties, ...]
 * similar help for ports?
 * 
 * @author ansgar
 * 
 */
public class ConnectorSelectionDialog extends AbstractElementListSelectionDialog {

	protected Package m_model;

	protected EList<Package> visitedPackages;

	protected Text m_description;

	protected Button fTypeOnly;

	private EList<Class> connectorList;

	private Feature m_selectedConnector;

	private Object[] fUpperElements;

	protected FilteredList fUpper;

	protected FilteredList fLower;

	private InteractionComponent initialSelection = null;

	public ConnectorSelectionDialog(Shell parent, Package model, Feature selectedConnector) {
		super(parent, new QNameLabelProvider());
		connectorList = new BasicEList<Class>();
		visitedPackages = new BasicEList<Package>();
		getAllConnectors(model, selectedConnector, connectorList);
		m_selectedConnector = selectedConnector;
		setMultipleSelection(false);
		m_model = model;
	}

	public Control createDialogArea(Composite parent) {
		Composite contents = (Composite)super.createDialogArea(parent);
		createMessageArea(contents);
		// createLabel(contents, "Connector type");
		GridData gridData = DialogUtils.createFillGridData();
		gridData.heightHint = 200;

		Group fUpperGroup = new Group(contents, SWT.NONE);
		fUpperGroup.setLayout(new GridLayout());
		fUpperGroup.setLayoutData(gridData);
		fUpperGroup.setText(" Abstract interaction components "); //$NON-NLS-1$
		createFilterText(fUpperGroup);
		fUpper = createFilteredList(fUpperGroup);

		final Group fLowerGroup = new Group(contents, SWT.NONE);
		fLowerGroup.setLayout(new GridLayout());
		fLowerGroup.setLayoutData(gridData);
		fLowerGroup.setText(" Realizations/groups of interaction components "); //$NON-NLS-1$
		// createLabel(contents, lowerLabel);
		fLower = createFilteredList(fLowerGroup);
		fFilteredList = fUpper;
		org.eclipse.papyrus.FCM.Connector fcmConnector = UMLUtil.getStereotypeApplication(m_selectedConnector, org.eclipse.papyrus.FCM.Connector.class);
		if(fcmConnector != null) {
			// getInitialElementSelections();
			initialSelection = fcmConnector.getIc();
		} else {
			initialSelection = null;
		}

		fTypeOnly = new Button(contents, SWT.CHECK);
		fTypeOnly.setText("Select type only"); //$NON-NLS-1$
		SelectionListener fTypeOnlySL = new SelectionListener() {

			public void widgetSelected(SelectionEvent e) {
				if(fTypeOnly.getSelection()) {
					fLower.setEnabled(false);
					fLowerGroup.setEnabled(false);
					fLower.setSelection(new int[0]);
					fLower.setElements(null);
				}
				else {
					fLower.setEnabled(true);
					fLowerGroup.setEnabled(true);
					handleSelectionChanged();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		};
		fTypeOnly.addSelectionListener(fTypeOnlySL);

		fUpper.setElements(connectorList.toArray());

		// create initial selections
		if(initialSelection instanceof InteractionComponent) {
			InteractionComponent componentComp = initialSelection;
			Class componentCompBase = componentComp.getBase_Class();
			if(!componentCompBase.isAbstract()) {
				// get first superclass that is a component type
				Class componentTypeBase = Utils.componentType(componentCompBase);
				componentComp = UMLUtil.getStereotypeApplication(componentTypeBase, InteractionComponent.class);
			} else {
				// fLower.setEnabled (false);
				// fLowerGroup.setEnabled(false);
				fTypeOnly.setSelection(true);
			}
			// update status (listener does not evaluate passed data)
			fTypeOnlySL.widgetSelected(null);

			// set initial selections
			fUpper.setSelection(new Object[]{ componentComp.getBase_Class() });
		}

		Label info = createMessageArea(contents);
		info.setText("Information about connector implementation:"); //$NON-NLS-1$
		// m_description = createMessageArea(contents);

		GridData descriptionGD = new GridData();
		descriptionGD.horizontalAlignment = GridData.FILL;
		descriptionGD.grabExcessHorizontalSpace = true;
		descriptionGD.heightHint = 80;
		descriptionGD.grabExcessVerticalSpace = true;
		descriptionGD.verticalAlignment = GridData.FILL;

		m_description = new Text(contents, SWT.NONE | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
		m_description.setLayoutData(descriptionGD);

		fLower.addSelectionListener(new SelectionListener() {

			public void widgetSelected(SelectionEvent e) {
				Object[] selected = fLower.getSelection();
				if((selected.length > 0) && (selected[0] instanceof NamedElement)) {
					NamedElement selectedNE = (NamedElement)selected[0];
					m_description.setText(Description.getDescription(selectedNE));
				}
				else {
					m_description.setText("");
					updateOkState();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		return contents;
	}

	@Override
	/**
	 * Update the enablement of the OK button based on whether or not there
	 * is a selection.
	 *
	 */
	protected void updateOkState() {
		if(fTypeOnly.getSelection()) {
			super.updateOkState();
		} else {
			Button okButton = getOkButton();
			if(okButton != null) {
				okButton.setEnabled(fLower.getSelection().length != 0);
			}
		}
	}


	protected Label createLabel(Composite parent, String name) {
		if(name == null) {
			return null;
		}
		Label label = new Label(parent, SWT.NONE);
		label.setText(name);
		label.setFont(parent.getFont());
		return label;
	}

	/**
	 * Creates a label if name was not <code>null</code>.
	 * 
	 * @param parent
	 *        the parent composite.
	 * @param name
	 *        the name of the label.
	 * @return returns a label if a name was given, <code>null</code> otherwise.
	 */


	/**
	 * @see SelectionStatusDialog#computeResult()
	 */
	protected void computeResult() {
		Object[] result = new Object[]{ getSelectedElement(fTypeOnly.getSelection()) };
		setResult(Arrays.asList(result));
	}

	/**
	 * @see AbstractElementListSelectionDialog#handleDefaultSelected()
	 */
	protected void handleDefaultSelected() {
		if(validateCurrentSelection() && (getSelectedElement(false) != null)) {
			buttonPressed(IDialogConstants.OK_ID);
		}
	}

	/**
	 * @see AbstractElementListSelectionDialog#handleSelectionChanged()
	 */
	protected void handleSelectionChanged() {
		handleUpperSelectionChanged();
	}

	private void handleUpperSelectionChanged() {
		int index = getSelectionIndex();
		if(index >= 0) {
			fUpperElements = getFoldedElements(index);

			if((fUpperElements == null) || fTypeOnly.getSelection()) {
				// lower list remains empty, if typeOnly is true
				fLower.setElements(new Object[]{});
			} else {
				fLower.setElements(getLowerList(fUpperElements[0]));
				if(initialSelection instanceof InteractionComponent) {
					// select existing selection in lower section
					fLower.setSelection(new Object[]{ initialSelection });
					// do not try to set it again.
					initialSelection = null;
				}
			}
		}
		validateCurrentSelection();
	}

	/**
	 * Returns the selected element from the upper or lower pane.
	 * 
	 * @return Object
	 */
	protected Object getSelectedElement(boolean upper) {
		Object[] selection;
		if(upper) {
			selection = fFilteredList.getSelection();
		} else {
			selection = fLower.getSelection();
		}
		if(selection.length > 0) {
			return selection[0];
		}
		return null;
	}

	protected Object[] getLowerList(Object selectedUpperObj) {
		EList<Class> connectorList = new UniqueEList<Class>();
		if(selectedUpperObj instanceof Class) {
			visitedPackages = new BasicEList<Package>();
			getImplGroups(m_model, (Class)selectedUpperObj, connectorList);
		}
		return connectorList.toArray();
	}

	void getAllConnectors(Package pkg, Feature selectedConnector, EList<Class> connectorList) {
		for(Element el : pkg.getMembers()) {
			if(el instanceof Package) {
				if(!visitedPackages.contains(el)) {
					visitedPackages.add((Package)el);
					getAllConnectors((Package)el, selectedConnector, connectorList);
				}
			} else if(el instanceof Class) {
				if(StereotypeUtil.isApplied(el, InteractionComponent.class) && Utils.isCompType((Class)el)) {

					if(selectedConnector == null) {
						connectorList.add((Class)el);
					} else {
						try {
							Element owner = (Class)selectedConnector.getOwner();
							if(owner instanceof Class) {
								Class composite = (Class)owner;
								Log.log(Status.INFO, Log.DIALOGS, "ConnectorSelectionDialog.getAllConnectors: try to bind connector " + //$NON-NLS-1$
									((Class)el).getQualifiedName());
								ConnectorBinding.obtainBinding(composite, selectedConnector, (Class)el, false);
								connectorList.add((Class)el);
							} else {
								connectorList.add((Class)el);
							}
						} catch (TransformationException e) {
							// transform into runtime exception
							throw new TransformationRTException(e.getMessage());
						}
					}
				}
			}
		}
	}

	void getImplGroups(Package pkg, Class selectedConnType, EList<Class> implGroupList) {
		for(Element el : pkg.getMembers()) {
			if(el instanceof Package) {
				if(!visitedPackages.contains(el)) {
					visitedPackages.add((Package)el);
					getImplGroups((Package)el, selectedConnType, implGroupList);
				}
			} else if(el instanceof Class) {
				Class class_ = (Class)el;
				if(class_.getGeneralization(selectedConnType) != null) {
					implGroupList.add((Class)el);
				}
			}
		}
	}
}

Back to the top