Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 46481457bab703a50ac65800329a62f1e4d29702 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 *
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.menu.toolbar;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gef.editparts.ZoomListener;
import org.eclipse.gef.editparts.ZoomManager;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.internal.actions.ZoomContributionItem;
import org.eclipse.gmf.runtime.diagram.ui.internal.util.IUIConstants;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IPage;
import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.papyrus.infra.ui.editor.CoreMultiDiagramEditor;
import org.eclipse.papyrus.infra.ui.util.WorkbenchPartHelper;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.menu.actions.ZoomAction;
import org.eclipse.papyrus.uml.diagram.menu.messages.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;

/**
 * This class provides a Combo box in the toolbar for the Zoom Action
 * This Combo is automatically refresh when the zoom changed
 *
 * A part of this code comes from {@link ZoomContributionItem}
 */
@SuppressWarnings("restriction")
public class ZoomToolbar extends WorkbenchWindowControlContribution implements ZoomListener, Listener, org.eclipse.papyrus.infra.core.sasheditor.editor.IPageChangedListener {

	/** the combo box */
	protected Combo combo = null;

	/** the part service */
	private IPartService partService = null;

	/** the listener for the part service */
	private IPartListener partListener = null;

	/** The previous part used to remove the listeners */
	private IWorkbenchPart previousPart = null;


	/**
	 *
	 * Constructor.
	 *
	 */
	public ZoomToolbar() {
		this(""); //$NON-NLS-1$
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param id
	 */
	public ZoomToolbar(String id) {
		super(id);
		init();
	}

	/**
	 *
	 * @see org.eclipse.jface.action.ContributionItem#dispose()
	 *
	 */
	@Override
	public void dispose() {
		super.dispose();
		if (this.partService != null) {
			this.partService.removePartListener(this.partListener);
		}
	}

	/**
	 * Adds a listener on the part Service and refresh the value displayed in the Combo
	 *
	 */
	protected void init() {
		addPartListener();
		refreshCombo();
	}

	/**
	 * adds the part listener
	 */
	protected void addPartListener() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		IWorkbenchWindow activeWorkbench = workbench.getActiveWorkbenchWindow();
		if (activeWorkbench != null) {
			partService = activeWorkbench.getPartService();
			if (partService != null) {
				this.partListener = new CustomPartListener();
				partService.addPartListener(this.partListener);
			}
		}
	}

	/**
	 * @see org.eclipse.jface.action.ControlContribution#createControl(org.eclipse.swt.widgets.Composite)
	 *
	 * @param parent
	 * @return
	 */
	@Override
	protected Control createControl(Composite parent) {
		ToolBar toolbar = new ToolBar(parent, SWT.NONE);

		ToolItem toolItem = new ToolItem(toolbar, SWT.SEPARATOR);
		Image zoomImage = Activator.getPluginIconImage("org.eclipse.papyrus.uml.diagram.menu", "/icons/zoomplus.gif"); //$NON-NLS-1$ //$NON-NLS-2$
		/*
		 * TODO : the image is not set, when we use SWT.SEPARATOR.
		 * It's a SWT bug.
		 * This image is used in the Perspective Customization
		 * That works fine with SWT.PUSH, SWT.PULL_DOWN, ..., but in this case it's the zoom which doesn't work
		 */
		toolItem.setImage(zoomImage);
		// this text is used in the perspective customization
		toolItem.setToolTipText(Messages.ZoomToolbar_Zoom);

		combo = new Combo(toolbar, SWT.DROP_DOWN);
		combo.setItems(getZoomLevelsAsText(getZoomManager()));
		combo.setVisibleItemCount(IUIConstants.DEFAULT_DROP_DOWN_SIZE);
		combo.pack();
		toolItem.setWidth(combo.getSize().x);
		toolItem.setControl(combo);
		combo.addListener(SWT.Selection, this);
		combo.addListener(SWT.KeyDown, this);

		// the combo is created each time that we change table!
		combo.setEnabled(getDiagramEditPart() != null);

		refreshCombo();
		toolbar.pack();

		return toolbar;
	}

	/**
	 *
	 * @see org.eclipse.jface.action.ContributionItem#isDynamic()
	 *
	 * @return
	 */
	@Override
	public boolean isDynamic() {
		return true;
	}


	/**
	 * Get the zoom levels as text string array from the zoom manager.
	 *
	 * @return String array with zoom levels.
	 *
	 * @see org.eclipse.gef.editparts.ZoomManager#getZoomLevelsAsText()
	 */
	public String[] getZoomLevelsAsText(ZoomManager manager) {
		int nNumericZoomLevels = 0;
		if (manager != null) {
			nNumericZoomLevels = manager.getZoomLevels().length;
		}
		String[] allZoomLevels = new String[nNumericZoomLevels + 6];
		allZoomLevels[0] = ZoomAction.ZOOM_IN;
		allZoomLevels[1] = ZoomAction.ZOOM_OUT;
		allZoomLevels[2] = ZoomAction.ZOOM_FIT;
		allZoomLevels[3] = ZoomAction.ZOOM_WIDTH;
		allZoomLevels[4] = ZoomAction.ZOOM_HEIGHT;
		allZoomLevels[5] = ZoomAction.ZOOM_SELECTION;

		if (manager != null) {
			String[] numericZoomLevels = manager.getZoomLevelsAsText();
			for (int i = 0; i < manager.getZoomLevels().length; i++) {
				allZoomLevels[i + 6] = numericZoomLevels[i];
			}
		}
		return allZoomLevels;
	}

	/**
	 * Retrieves the value of the <code>workbenchPart</code> instance
	 * variable.
	 *
	 * @return The value of the <code>workbenchPart</code> instance variable.
	 */
	protected IDiagramWorkbenchPart getWorkbenchPart() {
		IWorkbenchPart activePart = WorkbenchPartHelper.getCurrentActiveWorkbenchPart();
		IDiagramWorkbenchPart editorPart = null;

		if (activePart instanceof IDiagramWorkbenchPart) {
			editorPart = (IDiagramWorkbenchPart) activePart;
		}
		if (activePart instanceof IAdaptable) {
			editorPart = ((IAdaptable) activePart).getAdapter(IDiagramWorkbenchPart.class);
		}

		return editorPart;
	}

	/**
	 * A utility method to return the active part if it implements the <code>IDiagramWorkbenchPart</code> interface
	 *
	 * @return The current part if it implements <code>IDiagramWorkbenchPart</code>; <code>null</code> otherwise
	 */
	protected IDiagramWorkbenchPart getDiagramWorkbenchPart() {
		IDiagramWorkbenchPart part = getWorkbenchPart();
		return part instanceof IDiagramWorkbenchPart ? (IDiagramWorkbenchPart) part : null;
	}

	/**
	 * A utility method to return the active <code>DiagramEditPart</code> if the current part implements <code>IDiagramWorkbenchPart</code>
	 *
	 * @return The current diagram if the parts implements <code>IDiagramWorkbenchPart</code>; <code>null</code> otherwise
	 */
	protected DiagramEditPart getDiagramEditPart() {
		IDiagramWorkbenchPart editor = getDiagramWorkbenchPart();
		return editor != null ? editor.getDiagramEditPart() : null;
	}


	/**
	 * Returns the current zoom manager
	 *
	 * @return
	 * 		the current zoom manager
	 */
	protected ZoomManager getZoomManager() {
		IDiagramWorkbenchPart part = getDiagramWorkbenchPart();
		if (part != null) {
			return part.getAdapter(ZoomManager.class);
		}
		return null;
	}


	/**
	 *
	 * @see org.eclipse.gef.editparts.ZoomListener#zoomChanged(double)
	 *
	 * @param zoom
	 */
	@Override
	public void zoomChanged(double zoom) {
		refreshCombo();
	}

	/**
	 * refresh the combo status
	 */
	public void refreshCombo() {
		if (combo != null && !combo.isDisposed() && combo.isEnabled()) {
			ZoomManager zoomManager = getZoomManager();
			if (getZoomManager() != null) {
				combo.setItems(getZoomLevelsAsText(zoomManager));
				if (zoomManager != null) {
					String zoomText = zoomManager.getZoomAsText();
					int index = combo.indexOf(zoomText);
					if (index != -1) {
						combo.select(index);
					} else {
						combo.setText(zoomText);
					}
				}
			}
		}
	}

	/**
	 * Sets the zoom level to the zoom string using the zoom manager. First, it
	 * checks for the special cases. If it isn't one of the special cases, it
	 * uses the zoom manager to do the zooming. There are six special cases,
	 * zoom to fit, zoom in, zoom out, zoom to width, zoom to height, and zoom
	 * to selected shapes.
	 *
	 * @param zoomText
	 *            the zoom string which we will zoom to.
	 * @see org.eclipse.gef.editparts.ZoomManager#setZoomAsText(java.lang.String)
	 */
	public void setZoomAsText(String zoomText) {
		String parameter = null;
		if (zoomText.equals(ZoomAction.ZOOM_IN)) {
			parameter = ZoomAction.ZOOM_IN_PARAMETER;
		} else if (zoomText.equals(ZoomAction.ZOOM_OUT)) {
			parameter = ZoomAction.ZOOM_OUT_PARAMETER;
		} else if (zoomText.equals(ZoomAction.ZOOM_100)) {
			parameter = ZoomAction.ZOOM_100_PARAMETER;
		} else if (zoomText.equals(ZoomAction.ZOOM_FIT)) {
			parameter = ZoomAction.ZOOM_FIT_PARAMETER;
		} else if (zoomText.equals(ZoomAction.ZOOM_WIDTH)) {
			parameter = ZoomAction.ZOOM_WIDTH_PARAMETER;
		} else if (zoomText.equals(ZoomAction.ZOOM_WIDTH)) {
			parameter = ZoomAction.ZOOM_HEIGHT_PARAMETER;
		} else if (zoomText.equals(ZoomAction.ZOOM_SELECTION)) {
			parameter = ZoomAction.ZOOM_SELECTION_PARAMETER;
		} else {
			parameter = zoomText;
			ZoomManager manager = getZoomManager();
			if (manager != null) {
				manager.setZoomAsText(parameter);// to set a specific value to zoom
			}
		}

		ZoomAction action = new ZoomAction(parameter, getSelectedElements());
		if (action.isEnabled()) {
			action.doRun(null);
		} else {
			refreshCombo();
		}
	}




	/**
	 *
	 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
	 *
	 * @param event
	 */
	@Override
	public void handleEvent(Event event) {
		if (event.type == SWT.KeyDown && event.character != '\r') {
			return;
		}
		setZoomAsText(combo.getText());

	}

	/**
	 * Iterate over current selection and build a list of the {@link IGraphicalEditPart} contained in
	 * the selection.
	 *
	 * @return the currently selected {@link IGraphicalEditPart}
	 */
	protected List<IGraphicalEditPart> getSelectedElements() {
		List<IGraphicalEditPart> editparts = new ArrayList<>();

		IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (activeWorkbenchWindow != null) {
			ISelection selection = activeWorkbenchWindow.getSelectionService().getSelection();
			if (selection instanceof IStructuredSelection) {

				IStructuredSelection structuredSelection = (IStructuredSelection) selection;

				Iterator<?> it = structuredSelection.iterator();
				while (it.hasNext()) {
					Object object = it.next();
					if (object instanceof IGraphicalEditPart) {
						editparts.add((IGraphicalEditPart) object);
					}
				}

			} else if (selection instanceof IGraphicalEditPart) {
				editparts.add((IGraphicalEditPart) selection);
			}
		}
		return editparts;
	}



	/**
	 *
	 * @see org.eclipse.papyrus.sasheditor.editor.IPageChangedListener#pageChanged(org.eclipse.papyrus.sasheditor.editor.IPage)
	 *
	 * @param newPage
	 */
	@Override
	public void pageChanged(IPage newPage) {
		refreshCombo();

		// we update the listeners
		if (previousPart != null) {
			// Remove listeners if any were previously set, skip it otherwise
			removeListeners(previousPart);
		}

		IWorkbenchPart activePart = partService.getActivePart();
		addListeners(activePart);
		previousPart = activePart;
	}

	/**
	 * Removes the listeners on the zoom manager and on the ISashWindowsContainer
	 *
	 * @param part
	 *            the part owning the listener to remove
	 */
	protected void removeListeners(IWorkbenchPart part) {
		// we remove the zoom listener
		ZoomManager manager = part.getAdapter(ZoomManager.class);
		if (manager != null) {
			manager.removeZoomListener(getInstance());
		}

		// we remove the page changed listener
		ISashWindowsContainer windowContainer = part.getAdapter(ISashWindowsContainer.class);
		if (windowContainer != null) {
			windowContainer.removePageChangedListener(getInstance());
		}
		refreshStatusCombo(part);

	}

	/**
	 * Adds the listeners on the zoom manager and on the ISashWindowsContainer
	 *
	 * @param part
	 *            the part owning the listener to add
	 */
	protected void addListeners(IWorkbenchPart part) {
		// we add listener on the zoom manager
		ZoomManager manager = part.getAdapter(ZoomManager.class);
		if (manager != null) {
			manager.addZoomListener(getInstance());
		}

		// we add listener on the window container
		ISashWindowsContainer windowContainer = part.getAdapter(ISashWindowsContainer.class);
		if (windowContainer != null) {
			windowContainer.addPageChangedListener(getInstance());
		}
		refreshStatusCombo(part);
	}



	/**
	 *
	 * @param part
	 *            refresh the state of the combo
	 */
	protected void refreshStatusCombo(IWorkbenchPart part) {
		ZoomManager zoomManager = part.getAdapter(ZoomManager.class);

		if (combo != null && !combo.isDisposed()) {
			if (zoomManager == null) {
				combo.setEnabled(false);
			} else {
				combo.setEnabled(true && getDiagramEditPart() != null);
				refreshCombo();
			}
		}
	}

	/**
	 * Returns the current instance of {@link ZoomToolbar} (this)
	 *
	 * @return
	 * 		the current instance of {@link ZoomToolbar} (this)
	 */
	protected ZoomToolbar getInstance() {
		return this;
	}

	/**
	 *
	 * This listener is used to listen the part changes
	 *
	 *
	 */
	public class CustomPartListener implements IPartListener {

		/**
		 *
		 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
		 *
		 * @param part
		 */
		@Override
		public void partActivated(IWorkbenchPart part) {
			if (part instanceof CoreMultiDiagramEditor) {
				removeListeners(part);
				addListeners(part);
			}
			refreshStatusCombo(part);
		}

		/**
		 *
		 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
		 *
		 * @param part
		 */
		@Override
		public void partBroughtToTop(IWorkbenchPart part) {
			refreshStatusCombo(part);
		}

		/**
		 *
		 * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
		 *
		 * @param part
		 */
		@Override
		public void partClosed(IWorkbenchPart part) {
			refreshStatusCombo(part);
		}

		/**
		 *
		 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
		 *
		 * @param part
		 */
		@Override
		public void partDeactivated(IWorkbenchPart part) {
			if (part instanceof CoreMultiDiagramEditor) {

			}
			refreshStatusCombo(part);
		}

		/**
		 *
		 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
		 *
		 * @param part
		 */
		@Override
		public void partOpened(IWorkbenchPart part) {
			if (part instanceof CoreMultiDiagramEditor) {
				removeListeners(part);
				addListeners(part);
			}
			refreshStatusCombo(part);
		}

	}
}

Back to the top