Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cade8f285c04fcd7c23fef8b43da4e3c8240a679 (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
/*****************************************************************************
 * Copyright (c) 2014 Jonathan Geoffroy.
 *
 *
 * 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:
 * 	Jonathan Geoffroy	geoffroy.jonathan@gmail.com		initial implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.java.reverse.ui;

import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.TreeSet;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.impl.ShapeImpl;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.java.reverse.ui.dialog.DndReverseCodeDialog;
import org.eclipse.papyrus.java.reverse.ui.dialog.ReverseCodeDialog;
import org.eclipse.papyrus.uml.diagram.common.util.MDTUtil;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.NamedElement;

/**
 * Handler to display reverse result into current papyrus diagram
 * This Handler is activated when user drag'n'drop java resource into papyrus diagram.<br>
 * It display just non displayed java resource.
 * Moreover, it use DndReverseCodeDialog to know if it have to display model, packages and CompilationUnit or not.
 *
 * @author Jonathan Geoffroy
 *
 */
public class DndReverseCodeHandler extends ReverseCodeHandler {

	/**
	 * true if and only if user want to display reversed compilation unit
	 */
	private boolean displayCU;

	/**
	 * true if and only if user want to display reversed packages
	 */
	private boolean displayPackages;

	/**
	 * true if and only if user want to display reversed model
	 */
	private boolean displayModel;

	@Override
	/**
	 * Find all resource to display (selection - resources which are already in the diagram), run the reverse command, before running the display command<br/>
	 * @see org.eclipse.papyrus.java.reverse.ui.ReverseCodeHandler#doExecute(org.eclipse.papyrus.java.reverse.ui.dialog.ReverseCodeDialog)
	 *
	 * @param dialog
	 */
	protected void doExecute(ReverseCodeDialog dialog) {
		// Get user preferences on dialog
		DndReverseCodeDialog dndDialog = (DndReverseCodeDialog) dialog;
		displayModel = dndDialog.displayModel();
		displayPackages = dndDialog.displayPackages();
		displayCU = dndDialog.displayCU();

		// get current selection
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		ISelection selection = page.getSelection();
		TreeSelection treeSelection = (TreeSelection) selection;

		// Find active papyrus diagram
		Diagram diagram = null;
		IEditorPart activeEditor = MDTUtil.getActiveEditor();
		if (activeEditor != null) {
			if (activeEditor instanceof IMultiDiagramEditor) {
				diagram = (Diagram) ((IMultiDiagramEditor) activeEditor).getAdapter(Diagram.class);
			}
		}

		// Remove items already in diagram from selection
		List<IJavaElement> listSelection;
		try {
			listSelection = selectionMinusAlreadyInDiagram(treeSelection, diagram);
		} catch (JavaModelException e1) {
			e1.printStackTrace();
			return;
		}

		// Run the reverse
		super.doExecute(dialog);

		
		try {
			// Find model to display
			Model model = null;
			if (displayModel) {
				String modelName = dndDialog.getValue();
				model = getModelToDisplay(diagram, modelName);
			}

			// Run the reverse displayer
			DisplayReverse displayReverse = new DisplayReverse(listSelection, diagram, getUmlResource(), model);
			displayReverse.execute();
		} catch (JavaModelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	protected ReverseCodeDialog getDialog(Shell shell, String modelUid) {
		return new DndReverseCodeDialog(shell, modelUid, getSelectedProjectName(), null);
	}

	/**
	 *
	 * @param selection
	 *            java resources selected
	 * @param diagram
	 *            opened papyrus diagram
	 * @return selection - resources already in diagram
	 * @throws JavaModelException
	 */
	private List<IJavaElement> selectionMinusAlreadyInDiagram(TreeSelection selection, Diagram diagram) throws JavaModelException {
		// Add all diagram elements to Set
		TreeSet<String> alreadyExists = new TreeSet<String>();
		EList diagramList = diagram.getChildren();
		Iterator diagramIt = diagramList.iterator();
		ShapeImpl item;
		NamedElement e;
		while (diagramIt.hasNext()) {
			item = (ShapeImpl) diagramIt.next();
			e = (NamedElement) (item.getElement());
			alreadyExists.add(e.getName());
		}

		// remove all elements of Set from selection
		TreeSelectionList selectionList = new TreeSelectionList(selection, displayPackages, displayCU);
		ListIterator<IJavaElement> selectionIterator = selectionList.listIterator();
		String selectionItemName;
		while (selectionIterator.hasNext()) {
			selectionItemName = getName(selectionIterator.next());
			if (selectionItemName != null && alreadyExists.contains(selectionItemName)) {
				selectionIterator.remove();
			}
		}

		return selectionList;
	}

	/**
	 * Find the name of <code>item</code> For java compilation unit, remove ".java" extension
	 *
	 * @param item
	 * @return the name of item.
	 */
	private String getName(IJavaElement item) {
		String name = item.getElementName();
		if (item instanceof ICompilationUnit) {
			return name.substring(0, name.length() - 5);
		}
		return name;
	}

	/**
	 * find a model named <code>modelName</code>
	 *
	 * @param modelName
	 *            the name of the model the find
	 * @return the model which named <code>modelName</code>, or null if it doesn't exists into the current papyrus uml resource
	 * @throws ServiceException 
	 */
	public Model getModel(String modelName) throws ServiceException {
		TreeIterator<EObject> tree = getUmlResource().getAllContents();
		while (tree.hasNext()) {
			for (EObject o : tree.next().eContents()) {
				if (o instanceof Model) {
					// Add it into items to display
					Model model = (Model) o;

					// Search for model
					if (model.getName().equals(modelName)) {
						System.out.println("model = " + model);
						return model;
					}
				}
			}
		}
		System.out.println("model = null");
		return null;
	}

	/**
	 * find the model named <code>modelName</code>, and get it only if it has to be displayed
	 *
	 * @param diagram
	 *            active papyrus diagram
	 * @param modelName
	 *            the name of the model to find
	 * @return model corresponding to the modelName if it has to be displayed, i.e. if it doesn't already displayed into the diagram, or null
	 *         otherwise
	 * @throws ServiceException 
	 */
	private Model getModelToDisplay(Diagram diagram, String modelName) throws ServiceException {
		Model model = getModel(modelName);
		if (model != null && !isInDiagram(diagram, model)) {
			System.out.println("display model " + model);
			return model;
		}
		System.out.println("don't display model");
		return null;
	}

	/**
	 *
	 * @param diagram
	 * @param model
	 * @return true if model is in diagram
	 */
	private boolean isInDiagram(Diagram diagram, Model model) {
		EList diagramList = diagram.getChildren();
		Iterator diagramIt = diagramList.iterator();
		ShapeImpl item;
		while (diagramIt.hasNext()) {
			item = (ShapeImpl) diagramIt.next();
			if (item.getElement() == model) {
				return true;
			}
		}
		return false;
	}
}

Back to the top