Skip to content

Commit

Permalink
Added context menu entry to generate class diagram file (#4).
Browse files Browse the repository at this point in the history
.klaviz files can know be generated from Package Explorer selections via
context menu entry. This replaces the synthesize class diagram
functionality.
  • Loading branch information
Carsten Sprung committed Feb 22, 2016
1 parent 0e60984 commit ccbac05
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 442 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion plugins/de.cau.cs.kieler.klassviz/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 6 additions & 34 deletions plugins/de.cau.cs.kieler.klassviz/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any?after=additions">
<command
commandId="de.cau.cs.kieler.klassviz.synthesizeclassdiagramofselection"
commandId="de.cau.cs.kieler.klassviz.generateKlassVizFile"
icon="icons/class.gif"
label="Class Diagram from Selection"
label="Generate class diagram file from selection"
style="push"
tooltip="Automatically generate a class diagram containing the selected classes and members">
tooltip="Automatically generate a class diagram file containing the selected classes and members.">
<visibleWhen
checkEnabled="false">
<iterate
Expand All @@ -30,42 +30,14 @@
</iterate>
</visibleWhen>
</command>
<command
commandId="de.cau.cs.kieler.klassviz.getpreviousselectionofproject"
icon="icons/class.gif"
label="Restore Project&apos;s Previous Class Diagram"
style="push"
tooltip="Restore the last used selection of classes for generating a class diagram">
<visibleWhen
checkEnabled="false">
<and>
<count
value="1">
</count>
<iterate
ifEmpty="false"
operator="and">
<test
forcePluginActivation="true"
property="de.cau.cs.kieler.klassviz.containsStoredSelection">
</test>
</iterate>
</and>
</visibleWhen>
</command>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="de.cau.cs.kieler.klassviz.handlers.SynthesizeClassDiagramHandler"
id="de.cau.cs.kieler.klassviz.synthesizeclassdiagramofselection"
name="Class Diagram from Selection">
</command>
<command
defaultHandler="de.cau.cs.kieler.klassviz.handlers.RestoreSelectionHandler"
id="de.cau.cs.kieler.klassviz.getpreviousselectionofproject"
name="Restore Project's Previous Class Diagram">
defaultHandler="de.cau.cs.kieler.klassviz.handlers.GenerateKlassVizFileHandler"
id="de.cau.cs.kieler.klassviz.generateKlassVizFile"
name="Generate class diagram file from selection">
</command>
</extension>
<extension
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* 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 org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
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.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.IStructuredSelection;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.SaveAsDialog;
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;

/**
* Handler to generate a KlassViz file from selected java packages, classes and/or classmembers.
* The save location is queried from the user in a save as dialog and the selected items are serialized
* into a .klaviz file with then can be visualized as a class diagram.
*/
public final class GenerateKlassVizFileHandler extends AbstractHandler {

static final String PLUGIN_ID = "de.cau.cs.kieler.klassviz";
final ClassdataFactory factory = ClassdataFactory.eINSTANCE;


/**
* {@inheritDoc}
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
KClassModel classModel = generateClassModel(selection);
URI fileUri = URI.createPlatformResourceURI(handleWorkspaceBrowse(), true);
saveclassModel(classModel, fileUri);
return null;
}

/**
* Generate a {@link KClassModel} from the given selection.
*
* @param selection
* the selected java elements to generate the model from.
* @return the generated model.
*/
private KClassModel generateClassModel(IStructuredSelection selection) {

// Transform the model
JdtModelTransformation transformation = Guice.createInjector().getInstance(
JdtModelTransformation.class);
KClassModel classModel;
try {
classModel = transformation.transform(selection);
} 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 serialized.
if (!classModel.getPackages().isEmpty()) {
// Save the selection.
HashSet<IJavaProject> projects = new HashSet<IJavaProject>();
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;
}
}
}
Loading

0 comments on commit ccbac05

Please sign in to comment.