View | Details | Raw Unified | Return to bug 324557
Collapse All | Expand All

(-)src/org/eclipse/gef/ui/parts/GraphicalEditor.java (-80 / +145 lines)
Lines 30-35 Link Here
30
30
31
import org.eclipse.draw2d.ColorConstants;
31
import org.eclipse.draw2d.ColorConstants;
32
import org.eclipse.draw2d.IFigure;
32
import org.eclipse.draw2d.IFigure;
33
import org.eclipse.draw2d.Viewport;
34
import org.eclipse.draw2d.geometry.Dimension;
35
import org.eclipse.draw2d.geometry.Point;
36
import org.eclipse.draw2d.geometry.Rectangle;
33
37
34
import org.eclipse.gef.ContextMenuProvider;
38
import org.eclipse.gef.ContextMenuProvider;
35
import org.eclipse.gef.DefaultEditDomain;
39
import org.eclipse.gef.DefaultEditDomain;
Lines 38-43 Link Here
38
import org.eclipse.gef.GraphicalViewer;
42
import org.eclipse.gef.GraphicalViewer;
39
import org.eclipse.gef.commands.CommandStack;
43
import org.eclipse.gef.commands.CommandStack;
40
import org.eclipse.gef.commands.CommandStackListener;
44
import org.eclipse.gef.commands.CommandStackListener;
45
import org.eclipse.gef.handles.HandleBounds;
41
import org.eclipse.gef.ui.actions.ActionBarContributor;
46
import org.eclipse.gef.ui.actions.ActionBarContributor;
42
import org.eclipse.gef.ui.actions.ActionRegistry;
47
import org.eclipse.gef.ui.actions.ActionRegistry;
43
import org.eclipse.gef.ui.actions.DeleteAction;
48
import org.eclipse.gef.ui.actions.DeleteAction;
Lines 59-76 Link Here
59
 * 
64
 * 
60
 * @author hudsonr
65
 * @author hudsonr
61
 */
66
 */
62
public abstract class GraphicalEditor extends EditorPart implements
67
public abstract class GraphicalEditor extends EditorPart implements CommandStackListener,
63
		CommandStackListener, ISelectionListener {
68
		ISelectionListener
64
69
{
65
	private static class ActionIDList extends ArrayList {
70
66
		public boolean add(Object o) {
71
	private static class ActionIDList extends ArrayList
67
			if (o instanceof IAction) {
72
	{
68
				try {
73
		public boolean add(Object o)
74
		{
75
			if(o instanceof IAction)
76
			{
77
				try
78
				{
69
					IAction action = (IAction) o;
79
					IAction action = (IAction) o;
70
					o = action.getId();
80
					o = action.getId();
71
					throw new IllegalArgumentException(
81
					throw new IllegalArgumentException(
72
							"Action IDs should be added to lists, not the action: " + action); //$NON-NLS-1$
82
							"Action IDs should be added to lists, not the action: " + action); //$NON-NLS-1$
73
				} catch (IllegalArgumentException exc) {
83
				}
84
				catch(IllegalArgumentException exc)
85
				{
74
					exc.printStackTrace();
86
					exc.printStackTrace();
75
				}
87
				}
76
			}
88
			}
Lines 79-105 Link Here
79
	}
91
	}
80
92
81
	private DefaultEditDomain editDomain;
93
	private DefaultEditDomain editDomain;
94
82
	private GraphicalViewer graphicalViewer;
95
	private GraphicalViewer graphicalViewer;
96
83
	private ActionRegistry actionRegistry;
97
	private ActionRegistry actionRegistry;
98
84
	private SelectionSynchronizer synchronizer;
99
	private SelectionSynchronizer synchronizer;
100
85
	private List selectionActions = new ActionIDList();
101
	private List selectionActions = new ActionIDList();
102
86
	private List stackActions = new ActionIDList();
103
	private List stackActions = new ActionIDList();
104
87
	private List propertyActions = new ActionIDList();
105
	private List propertyActions = new ActionIDList();
88
106
89
	/**
107
	/**
90
	 * Constructs the editor part
108
	 * Constructs the editor part
91
	 */
109
	 */
92
	public GraphicalEditor() {
110
	public GraphicalEditor()
111
	{
93
	}
112
	}
94
113
95
	/**
114
	/**
96
	 * When the command stack changes, the actions interested in the command
115
	 * When the command stack changes, the actions interested in the command
97
	 * stack are updated.
116
	 * stack are updated.
98
	 * 
117
	 * 
99
	 * @param event
118
	 * @param event the change event
100
	 *            the change event
101
	 */
119
	 */
102
	public void commandStackChanged(EventObject event) {
120
	public void commandStackChanged(EventObject event)
121
	{
103
		updateActions(stackActions);
122
		updateActions(stackActions);
104
	}
123
	}
105
124
Lines 108-123 Link Here
108
	 * This is where the root editpart should be configured. Subclasses should
127
	 * This is where the root editpart should be configured. Subclasses should
109
	 * extend or override this method as needed.
128
	 * extend or override this method as needed.
110
	 */
129
	 */
111
	protected void configureGraphicalViewer() {
130
	protected void configureGraphicalViewer()
112
		getGraphicalViewer().getControl().setBackground(
131
	{
113
				ColorConstants.listBackground);
132
		getGraphicalViewer().getControl().setBackground(ColorConstants.listBackground);
114
	}
133
	}
115
134
116
	/**
135
	/**
117
	 * Creates actions for this editor. Subclasses should override this method
136
	 * Creates actions for this editor. Subclasses should override this method
118
	 * to create and register actions with the {@link ActionRegistry}.
137
	 * to create and register actions with the {@link ActionRegistry}.
119
	 */
138
	 */
120
	protected void createActions() {
139
	protected void createActions()
140
	{
121
		ActionRegistry registry = getActionRegistry();
141
		ActionRegistry registry = getActionRegistry();
122
		IAction action;
142
		IAction action;
123
143
Lines 146-156 Link Here
146
	/**
166
	/**
147
	 * Creates the GraphicalViewer on the specified <code>Composite</code>.
167
	 * Creates the GraphicalViewer on the specified <code>Composite</code>.
148
	 * 
168
	 * 
149
	 * @param parent
169
	 * @param parent the parent composite
150
	 *            the parent composite
151
	 */
170
	 */
152
	protected void createGraphicalViewer(Composite parent) {
171
	protected void createGraphicalViewer(Composite parent)
153
		GraphicalViewer viewer = new ScrollingGraphicalViewer();
172
	{
173
		GraphicalViewer viewer = new ScrollingGraphicalViewer()
174
		{
175
			public void reveal(EditPart part)
176
			{
177
				super.reveal(part);
178
				Viewport port = getFigureCanvas().getViewport();
179
				IFigure target = ((GraphicalEditPart) part).getFigure();
180
				Rectangle exposeRegion = target instanceof HandleBounds ? ((HandleBounds) target)
181
						.getHandleBounds().getCopy() : target.getBounds().getCopy();
182
				target = target.getParent();
183
				while(target != null && target != port)
184
				{
185
					target.translateToParent(exposeRegion);
186
					target = target.getParent();
187
				}
188
				exposeRegion.expand(5, 5);
189
190
				Dimension viewportSize = port.getClientArea().getSize();
191
192
				Point topLeft = exposeRegion.getTopLeft();
193
				Point bottomRight = exposeRegion.getBottomRight().translate(
194
						viewportSize.getNegated());
195
				Point finalLocation = new Point();
196
				if(viewportSize.width < exposeRegion.width) finalLocation.x = Math.min(
197
						bottomRight.x, Math.max(topLeft.x, port.getViewLocation().x));
198
				else finalLocation.x = Math.min(topLeft.x, Math.max(bottomRight.x, port
199
						.getViewLocation().x));
200
201
				if(viewportSize.height < exposeRegion.height) finalLocation.y = Math.min(
202
						bottomRight.y, Math.max(topLeft.y, port.getViewLocation().y));
203
				else finalLocation.y = Math.min(topLeft.y, Math.max(bottomRight.y, port
204
						.getViewLocation().y));
205
206
				getFigureCanvas().scrollSmoothTo(finalLocation.x, finalLocation.y);
207
			}
208
		};
154
		viewer.createControl(parent);
209
		viewer.createControl(parent);
155
		setGraphicalViewer(viewer);
210
		setGraphicalViewer(viewer);
156
		configureGraphicalViewer();
211
		configureGraphicalViewer();
Lines 164-183 Link Here
164
	 * WARNING: This method may or may not be called by the workbench prior to
219
	 * WARNING: This method may or may not be called by the workbench prior to
165
	 * {@link #dispose()}.
220
	 * {@link #dispose()}.
166
	 * 
221
	 * 
167
	 * @param parent
222
	 * @param parent the parent composite
168
	 *            the parent composite
169
	 */
223
	 */
170
	public void createPartControl(Composite parent) {
224
	public void createPartControl(Composite parent)
225
	{
171
		createGraphicalViewer(parent);
226
		createGraphicalViewer(parent);
172
	}
227
	}
173
228
174
	/**
229
	/**
175
	 * @see org.eclipse.ui.IWorkbenchPart#dispose()
230
	 * @see org.eclipse.ui.IWorkbenchPart#dispose()
176
	 */
231
	 */
177
	public void dispose() {
232
	public void dispose()
233
	{
178
		getCommandStack().removeCommandStackListener(this);
234
		getCommandStack().removeCommandStackListener(this);
179
		getSite().getWorkbenchWindow().getSelectionService()
235
		getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(this);
180
				.removeSelectionListener(this);
181
		getEditDomain().setActiveTool(null);
236
		getEditDomain().setActiveTool(null);
182
		getActionRegistry().dispose();
237
		getActionRegistry().dispose();
183
		super.dispose();
238
		super.dispose();
Lines 190-203 Link Here
190
	 * 
245
	 * 
191
	 * @see org.eclipse.ui.ISaveablePart#doSaveAs()
246
	 * @see org.eclipse.ui.ISaveablePart#doSaveAs()
192
	 */
247
	 */
193
	public void doSaveAs() {
248
	public void doSaveAs()
249
	{
194
		throw new RuntimeException("doSaveAs must be overridden"); //$NON-NLS-1$
250
		throw new RuntimeException("doSaveAs must be overridden"); //$NON-NLS-1$
195
	}
251
	}
196
252
197
	/**
253
	/**
198
	 * @see org.eclipse.ui.part.WorkbenchPart#firePropertyChange(int)
254
	 * @see org.eclipse.ui.part.WorkbenchPart#firePropertyChange(int)
199
	 */
255
	 */
200
	protected void firePropertyChange(int property) {
256
	protected void firePropertyChange(int property)
257
	{
201
		super.firePropertyChange(property);
258
		super.firePropertyChange(property);
202
		updateActions(propertyActions);
259
		updateActions(propertyActions);
203
	}
260
	}
Lines 207-215 Link Here
207
	 * 
264
	 * 
208
	 * @return the action registry
265
	 * @return the action registry
209
	 */
266
	 */
210
	protected ActionRegistry getActionRegistry() {
267
	protected ActionRegistry getActionRegistry()
211
		if (actionRegistry == null)
268
	{
212
			actionRegistry = new ActionRegistry();
269
		if(actionRegistry == null) actionRegistry = new ActionRegistry();
213
		return actionRegistry;
270
		return actionRegistry;
214
	}
271
	}
215
272
Lines 223-245 Link Here
223
	 * 
280
	 * 
224
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
281
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
225
	 */
282
	 */
226
	public Object getAdapter(Class type) {
283
	public Object getAdapter(Class type)
227
		if (type == org.eclipse.ui.views.properties.IPropertySheetPage.class) {
284
	{
285
		if(type == org.eclipse.ui.views.properties.IPropertySheetPage.class)
286
		{
228
			PropertySheetPage page = new PropertySheetPage();
287
			PropertySheetPage page = new PropertySheetPage();
229
			page.setRootEntry(new UndoablePropertySheetEntry(getCommandStack()));
288
			page.setRootEntry(new UndoablePropertySheetEntry(getCommandStack()));
230
			return page;
289
			return page;
231
		}
290
		}
232
		if (type == GraphicalViewer.class)
291
		if(type == GraphicalViewer.class) return getGraphicalViewer();
233
			return getGraphicalViewer();
292
		if(type == CommandStack.class) return getCommandStack();
234
		if (type == CommandStack.class)
293
		if(type == ActionRegistry.class) return getActionRegistry();
235
			return getCommandStack();
294
		if(type == EditPart.class && getGraphicalViewer() != null) return getGraphicalViewer()
236
		if (type == ActionRegistry.class)
295
				.getRootEditPart();
237
			return getActionRegistry();
296
		if(type == IFigure.class && getGraphicalViewer() != null) return ((GraphicalEditPart) getGraphicalViewer()
238
		if (type == EditPart.class && getGraphicalViewer() != null)
297
				.getRootEditPart()).getFigure();
239
			return getGraphicalViewer().getRootEditPart();
240
		if (type == IFigure.class && getGraphicalViewer() != null)
241
			return ((GraphicalEditPart) getGraphicalViewer().getRootEditPart())
242
					.getFigure();
243
		return super.getAdapter(type);
298
		return super.getAdapter(type);
244
	}
299
	}
245
300
Lines 248-254 Link Here
248
	 * 
303
	 * 
249
	 * @return the command stack
304
	 * @return the command stack
250
	 */
305
	 */
251
	protected CommandStack getCommandStack() {
306
	protected CommandStack getCommandStack()
307
	{
252
		return getEditDomain().getCommandStack();
308
		return getEditDomain().getCommandStack();
253
	}
309
	}
254
310
Lines 257-263 Link Here
257
	 * 
313
	 * 
258
	 * @return the edit domain
314
	 * @return the edit domain
259
	 */
315
	 */
260
	protected DefaultEditDomain getEditDomain() {
316
	protected DefaultEditDomain getEditDomain()
317
	{
261
		return editDomain;
318
		return editDomain;
262
	}
319
	}
263
320
Lines 266-272 Link Here
266
	 * 
323
	 * 
267
	 * @return the graphical viewer
324
	 * @return the graphical viewer
268
	 */
325
	 */
269
	protected GraphicalViewer getGraphicalViewer() {
326
	protected GraphicalViewer getGraphicalViewer()
327
	{
270
		return graphicalViewer;
328
		return graphicalViewer;
271
	}
329
	}
272
330
Lines 278-284 Link Here
278
	 * 
336
	 * 
279
	 * @return the list of property-dependant actions
337
	 * @return the list of property-dependant actions
280
	 */
338
	 */
281
	protected List getPropertyActions() {
339
	protected List getPropertyActions()
340
	{
282
		return propertyActions;
341
		return propertyActions;
283
	}
342
	}
284
343
Lines 292-298 Link Here
292
	 * @see #updateActions(List)
351
	 * @see #updateActions(List)
293
	 * @return the list of selection-dependant action IDs
352
	 * @return the list of selection-dependant action IDs
294
	 */
353
	 */
295
	protected List getSelectionActions() {
354
	protected List getSelectionActions()
355
	{
296
		return selectionActions;
356
		return selectionActions;
297
	}
357
	}
298
358
Lines 302-310 Link Here
302
	 * 
362
	 * 
303
	 * @return the syncrhonizer
363
	 * @return the syncrhonizer
304
	 */
364
	 */
305
	protected SelectionSynchronizer getSelectionSynchronizer() {
365
	protected SelectionSynchronizer getSelectionSynchronizer()
306
		if (synchronizer == null)
366
	{
307
			synchronizer = new SelectionSynchronizer();
367
		if(synchronizer == null) synchronizer = new SelectionSynchronizer();
308
		return synchronizer;
368
		return synchronizer;
309
	}
369
	}
310
370
Lines 317-323 Link Here
317
	 * 
377
	 * 
318
	 * @return the list of stack-dependant action IDs
378
	 * @return the list of stack-dependant action IDs
319
	 */
379
	 */
320
	protected List getStackActions() {
380
	protected List getStackActions()
381
	{
321
		return stackActions;
382
		return stackActions;
322
	}
383
	}
323
384
Lines 327-333 Link Here
327
	 * or more EditPartViewers in sync. The viewer is also registered as the
388
	 * or more EditPartViewers in sync. The viewer is also registered as the
328
	 * ISelectionProvider for the Editor's PartSite.
389
	 * ISelectionProvider for the Editor's PartSite.
329
	 */
390
	 */
330
	protected void hookGraphicalViewer() {
391
	protected void hookGraphicalViewer()
392
	{
331
		getSelectionSynchronizer().addViewer(getGraphicalViewer());
393
		getSelectionSynchronizer().addViewer(getGraphicalViewer());
332
		getSite().setSelectionProvider(getGraphicalViewer());
394
		getSite().setSelectionProvider(getGraphicalViewer());
333
	}
395
	}
Lines 340-352 Link Here
340
	 * 
402
	 * 
341
	 * @see org.eclipse.ui.IEditorPart#init(IEditorSite, IEditorInput)
403
	 * @see org.eclipse.ui.IEditorPart#init(IEditorSite, IEditorInput)
342
	 */
404
	 */
343
	public void init(IEditorSite site, IEditorInput input)
405
	public void init(IEditorSite site, IEditorInput input) throws PartInitException
344
			throws PartInitException {
406
	{
345
		setSite(site);
407
		setSite(site);
346
		setInput(input);
408
		setInput(input);
347
		getCommandStack().addCommandStackListener(this);
409
		getCommandStack().addCommandStackListener(this);
348
		getSite().getWorkbenchWindow().getSelectionService()
410
		getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this);
349
				.addSelectionListener(this);
350
		initializeActionRegistry();
411
		initializeActionRegistry();
351
	}
412
	}
352
413
Lines 358-364 Link Here
358
	 * This method may be called on Editor creation, or lazily the first time
419
	 * This method may be called on Editor creation, or lazily the first time
359
	 * {@link #getActionRegistry()} is called.
420
	 * {@link #getActionRegistry()} is called.
360
	 */
421
	 */
361
	protected void initializeActionRegistry() {
422
	protected void initializeActionRegistry()
423
	{
362
		createActions();
424
		createActions();
363
		updateActions(propertyActions);
425
		updateActions(propertyActions);
364
		updateActions(stackActions);
426
		updateActions(stackActions);
Lines 377-383 Link Here
377
	 * 
439
	 * 
378
	 * @see org.eclipse.ui.ISaveablePart#isDirty()
440
	 * @see org.eclipse.ui.ISaveablePart#isDirty()
379
	 */
441
	 */
380
	public boolean isDirty() {
442
	public boolean isDirty()
443
	{
381
		return getCommandStack().isDirty();
444
		return getCommandStack().isDirty();
382
	}
445
	}
383
446
Lines 387-393 Link Here
387
	 * 
450
	 * 
388
	 * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
451
	 * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
389
	 */
452
	 */
390
	public boolean isSaveAsAllowed() {
453
	public boolean isSaveAsAllowed()
454
	{
391
		return false;
455
		return false;
392
	}
456
	}
393
457
Lines 395-440 Link Here
395
	 * @see org.eclipse.ui.ISelectionListener#selectionChanged(IWorkbenchPart,
459
	 * @see org.eclipse.ui.ISelectionListener#selectionChanged(IWorkbenchPart,
396
	 *      ISelection)
460
	 *      ISelection)
397
	 */
461
	 */
398
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
462
	public void selectionChanged(IWorkbenchPart part, ISelection selection)
463
	{
399
		// If not the active editor, ignore selection changed.
464
		// If not the active editor, ignore selection changed.
400
		if (this.equals(getSite().getPage().getActiveEditor()))
465
		if(this.equals(getSite().getPage().getActiveEditor())) updateActions(selectionActions);
401
			updateActions(selectionActions);
402
	}
466
	}
403
467
404
	/**
468
	/**
405
	 * Sets the ActionRegistry for this EditorPart.
469
	 * Sets the ActionRegistry for this EditorPart.
406
	 * 
470
	 * 
407
	 * @param registry
471
	 * @param registry the registry
408
	 *            the registry
409
	 */
472
	 */
410
	protected void setActionRegistry(ActionRegistry registry) {
473
	protected void setActionRegistry(ActionRegistry registry)
474
	{
411
		actionRegistry = registry;
475
		actionRegistry = registry;
412
	}
476
	}
413
477
414
	/**
478
	/**
415
	 * Sets the EditDomain for this EditorPart.
479
	 * Sets the EditDomain for this EditorPart.
416
	 * 
480
	 * 
417
	 * @param ed
481
	 * @param ed the domain
418
	 *            the domain
419
	 */
482
	 */
420
	protected void setEditDomain(DefaultEditDomain ed) {
483
	protected void setEditDomain(DefaultEditDomain ed)
484
	{
421
		this.editDomain = ed;
485
		this.editDomain = ed;
422
	}
486
	}
423
487
424
	/**
488
	/**
425
	 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
489
	 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
426
	 */
490
	 */
427
	public void setFocus() {
491
	public void setFocus()
492
	{
428
		getGraphicalViewer().getControl().setFocus();
493
		getGraphicalViewer().getControl().setFocus();
429
	}
494
	}
430
495
431
	/**
496
	/**
432
	 * Sets the graphicalViewer for this EditorPart.
497
	 * Sets the graphicalViewer for this EditorPart.
433
	 * 
498
	 * 
434
	 * @param viewer
499
	 * @param viewer the graphical viewer
435
	 *            the graphical viewer
436
	 */
500
	 */
437
	protected void setGraphicalViewer(GraphicalViewer viewer) {
501
	protected void setGraphicalViewer(GraphicalViewer viewer)
502
	{
438
		getEditDomain().addViewer(viewer);
503
		getEditDomain().addViewer(viewer);
439
		this.graphicalViewer = viewer;
504
		this.graphicalViewer = viewer;
440
	}
505
	}
Lines 446-461 Link Here
446
	 * is an {@link UpdateAction}, it will have its <code>update()</code> method
511
	 * is an {@link UpdateAction}, it will have its <code>update()</code> method
447
	 * called.
512
	 * called.
448
	 * 
513
	 * 
449
	 * @param actionIds
514
	 * @param actionIds the list of IDs to update
450
	 *            the list of IDs to update
451
	 */
515
	 */
452
	protected void updateActions(List actionIds) {
516
	protected void updateActions(List actionIds)
517
	{
453
		ActionRegistry registry = getActionRegistry();
518
		ActionRegistry registry = getActionRegistry();
454
		Iterator iter = actionIds.iterator();
519
		Iterator iter = actionIds.iterator();
455
		while (iter.hasNext()) {
520
		while(iter.hasNext())
521
		{
456
			IAction action = registry.getAction(iter.next());
522
			IAction action = registry.getAction(iter.next());
457
			if (action instanceof UpdateAction)
523
			if(action instanceof UpdateAction) ((UpdateAction) action).update();
458
				((UpdateAction) action).update();
459
		}
524
		}
460
	}
525
	}
461
526
(-)src/org/eclipse/gef/ui/parts/ScrollingGraphicalViewer.java (-1 / +4 lines)
Lines 22-27 Link Here
22
22
23
import org.eclipse.gef.EditPart;
23
import org.eclipse.gef.EditPart;
24
import org.eclipse.gef.GraphicalEditPart;
24
import org.eclipse.gef.GraphicalEditPart;
25
import org.eclipse.gef.handles.HandleBounds;
25
26
26
/**
27
/**
27
 * A Graphical Viewer implementation which uses a
28
 * A Graphical Viewer implementation which uses a
Lines 88-94 Link Here
88
		super.reveal(part);
89
		super.reveal(part);
89
		Viewport port = getFigureCanvas().getViewport();
90
		Viewport port = getFigureCanvas().getViewport();
90
		IFigure target = ((GraphicalEditPart) part).getFigure();
91
		IFigure target = ((GraphicalEditPart) part).getFigure();
91
		Rectangle exposeRegion = target.getBounds().getCopy();
92
		Rectangle exposeRegion = target instanceof HandleBounds ?
93
				((HandleBounds) target).getHandleBounds().getCopy() :
94
				target.getBounds().getCopy();
92
		target = target.getParent();
95
		target = target.getParent();
93
		while (target != null && target != port) {
96
		while (target != null && target != port) {
94
			target.translateToParent(exposeRegion);
97
			target.translateToParent(exposeRegion);

Return to bug 324557