| /******************************************************************************* |
| * Copyright (c) 2010 Zoltan Ujhelyi and Istvan Rath and Daniel Varro. |
| * 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: |
| * Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.viatra2.visualisation.common.labelproviders; |
| |
| import java.lang.ref.WeakReference; |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.Map; |
| import java.util.TreeMap; |
| |
| import org.eclipse.jface.resource.ColorRegistry; |
| import org.eclipse.swt.graphics.Color; |
| import org.eclipse.swt.graphics.RGB; |
| import org.eclipse.viatra2.core.IModelElement; |
| import org.eclipse.viatra2.core.IModelSpace; |
| |
| /** |
| * "Smart" label provider that displays useful labels. |
| * This variant supports highlighting of certain elements. |
| * @author istvan rath |
| * |
| */ |
| public class HighlightableSmartLabelProvider extends SmartLabelProvider { |
| |
| /** |
| * FQN -> Set<Weakref<IModelElement> map to hold |
| * weak refs to model element from multiple model spaces. |
| */ |
| private static Map<String, Collection<WeakReference<IModelElement>>> highlightedElements = |
| new TreeMap<String, Collection<WeakReference<IModelElement>>>(); |
| |
| private static Map<String, Color> highlightColor = |
| new TreeMap<String, Color>(); |
| |
| /** |
| * Add an element to the set of highlighted elements. |
| * @param me the model element to highlight |
| */ |
| public static void highlightElement(IModelElement me) { |
| highlightElement(me, null); |
| } |
| |
| /** |
| * Highlights an element with a selected color string. Color strings could |
| * be instantiated using |
| * {@link HighlightableSmartLabelProvider#registerColor(int, int, int)}. |
| * @param me |
| * the model element to highlight |
| * @param colorKey |
| * the color key to use |
| */ |
| public static void highlightElement(IModelElement me, String colorKey) { |
| String key = getKey(me); |
| Collection<WeakReference<IModelElement>> set = |
| highlightedElements.get(key); |
| if (set==null) { |
| set = new ArrayList<WeakReference<IModelElement>>(); |
| highlightedElements.put(key, set); |
| } |
| set.add(new WeakReference<IModelElement>(me)); |
| if (colorKey!=null && hlCReg.get(colorKey)!=null){ |
| highlightColor.put(key, hlCReg.get(colorKey)); |
| } |
| } |
| /** |
| * Clears all the highlights from a given modelspace. |
| * @param msp |
| */ |
| public static void clearHighlights(IModelSpace msp){ |
| for (Collection<WeakReference<IModelElement>> c :highlightedElements.values()) { |
| Collection<WeakReference<IModelElement>> toRemove = |
| new ArrayList<WeakReference<IModelElement>>(); |
| for (WeakReference<IModelElement> wr : c) { |
| if (wr.get().getModelSpace().equals(msp)) { |
| toRemove.add(wr); |
| } |
| } |
| c.removeAll(toRemove); |
| } |
| } |
| |
| private static String getKey(IModelElement me) { |
| return me.getID(); |
| } |
| |
| /** |
| * Remove an element from the set of highlighted ones. |
| * @param me |
| */ |
| public static void unhighlightElement(IModelElement me) { |
| String key = getKey(me); |
| Collection<WeakReference<IModelElement>> set = |
| highlightedElements.get(key); |
| if (set!=null) { |
| WeakReference<IModelElement> toRemove = null; |
| for (WeakReference<IModelElement> target : set) { |
| if (Equals(target.get(), me)) { |
| toRemove = target; |
| break; |
| } |
| } |
| if (toRemove!=null) { |
| set.remove(toRemove); |
| } |
| } |
| highlightColor.remove(key); |
| } |
| |
| private static boolean Equals(IModelElement me0, IModelElement me1) { |
| return me0.equals(me1) && me0.getModelSpace().equals(me1.getModelSpace()); |
| } |
| |
| public static boolean isHighlighted(IModelElement me) { |
| String key = getKey(me); |
| Collection<WeakReference<IModelElement>> set = |
| highlightedElements.get(key); |
| if (set!=null) { |
| for (WeakReference<IModelElement> target : set) { |
| if (Equals(target.get(), me)) { |
| return true; |
| } |
| } |
| } |
| return false; |
| } |
| |
| /** |
| * Color registry for storing highlight colors. |
| */ |
| private static ColorRegistry hlCReg = new ColorRegistry(); |
| |
| private Color HL; |
| private static final int highlightwidth = 3; |
| |
| |
| public static String registerColor(int r, int g, int b) { |
| RGB rgb = new RGB(r,g,b); |
| if (hlCReg.get(rgb.toString())==null) { |
| hlCReg.put(rgb.toString(), rgb); |
| } |
| return rgb.toString(); |
| } |
| |
| public HighlightableSmartLabelProvider() { |
| hlCReg.put("highlight", new RGB(255, 0, 0)); |
| HL = hlCReg.get("highlight"); |
| } |
| |
| @Override |
| public int getBorderWidth(Object entity) { |
| // if an entity is highlighted -> increase border width |
| if (entity instanceof IModelElement && isHighlighted((IModelElement)entity)) { |
| return highlightwidth; |
| } |
| return super.getBorderWidth(entity); |
| } |
| |
| @Override |
| public Color getBorderColor(Object entity) { |
| Color c = _getColor(entity); |
| return c!=null?c:super.getBorderColor(entity); |
| } |
| |
| @Override |
| public Color getBackgroundColour(Object entity) { |
| Color c = _getColor(entity); |
| return c!=null?c:super.getBackgroundColour(entity); |
| } |
| |
| @Override |
| public int getLineWidth(Object rel) { |
| // if a relation is highlighted -> increase width |
| if (rel instanceof IModelElement && isHighlighted((IModelElement)rel)) { |
| return highlightwidth; |
| } |
| return super.getLineWidth(rel); |
| } |
| |
| @Override |
| public Color getColor(Object entity) { |
| Color c = _getColor(entity); |
| return c!=null?c:super.getColor(entity); |
| } |
| |
| private Color _getColor(Object entity) { |
| if (entity instanceof IModelElement && isHighlighted((IModelElement)entity)) { |
| Color c = highlightColor.get(getKey((IModelElement)entity)); |
| if (c!=null) { |
| return c; |
| } |
| return HL; |
| } |
| return null; |
| } |
| } |