Просмотр исходного кода

Batch execute a List of paint actions to increase performance

Andreas Muelder 10 лет назад
Родитель
Сommit
9c0e625974

+ 5 - 9
plugins/org.yakindu.base.gmf.runtime/src/org/yakindu/base/gmf/runtime/highlighting/HighlightingParameters.java

@@ -16,28 +16,24 @@ import org.eclipse.swt.graphics.Color;
 /**
  * 
  * @author Alexander Nyssen
+ * @author Andreas Muelder - removed grayscaling and fadingtime due to
+ *         performance issues
  * 
  */
 public class HighlightingParameters {
 
 	public static final HighlightingParameters DEFAULT = new HighlightingParameters();
-	
-	public long fadingTime = 500;
+
 	public Color foregroundFadingColor = ColorConstants.red;
-	// light red
-	public Color backgroundFadingColor = new Color(null, 255, 128, 128);
 
-	public boolean grayScale = false;
+	public Color backgroundFadingColor = new Color(null, 255, 128, 128);
 
 	public HighlightingParameters() {
 	}
 
-	public HighlightingParameters(long fadingTime, Color foregroundFadingColor,
-			Color backgroundFadingColor, boolean grayScale) {
-		this.fadingTime = fadingTime; // used for fade-in and fade-out
+	public HighlightingParameters(Color foregroundFadingColor, Color backgroundFadingColor) {
 		this.foregroundFadingColor = foregroundFadingColor;
 		this.backgroundFadingColor = backgroundFadingColor;
-		this.grayScale = grayScale;
 	}
 
 }

+ 51 - 131
plugins/org.yakindu.base.gmf.runtime/src/org/yakindu/base/gmf/runtime/highlighting/HighlightingSupportAdapter.java

@@ -15,7 +15,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.eclipse.core.runtime.Assert;
 import org.eclipse.draw2d.IFigure;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
@@ -23,11 +22,7 @@ import org.eclipse.gmf.runtime.diagram.ui.figures.BorderedNodeFigure;
 import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
 import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor;
 import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
-import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Image;
-import org.eclipse.swt.graphics.ImageData;
-import org.eclipse.swt.graphics.PaletteData;
 import org.eclipse.swt.widgets.Display;
 import org.yakindu.base.gmf.runtime.util.EditPartUtils;
 
@@ -67,10 +62,8 @@ public class HighlightingSupportAdapter implements IHighlightingSupport {
 		private final Color sourceBackgroundColor;
 		private final Color targetBackgroundColor;
 
-		private Fader(IFigure figure, Color sourceForegroundColor,
-				Color targetForegroundColor, Color sourceBackgroundColor,
-				Color targetBackgroundColor, int fadingTime,
-				boolean shouldFadeBack) {
+		private Fader(IFigure figure, Color sourceForegroundColor, Color targetForegroundColor,
+				Color sourceBackgroundColor, Color targetBackgroundColor, int fadingTime, boolean shouldFadeBack) {
 			this.figure = figure;
 			this.sourceForegroundColor = sourceForegroundColor;
 			this.targetForegroundColor = targetForegroundColor;
@@ -91,117 +84,29 @@ public class HighlightingSupportAdapter implements IHighlightingSupport {
 			figure.setBackgroundColor(targetBackgroundColor);
 			figure.invalidate();
 			if (shouldFadeBack) {
-				Display.getCurrent().timerExec(
-						fadingTime,
-						new Fader(figure, targetForegroundColor,
-								sourceForegroundColor, targetBackgroundColor,
-								sourceBackgroundColor, fadingTime, false));
+				Display.getCurrent().timerExec(fadingTime, new Fader(figure, targetForegroundColor,
+						sourceForegroundColor, targetBackgroundColor, sourceBackgroundColor, fadingTime, false));
 			}
 		}
 	}
 
 	private final Map<IFigure, ColorMemento> figureStates = new HashMap<IFigure, ColorMemento>();
-	private final Map<Color, Color> greyscaleColors = new HashMap<Color, Color>();
 	private boolean locked = false;
 	private final IDiagramWorkbenchPart diagramWorkbenchPart;
+	private Map<EObject, IGraphicalEditPart> object2editPart = new HashMap<EObject, IGraphicalEditPart>();
 
 	public HighlightingSupportAdapter(IDiagramWorkbenchPart diagramWorkbenchPart) {
 		this.diagramWorkbenchPart = diagramWorkbenchPart;
 	}
 
-	public synchronized void fadeIn(EObject semanticElement,
-			HighlightingParameters parameters) {
-
-		IGraphicalEditPart editPart = getEditPartForSemanticElement(semanticElement);
-		if(editPart == null)
-			return;
-		IFigure figure = getTargetFigure(editPart);
-		Assert.isNotNull(figure, "Could not obtain target figure");
-		// store fore and background color
-		Display.getCurrent().asyncExec(
-				new Fader(figure, figure.getForegroundColor(),
-						parameters.foregroundFadingColor, figure
-								.getBackgroundColor(),
-						parameters.backgroundFadingColor,
-						(int) parameters.fadingTime, false));
-	}
-
-	public synchronized void fadeOut(EObject semanticElement,
-			HighlightingParameters parameters) {
-		if (!locked) {
-			throw new IllegalStateException(
-					"May only highlight if editor is locked");
-		}
-
-		IGraphicalEditPart editPart = getEditPartForSemanticElement(semanticElement);
-		if(editPart == null)
-			return;
-		IFigure figure = getTargetFigure(editPart);
-		Assert.isNotNull(figure, "Could not obtain target figure");
-		Color foregroundColor = figureStates.get(figure).foregroundColor;
-		Color backgroundColor = figureStates.get(figure).backgroundColor;
-
-		Display.getCurrent().asyncExec(
-				new Fader(figure, parameters.foregroundFadingColor,
-						parameters.grayScale ? getGreyscaled(foregroundColor)
-								: foregroundColor,
-						parameters.backgroundFadingColor,
-						parameters.grayScale ? getGreyscaled(backgroundColor)
-								: backgroundColor, (int) parameters.fadingTime,
-						false));
-	}
-
-	public synchronized void flash(final EObject semanticElement,
-			final HighlightingParameters parameters) {
-		if (!locked) {
-			throw new IllegalStateException(
-					"May only highlight if editor is locked");
-		}
-		final IGraphicalEditPart editPart = getEditPartForSemanticElement(semanticElement);
-		if(editPart == null) {
-			return;
-		}
-
-		final IFigure figure = getTargetFigure(editPart);
-				
-//				(editPart instanceof FixedSizeShapeNodeEditPart) 
-//				? (IFigure) 	editPart.getFigure().getChildren().get(0) 
-//				: editPart.getFigure();
-		
-		Display.getCurrent().asyncExec(
-				new Fader(figure, figure.getForegroundColor(),
-						parameters.foregroundFadingColor, figure
-								.getBackgroundColor(),
-						parameters.backgroundFadingColor,
-						(int) parameters.fadingTime, true));
-	}
-
-	private IGraphicalEditPart getEditPartForSemanticElement(
-			EObject semanticElement) {
-		return EditPartUtils.findEditPartForSemanticElement(
-				diagramWorkbenchPart.getDiagramGraphicalViewer()
-						.getRootEditPart(), semanticElement);
-	}
-
-	private Color getGreyscaled(Color color) {
-		// check cache (if color has already be computed)
-		if (greyscaleColors.containsKey(color)) {
-			return greyscaleColors.get(color);
-		}
-		// use image to convert colors to greyscale
-		ImageData imageData = new ImageData(1, 1, 24, new PaletteData(0xFF,
-				0xFF00, 0xFF0000));
-		imageData.setPixel(0, 0, imageData.palette.getPixel(color.getRGB()));
-		Image image = new Image(Display.getCurrent(), imageData);
-		Image convertedImage = new Image(Display.getCurrent(), image,
-				SWT.IMAGE_DISABLE);
-		Color newColor = new Color(Display.getCurrent(),
-				convertedImage.getImageData().palette.getRGB(convertedImage
-						.getImageData().getPixel(0, 0)));
-		greyscaleColors.put(color, newColor);
-		convertedImage.dispose();
-		image.dispose();
-		return newColor;
+	private IGraphicalEditPart getEditPartForSemanticElement(EObject semanticElement) {
+		IGraphicalEditPart result = object2editPart.get(semanticElement);
+		if (result != null)
+			return result;
+		result = EditPartUtils.findEditPartForSemanticElement(
+				diagramWorkbenchPart.getDiagramGraphicalViewer().getRootEditPart(), semanticElement);
+		object2editPart.put(semanticElement, result);
+		return result;
 	}
 
 	private IFigure getTargetFigure(IGraphicalEditPart editPart) {
@@ -221,8 +126,7 @@ public class HighlightingSupportAdapter implements IHighlightingSupport {
 		}
 
 		setSanityCheckEnablementState(false);
-		for (Object editPart : diagramWorkbenchPart.getDiagramGraphicalViewer()
-				.getEditPartRegistry().values()) {
+		for (Object editPart : diagramWorkbenchPart.getDiagramGraphicalViewer().getEditPartRegistry().values()) {
 			if (editPart instanceof IGraphicalEditPart) {
 				IGraphicalEditPart graphicalEditPart = (IGraphicalEditPart) editPart;
 				IFigure figure = getTargetFigure(graphicalEditPart);
@@ -234,9 +138,8 @@ public class HighlightingSupportAdapter implements IHighlightingSupport {
 
 	private void setSanityCheckEnablementState(boolean state) {
 		try {
-			Method enableMethod = DiagramDocumentEditor.class
-					.getDeclaredMethod("enableSanityChecking",
-							new Class[] { boolean.class });
+			Method enableMethod = DiagramDocumentEditor.class.getDeclaredMethod("enableSanityChecking",
+					new Class[] { boolean.class });
 			enableMethod.setAccessible(true);
 			enableMethod.invoke(diagramWorkbenchPart, new Object[] { state });
 		} catch (Exception e) {
@@ -256,37 +159,54 @@ public class HighlightingSupportAdapter implements IHighlightingSupport {
 		figureStates.clear();
 		diagramWorkbenchPart.getDiagramEditPart().enableEditMode();
 		setSanityCheckEnablementState(true);
-
+		object2editPart.clear();
 		locked = false;
 	}
 
-	public void highlight(EObject semanticElement,
-			HighlightingParameters parameters) {
-		IGraphicalEditPart editPartForSemanticElement = getEditPartForSemanticElement(semanticElement);
-		if (editPartForSemanticElement != null) {
-			IFigure figure = getTargetFigure(editPartForSemanticElement);
-			if (parameters != null) {
-				figure.setForegroundColor(parameters.foregroundFadingColor);
-				figure.setBackgroundColor(parameters.backgroundFadingColor);
-				figure.invalidate();
-			} else {
-				ColorMemento memento = figureStates.get(figure);
-				if ( memento != null ) memento.restore();
+	public void highlight(List<? extends EObject> semanticElements, HighlightingParameters parameters) {
+		synchronized (semanticElements) {
+			for (EObject semanticElement : semanticElements) {
+				IGraphicalEditPart editPartForSemanticElement = getEditPartForSemanticElement(semanticElement);
+				if (editPartForSemanticElement != null) {
+					IFigure figure = getTargetFigure(editPartForSemanticElement);
+					if (parameters != null) {
+						figure.setForegroundColor(parameters.foregroundFadingColor);
+						figure.setBackgroundColor(parameters.backgroundFadingColor);
+						figure.invalidate();
+					} else {
+						ColorMemento memento = figureStates.get(figure);
+						if (memento != null)
+							memento.restore();
+					}
+				}
 			}
 		}
+	}
 
+	public void flash(List<? extends EObject> semanticElements, HighlightingParameters parameters, int flashTime) {
+		synchronized (semanticElements) {
+			for (EObject semanticElement : semanticElements) {
+				IGraphicalEditPart editPartForSemanticElement = getEditPartForSemanticElement(semanticElement);
+				if (editPartForSemanticElement != null) {
+					IFigure figure = getTargetFigure(editPartForSemanticElement);
+					Fader fader = new Fader(figure, figure.getForegroundColor(), parameters.foregroundFadingColor,
+							figure.getBackgroundColor(), parameters.backgroundFadingColor, (int) flashTime, true);
+					Display.getCurrent().asyncExec(fader);
+				}
+			}
+		}
 	}
 
 	public boolean isLocked() {
 		return locked;
 	}
 
-	public void executeBatch(final List<Action> actions) {
-		if ( actions != null ) {
-			
-			Display.getCurrent().asyncExec( new Runnable() {				
+	public void execute(final List<Action> actions) {
+		if (actions != null) {
+
+			Display.getDefault().asyncExec(new Runnable() {
 				public void run() {
-					for ( Action a : actions ) {
+					for (Action a : actions) {
 						a.execute(HighlightingSupportAdapter.this);
 					}
 				}

+ 55 - 68
plugins/org.yakindu.base.gmf.runtime/src/org/yakindu/base/gmf/runtime/highlighting/IHighlightingSupport.java

@@ -10,6 +10,7 @@
  */
 package org.yakindu.base.gmf.runtime.highlighting;
 
+import java.util.Collections;
 import java.util.List;
 
 import org.eclipse.emf.ecore.EObject;
@@ -21,108 +22,94 @@ import org.eclipse.emf.ecore.EObject;
  */
 public interface IHighlightingSupport {
 
-	/**
-	 * Prevent user from editing
-	 */
 	void lockEditor();
 
 	boolean isLocked();
-	/**
-	 * Allow user to edit model again
-	 */
+
 	void releaseEditor();
 
-	void highlight(EObject semanticElement, HighlightingParameters parameters);
-
-	/**
-	 * Highlight a model element. Fading time may be zero to indicate that
-	 * hightlighting should take place immediately.
-	 * 
-	 * @param modelElement
-	 * @param parameterObject
-	 */
-	void fadeIn(EObject semanticElement, HighlightingParameters parameters);
-
-	/**
-	 * Unhighlight a model element. Fading time may be zero to indicate that
-	 * hightlighting should be directly removed.
-	 * 
-	 * @param modelElement
-	 * @param fadeOutTime
-	 */
-	void fadeOut(EObject semanticElement, HighlightingParameters parameters);
-
-	/**
-	 * Shortly highlight the given model element. The given fading time will be
-	 * used for entering and leaving the highlighted state.
-	 * 
-	 * @param modelElement
-	 * @param parameters
-	 */
-	void flash(EObject semanticElemesnt, HighlightingParameters parameters);
-
-	/**
-	 * 
-	 * @param actions
-	 */
-	void executeBatch(List<Action> actions);
-	
-	
+	void highlight(List<? extends EObject> semanticElement, HighlightingParameters parameters);
+
+	void flash(List<? extends EObject> semanticElemesnt, HighlightingParameters parameters, int highlightTime);
+
+	void execute(List<Action> actions);
+
 	public static interface Action {
 		public void execute(IHighlightingSupport hs);
 	}
-	
 
 	public static class Highlight implements Action {
 
-		protected EObject semanticElement;
+		protected List<? extends EObject> semanticElements;
 		protected HighlightingParameters highligtingParams;
-		
+
 		public Highlight(EObject semanticElement, HighlightingParameters parameters) {
-			this.semanticElement = semanticElement;
+			this(Collections.singletonList(semanticElement), parameters);
+		}
+
+		public Highlight(List<? extends EObject> semanticElements, HighlightingParameters parameters) {
+			this.semanticElements = semanticElements;
 			this.highligtingParams = parameters;
 		}
-		
+
 		public void execute(IHighlightingSupport hs) {
-			hs.highlight(semanticElement, highligtingParams);
+			hs.highlight(semanticElements, highligtingParams);
 		}
-		
 	}
 
-	
-	public static class HighlightingSupportNullImpl implements
-			IHighlightingSupport {
+	public static class Flash implements Action {
 
-		public void lockEditor() {
-		}
+		protected List<? extends EObject> semanticElements;
+		protected HighlightingParameters highligtingParams;
+		private int flashTime;
 
-		public void releaseEditor() {
+		public Flash(EObject semanticElement, HighlightingParameters parameters, int flashTime) {
+			this(Collections.singletonList(semanticElement), parameters, flashTime);
 		}
 
-		public void fadeIn(EObject semanticElement,
-				HighlightingParameters parameters) {
+		public Flash(List<? extends EObject> semanticElements, HighlightingParameters parameters, int flashTime) {
+			this.semanticElements = semanticElements;
+			this.highligtingParams = parameters;
+			this.flashTime = flashTime;
 		}
 
-		public void fadeOut(EObject semanticElement,
-				HighlightingParameters parameters) {
+		public void execute(IHighlightingSupport hs) {
+			hs.flash(semanticElements, highligtingParams, flashTime);
 		}
 
-		public void flash(EObject semanticElement,
-				HighlightingParameters parameters) {
-		}
+	}
+
+	public static class HighlightingSupportNullImpl implements IHighlightingSupport {
+
+		@Override
+		public void lockEditor() {
 
-		public void highlight(EObject semanticElement,
-				HighlightingParameters parameters) {
-			
 		}
 
+		@Override
 		public boolean isLocked() {
 			return false;
 		}
 
-		public void executeBatch(List<Action> actions) {
+		@Override
+		public void releaseEditor() {
+
+		}
+
+		@Override
+		public void execute(List<Action> actions) {
 		}
-		
-		
+
+		@Override
+		public void highlight(List<? extends EObject> semanticElement, HighlightingParameters parameters) {
+
+		}
+
+		@Override
+		public void flash(List<? extends EObject> semanticElemesnt, HighlightingParameters parameters,
+				int highlightTime) {
+
+		}
+
 	}
 }