diff options
author | Jonathan GEOFFROY | 2014-07-30 09:00:59 +0000 |
---|---|---|
committer | Jonathan GEOFFROY | 2014-07-30 09:00:59 +0000 |
commit | 9f9f5e5d44a3ed4820d3a3b4b891b75fd8a1b233 (patch) | |
tree | dc5b90338a585ae8616348f3367a8d4f0c7c3834 /extraplugins/java | |
parent | e1a90530d941b0fff6f5de0f20a4932e7b476fe9 (diff) | |
download | org.eclipse.papyrus-9f9f5e5d44a3ed4820d3a3b4b891b75fd8a1b233.tar.gz org.eclipse.papyrus-9f9f5e5d44a3ed4820d3a3b4b891b75fd8a1b233.tar.xz org.eclipse.papyrus-9f9f5e5d44a3ed4820d3a3b4b891b75fd8a1b233.zip |
Bug 440655 - [Java Code Reverse] drag and drop handler should allow to
display reversed java elements
Change-Id: Ief17a03f4ca7edb7333d2a4ad6f7583aa693f256
Signed-off-by: Jonathan GEOFFROY <geoffroy.jonathan@gmail.com>
Diffstat (limited to 'extraplugins/java')
9 files changed, 822 insertions, 151 deletions
diff --git a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/META-INF/MANIFEST.MF b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/META-INF/MANIFEST.MF index 3eb297cf2cc..88365d4c28e 100644 --- a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/META-INF/MANIFEST.MF +++ b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/META-INF/MANIFEST.MF @@ -5,7 +5,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.jdt.core;bundle-version="3.4.0",
org.eclipse.papyrus.java.reverse;bundle-version="1.0.0",
org.eclipse.papyrus.infra.core;bundle-version="1.0.0",
- org.eclipse.papyrus.uml.tools;bundle-version="1.0.0"
+ org.eclipse.papyrus.uml.tools;bundle-version="1.0.0",
+ org.eclipse.gmf.runtime.diagram.core
Bundle-Vendor: %providerName
Bundle-ActivationPolicy: lazy
Bundle-Version: 1.0.0.qualifier
@@ -16,5 +17,7 @@ Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.papyrus.java.reverse.ui;singleton:=tr
ue
Bundle-RequiredExecutionEnvironment: J2SE-1.5
-Import-Package: org.eclipse.papyrus.infra.gmfdiag.dnd.strategy
+Import-Package: org.eclipse.gmf.runtime.notation,
+ org.eclipse.papyrus.infra.gmfdiag.dnd.strategy,
+ org.eclipse.papyrus.uml.diagram.common.util
diff --git a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/DisplayReverse.java b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/DisplayReverse.java new file mode 100644 index 00000000000..e93737038c1 --- /dev/null +++ b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/DisplayReverse.java @@ -0,0 +1,181 @@ +/***************************************************************************** + * 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.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.emf.common.util.TreeIterator; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint; +import org.eclipse.gmf.runtime.diagram.core.services.ViewService; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.uml2.uml.Model; +import org.eclipse.uml2.uml.NamedElement; + +/** + * Command which allow to display java elements into papyrus diagram. + * The java element must exists into papyrus model (for example by using reverse plugin) + * + * @author Jonathan Geoffroy + * + */ +public class DisplayReverse { + + private static final String MODEL_TYPE = "2005", PACKAGE_TYPE = "2007", CU_TYPE = "2008"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + + /** + * items to display into papyrus diagram + */ + private List<IJavaElement> items; + + /** + * Contains all uml resources. + * Used to improve research into umlModel. + */ + private Map<String, NamedElement> umlModelNames; + + /** + * Graphical container for the reverse result + */ + private Diagram diagram; + + /** + * model to display into diagram. Null if user doesn't want to display any model + */ + private Model model; + + /** + * Constructor. + * + * @param items + * items to display into papyrus diagram + * @param diagram + * container where to display items + * @param umlModel + * resource representing the papyrus uml model + * @param model + * model to display into diagram. Null if user doesn't want to display any model + */ + public DisplayReverse(List<IJavaElement> items, Diagram diagram, Resource umlModel, Model model) { + super(); + this.items = items; + this.diagram = diagram; + this.model = model; + + // Change uml model collection to improve search + umlModelNames = new HashMap<String, NamedElement>(); + TreeIterator<EObject> tree = umlModel.getAllContents(); + while(tree.hasNext()) { + for(EObject o : tree.next().eContents()) { + /* + * Only add reversable elements. + * The non Named element cannot been added into model (comments ...) + */ + if(o instanceof NamedElement) { + // Add it into items to display + NamedElement e = (NamedElement)o; + umlModelNames.put(e.getName(), e); + } + } + } + + + } + + /** + * Execute the display command. + * Should call this method when you want display the selection into papyrus diagram. + * + * @throws JavaModelException + */ + public void execute() throws JavaModelException { + // Display the model + if(model != null) { + display(model.getName(), MODEL_TYPE); + } + + // Cross selection to display each selected item + for(IJavaElement item : items) { + scanJavaElement(item); + } + } + + /** + * Scan a Compilation Unit item + * Just display it + * + * @param item + * a selection compilation unit + */ + private void scanCU(ICompilationUnit item) { + String itemName = item.getElementName().substring(0, item.getElementName().length() - 5); // remove ".java" to the compilation unit name + display(itemName, CU_TYPE); + } + + /** + * Scan a package. + * Display it and cross its childs to display all sub-packages (and recursively) + * + * @param item + * the package to scan + * @throws JavaModelException + */ + private void scanPackage(IPackageFragment item) throws JavaModelException { + String itemName = item.getElementName(); + display(itemName, PACKAGE_TYPE); + } + + /** + * scan and display a JavaElement by delegates to scan{typeOfJavaElement} + * + * @param item + * the item to display + * @throws JavaModelException + */ + private void scanJavaElement(IJavaElement item) throws JavaModelException { + try { + switch(item.getElementType()) { + case IJavaElement.PACKAGE_FRAGMENT: + scanPackage((IPackageFragment)item); + break; + case IJavaElement.COMPILATION_UNIT: + scanCU((ICompilationUnit)item); + break; + } + } catch (NullPointerException e) { + System.err.println(item.getElementName() + "is NOT in selected uml model"); //$NON-NLS-1$ + } + } + + /** + * Display an element into papyrus diagram + * + * @param itemName + * the name of element to display + */ + private void display(String itemName, String type) { + EObject item = this.umlModelNames.get(itemName); + if(!diagram.getChildren().contains(item)) { + ViewService.createNode(diagram, item, type, new PreferencesHint("org.eclipse.papyrus.uml.diagram.clazz")); //$NON-NLS-1$ + } + } +} diff --git a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/DndReverseCodeHandler.java b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/DndReverseCodeHandler.java new file mode 100644 index 00000000000..5aa1483dc9a --- /dev/null +++ b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/DndReverseCodeHandler.java @@ -0,0 +1,246 @@ +/***************************************************************************** + * 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.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); + + // 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); + try { + displayReverse.execute(); + } catch (JavaModelException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Override + protected ReverseCodeDialog getDialog(Shell shell, String modelUid) { + return new DndReverseCodeDialog(shell, modelUid, null, 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 + */ + public Model getModel(String modelName) { + 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 + */ + private Model getModelToDisplay(Diagram diagram, String modelName) { + 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; + } +} diff --git a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/JavaCodeReverse.java b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/JavaCodeReverse.java index 48e9dff7477..79ea329a879 100644 --- a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/JavaCodeReverse.java +++ b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/JavaCodeReverse.java @@ -8,6 +8,7 @@ import japa.parser.ParseException; import japa.parser.ast.CompilationUnit;
import java.io.StringReader;
+import java.util.Iterator;
import java.util.List;
import javagen.umlparser.CompilationUnitAnalyser;
@@ -18,10 +19,16 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ITypeRoot;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Package;
/**
@@ -203,5 +210,76 @@ public class JavaCodeReverse { javaAnalyser.processCompilationUnit(cu);
}
+ /**
+ * Real Implementation of the command.
+ *
+ * @param generationPackageName
+ * @param searchPaths
+ */
+ public void executeCodeReverse(Resource umlResource, String generationPackageName, List<String> searchPaths) {
+ System.out.println("executeCodeReverse()");
+
+ // Get current selection
+ IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ ISelection selection = page.getSelection();
+
+ TreeSelection treeSelection = (TreeSelection)selection;
+ // String filename = treeSelection.
+ @SuppressWarnings("rawtypes")
+ Iterator iter = treeSelection.iterator();
+ while(iter.hasNext()) {
+ Object obj = iter.next();
+ // Translate java ICompilationUnit to Iresource
+ if(obj instanceof ICompilationUnit) {
+ ICompilationUnit u = (ICompilationUnit)obj;
+ try {
+ obj = u.getCorrespondingResource();
+ } catch (JavaModelException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ if(obj instanceof IPackageFragment)
+ {
+ IPackageFragment u = (IPackageFragment)obj;
+ try {
+ IResource res = u.getCorrespondingResource();
+ if(res != null)
+ obj = res;
+ } catch (JavaModelException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ // This happen when selection is an element from a jar
+ if(obj instanceof IJavaElement)
+ {
+ IJavaElement u = (IJavaElement)obj;
+ try {
+
+ reverseJavaElement(u);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ // This is a regular java file
+ if(obj instanceof IResource )
+ {
+ try {
+ reverseResource((IResource)obj);
+ } catch (ParseException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (CoreException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ System.out.println("reverse done");
+
+ }
}
diff --git a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/ReverseCodeHandler.java b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/ReverseCodeHandler.java index 626a5539c72..24c33edcde1 100644 --- a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/ReverseCodeHandler.java +++ b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/ReverseCodeHandler.java @@ -3,31 +3,16 @@ */
package org.eclipse.papyrus.java.reverse.ui;
-import japa.parser.ParseException;
-
import java.util.Arrays;
-import java.util.Iterator;
import java.util.List;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.jdt.core.ICompilationUnit;
-import org.eclipse.jdt.core.IJavaElement;
-import org.eclipse.jdt.core.IPackageFragment;
-import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.services.ServiceException;
@@ -36,7 +21,6 @@ import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers; import org.eclipse.papyrus.java.reverse.ui.dialog.ReverseCodeDialog;
import org.eclipse.papyrus.uml.tools.model.UmlUtils;
import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Package;
@@ -47,7 +31,7 @@ import org.eclipse.uml2.uml.Package; */
public class ReverseCodeHandler extends AbstractHandler implements IHandler {
- private static String DefaultGenerationPackageName = "generated";
+ private static String DefaultGenerationModeleName = "generated";
/**
* Method called when button is pressed.
@@ -59,19 +43,13 @@ public class ReverseCodeHandler extends AbstractHandler implements IHandler { final Resource umlResource = getUmlResource();
// String rootModelName = getRootModelName(umlResource);
- // Try to compute a uid identifying the model. Used to store user settings.
- String modelUid = umlResource.getURI().toPlatformString(true);
- if( modelUid == null)
- {
- System.err.println("Can't compute relatif model uid. Use absolute one");
- modelUid = umlResource.getURI().path();
- }
+ String modelUid = getModelUid(umlResource);
System.out.println("Model uid :" + modelUid);
// Get reverse parameters from a dialog
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getShell();
// ReverseCodeDialog dialog = new ReverseCodeDialog(shell, DefaultGenerationPackageName, Arrays.asList("generated") );
- ReverseCodeDialog dialog = new ReverseCodeDialog(shell, modelUid, null, null);
+ final ReverseCodeDialog dialog = getDialog(shell, modelUid);
int res = dialog.open();
// System.out.println("dialog result =" + res);
@@ -79,27 +57,9 @@ public class ReverseCodeHandler extends AbstractHandler implements IHandler { System.out.println("Canceled by user.");
return null;
}
- // Get returned name
- String generationPackageName = dialog.getValue();
- if(generationPackageName == null || generationPackageName.length() == 0) {
- generationPackageName = DefaultGenerationPackageName;
- }
-
- // Adjust name
- // generationPackageName = rootModelName + "/" + generationPackageName;
- // Create searchpaths. Add the rootmodelname as prefix.
- final List<String> searchPaths = Arrays.asList(dialog.getSearchPath());
- // final List<String> searchPaths = new ArrayList<String>();
- // for(String path : dialog.getSearchPath())
- // {
- // searchPaths.add( rootModelName + "/" + path);
- // }
-
- // Create revers object
-
// Execute the reverse with provided parameters
- final TransactionalEditingDomain editingDomain;
+ TransactionalEditingDomain editingDomain;
try {
editingDomain = getEditingDomain();
} catch (ServiceException e) {
@@ -108,129 +68,84 @@ public class ReverseCodeHandler extends AbstractHandler implements IHandler { throw new ExecutionException(e.getMessage());
}
- // Get current selection
- IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
- final ISelection selection = page.getSelection();
-
- final String pname = generationPackageName;
-
- // Instantiate a new Eclipse Job to run reverse
- Job job = new Job("Reverse Java Code") {
+ RecordingCommand command = new RecordingCommand(editingDomain, "Reverse Java Code") {
@Override
- protected IStatus run(final IProgressMonitor monitor) {
- // Instantiate the reverse.
- RecordingCommand command = new RecordingCommand(editingDomain, "Reverse Java Code") {
-
- @Override
- protected void doExecute() {
- executeCodeReverse(umlResource, pname, searchPaths, selection, monitor);
- }
-
- };
-
- // Run the reverse.
- editingDomain.getCommandStack().execute(command);
-
- return new Status(Status.OK, Activator.PLUGIN_ID, "Reverse done.");
+ protected void doExecute() {
+ ReverseCodeHandler.this.doExecute(dialog);
}
+
};
-
- // Run the reverse Job
- job.setUser(true);
- job.schedule();
+
+ editingDomain.getCommandStack().execute(command);
return null;
}
/**
- * Real Implementation of the command.
+ * Find the modelUid name contains into umlResource taken in parameter
*
- * @param generationPackageName
- * @param searchPaths
- * @param monitor the monitor progress bar from Eclipse Task
+ * @param umlResource
+ * @return the modelUid name
*/
- private void executeCodeReverse(Resource umlResource, String generationPackageName, List<String> searchPaths, ISelection selection, IProgressMonitor monitor) {
- System.out.println("executeCodeReverse()");
- JavaCodeReverse codeReverse = new JavaCodeReverse(getRootPackage(umlResource), generationPackageName, searchPaths);
-
- TreeSelection treeSelection = (TreeSelection)selection;
-
- // Notify to Eclipse that the job's began
- monitor.beginTask("Reverse Java Code", treeSelection.size());
- int workNumber = 0;
-
- // String filename = treeSelection.
- @SuppressWarnings("rawtypes")
- Iterator iter = treeSelection.iterator();
- while(iter.hasNext()) {
- Object obj = iter.next();
- // Translate java ICompilationUnit to Iresource
- if(obj instanceof ICompilationUnit) {
- ICompilationUnit u = (ICompilationUnit)obj;
- try {
- obj = u.getCorrespondingResource();
- } catch (JavaModelException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if(obj instanceof IPackageFragment)
- {
- IPackageFragment u = (IPackageFragment)obj;
- try {
- IResource res = u.getCorrespondingResource();
- if(res != null)
- obj = res;
- } catch (JavaModelException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // This happen when selection is an element from a jar
- if(obj instanceof IJavaElement)
- {
- IJavaElement u = (IJavaElement)obj;
- try {
-
- codeReverse.reverseJavaElement(u);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // This is a regular java file
- if(obj instanceof IResource )
- {
- try {
- codeReverse.reverseResource((IResource)obj);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (CoreException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- // Notify Eclipse that the work's in progress.
- monitor.worked(workNumber);
- workNumber++;
+ private String getModelUid(Resource umlResource) {
+ // Try to compute a uid identifying the model. Used to store user settings.
+ String modelUid = umlResource.getURI().toPlatformString(true);
+ if(modelUid == null) {
+ System.err.println("Can't compute relatif model uid. Use absolute one");
+ modelUid = umlResource.getURI().path();
}
+ return modelUid;
+ }
- // Notify that the Eclipse job's done.
- System.out.println("reverse done");
- monitor.done();
+ /**
+ * Command ran in a RecordingCommand, after the dialog
+ * Run the @link{JavaCodeReverse.executeCodeReverse}
+ * Shall be override to change command behavior
+ */
+ protected void doExecute(ReverseCodeDialog dialog) {
+ // Create searchpaths. Add the rootmodelname as prefix.
+ final List<String> searchPaths = Arrays.asList(dialog.getSearchPath());
+ Resource umlResource = getUmlResource();
+ String packageName = getPackageName(dialog);
+ JavaCodeReverse reverse = new JavaCodeReverse(getRootPackage(umlResource), packageName, searchPaths);
+ reverse.executeCodeReverse(umlResource, packageName, searchPaths);
}
+ /**
+ * The dialog used for user.
+ *
+ * @param shell
+ * @param modelUid
+ * @return the dialog to show to user
+ */
+ protected ReverseCodeDialog getDialog(Shell shell, String modelUid) {
+ return new ReverseCodeDialog(shell, modelUid, null, null);
+ }
+
+ /**
+ * Find the name of the model provided by the dialog
+ *
+ * @param dialog
+ * opened dialog to user
+ * @return the name of the model. If the user has changed this name, return the name provided by the user; return the default model name
+ * otherwise.
+ */
+ protected String getPackageName(ReverseCodeDialog dialog) {
+ String generationPackageName = dialog.getValue();
+ if(generationPackageName == null || generationPackageName.length() == 0) {
+ generationPackageName = DefaultGenerationModeleName;
+ }
+ return generationPackageName;
+ }
/**
* Get the uml resource used by the model.
*
- * @return
+ * @return the Uml Resource
*/
- private Resource getUmlResource() {
+ protected Resource getUmlResource() {
Resource umlResource = UmlUtils.getUmlModel().getResource();
return umlResource;
}
@@ -240,7 +155,7 @@ public class ReverseCodeHandler extends AbstractHandler implements IHandler { *
* @return
*/
- private Package getRootPackage(Resource umlResource) {
+ protected Package getRootPackage(Resource umlResource) {
Package rootPackage = (Package)umlResource.getContents().get(0);
return rootPackage;
}
diff --git a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/TreeSelectionList.java b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/TreeSelectionList.java new file mode 100644 index 00000000000..6939ea82213 --- /dev/null +++ b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/TreeSelectionList.java @@ -0,0 +1,153 @@ +/***************************************************************************** + * 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.LinkedList; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jface.viewers.TreeSelection; + +/** + * Transform a TreeSelection collection, provided by java resource selection, into a java List (java.util.List). <br> + * Add each element of TreeSelection which have to be displayed. i.e. include packages if <code>includePackages</code> == true; include compilation + * unit if <code>inclupdeCU</code> == true<br/> + * Note the each element of TreeSelection is a IJavaElement. + * + * @author Jonathan Geoffroy + * + */ +public class TreeSelectionList extends LinkedList<IJavaElement> { + + /** + * + */ + private static final long serialVersionUID = -5749380826507346600L; + + /** + * True if and only if this list includes packages + */ + private boolean includePackages; + + /** + * True if and only if this list includes packages + */ + private boolean includeCU; + + /** + * + * Constructor. + * + * @param treeSelection + * java resources selection + * @param includePackages + * True if and only if this list includes packages from <code>treeSelection</code> + * @param includeCU + * True if and only if this list includes compilation units from <code>treeSelection</code> + * @throws JavaModelException + */ + public TreeSelectionList(TreeSelection treeSelection, boolean includePackages, boolean includeCU) throws JavaModelException { + super(); + this.includePackages = includePackages; + this.includeCU = includeCU; + + // Add each treeSelection item into the list + addAll(treeSelection); + } + + /** + * + * @param selection + * @throws JavaModelException + */ + private void addAll(TreeSelection selection) throws JavaModelException { + Iterator<IJavaElement> selectionIterator = selection.iterator(); + while(selectionIterator.hasNext()) { + IJavaElement item = selectionIterator.next(); + scanJavaElement(item); + } + } + + /** + * Scan a Compilation Unit item + * Just add it if it's necessary + * + * @param item + * a selection compilation unit + */ + private void scanCU(ICompilationUnit item) { + if(includeCU) { + add(item); + } + } + + /** + * Scan a package. + * add it if it's necessary and cross its childs to display all sub-packages (and recursively) + * + * @param item + * the package to scan + * @throws JavaModelException + */ + private void scanPackage(IPackageFragment item) throws JavaModelException { + if(includePackages) { + add(item); + } + + // Recursive search in tree + for(IJavaElement child : item.getChildren()) { + scanJavaElement(child); + } + } + + /** + * Scan a source folder. + * add its childs (and recursively) by calling scanJavaElement on each child + * + * @param item + * the folder to scan + * @throws JavaModelException + */ + private void scanFolder(IPackageFragmentRoot item) throws JavaModelException { + // Recursive search in tree + for(IJavaElement child : item.getChildren()) { + scanJavaElement(child); + } + } + + /** + * scan and add a JavaElement by delegates to scan{typeOfJavaElement} + * + * @param item + * the item to display + * @throws JavaModelException + */ + private void scanJavaElement(IJavaElement item) throws JavaModelException { + switch(item.getElementType()) { + case IJavaElement.PACKAGE_FRAGMENT_ROOT: + scanFolder((IPackageFragmentRoot)item); + break; + case IJavaElement.PACKAGE_FRAGMENT: + scanPackage((IPackageFragment)item); + break; + case IJavaElement.COMPILATION_UNIT: + scanCU((ICompilationUnit)item); + break; + } + } +} diff --git a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dialog/DndReverseCodeDialog.java b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dialog/DndReverseCodeDialog.java new file mode 100644 index 00000000000..7b3df6c5fc4 --- /dev/null +++ b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dialog/DndReverseCodeDialog.java @@ -0,0 +1,93 @@ +package org.eclipse.papyrus.java.reverse.ui.dialog; + +import java.util.List; + +import org.eclipse.jface.dialogs.IDialogSettings; +import org.eclipse.papyrus.java.reverse.ui.Activator; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Shell; + + +public class DndReverseCodeDialog extends ReverseCodeDialog { + + private static final String DISPLAY_MODEL_UID = ":DISPLAY_MODEL"; //$NON-NLS-1$ + private static final String DISPLAY_PACKAGES_UID = ":DISPLAY_PACKAGES"; //$NON-NLS-1$ + private static final String DISPLAY_CU_UID = ":DISPLAY_CU"; //$NON-NLS-1$ + private Button displayModelCheckbox; + private Button displayPackagesCheckbox; + private Button displayCUCheckbox; + + private boolean displayModel; + private boolean displayPackages; + private boolean displayCU; + + public DndReverseCodeDialog(Shell parentShell, String modelUid, String initialValue, List<String> searchPathsInitialValues) { + super(parentShell, modelUid, initialValue, searchPathsInitialValues); + + } + + @Override + protected Control createDialogArea(Composite parent) { + IDialogSettings settings = Activator.getDefault().getDialogSettings(); + Composite composite = (Composite)super.createDialogArea(parent); + + // Create button to display or not the model + displayModelCheckbox = new Button(composite, SWT.CHECK); + displayModelCheckbox.setText("Display reversed Model into diagram"); + displayModelCheckbox.setSelection(settings.getBoolean(MODEL_UID + DISPLAY_MODEL_UID)); + + // Create button to display or not package + displayPackagesCheckbox = new Button(composite, SWT.CHECK); + displayPackagesCheckbox.setText("Display reversed UML::packages into diagram"); + displayPackagesCheckbox.setSelection(settings.getBoolean(MODEL_UID + DISPLAY_PACKAGES_UID)); + + // Create button to display or not CompilationUnit + displayCUCheckbox = new Button(composite, SWT.CHECK); + displayCUCheckbox.setText("Display UML::classes, UML::interfaces, UML::enumerations into diagram"); + displayCUCheckbox.setSelection(settings.getBoolean(MODEL_UID + DISPLAY_CU_UID)); + + return composite; + } + + @Override + protected void okPressed() { + // add selection into fields to prevent dialog disposing + displayModel = displayModelCheckbox.getSelection(); + displayPackages = displayPackagesCheckbox.getSelection(); + displayCU = displayCUCheckbox.getSelection(); + + // save selection into settings + IDialogSettings settings = Activator.getDefault().getDialogSettings(); + settings.put(MODEL_UID + DISPLAY_MODEL_UID, displayModel); + settings.put(MODEL_UID + DISPLAY_PACKAGES_UID, displayPackages); + settings.put(MODEL_UID + DISPLAY_CU_UID, displayCU); + super.okPressed(); + } + + /** + * + * @return true if displayModelCheckbox is checked + */ + public boolean displayModel() { + return displayModel; + } + + /** + * + * @return true if displayPackagesCheckbox is checked + */ + public boolean displayPackages() { + return displayPackages; + } + + /** + * + * @return true if displayCUCheckbox is checked + */ + public boolean displayCU() { + return displayCU; + } +} diff --git a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dialog/ReverseCodeDialog.java b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dialog/ReverseCodeDialog.java index f87d2b2f4b1..4406dbfb160 100644 --- a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dialog/ReverseCodeDialog.java +++ b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dialog/ReverseCodeDialog.java @@ -38,7 +38,7 @@ public class ReverseCodeDialog extends InputDialog { private String SEARCHPATHS_UID = ":searchpaths";
private String CREATIONPATHS_UID = ":creationpaths";
private static String DEFAULT_PACKAGE_NAME_UID = ":defaultpackagename";
- private String MODEL_UID = "nomodeluid";
+ protected String MODEL_UID = "nomodeluid";
private static String textMsg = "Default creation package.";
@SuppressWarnings("unused")
@@ -205,6 +205,7 @@ public class ReverseCodeDialog extends InputDialog { settings.put(MODEL_UID + CREATIONPATHS_UID, creationPaths);
settings.put(MODEL_UID + DEFAULT_PACKAGE_NAME_UID, getValue());
+
super.okPressed();
}
diff --git a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dnd/DropReverseCommand.java b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dnd/DropReverseCommand.java index 6fd08880aca..fd5dbad14c7 100755 --- a/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dnd/DropReverseCommand.java +++ b/extraplugins/java/org.eclipse.papyrus.java.reverse.ui/src/org/eclipse/papyrus/java/reverse/ui/dnd/DropReverseCommand.java @@ -23,6 +23,7 @@ import org.eclipse.gef.commands.Command; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.papyrus.java.reverse.ui.DndReverseCodeHandler; import org.eclipse.papyrus.java.reverse.ui.ReverseCodeHandler; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; @@ -78,7 +79,7 @@ public class DropReverseCommand extends Command { @Override public void execute() { - ReverseCodeHandler reverse = new ReverseCodeHandler(); + DndReverseCodeHandler reverse = new DndReverseCodeHandler(); try { reverse.execute(null); } catch (ExecutionException e) { |