Browse Source

Merge pull request #751 from Yakindu/issue_680

#680 added Transition Priority Labels, disabled by default
Andreas Mülder 9 years ago
parent
commit
f26501a8a1

+ 5 - 0
plugins/org.yakindu.sct.ui.editor/plugin.xml

@@ -459,6 +459,11 @@
 			<Priority name="Lowest" />
  		</decoratorProvider> 
  </extension> 
+ <extension point="org.eclipse.gmf.runtime.diagram.ui.decoratorProviders"> 
+		<decoratorProvider class="org.yakindu.sct.ui.editor.providers.TransitionPriorityDecorationProvider"> 
+			<Priority name="Lowest" />
+ 		</decoratorProvider> 
+ </extension>  
    
    <!-- Global Action handler -->
     <extension point="org.eclipse.gmf.runtime.common.ui.services.action.globalActionHandlerProviders" id="global-print-actions">

+ 45 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/editor/figures/PriorityFigure.java

@@ -0,0 +1,45 @@
+/**
+ * Copyright (c) 2016 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.ui.editor.editor.figures;
+
+import org.eclipse.draw2d.ColorConstants;
+import org.eclipse.draw2d.Ellipse;
+import org.eclipse.draw2d.Label;
+import org.eclipse.draw2d.StackLayout;
+import org.eclipse.gmf.runtime.draw2d.ui.mapmode.IMapMode;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Font;
+
+/**
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class PriorityFigure extends Ellipse {
+
+	private static final Font SMALL_FONT = new Font(null, "Arial", 7, SWT.NORMAL);
+	private static final Font NORMAL_FONT = new Font(null, "Arial", 8, SWT.NORMAL);
+	protected IMapMode mapMode;
+
+	public PriorityFigure(IMapMode mapMode, int priority) {
+		this.setLayoutManager(new StackLayout());
+		setForegroundColor(ColorConstants.black);
+		setBackgroundColor(ColorConstants.white);
+		this.setOutline(false);
+		setOpaque(false);
+		setFill(true);
+		this.setLineWidth(-1);
+		Label label = new Label(String.valueOf(priority));
+		label.setFont(priority > 9 ? SMALL_FONT : NORMAL_FONT);
+		add(label);
+	}
+
+}

+ 5 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/preferences/PreferenceInitializer.java

@@ -66,6 +66,11 @@ public class PreferenceInitializer extends DiagramPreferenceInitializer
 		PreferenceConverter.setDefault(getPreferenceStore(),
 				StatechartPreferenceConstants.PREF_REGION_LINE,
 				StatechartColorConstants.REGION_LINE_COLOR.getRGB());
+		
+		//Transition Priority Labels
+		getPreferenceStore().setDefault(
+				StatechartPreferenceConstants.PREF_PRIORITY_LABELS, false);
+		
 
 	}
 

+ 9 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/preferences/StatechartAppearancePreferencePage.java

@@ -13,6 +13,7 @@ package org.yakindu.sct.ui.editor.preferences;
 import org.eclipse.gmf.runtime.common.ui.preferences.ComboFieldEditor;
 import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramUIMessages;
 import org.eclipse.gmf.runtime.diagram.ui.preferences.IPreferenceConstants;
+import org.eclipse.jface.preference.BooleanFieldEditor;
 import org.eclipse.jface.preference.ColorFieldEditor;
 import org.eclipse.jface.preference.FieldEditorPreferencePage;
 import org.eclipse.jface.preference.IPreferenceStore;
@@ -49,6 +50,14 @@ public class StatechartAppearancePreferencePage extends FieldEditorPreferencePag
 		Composite main = createPageLayout(parent);
 		createColorEditors(main);
 		createLineStyleEditors(main);
+		createPriorityLabelEditor(main);
+	}
+
+	protected void createPriorityLabelEditor(Composite main) {
+		Composite composite = createGroupComposite(main, "Transition Decorator");
+		BooleanFieldEditor editor = new BooleanFieldEditor(StatechartPreferenceConstants.PREF_PRIORITY_LABELS,
+				"Show transition priority", composite);
+		addField(editor);
 	}
 
 	protected void createLineStyleEditors(Composite parent) {

+ 1 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/preferences/StatechartPreferenceConstants.java

@@ -17,4 +17,5 @@ public interface StatechartPreferenceConstants extends IPreferenceConstants {
 	public static final String PREF_STATE_LINE = "Appearance.state.line";
 	public static final String PREF_REGION_BACKGROUND = "Appearance.region.background";
 	public static final String PREF_REGION_LINE = "Appearance.region.line";
+	public static final String PREF_PRIORITY_LABELS = "Appearance.transition.priority";
 }

+ 134 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/providers/TransitionPriorityDecorationProvider.java

@@ -0,0 +1,134 @@
+/**
+ * Copyright (c) 2016 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.ui.editor.providers;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.gef.EditDomain;
+import org.eclipse.gef.EditPart;
+import org.eclipse.gef.editparts.AbstractConnectionEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditDomain;
+import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecorator;
+import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget;
+import org.eclipse.gmf.runtime.draw2d.ui.mapmode.MapModeUtil;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.jface.util.IPropertyChangeListener;
+import org.eclipse.jface.util.PropertyChangeEvent;
+import org.eclipse.ui.IEditorPart;
+import org.yakindu.base.gmf.runtime.decorators.AbstractDecoratorProvider;
+import org.yakindu.base.gmf.runtime.decorators.BaseDecorator;
+import org.yakindu.sct.model.sgraph.Transition;
+import org.yakindu.sct.model.sgraph.Vertex;
+import org.yakindu.sct.ui.editor.DiagramActivator;
+import org.yakindu.sct.ui.editor.editor.StatechartDiagramEditor;
+import org.yakindu.sct.ui.editor.editor.figures.PriorityFigure;
+import org.yakindu.sct.ui.editor.editparts.TransitionEditPart;
+import org.yakindu.sct.ui.editor.preferences.StatechartPreferenceConstants;
+
+/**
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class TransitionPriorityDecorationProvider extends AbstractDecoratorProvider implements IPropertyChangeListener {
+
+	private static final String PRIORITY_ID = TransitionPriorityDecorationProvider.class.getSimpleName();
+	private List<IDecorator> decorators;
+
+	public TransitionPriorityDecorationProvider() {
+		decorators = new ArrayList<>();
+		DiagramActivator.getDefault().getPreferenceStore().addPropertyChangeListener(this);
+	}
+
+	@Override
+	public void createDecorators(IDecoratorTarget decoratorTarget) {
+		EditPart editPart = (EditPart) decoratorTarget.getAdapter(EditPart.class);
+		if (editPart instanceof GraphicalEditPart || editPart instanceof AbstractConnectionEditPart) {
+			EditDomain ed = editPart.getViewer().getEditDomain();
+			if (!(ed instanceof DiagramEditDomain)) {
+				return;
+			}
+			if (shouldInstall(((DiagramEditDomain) ed).getEditorPart())) {
+				IDecorator decorator = createStatusDecorator(decoratorTarget);
+				decorators.add(decorator);
+				decoratorTarget.installDecorator(getDecoratorKey(), decorator);
+			}
+		}
+	}
+
+	protected IDecorator createStatusDecorator(IDecoratorTarget decoratorTarget) {
+		return new PriorityDecorator(decoratorTarget);
+	}
+
+	protected Object getDecoratorKey() {
+		return PRIORITY_ID;
+	}
+
+	protected boolean shouldInstall(IEditorPart editorPart) {
+		return (editorPart instanceof StatechartDiagramEditor);
+	}
+
+	public static class PriorityDecorator extends BaseDecorator {
+
+		public PriorityDecorator(IDecoratorTarget decoratorTarget) {
+			super(decoratorTarget);
+		}
+
+		@Override
+		public void refresh() {
+			removeDecoration();
+			boolean showLabels = DiagramActivator.getDefault().getPreferenceStore()
+					.getBoolean(StatechartPreferenceConstants.PREF_PRIORITY_LABELS);
+			if (!showLabels)
+				return;
+			View view = (View) getDecoratorTarget().getAdapter(View.class);
+			if (view == null || view.eResource() == null) {
+				return;
+			}
+			EditPart editPart = (EditPart) getDecoratorTarget().getAdapter(EditPart.class);
+			if (editPart == null || editPart.getViewer() == null) {
+				return;
+			}
+			if (editPart instanceof TransitionEditPart && (needsDecoration((TransitionEditPart) editPart)))
+				createDecorators((TransitionEditPart) editPart);
+		}
+
+		public boolean needsDecoration(TransitionEditPart editPart) {
+			Transition transition = (Transition) editPart.resolveSemanticElement();
+			Vertex container = (Vertex) transition.eContainer();
+			return container.getOutgoingTransitions().size() > 1;
+
+		}
+
+		public void createDecorators(TransitionEditPart editPart) {
+			PriorityFigure figure = new PriorityFigure(MapModeUtil.getMapMode(), getPriority(editPart));
+			figure.setSize(10, 10);
+			setDecoration(getDecoratorTarget().addConnectionDecoration(figure, 5, false));
+		}
+
+		public int getPriority(TransitionEditPart editPart) {
+			Transition transition = ((Transition) editPart.resolveSemanticElement());
+			Vertex container = (Vertex) transition.eContainer();
+			return container.getOutgoingTransitions().indexOf(transition);
+		}
+	}
+
+	@Override
+	public void propertyChange(PropertyChangeEvent event) {
+		if (StatechartPreferenceConstants.PREF_PRIORITY_LABELS.equals(event.getProperty())) {
+			for (IDecorator decorator : decorators) {
+				decorator.refresh();
+			}
+		}
+	}
+}