Jelajahi Sumber

Added Highlighting in Subdecorator image tooltip

Andreas Mülder 11 tahun lalu
induk
melakukan
38da1e4bf4

+ 0 - 1
plugins/org.yakindu.sct.simulation.core/src/org/yakindu/sct/simulation/core/launch/AbstractSCTLaunchConfigurationDelegate.java

@@ -46,7 +46,6 @@ public abstract class AbstractSCTLaunchConfigurationDelegate extends LaunchConfi
 		SCTDebugTarget target = new SCTDebugTarget(launch, statechart, createExecutionContainer(launch, statechart));
 		launch.addDebugTarget(target);
 		target.start();
-
 	}
 
 	protected Statechart loadStatechart(String filename) {

+ 3 - 1
plugins/org.yakindu.sct.simulation.ui/META-INF/MANIFEST.MF

@@ -29,9 +29,11 @@ Require-Bundle: org.eclipse.ui,
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
 Bundle-ActivationPolicy: lazy
 Import-Package: org.eclipse.emf.edit.ui.provider,
+ org.eclipse.gmf.runtime.diagram.ui.render.util,
  org.eclipse.ui.dialogs,
  org.yakindu.sct.model.sgraph.provider
 Export-Package: org.yakindu.sct.simulation.ui,
  org.yakindu.sct.simulation.ui.preferences,
- org.yakindu.sct.simulation.ui.view
+ org.yakindu.sct.simulation.ui.view,
+ org.yakindu.sct.simulation.ui.view.decorator
 

+ 6 - 0
plugins/org.yakindu.sct.simulation.ui/plugin.xml

@@ -88,4 +88,10 @@
        point="org.eclipse.ui.startup">
     <startup></startup>
  </extension>
+    <!-- Decoration Providers -->
+   <extension point="org.eclipse.gmf.runtime.diagram.ui.decoratorProviders"> 
+		<decoratorProvider class="org.yakindu.sct.simulation.ui.view.decorator.HighlightingSubmachineDecorationProvider"> 
+			<Priority name="Medium" />
+ 		</decoratorProvider> 
+ </extension> 
 </plugin>

+ 20 - 3
plugins/org.yakindu.sct.simulation.ui/src/org/yakindu/sct/simulation/ui/model/presenter/SCTSourceDisplay.java

@@ -25,7 +25,9 @@ import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
 import org.eclipse.gmf.runtime.notation.Diagram;
 import org.eclipse.gmf.runtime.notation.NotationPackage;
 import org.eclipse.ui.IEditorDescriptor;
+import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
@@ -80,7 +82,24 @@ public class SCTSourceDisplay implements ISourceDisplay {
 
 	private IEditorPart openEditor(DebugElement debugElement, IWorkbenchPage page) {
 		EObject semanticObject = (EObject) debugElement.getAdapter(EObject.class);
+		IFile file = (IFile) debugElement.getAdapter(IFile.class);
+		if (file == null)
+			file = WorkspaceSynchronizer.getFile(semanticObject.eResource());
 
+		// check if an editor for the resource is already open, the return the
+		// opened editor.
+		// This is important for simulating subdiagrams
+		IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
+				.getActiveEditor();
+		if (activeEditor != null) {
+			IEditorInput editorInput = activeEditor.getEditorInput();
+			if (editorInput instanceof IFileEditorInput) {
+				if (((IFileEditorInput) editorInput).getFile().equals(file))
+					return activeEditor;
+			}
+		}
+		// check if a Diagram is available and open the editor for the
+		// coressponding diagram
 		Diagram diagram = DiagramPartitioningUtil.getDiagramContaining(semanticObject);
 		if (diagram != null) {
 			if (URIConverter.INSTANCE.exists(semanticObject.eResource().getURI(), null)) {
@@ -98,9 +117,6 @@ public class SCTSourceDisplay implements ISourceDisplay {
 			// No diagram for the semantic element -> open the default editor
 			// for the file
 		} else {
-			IFile file = (IFile) debugElement.getAdapter(IFile.class);
-			if (file == null)
-				file = WorkspaceSynchronizer.getFile(semanticObject.eResource());
 			IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
 			try {
 				return page.openEditor(new FileEditorInput(file), desc.getId());
@@ -108,6 +124,7 @@ public class SCTSourceDisplay implements ISourceDisplay {
 				e.printStackTrace();
 			}
 		}
+		// No editor found
 		throw new RuntimeException("No editor found for semantic element " + semanticObject);
 	}
 

+ 108 - 0
plugins/org.yakindu.sct.simulation.ui/src/org/yakindu/sct/simulation/ui/view/decorator/HighlightingSubmachineDecorationProvider.java

@@ -0,0 +1,108 @@
+/**
+ * Copyright (c) 2012 committers of YAKINDU 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.simulation.ui.view.decorator;
+
+import org.eclipse.core.runtime.PlatformObject;
+import org.eclipse.debug.core.DebugEvent;
+import org.eclipse.debug.core.DebugPlugin;
+import org.eclipse.debug.core.IDebugEventSetListener;
+import org.eclipse.debug.core.model.IDebugTarget;
+import org.eclipse.debug.ui.DebugUITools;
+import org.eclipse.debug.ui.contexts.DebugContextEvent;
+import org.eclipse.debug.ui.contexts.IDebugContextListener;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
+import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.swt.widgets.Display;
+import org.yakindu.sct.model.sgraph.State;
+import org.yakindu.sct.simulation.core.sruntime.ExecutionContext;
+import org.yakindu.sct.ui.editor.submachine.SubmachineDecorationProvider;
+
+/**
+ * Extends the SubmachineDecorationProvider with highlighting captabilities
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class HighlightingSubmachineDecorationProvider extends SubmachineDecorationProvider implements
+		IDebugContextListener, IDebugEventSetListener {
+
+	private static SimulationImageRenderer renderer = new SimulationImageRenderer();
+
+	protected IDebugTarget debugTarget;
+
+	public HighlightingSubmachineDecorationProvider() {
+		DebugUITools.getDebugContextManager().addDebugContextListener(this);
+		DebugPlugin.getDefault().addDebugEventListener(this);
+	}
+
+	public void createDecorators(IDecoratorTarget decoratorTarget) {
+		Object adapter = decoratorTarget.getAdapter(EObject.class);
+		if (adapter instanceof State)
+			decoratorTarget.installDecorator(DECORATOR_KEY, new HighlightingSubmachineDecorator(decoratorTarget));
+	}
+
+	public void handleDebugEvents(DebugEvent[] events) {
+		for (DebugEvent debugEvent : events) {
+			handleDebugEvent(debugEvent);
+		}
+	}
+
+	protected void handleDebugEvent(DebugEvent debugEvent) {
+		switch (debugEvent.getKind()) {
+		case DebugEvent.TERMINATE:
+			Display.getDefault().asyncExec(new Runnable() {
+				public void run() {
+					debugTarget = null;
+				}
+			});
+			break;
+		case DebugEvent.SUSPEND:
+			break;
+		case DebugEvent.RESUME:
+			break;
+		}
+	}
+
+	public void debugContextChanged(DebugContextEvent event) {
+		if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
+			PlatformObject object = (PlatformObject) ((IStructuredSelection) event.getContext()).getFirstElement();
+			if (object == null)
+				return;
+			IDebugTarget newTarget = (IDebugTarget) object.getAdapter(IDebugTarget.class);
+			if (newTarget != debugTarget && newTarget != null && !newTarget.isTerminated()) {
+				debugTarget = newTarget;
+			}
+		}
+	}
+
+	public final class HighlightingSubmachineDecorator extends SubmachineDecorator {
+
+		public HighlightingSubmachineDecorator(IDecoratorTarget decoratorTarget) {
+			super(decoratorTarget);
+		}
+
+		@Override
+		protected Diagram getTooltipDiagramToRender(State state) {
+			Diagram diagram = super.getTooltipDiagramToRender(state);
+			if (debugTarget != null) {
+				// We have to to reload the diagram cause we change the styles
+				diagram = renderer.getDiagramCopy(diagram);
+				ExecutionContext context = (ExecutionContext) debugTarget.getAdapter(ExecutionContext.class);
+				renderer.highlightActiveStates(context, diagram);
+			}
+			return diagram;
+		}
+	}
+}

+ 163 - 0
plugins/org.yakindu.sct.simulation.ui/src/org/yakindu/sct/simulation/ui/view/decorator/SimulationImageRenderer.java

@@ -0,0 +1,163 @@
+/**
+ * Copyright (c) 2012 committers of YAKINDU 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.simulation.ui.view.decorator;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.common.util.TreeIterator;
+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.Resource.Factory;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryImpl;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.transaction.util.TransactionUtil;
+import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
+import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.gmf.runtime.diagram.ui.render.util.DiagramRenderUtil;
+import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
+import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.gmf.runtime.notation.Node;
+import org.eclipse.gmf.runtime.notation.NotationPackage;
+import org.eclipse.gmf.runtime.notation.ShapeStyle;
+import org.eclipse.jface.preference.PreferenceConverter;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.swt.widgets.Display;
+import org.yakindu.sct.model.sgraph.RegularState;
+import org.yakindu.sct.simulation.core.sruntime.ExecutionContext;
+import org.yakindu.sct.simulation.ui.SimulationActivator;
+import org.yakindu.sct.simulation.ui.preferences.SimulationPreferenceConstants;
+
+/**
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class SimulationImageRenderer {
+	/**
+	 * Renderes an image with highlighted states
+	 * 
+	 * @param statechart
+	 * @param context
+	 * @return
+	 */
+
+	public Image renderImage(IFile file, ExecutionContext context) {
+		Diagram diagram = getDiagramCopy(file);
+		return renderImage(diagram);
+	}
+
+	public Image renderImage(Diagram diagram, ExecutionContext context) {
+		highlightActiveStates(context, diagram);
+		return renderImage(diagram);
+
+	}
+
+	protected Image renderImage(Diagram diagram) {
+		Assert.isNotNull(diagram);
+		return DiagramRenderUtil.renderToSWTImage(diagram);
+	}
+
+	public void highlightActiveStates(final ExecutionContext context, final Diagram diagram) {
+		AbstractTransactionalCommand cmd = new AbstractTransactionalCommand(TransactionUtil.getEditingDomain(diagram),
+				"", null) {
+
+			@Override
+			protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info)
+					throws ExecutionException {
+				// Init colors from preferences
+				RGB rgbforeGround = PreferenceConverter.getColor(SimulationActivator.getDefault().getPreferenceStore(),
+						SimulationPreferenceConstants.STATE_FOREGROUND_HIGHLIGHTING_COLOR);
+				RGB rgbBackGround = PreferenceConverter.getColor(SimulationActivator.getDefault().getPreferenceStore(),
+						SimulationPreferenceConstants.STATE_BACKGROUND_HIGHLIGHTING_COLOR);
+				Color color = new Color(Display.getDefault(), rgbforeGround);
+				Integer foreGround = FigureUtilities.colorToInteger(color);
+				color.dispose();
+				color = new Color(Display.getDefault(), rgbBackGround);
+				Integer background = FigureUtilities.colorToInteger(color);
+				color.dispose();
+				
+				// Set styling
+				TreeIterator<EObject> eAllContents = diagram.eAllContents();
+				while (eAllContents.hasNext()) {
+					EObject next = eAllContents.next();
+					if (next instanceof Node) {
+						List<RegularState> activeStates = context.getActiveStates();
+						for (RegularState regularState : activeStates) {
+							EObject element = ((Node) next).getElement();
+							if (EcoreUtil.getURI(regularState).equals(EcoreUtil.getURI(element))) {
+								ShapeStyle style = (ShapeStyle) ((Node) next)
+										.getStyle(NotationPackage.Literals.SHAPE_STYLE);
+								if (style != null) {
+									style.setFillColor(background);
+									style.setLineColor(foreGround);
+								}
+							}
+						}
+					}
+				}
+				return CommandResult.newOKCommandResult();
+			}
+		};
+		try {
+			cmd.execute(new NullProgressMonitor(), null);
+		} catch (ExecutionException e) {
+			e.printStackTrace();
+		}
+	}
+
+	public Diagram getDiagramCopy(IFile file) {
+		Resource resource = reload(file);
+		Diagram diagram = (Diagram) resource.getContents().get(1);
+		return diagram;
+	}
+
+	private Resource reload(IFile file) {
+		final URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
+		Factory factory = ResourceFactoryRegistryImpl.INSTANCE.getFactory(uri);
+		Resource resource = factory.createResource(uri);
+		ResourceSet resourceSet = new ResourceSetImpl();
+		TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain(resourceSet);
+		resourceSet.getResources().add(resource);
+		try {
+			resource.load(Collections.EMPTY_MAP);
+		} catch (IOException e) {
+			throw new IllegalStateException("Error loading resource", e);
+		}
+		return resource;
+	}
+
+	public Diagram getDiagramCopy(Diagram diagram) {
+		Resource resource = reload(WorkspaceSynchronizer.getFile(diagram.eResource()));
+		EList<EObject> contents = resource.getContents();
+		for (EObject eObject : contents) {
+			if (EcoreUtil.getURI(diagram).equals(EcoreUtil.getURI(eObject)))
+				return (Diagram) eObject;
+		}
+		// Fall back return the first diagram
+		return (Diagram) resource.getContents().get(1);
+	}
+}

+ 3 - 2
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/submachine/SubmachineDecorationProvider.java

@@ -38,7 +38,7 @@ import de.itemis.gmf.runtime.commons.decorators.AbstractDecoratorProvider;
  */
 public class SubmachineDecorationProvider extends AbstractDecoratorProvider implements IDecoratorProvider {
 
-	private static final String DECORATOR_KEY = SubmachineDecorator.class.getSimpleName();
+	protected static final String DECORATOR_KEY = SubmachineDecorator.class.getSimpleName();
 
 	public void createDecorators(IDecoratorTarget decoratorTarget) {
 		Object adapter = decoratorTarget.getAdapter(EObject.class);
@@ -55,7 +55,8 @@ public class SubmachineDecorationProvider extends AbstractDecoratorProvider impl
 		@Override
 		protected boolean shouldDecorate(State state) {
 			IGraphicalEditPart adapter = (IGraphicalEditPart) getDecoratorTarget().getAdapter(IGraphicalEditPart.class);
-			BooleanValueStyle style = GMFNotationUtil.getBooleanValueStyle(adapter.getNotationView(), DiagramPartitioningUtil.INLINE_STYLE);
+			BooleanValueStyle style = GMFNotationUtil.getBooleanValueStyle(adapter.getNotationView(),
+					DiagramPartitioningUtil.INLINE_STYLE);
 			return style == null ? false : !style.isBooleanValue();
 		}