Commit | Line | Data |
---|---|---|
73005152 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2005, 2008, 2011 IBM Corporation and others. | |
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 | |
7 | * $Id: Zoom.java,v 1.3 2008/01/24 02:28:52 apnan Exp $ | |
8 | * | |
9 | * Contributors: | |
10 | * IBM - Initial API and implementation | |
11 | * Bernd Hufmann - Updated for TMF | |
12 | **********************************************************************/ | |
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; | |
d34665f9 FC |
20 | import org.eclipse.linuxtools.internal.tmf.ui.ITmfImageConstants; |
21 | import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin; | |
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 | /** | |
31 | * @author sveyrier | |
32 | * | |
33 | */ | |
34 | public class Zoom extends Action { | |
35 | ||
36 | // ------------------------------------------------------------------------ | |
37 | // Attributes | |
38 | // ------------------------------------------------------------------------ | |
39 | public final static String ZOOM_IN_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ZoomInCoolBar"; //$NON-NLS-1$ | |
40 | public final static String ZOOM_OUT_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ZoomOutCoolBar"; //$NON-NLS-1$ | |
41 | public final static String RESET_ZOOM_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ResetZoom"; //$NON-NLS-1$ | |
42 | public final static String NO_ZOOM_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.NoZoom"; //$NON-NLS-1$ | |
43 | ||
44 | protected SDView fView = null; | |
45 | protected boolean fLastZoomIn = false; | |
46 | protected boolean fLastZoomOut = false; | |
47 | protected boolean fLastZoom = true; | |
48 | ||
e6ace8bb BH |
49 | private static Cursor fZoomInCursor; |
50 | private static Cursor fZoomOutCursor; | |
73005152 BH |
51 | |
52 | public static enum ZoomType { | |
53 | ZOOM_NONE, ZOOM_IN, ZOOM_OUT, ZOOM_RESET | |
54 | }; | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Constructors | |
58 | // ------------------------------------------------------------------------ | |
59 | public Zoom(SDView _view, ZoomType type) { | |
60 | super("", AS_RADIO_BUTTON);//$NON-NLS-1$ | |
61 | ||
62 | fView = _view; | |
63 | ||
64 | if ((fZoomInCursor != null) && (!fZoomInCursor.isDisposed())) { | |
65 | fZoomInCursor.dispose(); | |
66 | } | |
67 | if ((fZoomOutCursor != null) && (!fZoomOutCursor.isDisposed())) { | |
68 | fZoomOutCursor.dispose(); | |
69 | } | |
70 | ||
71 | // Pre-create zooming cursors | |
72 | fZoomInCursor = new Cursor(Display.getCurrent(), | |
73 | TmfUiPlugin.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_IN).getImageData(), | |
74 | TmfUiPlugin.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0); | |
75 | ||
76 | fZoomOutCursor = new Cursor(Display.getCurrent(), | |
77 | TmfUiPlugin.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_OUT).getImageData(), | |
78 | TmfUiPlugin.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0); | |
79 | ||
80 | switch (type) { | |
81 | case ZOOM_IN: | |
82 | setText(SDMessages._47); | |
83 | setToolTipText(SDMessages._48); | |
84 | setId(ZOOM_IN_ID); | |
85 | setImageDescriptor(TmfUiPlugin.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_IN_MENU)); | |
86 | break; | |
87 | ||
88 | case ZOOM_OUT: | |
89 | setText(SDMessages._51); | |
90 | setToolTipText(SDMessages._52); | |
91 | setId(ZOOM_OUT_ID); | |
92 | setImageDescriptor(TmfUiPlugin.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_OUT_MENU)); | |
93 | break; | |
94 | ||
95 | case ZOOM_RESET: | |
96 | setText(SDMessages._49); | |
97 | setToolTipText(SDMessages._50); | |
98 | setId(RESET_ZOOM_ID); | |
99 | setImageDescriptor(TmfUiPlugin.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_HOME_MENU)); | |
100 | break; | |
101 | ||
102 | default: | |
103 | // ZoomType.NONE | |
104 | setText(SDMessages._53); | |
105 | setToolTipText(SDMessages._54); | |
106 | setId(NO_ZOOM_ID); | |
107 | setImageDescriptor(TmfUiPlugin.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SELECT_MENU)); | |
108 | break; | |
109 | } | |
110 | } | |
111 | ||
112 | // ------------------------------------------------------------------------ | |
113 | // Operations | |
114 | // ------------------------------------------------------------------------ | |
115 | /* | |
116 | * (non-Javadoc) | |
117 | * @see org.eclipse.jface.action.Action#run() | |
118 | */ | |
119 | @Override | |
120 | public void run() { | |
121 | ||
122 | if ((fView == null) || (fView.getSDWidget() == null)) { | |
123 | return; | |
124 | } | |
125 | ||
126 | SDWidget viewer = fView.getSDWidget(); | |
127 | ||
128 | if (getId().equals(ZOOM_OUT_ID)) { | |
129 | // Eclipse 3.0 M7 workaround | |
130 | if (fLastZoomOut == isChecked()) { | |
131 | setChecked(!isChecked()); | |
132 | } | |
133 | ||
134 | viewer.setZoomOutMode(isChecked()); | |
135 | fLastZoomOut = isChecked(); | |
136 | fLastZoom = false; | |
137 | if (isChecked()) { | |
138 | viewer.setCursor(fZoomOutCursor); | |
139 | setActionChecked(NO_ZOOM_ID, false); | |
140 | } else { | |
141 | viewer.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_ARROW)); | |
142 | setActionChecked(NO_ZOOM_ID, true); | |
143 | } | |
144 | } else if (getId().equals(ZOOM_IN_ID)) { | |
145 | // Eclipse 3.0 M7 workaround | |
146 | if (fLastZoomIn == isChecked()) { | |
147 | setChecked(!isChecked()); | |
148 | } | |
149 | ||
150 | viewer.setZoomInMode(isChecked()); | |
151 | fLastZoomIn = isChecked(); | |
152 | fLastZoom = false; | |
153 | if (isChecked()) { | |
154 | viewer.setCursor(fZoomInCursor); | |
155 | setActionChecked(NO_ZOOM_ID, false); | |
156 | } else { | |
157 | viewer.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_ARROW)); | |
158 | setActionChecked(NO_ZOOM_ID, true); | |
159 | } | |
160 | } else if (getId().equals(RESET_ZOOM_ID)) { | |
161 | viewer.resetZoomFactor(); | |
162 | ||
163 | // The reset action is a radio button only to uncheck the zoom in and out button | |
164 | // when it is clicked. This avoid adding code to do it manually | |
165 | // We only have to force it to false every time | |
166 | fLastZoom = false; | |
167 | setChecked(false); | |
168 | setActionChecked(NO_ZOOM_ID, true); | |
169 | } else if (getId().equals(NO_ZOOM_ID)) { | |
170 | setChecked(true); | |
171 | viewer.setZoomInMode(false); | |
172 | viewer.setZoomInMode(false); | |
173 | viewer.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_ARROW)); | |
174 | } | |
175 | } | |
176 | ||
177 | /** | |
178 | * Set action check state of a view action for a given action ID. | |
179 | * @param id The action ID | |
180 | * @param checked true to check the action, false to uncheck the action | |
181 | */ | |
182 | protected void setActionChecked(String id, boolean checked) { | |
183 | if (fView != null) { | |
184 | IActionBars bar = fView.getViewSite().getActionBars(); | |
185 | if (bar == null) { | |
186 | return; | |
187 | } | |
188 | IToolBarManager barManager = bar.getToolBarManager(); | |
189 | if (barManager == null) { | |
190 | return; | |
191 | } | |
192 | IContributionItem nextPage = barManager.find(id); | |
193 | if (nextPage != null && nextPage instanceof ActionContributionItem) { | |
194 | IAction action = ((ActionContributionItem) nextPage).getAction(); | |
195 | if (action != null) { | |
196 | action.setChecked(checked); | |
197 | } | |
198 | } | |
199 | } | |
200 | } | |
201 | } |