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