Java Doc and API clean up of TMF UML Sequence diagram framework
[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.ITmfImageConstants;
22 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
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 static Cursor fZoomInCursor;
83 /**
84 * The cursor used when zooming out.
85 */
86 private static Cursor fZoomOutCursor;
87
88 public static enum ZoomType {
89 ZOOM_NONE, ZOOM_IN, ZOOM_OUT, 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("", AS_RADIO_BUTTON);//$NON-NLS-1$
103
104 fView = _view;
105
106 if ((fZoomInCursor != null) && (!fZoomInCursor.isDisposed())) {
107 fZoomInCursor.dispose();
108 }
109
110 if ((fZoomOutCursor != null) && (!fZoomOutCursor.isDisposed())) {
111 fZoomOutCursor.dispose();
112 }
113
114 // Pre-create zooming cursors
115 fZoomInCursor = new Cursor(Display.getCurrent(),
116 TmfUiPlugin.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_IN).getImageData(),
117 TmfUiPlugin.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0);
118
119 fZoomOutCursor = new Cursor(Display.getCurrent(),
120 TmfUiPlugin.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_OUT).getImageData(),
121 TmfUiPlugin.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0);
122
123 switch (type) {
124 case ZOOM_IN:
125 setText(SDMessages._47);
126 setToolTipText(SDMessages._48);
127 setId(ZOOM_IN_ID);
128 setImageDescriptor(TmfUiPlugin.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_IN_MENU));
129 break;
130
131 case ZOOM_OUT:
132 setText(SDMessages._51);
133 setToolTipText(SDMessages._52);
134 setId(ZOOM_OUT_ID);
135 setImageDescriptor(TmfUiPlugin.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_OUT_MENU));
136 break;
137
138 case ZOOM_RESET:
139 setText(SDMessages._49);
140 setToolTipText(SDMessages._50);
141 setId(RESET_ZOOM_ID);
142 setImageDescriptor(TmfUiPlugin.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_HOME_MENU));
143 break;
144
145 default:
146 // ZoomType.NONE
147 setText(SDMessages._53);
148 setToolTipText(SDMessages._54);
149 setId(NO_ZOOM_ID);
150 setImageDescriptor(TmfUiPlugin.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SELECT_MENU));
151 break;
152 }
153 }
154
155 // ------------------------------------------------------------------------
156 // Methods
157 // ------------------------------------------------------------------------
158 /*
159 * (non-Javadoc)
160 * @see org.eclipse.jface.action.Action#run()
161 */
162 @Override
163 public void run() {
164
165 if ((fView == null) || (fView.getSDWidget() == null)) {
166 return;
167 }
168
169 SDWidget viewer = fView.getSDWidget();
170
171 if (getId().equals(ZOOM_OUT_ID)) {
172 // Eclipse 3.0 M7 workaround
173 if (fLastZoomOut == isChecked()) {
174 setChecked(!isChecked());
175 }
176
177 viewer.setZoomOutMode(isChecked());
178 fLastZoomOut = isChecked();
179 fLastZoom = false;
180 if (isChecked()) {
181 viewer.setCursor(fZoomOutCursor);
182 setActionChecked(NO_ZOOM_ID, false);
183 } else {
184 viewer.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_ARROW));
185 setActionChecked(NO_ZOOM_ID, true);
186 }
187 } else if (getId().equals(ZOOM_IN_ID)) {
188 // Eclipse 3.0 M7 workaround
189 if (fLastZoomIn == isChecked()) {
190 setChecked(!isChecked());
191 }
192
193 viewer.setZoomInMode(isChecked());
194 fLastZoomIn = isChecked();
195 fLastZoom = false;
196 if (isChecked()) {
197 viewer.setCursor(fZoomInCursor);
198 setActionChecked(NO_ZOOM_ID, false);
199 } else {
200 viewer.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_ARROW));
201 setActionChecked(NO_ZOOM_ID, true);
202 }
203 } else if (getId().equals(RESET_ZOOM_ID)) {
204 viewer.resetZoomFactor();
205
206 // The reset action is a radio button only to uncheck the zoom in and out button
207 // when it is clicked. This avoid adding code to do it manually
208 // We only have to force it to false every time
209 fLastZoom = false;
210 setChecked(false);
211 setActionChecked(NO_ZOOM_ID, true);
212 } else if (getId().equals(NO_ZOOM_ID)) {
213 setChecked(true);
214 viewer.setZoomInMode(false);
215 viewer.setZoomInMode(false);
216 viewer.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_ARROW));
217 }
218 }
219
220 /**
221 * Set action check state of a view action for a given action ID.
222 *
223 * @param id The action ID
224 * @param checked true to check the action, false to uncheck the action
225 */
226 protected void setActionChecked(String id, boolean checked) {
227 if (fView != null) {
228 IActionBars bar = fView.getViewSite().getActionBars();
229 if (bar == null) {
230 return;
231 }
232 IToolBarManager barManager = bar.getToolBarManager();
233 if (barManager == null) {
234 return;
235 }
236 IContributionItem nextPage = barManager.find(id);
237 if (nextPage != null && nextPage instanceof ActionContributionItem) {
238 IAction action = ((ActionContributionItem) nextPage).getAction();
239 if (action != null) {
240 action.setChecked(checked);
241 }
242 }
243 }
244 }
245 }
This page took 0.035613 seconds and 5 git commands to generate.