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