ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / handlers / Zoom.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2014 IBM Corporation, Ericsson
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 - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers;
14
15 import org.eclipse.jface.action.ActionContributionItem;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.IContributionItem;
18 import org.eclipse.jface.action.IToolBarManager;
19 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
20 import org.eclipse.linuxtools.internal.tmf.ui.ITmfImageConstants;
21 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView;
22 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget;
23 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.Messages;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Cursor;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.ui.IActionBars;
28
29 /**
30 * Action class implementation for zooming in, out or reset of zoom.
31 *
32 * @version 1.0
33 * @author sveyrier
34 *
35 */
36 public class Zoom extends BaseSDAction {
37
38 // ------------------------------------------------------------------------
39 // Constants
40 // ------------------------------------------------------------------------
41 /**
42 * The Action ID for zooming in.
43 */
44 public static final String ZOOM_IN_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ZoomInCoolBar"; //$NON-NLS-1$
45 /**
46 * The Action ID for zooming out.
47 */
48 public static final String ZOOM_OUT_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ZoomOutCoolBar"; //$NON-NLS-1$
49 /**
50 * The Action ID for reset zooming.
51 */
52 public static final String RESET_ZOOM_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ResetZoom"; //$NON-NLS-1$
53 /**
54 * The Action ID for no zoominf.
55 */
56 public static final String NO_ZOOM_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.NoZoom"; //$NON-NLS-1$
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
61 /**
62 * Flag to indicate last zoom in.
63 */
64 private boolean fLastZoomIn = false;
65 /**
66 * Flag to indicate last zoom out.
67 */
68 private boolean fLastZoomOut = false;
69 /**
70 * The cursor used when zooming in.
71 */
72 private final Cursor fZoomInCursor;
73 /**
74 * The cursor used when zooming out.
75 */
76 private final Cursor fZoomOutCursor;
77
78 /**
79 * The different zoom actions
80 */
81 public static enum ZoomType {
82 /** No zoom information */
83 ZOOM_NONE,
84 /** Zoom in */
85 ZOOM_IN,
86 /** Zoom out */
87 ZOOM_OUT,
88 /** Reset to the default zoom level */
89 ZOOM_RESET
90 }
91
92 // ------------------------------------------------------------------------
93 // Constructors
94 // ------------------------------------------------------------------------
95
96 /**
97 * Constructor
98 * @param view The view reference
99 * @param type The type of zoom.
100 */
101 public Zoom(SDView view, ZoomType type) {
102 super(view, "", AS_RADIO_BUTTON); //$NON-NLS-1$
103
104 // Pre-create zooming cursors
105 fZoomInCursor = new Cursor(Display.getCurrent(),
106 Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_IN).getImageData(),
107 Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0);
108
109 fZoomOutCursor = new Cursor(Display.getCurrent(),
110 Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_OUT).getImageData(),
111 Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0);
112
113 switch (type) {
114 case ZOOM_IN:
115 setText(Messages.SequenceDiagram_ZoomIn);
116 setToolTipText(Messages.SequenceDiagram_ZoomInTheDiagram);
117 setId(ZOOM_IN_ID);
118 setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_IN_MENU));
119 break;
120
121 case ZOOM_OUT:
122 setText(Messages.SequenceDiagram_ZoomOut);
123 setToolTipText(Messages.SequenceDiagram_ZoomOutTheDiagram);
124 setId(ZOOM_OUT_ID);
125 setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_OUT_MENU));
126 break;
127
128 case ZOOM_RESET:
129 setText(Messages.SequenceDiagram_ResetZoomFactor);
130 setToolTipText(Messages.SequenceDiagram_ResetZoomFactor);
131 setId(RESET_ZOOM_ID);
132 setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_HOME_MENU));
133 break;
134
135 case ZOOM_NONE:
136 default:
137 setText(Messages.SequenceDiagram_Select);
138 setToolTipText(Messages.SequenceDiagram_Select);
139 setId(NO_ZOOM_ID);
140 setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SELECT_MENU));
141 break;
142 }
143 }
144
145 // ------------------------------------------------------------------------
146 // Methods
147 // ------------------------------------------------------------------------
148
149 @Override
150 public void run() {
151
152 if ((getView() == null) || (getView().getSDWidget() == null)) {
153 return;
154 }
155
156 SDWidget viewer = getView().getSDWidget();
157
158 if (getId().equals(ZOOM_OUT_ID)) {
159 // Eclipse 3.0 M7 workaround
160 if (fLastZoomOut == isChecked()) {
161 setChecked(!isChecked());
162 }
163
164 viewer.setZoomOutMode(isChecked());
165 fLastZoomOut = isChecked();
166 if (isChecked()) {
167 viewer.setCursor(fZoomOutCursor);
168 setActionChecked(NO_ZOOM_ID, false);
169 } else {
170 viewer.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_ARROW));
171 setActionChecked(NO_ZOOM_ID, true);
172 }
173 } else if (getId().equals(ZOOM_IN_ID)) {
174 // Eclipse 3.0 M7 workaround
175 if (fLastZoomIn == isChecked()) {
176 setChecked(!isChecked());
177 }
178
179 viewer.setZoomInMode(isChecked());
180 fLastZoomIn = isChecked();
181 if (isChecked()) {
182 viewer.setCursor(fZoomInCursor);
183 setActionChecked(NO_ZOOM_ID, false);
184 } else {
185 viewer.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_ARROW));
186 setActionChecked(NO_ZOOM_ID, true);
187 }
188 } else if (getId().equals(RESET_ZOOM_ID)) {
189 viewer.resetZoomFactor();
190
191 // The reset action is a radio button only to uncheck the zoom in and out button
192 // when it is clicked. This avoid adding code to do it manually
193 // We only have to force it to false every time
194 setChecked(false);
195 setActionChecked(NO_ZOOM_ID, true);
196 } else if (getId().equals(NO_ZOOM_ID)) {
197 setChecked(true);
198 viewer.setZoomInMode(false);
199 viewer.setZoomInMode(false);
200 viewer.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_ARROW));
201 }
202 }
203
204 /**
205 * Set action check state of a view action for a given action ID.
206 *
207 * @param id The action ID
208 * @param checked true to check the action, false to uncheck the action
209 */
210 protected void setActionChecked(String id, boolean checked) {
211 if (getView() != null) {
212 IActionBars bar = getView().getViewSite().getActionBars();
213 if (bar == null) {
214 return;
215 }
216 IToolBarManager barManager = bar.getToolBarManager();
217 if (barManager == null) {
218 return;
219 }
220 IContributionItem nextPage = barManager.find(id);
221 if (nextPage instanceof ActionContributionItem) {
222 IAction action = ((ActionContributionItem) nextPage).getAction();
223 if (action != null) {
224 action.setChecked(checked);
225 }
226 }
227 }
228 }
229
230 /**
231 * Dispose the action and its resources
232 *
233 * @since 3.2
234 */
235 public void dispose() {
236 fZoomInCursor.dispose();
237 fZoomOutCursor.dispose();
238 }
239 }
This page took 0.060535 seconds and 5 git commands to generate.