Skip to main content
summaryrefslogtreecommitdiffstats
blob: c0b2811e7d03abbcde34101789c4d9d2ca859a32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package org.eclipse.emf.refactor.metrics.papyrus.ui;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayoutListener;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.refactor.metrics.papyrus.managers.HighlightManager;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker;
import org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.AbstractDecorator;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.graphics.Color;
//import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;

public class PapyrusDecorator extends AbstractDecorator {
	
	private HighlightManager manager = HighlightManager.getInstance();
	
	private Map<PolylineConnection, Style> decoratedLines = null;
	
	private LayoutListener layoutListener = new LayoutListener() {		
		@Override
		public void setConstraint(IFigure child, Object constraint) {}
		
		@Override
		public void remove(IFigure child) {}
		
		@Override
		public void postLayout(IFigure container) {
			refresh();
		}
		
		@Override
		public boolean layout(IFigure container) { return false; }
		
		@Override
		public void invalidate(IFigure container) {}};
	
	private NotificationListener notificationListener = new NotificationListener() {
		public void notifyChanged(Notification notification) {
            refresh();
        }};


	public PapyrusDecorator(IDecoratorTarget decoratorTarget) {
		super(decoratorTarget);
		decoratedLines = new HashMap<PolylineConnection, Style>();
	}

	@Override
	public void activate() {
		IGraphicalEditPart gep = (IGraphicalEditPart) getDecoratorTarget().getAdapter(IGraphicalEditPart.class);
		assert gep != null;
		DiagramEventBroker.getInstance(gep.getEditingDomain()).addNotificationListener(gep.getNotationView(), 
				NotationPackage.eINSTANCE.getDescriptionStyle_Description(), notificationListener);
		
		GraphicalEditPart editPart = (GraphicalEditPart) getDecoratorTarget().getAdapter(GraphicalEditPart.class);
		editPart.getFigure().addLayoutListener(layoutListener);
		
		manager.registerDecorator(this, getDecoratorTarget());
	}
	
	@Override
	public void deactivate() {
		removeDecoration();

		IGraphicalEditPart gep = (IGraphicalEditPart) getDecoratorTarget().getAdapter(IGraphicalEditPart.class);
		assert gep != null;
		DiagramEventBroker.getInstance(gep.getEditingDomain()).removeNotificationListener(gep.getNotationView(), 
				NotationPackage.eINSTANCE.getDescriptionStyle_Description(), notificationListener);
		
		GraphicalEditPart editPart = (GraphicalEditPart) getDecoratorTarget().getAdapter(GraphicalEditPart.class);
		editPart.getFigure().removeLayoutListener(layoutListener);
		
		manager.unregisterDecorator(this);
	}
	
	@Override
	protected void removeDecoration() {
		super.removeDecoration();	
		for (PolylineConnection connection : decoratedLines.keySet()){
			connection.setForegroundColor(decoratedLines.get(connection).color);
			connection.setLineWidth(decoratedLines.get(connection).lineWidth);
		}
	}

	@Override
	public void refresh() {
//		System.out.println("Start REFRESH");
		removeDecoration();
		IGraphicalEditPart editPart = (IGraphicalEditPart) getDecoratorTarget().getAdapter(EditPart.class);
		
		View view = (View) getDecoratorTarget().getAdapter(View.class);
		
		if (manager.getPrefferedDecoratorTarget(view.getElement()) == getDecoratorTarget()){
			if(selectionContains(view.getElement())) {
				int x = editPart.getFigure().getBounds().x;
				int y = editPart.getFigure().getBounds().y;
				FigureCanvas canvas = (FigureCanvas) editPart.getViewer().getControl();
				canvas.scrollSmoothTo(x, y);
				if (view instanceof Node) {
					IFigure figure = editPart.getFigure();
					IFigure decoration = new PapyrusDecorationFigure();
					decoration.setSize(figure.getSize());
					setDecoration(getDecoratorTarget().addShapeDecoration(decoration, IDecoratorTarget.Direction.CENTER, 0, false));
				} 
				if (view instanceof Edge) {
					PolylineConnection connection = (PolylineConnection) editPart.getFigure();
					decoratedLines.put(connection, new Style(connection.getForegroundColor(), connection.getLineWidth()));
					connection.setForegroundColor(ColorConstants.red);
					connection.setLineWidth(2);
				} 
			}
		}
//		System.out.println("End REFRESH");
	}
	
	private boolean selectionContains(EObject element){
		boolean contained = false;
		if (element != null) {
			for(EObject selected : manager.getSelected()){
				String fragmentA = EcoreUtil.getURI(selected).fragment();
				String fragmentB = EcoreUtil.getURI(element).fragment();
				contained |= fragmentA.equals(fragmentB);
			}
		}
		return contained;
	}
	
	private static class Style {
		
		public Color color = null;
		public int lineWidth = 0;
	
		public Style(Color c, int lw){
			color = c;
			lineWidth = lw;
		}
	}

}

Back to the top