Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 768cc8b42676bd9e32e66a294d8e85ec264bcf52 (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
/*****************************************************************************
 * Copyright (c) 2009, 2014 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:
 *  Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 410346
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.composite.custom.edit.command;

import java.util.ArrayList;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.extensionpoints.editors.Activator;
import org.eclipse.papyrus.extensionpoints.editors.configuration.IDirectEditorConfiguration;
import org.eclipse.papyrus.extensionpoints.editors.ui.ExtendedDirectEditionDialog;
import org.eclipse.papyrus.extensionpoints.editors.utils.DirectEditorsUtil;
import org.eclipse.papyrus.extensionpoints.editors.utils.IDirectEditorsIds;
import org.eclipse.papyrus.uml.diagram.composite.custom.messages.Messages;
import org.eclipse.papyrus.uml.diagram.composite.custom.ui.InformationItemElementTreeSelectionDialog;
import org.eclipse.papyrus.uml.diagram.composite.custom.ui.InformationItemValidator;
import org.eclipse.papyrus.uml.diagram.composite.custom.ui.NameLabelValidator;
import org.eclipse.papyrus.uml.diagram.composite.providers.ElementInitializers;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.InformationFlow;
import org.eclipse.uml2.uml.InformationItem;
import org.eclipse.uml2.uml.UMLFactory;

/**
 * <pre>
 * This class provides a custom creation command for InformationFlow
 * InformationFlow are references toward any {@link InformationItem} in the model
 * </pre>
 */
public class InformationFlowCreateCommand extends org.eclipse.papyrus.uml.diagram.composite.edit.commands.InformationFlowCreateCommand {

	public InformationFlowCreateCommand(CreateRelationshipRequest request, EObject source, EObject target) {
		super(request, source, target);

	}

	/**
	 * <pre>
	 *  The method executes the creation :
	 *  - opens a selection dialog to choose {@link InformationItem} or {@link Classifier} reference as a convoyed Classifier
	 *   by the {@link InformationFlow} type
	 *  - create the InformationFlow between the source and the target
	 *  
	 * {@inheritDoc}
	 * </pre>
	 */
	@Override
	protected CommandResult doExecuteWithResult(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {

		// Create and open the selection dialog
		ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
		Shell currentShell = new Shell(Display.getCurrent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		InformationItemElementTreeSelectionDialog dialog = new InformationItemElementTreeSelectionDialog(currentShell, new AdapterFactoryLabelProvider(adapterFactory), new AdapterFactoryContentProvider(adapterFactory));

		try {
			// Set dialog parameters
			dialog.setTitle(Messages.InformationFlowSelectionDialog_Title);
			dialog.setMessage(Messages.InformationFlowSelectionDialog_Message);
			dialog.setAllowMultiple(true);
			dialog.setHelpAvailable(false);
			dialog.setInput(getSource().getModel());
			dialog.setValidator(new InformationItemValidator());
	
			ArrayList<Classifier> initialSelection = new ArrayList<Classifier>();
	
			// here the dialog.getReturnCode is IDialogConstants.OK_ID
			while(dialog.open() != IDialogConstants.CANCEL_ID) {
	
				/*
				 * If classifiers have been selected, complete command execution and write the selection
				 * in the conveyed:Classifief Association
				 */
				if(dialog.getReturnCode() == ElementTreeSelectionDialog.OK) {
					Object[] conveyedClassified = dialog.getResult();
	
					InformationFlow newInformationFlow = UMLFactory.eINSTANCE.createInformationFlow();
					getContainer().getPackagedElements().add(newInformationFlow);
					newInformationFlow.getInformationSources().add(getSource());
					newInformationFlow.getInformationTargets().add(getTarget());
	
					// add the classifier in the list of conveyed Classifier
					for(int i = 0; i < conveyedClassified.length; i++) {
						newInformationFlow.getConveyeds().add((Classifier)conveyedClassified[i]);
					}
	
					ElementInitializers.getInstance().init_InformationFlow_4021(newInformationFlow);
	
					((CreateElementRequest)getRequest()).setNewElement(newInformationFlow);
					return CommandResult.newOKCommandResult(newInformationFlow);
	
				} else if(dialog.getReturnCode() == InformationItemElementTreeSelectionDialog.newInformationItemButton_ID) {
					// create a new InformationItem
					initialSelection.add((Classifier)createNewInformationItem());
					dialog.setInitialElementSelections(initialSelection);
				}
			}// end of while()
		} finally {
			adapterFactory.dispose();
		}
		
		// No Classifier selected: abort element creation
		return CommandResult.newCancelledCommandResult();
	}

	/**
	 * - Create a new {@link InformationItem}
	 * 
	 * - Ask for the {@link InformationItem}'s name
	 * 
	 * @return the new InformationItem or null if canceled
	 */

	// TODO should be moved?
	private InformationItem createNewInformationItem() {
		// create the new InformationItem
		InformationItem infoItem = UMLFactory.eINSTANCE.createInformationItem();
		getContainer().getPackagedElements().add(infoItem);
		ElementInitializers.getInstance().init_InformationItem_2080(infoItem);

		// ask for the name of the new InformationItem
		// Create the editor
		Shell currentShell = new Shell(Display.getCurrent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		IDirectEditorConfiguration configuration;// = new DefaultDirectEditorConfiguration();
		String languagePreferred = Activator.getDefault().getPreferenceStore().getString(IDirectEditorsIds.EDITOR_FOR_ELEMENT + infoItem.eClass().getInstanceClassName());
		configuration = DirectEditorsUtil.findEditorConfiguration(languagePreferred, infoItem.eClass().getInstanceClassName());
		configuration.preEditAction(infoItem);
		configuration.setInputValidator(new NameLabelValidator(Messages.InformationItemGetNameDialog_Error_Message));
		ExtendedDirectEditionDialog dialog = new ExtendedDirectEditionDialog(currentShell, infoItem, infoItem.getName(), configuration);

		dialog.open();

		if(dialog.getReturnCode() == Window.CANCEL) {
			// We delete the newInformationItem
			int index = getContainer().getPackagedElements().indexOf(infoItem);
			getContainer().getPackagedElements().remove(index);
			return null;
		} else {
			infoItem.setName(dialog.getValue());
			((CreateElementRequest)getRequest()).setNewElement(infoItem);
			return infoItem;
		}
	}

}

Back to the top