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