Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
df0b8ff4 BH |
2 | * Copyright (c) 2005, 2008 IBM Corporation and others. |
3 | * Copyright (c) 2011, 2012 Ericsson. | |
a55887ca | 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 | |
a55887ca AM |
9 | * |
10 | * Contributors: | |
73005152 BH |
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; | |
8fd82db5 | 21 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
a55887ca | 22 | import org.eclipse.linuxtools.internal.tmf.ui.ITmfImageConstants; |
73005152 BH |
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 | /** | |
df0b8ff4 | 32 | * Action class implementation for zooming in, out or reset of zoom. |
a55887ca | 33 | * |
df0b8ff4 | 34 | * @version 1.0 |
73005152 | 35 | * @author sveyrier |
a55887ca | 36 | * |
73005152 BH |
37 | */ |
38 | public 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 | 58 | public final static String NO_ZOOM_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.NoZoom"; //$NON-NLS-1$ |
a55887ca | 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 | */ | |
a55887ca | 82 | private final Cursor fZoomInCursor; |
df0b8ff4 BH |
83 | /** |
84 | * The cursor used when zooming out. | |
85 | */ | |
a55887ca | 86 | private final Cursor fZoomOutCursor; |
73005152 | 87 | |
a55887ca AM |
88 | /** |
89 | * The different zoom actions | |
90 | */ | |
73005152 | 91 | public static enum ZoomType { |
a55887ca AM |
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 | ||
73005152 BH |
102 | // ------------------------------------------------------------------------ |
103 | // Constructors | |
104 | // ------------------------------------------------------------------------ | |
a55887ca | 105 | |
df0b8ff4 BH |
106 | /** |
107 | * Constructor | |
eb63f5ff | 108 | * @param view The view reference |
df0b8ff4 BH |
109 | * @param type The type of zoom. |
110 | */ | |
eb63f5ff | 111 | public Zoom(SDView view, ZoomType type) { |
73005152 BH |
112 | super("", AS_RADIO_BUTTON);//$NON-NLS-1$ |
113 | ||
eb63f5ff | 114 | fView = view; |
73005152 BH |
115 | |
116 | // Pre-create zooming cursors | |
a55887ca AM |
117 | fZoomInCursor = new Cursor(Display.getCurrent(), |
118 | Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_IN).getImageData(), | |
8fd82db5 | 119 | Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0); |
df0b8ff4 | 120 | |
a55887ca | 121 | fZoomOutCursor = new Cursor(Display.getCurrent(), |
8fd82db5 FC |
122 | Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_OUT).getImageData(), |
123 | Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0); | |
73005152 BH |
124 | |
125 | switch (type) { | |
126 | case ZOOM_IN: | |
127 | setText(SDMessages._47); | |
128 | setToolTipText(SDMessages._48); | |
129 | setId(ZOOM_IN_ID); | |
8fd82db5 | 130 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_IN_MENU)); |
73005152 BH |
131 | break; |
132 | ||
133 | case ZOOM_OUT: | |
134 | setText(SDMessages._51); | |
135 | setToolTipText(SDMessages._52); | |
136 | setId(ZOOM_OUT_ID); | |
8fd82db5 | 137 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_OUT_MENU)); |
73005152 BH |
138 | break; |
139 | ||
140 | case ZOOM_RESET: | |
141 | setText(SDMessages._49); | |
142 | setToolTipText(SDMessages._50); | |
143 | setId(RESET_ZOOM_ID); | |
8fd82db5 | 144 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_HOME_MENU)); |
73005152 BH |
145 | break; |
146 | ||
a55887ca | 147 | case ZOOM_NONE: |
73005152 | 148 | default: |
73005152 BH |
149 | setText(SDMessages._53); |
150 | setToolTipText(SDMessages._54); | |
a55887ca | 151 | setId(NO_ZOOM_ID); |
8fd82db5 | 152 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SELECT_MENU)); |
73005152 BH |
153 | break; |
154 | } | |
155 | } | |
156 | ||
157 | // ------------------------------------------------------------------------ | |
df0b8ff4 | 158 | // Methods |
73005152 BH |
159 | // ------------------------------------------------------------------------ |
160 | /* | |
161 | * (non-Javadoc) | |
162 | * @see org.eclipse.jface.action.Action#run() | |
163 | */ | |
164 | @Override | |
165 | public void run() { | |
a55887ca | 166 | |
73005152 BH |
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. | |
a55887ca | 224 | * |
73005152 BH |
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); | |
eb63f5ff | 239 | if (nextPage instanceof ActionContributionItem) { |
73005152 BH |
240 | IAction action = ((ActionContributionItem) nextPage).getAction(); |
241 | if (action != null) { | |
242 | action.setChecked(checked); | |
243 | } | |
244 | } | |
245 | } | |
246 | } | |
247 | } |