From ccbac05e1b4246c98e8b218577ea0aaafdcb435a Mon Sep 17 00:00:00 2001 From: Carsten Sprung Date: Mon, 22 Feb 2016 14:50:28 +0100 Subject: [PATCH] Added context menu entry to generate class diagram file (#4). .klaviz files can know be generated from Package Explorer selections via context menu entry. This replaces the synthesize class diagram functionality. --- .../ClassDataSemanticSequencer.java | 24 ++ .../META-INF/MANIFEST.MF | 3 +- plugins/de.cau.cs.kieler.klassviz/plugin.xml | 40 +--- .../ContainsStoredSelectionTester.java | 57 ----- .../handlers/GenerateKlassVizFileHandler.java | 162 ++++++++++++++ .../handlers/RestoreSelectionHandler.java | 209 ------------------ .../SynthesizeClassDiagramHandler.java | 141 ------------ 7 files changed, 194 insertions(+), 442 deletions(-) delete mode 100644 plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/ContainsStoredSelectionTester.java create mode 100644 plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/GenerateKlassVizFileHandler.java delete mode 100644 plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/RestoreSelectionHandler.java delete mode 100644 plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/SynthesizeClassDiagramHandler.java diff --git a/plugins/de.cau.cs.kieler.klassviz.text/src/de/cau/cs/kieler/klassviz/text/serializer/ClassDataSemanticSequencer.java b/plugins/de.cau.cs.kieler.klassviz.text/src/de/cau/cs/kieler/klassviz/text/serializer/ClassDataSemanticSequencer.java index 5f0ac58..f0b3b79 100644 --- a/plugins/de.cau.cs.kieler.klassviz.text/src/de/cau/cs/kieler/klassviz/text/serializer/ClassDataSemanticSequencer.java +++ b/plugins/de.cau.cs.kieler.klassviz.text/src/de/cau/cs/kieler/klassviz/text/serializer/ClassDataSemanticSequencer.java @@ -16,6 +16,8 @@ import org.eclipse.emf.ecore.EObject; +import de.cau.cs.kieler.klassviz.model.classdata.KField; +import de.cau.cs.kieler.klassviz.model.classdata.KMethod; import de.cau.cs.kieler.klassviz.model.classdata.KTypeReference; public class ClassDataSemanticSequencer extends AbstractClassDataSemanticSequencer { @@ -37,4 +39,26 @@ protected void sequence_KTypeReference(EObject context, KTypeReference typeRef) // Restore the original signature. typeRef.setSignature(origSig); } + + /** + * {@inheritDoc} + */ + @Override + protected void sequence_KField(EObject context, KField semanticObject) { + // Only serialize fields that are explicitly selected. + if (semanticObject.isSelected()) { + super.sequence_KField(context, semanticObject); + } + } + + /** + * {@inheritDoc} + */ + @Override + protected void sequence_KMethod(EObject context, KMethod semanticObject) { + // Only serialize fields that are explicitly selected. + if (semanticObject.isSelected()) { + super.sequence_KMethod(context, semanticObject); + } + } } diff --git a/plugins/de.cau.cs.kieler.klassviz/META-INF/MANIFEST.MF b/plugins/de.cau.cs.kieler.klassviz/META-INF/MANIFEST.MF index a50a4f0..b241dc2 100644 --- a/plugins/de.cau.cs.kieler.klassviz/META-INF/MANIFEST.MF +++ b/plugins/de.cau.cs.kieler.klassviz/META-INF/MANIFEST.MF @@ -21,7 +21,8 @@ Require-Bundle: com.google.guava, de.cau.cs.kieler.klighd;bundle-version="0.6.0", de.cau.cs.kieler.klighd.ui, de.cau.cs.kieler.klassviz.model, - de.cau.cs.kieler.klighd.ui.view;bundle-version="0.2.0" + de.cau.cs.kieler.klighd.ui.view;bundle-version="0.2.0", + org.eclipse.ui.ide Bundle-RequiredExecutionEnvironment: J2SE-1.5 Import-Package: org.eclipse.jdt.internal.ui.packageview, org.eclipse.jdt.ui diff --git a/plugins/de.cau.cs.kieler.klassviz/plugin.xml b/plugins/de.cau.cs.kieler.klassviz/plugin.xml index e803528..7f17feb 100644 --- a/plugins/de.cau.cs.kieler.klassviz/plugin.xml +++ b/plugins/de.cau.cs.kieler.klassviz/plugin.xml @@ -14,11 +14,11 @@ allPopups="false" locationURI="popup:org.eclipse.ui.popup.any?after=additions"> + tooltip="Automatically generate a class diagram file containing the selected classes and members."> - - - - - - - - - - - - - - + defaultHandler="de.cau.cs.kieler.klassviz.handlers.GenerateKlassVizFileHandler" + id="de.cau.cs.kieler.klassviz.generateKlassVizFile" + name="Generate class diagram file from selection"> projects = new HashSet(); + Iterator selectionIter = selection.iterator(); + while (selectionIter.hasNext()) { + Object obj = selectionIter.next(); + if (obj instanceof IJavaElement) { + projects.add(((IJavaElement) obj).getJavaProject()); + } + } + for (IJavaProject project : projects) { + classModel.getJavaProjects().add(project.getElementName()); + } + } + return classModel; + } + + /** + * Save the given class model at the given URI. + * + * @param classModel + * the classmodel to save. + * @param fileUri + * the URI to save the model to. + */ + private void saveclassModel(KClassModel classModel, URI fileUri) { + if (classModel == null || fileUri == null) { + return; + } + + // Create a resource set. + ResourceSet resourceSet = new ResourceSetImpl(); +// ResourceSet testResourceSet = new ResourceSetImpl(); + // Create a resource for this file. + Resource resource = resourceSet.createResource(fileUri); +// Resource testResource = testResourceSet.createResource(URI.createFileURI("asdf/model.xmi")); + // Add the model object to the contents. + resource.getContents().add(classModel); +// testResource.getContents().add(classModel); + + // Save the contents of the resource to the file system. + try { + resource.save(Collections.EMPTY_MAP); +// testResource.save(Collections.EMPTY_MAP); + } catch (IOException exception) { + IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, + "Could not save selection to project meta data.", exception); + StatusManager.getManager().handle(status); + } + } + + /** + * Query the user for a file path and name. + * + * @return the file URI as a string with extension {@code ".klaviz"}. + */ + private String handleWorkspaceBrowse() { + // TODO a better workspace selection dialog would be good, but it seems + // such a thing does not exist in Eclipse for some reason + final SaveAsDialog fileDialog = + new SaveAsDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); + final int status = fileDialog.open(); + if (status == SaveAsDialog.OK) { + final IPath filePath = fileDialog.getResult(); + return filePath.removeFileExtension().addFileExtension("klaviz").toString(); + } else { + return null; + } + } +} diff --git a/plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/RestoreSelectionHandler.java b/plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/RestoreSelectionHandler.java deleted file mode 100644 index 2e14627..0000000 --- a/plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/RestoreSelectionHandler.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * KlassViz - Kieler Class Diagram Visualization - * - * A part of OpenKieler - * https://github.com/OpenKieler - * - * Copyright 2014 by - * + Christian-Albrechts-University of Kiel - * + Department of Computer Science - * + Real-Time and Embedded Systems Group - * - * This code is provided under the terms of the Eclipse Public License (EPL). - * See the file epl-v10.html for the license text. - */ -package de.cau.cs.kieler.klassviz.handlers; - -import java.util.ArrayList; -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.resources.IProject; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Status; -import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; -import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.IType; -import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.core.Signature; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.viewers.StructuredSelection; -import org.eclipse.ui.IViewPart; -import org.eclipse.ui.IWorkbenchPage; -import org.eclipse.ui.PartInitException; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.handlers.HandlerUtil; -import org.eclipse.ui.statushandlers.StatusManager; - -import de.cau.cs.kieler.klassviz.model.classdata.KClassModel; -import de.cau.cs.kieler.klassviz.model.classdata.KField; -import de.cau.cs.kieler.klassviz.model.classdata.KMethod; -import de.cau.cs.kieler.klassviz.model.classdata.KPackage; -import de.cau.cs.kieler.klassviz.model.classdata.KType; -import de.cau.cs.kieler.klassviz.model.classdata.KTypeReference; -import de.cau.cs.kieler.klighd.ui.DiagramViewManager; - -/** - * This class checks which element is selected to restore the selection of the Java project that - * contains this element. The data is taken from the XMI file referred to this project. After that - * it also starts a diagram synthesis in Xtend. - */ -public final class RestoreSelectionHandler extends AbstractHandler { - - static final String PLUGIN_ID = "de.cau.cs.kieler.klassviz"; - - static URI getStoredSelectionURI(IPath projectPath) { - return URI.createURI("platform:/meta/" + PLUGIN_ID + projectPath + "/selection.xmi"); - } - - public Object execute(ExecutionEvent event) throws ExecutionException { - // Get the Java project of the first item in the selection. - ISelection selection = HandlerUtil.getCurrentSelection(event); - Object selectedElement = ((IStructuredSelection) selection).getFirstElement(); - IPath projectPath = null; - IJavaProject javaProject = null; - try { - if (selectedElement instanceof IProject) { - IProject project = (IProject) selectedElement; - projectPath = project.getFullPath(); - if (project.hasNature(JavaCore.NATURE_ID)) { - javaProject = JavaCore.create(project); - } - } else if (selectedElement instanceof IJavaElement) { - IJavaElement javaElement = (IJavaElement) selectedElement; - projectPath = javaElement.getJavaProject().getPath(); - javaProject = javaElement.getJavaProject(); - } - if (projectPath == null) { - return null; - } - - // Load the class data of the URI referred to the project. - ResourceSet resourceSet = new ResourceSetImpl(); - URI platformURI = getStoredSelectionURI(projectPath); - EObject modelObject; - try { - Resource resource = resourceSet.getResource(platformURI, true); - modelObject = resource.getContents().get(0); - } catch (RuntimeException exception) { - IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, - "There is no stored class selection for this project.", exception); - StatusManager.getManager().handle(status, StatusManager.SHOW); - return null; - } - - if (modelObject instanceof KClassModel) { - KClassModel classDataSelection = (KClassModel) modelObject; - List toBeSelectedElements = new ArrayList(); - // Restore the full data of the previously selected types, - // including their fields and methods in the model. Also add the - // the previously selected types, fields and methods to - // 'toBeSelectedElements'. - for (KPackage kPackage : classDataSelection.getPackages()) { - for (KType kType : kPackage.getTypes()) { - if (kType.isSelected()) { - IType jdtType = javaProject.findType(getQualifiedName(kType)); - if (jdtType != null) { - toBeSelectedElements.add(jdtType); - for (KField kField : kType.getFields()) { - // If field was previously selected add it to - // 'toBeSelectedElements'. - if (kField.isSelected()) { - toBeSelectedElements.add(jdtType.getField(kField.getName())); - } - } - for (KMethod kMethod : kType.getMethods()) { - // Get method based on name and parameter types. - // If method was previously selected add it to - // 'toBeSelectedElements'. - if (kMethod.isSelected()) { - toBeSelectedElements.add(jdtType.getMethod(kMethod.getName(), - getMethodParameters(kMethod))); - } - } - } - } - } - } - ISelection newSelection = new StructuredSelection(toBeSelectedElements.toArray()); - - // restore the selection respectively which view is focused - // right now (package explorer or project explorer) - IWorkbenchPage wbp = PlatformUI.getWorkbench().getActiveWorkbenchWindow() - .getActivePage(); - String id = wbp.getActivePartReference().getId(); - IViewPart view = wbp.showView(id); - - view.getSite().getSelectionProvider().setSelection(newSelection); - - // Start the synthesis with Xtend and visualize with KlighD. - DiagramViewManager.createView( - "de.cau.cs.kieler.klassviz.ClassDataDiagramSynthesis", - javaProject.getElementName(), classDataSelection, null); - } - } catch (JavaModelException exception) { - IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, - "Error while restoring previous selection.", exception); - StatusManager.getManager().handle(status, StatusManager.SHOW); - } catch (PartInitException exception) { - IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, - "Error while activating view.", exception); - StatusManager.getManager().handle(status, StatusManager.SHOW); - } catch (CoreException exception) { - StatusManager.getManager().handle(exception, PLUGIN_ID); - } - - return null; - } - - /** - * Get all method parameters based on the fully qualified name of the type of each parameter. So - * the method can be unmistakely identified. - * - * @param eMethod a method - * @return an array of parameter type signatures - */ - private String[] getMethodParameters(KMethod eMethod) { - List parameterSignatureTypes = new ArrayList(); - for (KTypeReference typeReference : eMethod.getParameters()) { - parameterSignatureTypes.add(Signature.createTypeSignature( - Signature.getSimpleName(typeReference.getSignature()), false)); - } - return parameterSignatureTypes.toArray(new String[0]); - } - - /** - * Create a qualified name for the given type. - * - * @param type a type - * @return a qualified name for the type - */ - private String getQualifiedName(KType type) { - StringBuilder result = new StringBuilder(type.getName()); - EObject container = type.eContainer(); - while (!(container instanceof KPackage) && container != null) { - if (container instanceof KType) { - result.insert(0, '$'); - result.insert(0, ((KType) container).getName()); - } - container = container.eContainer(); - } - if (container instanceof KPackage) { - result.insert(0, '.'); - result.insert(0, ((KPackage) container).getName()); - } - return result.toString(); - } - -} diff --git a/plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/SynthesizeClassDiagramHandler.java b/plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/SynthesizeClassDiagramHandler.java deleted file mode 100644 index 043c512..0000000 --- a/plugins/de.cau.cs.kieler.klassviz/src/de/cau/cs/kieler/klassviz/handlers/SynthesizeClassDiagramHandler.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * KlassViz - Kieler Class Diagram Visualization - * - * A part of OpenKieler - * https://github.com/OpenKieler - * - * Copyright 2014 by - * + Christian-Albrechts-University of Kiel - * + Department of Computer Science - * + Real-Time and Embedded Systems Group - * - * This code is provided under the terms of the Eclipse Public License (EPL). - * See the file epl-v10.html for the license text. - */ -package de.cau.cs.kieler.klassviz.handlers; - -import java.io.IOException; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Status; -import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; -import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.ui.handlers.HandlerUtil; -import org.eclipse.ui.statushandlers.StatusManager; - -import com.google.inject.Guice; - -import de.cau.cs.kieler.klassviz.model.classdata.ClassdataFactory; -import de.cau.cs.kieler.klassviz.model.classdata.KClassModel; -import de.cau.cs.kieler.klassviz.synthesis.JdtModelTransformation; -import de.cau.cs.kieler.klighd.ui.DiagramViewManager; - -/** - * This class initiates the class diagram synthesis. Therefore a selection of classes, fields and - * methods, that is made via the package or project explorer is transferred to a metamodel. After - * that is done the selection is saved in the metadata of the plug-in. Then it starts a - * diagram synthesis in Xtend. - */ -public final class SynthesizeClassDiagramHandler extends AbstractHandler { - - static final String PLUGIN_ID = "de.cau.cs.kieler.klassviz"; - final ClassdataFactory factory = ClassdataFactory.eINSTANCE; - - /** - * Initiates the class diagram synthesis. Therefore a selection of classes, fields and methods, - * that is made via the package or project explorer is transferred to a metamodel. After that is - * done the selection is saved in the metadata of the plug-in. Then it starts a diagram synthesis - * in Xtend. - */ - public Object execute(ExecutionEvent event) throws ExecutionException { - - // Get the Selection - ISelection selection = HandlerUtil.getCurrentSelection(event); - IStructuredSelection sSelection = (IStructuredSelection) selection; - - // Transform the model - JdtModelTransformation transformation = Guice.createInjector().getInstance( - JdtModelTransformation.class); - KClassModel classModel; - try { - classModel = transformation.transform(sSelection); - } catch (JavaModelException exception) { - IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, - "Error while transforming Java model.", exception); - StatusManager.getManager().handle(status, StatusManager.SHOW); - return null; - } - - // When the List is still empty, there was no IJavaElement in the - // Selection and nothing can be visualized. - if (!classModel.getPackages().isEmpty()) { - - // Save the selection that will be visualized. - HashSet projects = new HashSet(); - Iterator selectionIter = sSelection.iterator(); - while (selectionIter.hasNext()) { - Object obj = selectionIter.next(); - if (obj instanceof IJavaElement) { - projects.add(((IJavaElement) obj).getJavaProject()); - } - } - for (IJavaProject project : projects) { - classModel.getJavaProjects().add(project.getElementName()); - } - saveSelection(classModel, projects); - - // Start synthesis with Xtend and visualize with KlighD. - String viewTitle = projects.iterator().next().getElementName(); - DiagramViewManager.createView( - "de.cau.cs.kieler.klassviz.ClassDataDiagramSynthesis", - viewTitle, classModel, null); - } - - return null; - } - - /** - * Save the selection in the plug-ins folder of the projects that has an selected element. - * - * @param classModel the class model - * @param projects the set of projects that have been selected - */ - private void saveSelection(KClassModel classModel, Set projects) { - // Create a resource set. - ResourceSet resourceSet = new ResourceSetImpl(); - - for (IJavaProject javaProject : projects) { - // Path is: /metadata of the plugin/javaproject/selection.xmi - URI platformURI = RestoreSelectionHandler.getStoredSelectionURI(javaProject.getPath()); - - // Create a resource for this file. - Resource resource = resourceSet.createResource(platformURI); - - // Add the model objects to the contents. - resource.getContents().add(classModel); - - // Save the contents of the resource to the file system. - try { - resource.save(Collections.EMPTY_MAP); - } catch (IOException exception) { - IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, - "Could not save selection to project meta data.", exception); - StatusManager.getManager().handle(status); - } - } - } -}