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