1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.gef.editparts;
|
12
|
|
13
|
import java.beans.PropertyChangeEvent;
|
14
|
import java.beans.PropertyChangeListener;
|
15
|
|
16
|
import org.eclipse.draw2d.ConnectionLayer;
|
17
|
import org.eclipse.draw2d.FreeformLayer;
|
18
|
import org.eclipse.draw2d.FreeformLayeredPane;
|
19
|
import org.eclipse.draw2d.FreeformViewport;
|
20
|
import org.eclipse.draw2d.IFigure;
|
21
|
import org.eclipse.draw2d.LayeredPane;
|
22
|
import org.eclipse.draw2d.geometry.Dimension;
|
23
|
import org.eclipse.draw2d.geometry.Point;
|
24
|
|
25
|
import org.eclipse.gef.AutoexposeHelper;
|
26
|
import org.eclipse.gef.DragTracker;
|
27
|
import org.eclipse.gef.EditPart;
|
28
|
import org.eclipse.gef.EditPartViewer;
|
29
|
import org.eclipse.gef.LayerConstants;
|
30
|
import org.eclipse.gef.Request;
|
31
|
import org.eclipse.gef.SnapToGrid;
|
32
|
import org.eclipse.gef.tools.MarqueeDragTracker;
|
33
|
|
34
|
/**
|
35
|
* A graphical root that uses {@link org.eclipse.draw2d.FreeformFigure
|
36
|
* FreeformFigures} as the layers in the diagram. The
|
37
|
* {@link EditPartViewer#setContents(EditPart) contents} editpart must provide a
|
38
|
* FreeformFigure as its figure. Freeform figures are special because they can
|
39
|
* expand in any direction. This allows the user to drag objects or bendpoints
|
40
|
* into the negative X and Y coordinates of a diagram. If this feature is not
|
41
|
* being used, clients should use the {@link FreeformGraphicalRootEditPart} as
|
42
|
* their viewer's root editpart.
|
43
|
* <P>
|
44
|
* <EM>IMPORTANT:</EM> The contents editpart that is added to a freeform root
|
45
|
* should have a <code>FreeformFigure</code> (such as FreeformLayer) as its
|
46
|
* Figure. The primary layer is <EM>not</EM> using a draw2d LayoutManager, and
|
47
|
* will not size the contents' figure properly unless it is a freeform figure.
|
48
|
* <P>
|
49
|
* <EM>IMPORTANT:</EM>The freeform root uses a <code>FreeformViewport</code> as
|
50
|
* its primary figure. This class must be used with the
|
51
|
* {@link org.eclipse.gef.ui.parts.ScrollingGraphicalViewer}. The viewport gets
|
52
|
* installed into that viewer's {@link org.eclipse.draw2d.FigureCanvas}, which
|
53
|
* provides native scrollbars for scrolling the viewport.
|
54
|
* <P>
|
55
|
* This root serves as the diagram's
|
56
|
* {@link org.eclipse.gef.editparts.LayerManager}, providing the following layer
|
57
|
* structure, in top-to-bottom order:
|
58
|
* <table cellspacing="0" cellpadding="0">
|
59
|
* <tr>
|
60
|
* <td colspan="2">Root Freeform Layered Pane</td>
|
61
|
* </tr>
|
62
|
* <tr>
|
63
|
* <td>├</td>
|
64
|
* <td> Feedback Layer</td>
|
65
|
* </tr>
|
66
|
* <tr>
|
67
|
* <td>├</td>
|
68
|
* <td> Handle Layer</td>
|
69
|
* </tr>
|
70
|
* <tr>
|
71
|
* <td>└</td>
|
72
|
* <td> Printable Layers</td>
|
73
|
* </tr>
|
74
|
* <tr>
|
75
|
* <td> </td>
|
76
|
* <td>├ Connection Layer</td>
|
77
|
* </tr>
|
78
|
* <tr>
|
79
|
* <td> </td>
|
80
|
* <td>└ Primary Layer</td>
|
81
|
* </tr>
|
82
|
* </table>
|
83
|
*
|
84
|
*/
|
85
|
public class FreeformGraphicalRootEditPart extends SimpleRootEditPart implements
|
86
|
LayerConstants, LayerManager {
|
87
|
|
88
|
private LayeredPane innerLayers;
|
89
|
private LayeredPane printableLayers;
|
90
|
private PropertyChangeListener gridListener = new PropertyChangeListener() {
|
91
|
public void propertyChange(PropertyChangeEvent evt) {
|
92
|
String property = evt.getPropertyName();
|
93
|
if (property.equals(SnapToGrid.PROPERTY_GRID_ORIGIN)
|
94
|
|| property.equals(SnapToGrid.PROPERTY_GRID_SPACING)
|
95
|
|| property.equals(SnapToGrid.PROPERTY_GRID_VISIBLE))
|
96
|
refreshGridLayer();
|
97
|
}
|
98
|
};
|
99
|
|
100
|
/**
|
101
|
* @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
|
102
|
*/
|
103
|
protected IFigure createFigure() {
|
104
|
FreeformViewport viewport = new FreeformViewport();
|
105
|
innerLayers = new FreeformLayeredPane();
|
106
|
createLayers(innerLayers);
|
107
|
viewport.setContents(innerLayers);
|
108
|
return viewport;
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Creates a {@link GridLayer grid}. Sub-classes can override this method to
|
113
|
* customize the appearance of the grid. The grid layer should be the first
|
114
|
* layer (i.e., beneath the primary layer) if it is not to cover up parts on
|
115
|
* the primary layer. In that case, the primary layer should be transparent
|
116
|
* so that the grid is visible.
|
117
|
*
|
118
|
* @return the newly created GridLayer
|
119
|
*/
|
120
|
protected GridLayer createGridLayer() {
|
121
|
return new GridLayer();
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Creates the top-most set of layers on the given layered pane.
|
126
|
*
|
127
|
* @param layeredPane
|
128
|
* the parent for the created layers
|
129
|
*/
|
130
|
protected void createLayers(LayeredPane layeredPane) {
|
131
|
layeredPane.add(createGridLayer(), GRID_LAYER);
|
132
|
layeredPane.add(getPrintableLayers(), PRINTABLE_LAYERS);
|
133
|
layeredPane.add(new FreeformLayer(), HANDLE_LAYER);
|
134
|
layeredPane.add(new FeedbackLayer(), FEEDBACK_LAYER);
|
135
|
layeredPane.add(new GuideLayer(), GUIDE_LAYER);
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Creates a layered pane and the layers that should be printed.
|
140
|
*
|
141
|
* @see org.eclipse.gef.print.PrintGraphicalViewerOperation
|
142
|
* @return a new LayeredPane containing the printable layers
|
143
|
*/
|
144
|
protected LayeredPane createPrintableLayers() {
|
145
|
FreeformLayeredPane layeredPane = new FreeformLayeredPane();
|
146
|
layeredPane.add(new FreeformLayer(), PRIMARY_LAYER);
|
147
|
layeredPane.add(new ConnectionLayer(), CONNECTION_LAYER);
|
148
|
return layeredPane;
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
|
153
|
*/
|
154
|
public Object getAdapter(Class adapter) {
|
155
|
if (adapter == AutoexposeHelper.class)
|
156
|
return new ViewportAutoexposeHelper(this);
|
157
|
return super.getAdapter(adapter);
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* The contents' Figure will be added to the PRIMARY_LAYER.
|
162
|
*
|
163
|
* @see org.eclipse.gef.GraphicalEditPart#getContentPane()
|
164
|
*/
|
165
|
public IFigure getContentPane() {
|
166
|
return getLayer(PRIMARY_LAYER);
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Should not be called, but returns a MarqeeDragTracker for good measure.
|
171
|
*
|
172
|
* @see org.eclipse.gef.EditPart#getDragTracker(org.eclipse.gef.Request)
|
173
|
*/
|
174
|
public DragTracker getDragTracker(Request req) {
|
175
|
/*
|
176
|
* The root will only be asked for a drag tracker if for some reason the
|
177
|
* contents editpart says it is neither selector nor opaque.
|
178
|
*/
|
179
|
return new MarqueeDragTracker();
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Returns the layer indicated by the key. Searches all layered panes.
|
184
|
*
|
185
|
* @see LayerManager#getLayer(Object)
|
186
|
*/
|
187
|
public IFigure getLayer(Object key) {
|
188
|
if (innerLayers == null)
|
189
|
return null;
|
190
|
IFigure layer = innerLayers.getLayer(key);
|
191
|
if (layer != null)
|
192
|
return layer;
|
193
|
if (printableLayers == null)
|
194
|
return null;
|
195
|
return printableLayers.getLayer(key);
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* The root editpart does not have a real model. The LayerManager ID is
|
200
|
* returned so that this editpart gets registered using that key.
|
201
|
*
|
202
|
* @see org.eclipse.gef.EditPart#getModel()
|
203
|
*/
|
204
|
public Object getModel() {
|
205
|
return LayerManager.ID;
|
206
|
}
|
207
|
|
208
|
/**
|
209
|
* Returns the LayeredPane that should be used during printing. This layer
|
210
|
* will be identified using {@link LayerConstants#PRINTABLE_LAYERS}.
|
211
|
*
|
212
|
* @return the layered pane containing all printable content
|
213
|
*/
|
214
|
protected LayeredPane getPrintableLayers() {
|
215
|
if (printableLayers == null)
|
216
|
printableLayers = createPrintableLayers();
|
217
|
return printableLayers;
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* Updates the {@link GridLayer grid} based on properties set on the
|
222
|
* {@link #getViewer() graphical viewer}:
|
223
|
* {@link SnapToGrid#PROPERTY_GRID_VISIBLE},
|
224
|
* {@link SnapToGrid#PROPERTY_GRID_SPACING}, and
|
225
|
* {@link SnapToGrid#PROPERTY_GRID_ORIGIN}.
|
226
|
* <p>
|
227
|
* This method is invoked initially when the GridLayer is created, and when
|
228
|
* any of the above-mentioned properties are changed on the viewer.
|
229
|
*/
|
230
|
protected void refreshGridLayer() {
|
231
|
boolean visible = false;
|
232
|
GridLayer grid = (GridLayer) getLayer(GRID_LAYER);
|
233
|
Boolean val = (Boolean) getViewer().getProperty(
|
234
|
SnapToGrid.PROPERTY_GRID_VISIBLE);
|
235
|
if (val != null)
|
236
|
visible = val.booleanValue();
|
237
|
grid.setOrigin((Point) getViewer().getProperty(
|
238
|
SnapToGrid.PROPERTY_GRID_ORIGIN));
|
239
|
grid.setSpacing((Dimension) getViewer().getProperty(
|
240
|
SnapToGrid.PROPERTY_GRID_SPACING));
|
241
|
grid.setVisible(visible);
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* @see org.eclipse.gef.editparts.AbstractEditPart#register()
|
246
|
*/
|
247
|
protected void register() {
|
248
|
super.register();
|
249
|
if (getLayer(GRID_LAYER) != null) {
|
250
|
getViewer().addPropertyChangeListener(gridListener);
|
251
|
refreshGridLayer();
|
252
|
}
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* @see AbstractEditPart#unregister()
|
257
|
*/
|
258
|
protected void unregister() {
|
259
|
getViewer().removePropertyChangeListener(gridListener);
|
260
|
super.unregister();
|
261
|
}
|
262
|
|
263
|
class FeedbackLayer extends FreeformLayer {
|
264
|
FeedbackLayer() {
|
265
|
setEnabled(false);
|
266
|
}
|
267
|
}
|
268
|
|
269
|
}
|