| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2005, 2012 IBM Corporation, Ericsson |
| 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 | * |
| 8 | * Contributors: |
| 9 | * IBM - Initial API and implementation |
| 10 | * Bernd Hufmann - Updated for TMF |
| 11 | **********************************************************************/ |
| 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; |
| 20 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
| 21 | import org.eclipse.linuxtools.internal.tmf.ui.ITmfImageConstants; |
| 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 | * Action class implementation for zooming in, out or reset of zoom. |
| 32 | * |
| 33 | * @version 1.0 |
| 34 | * @author sveyrier |
| 35 | * |
| 36 | */ |
| 37 | public class Zoom extends Action { |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | // Constants |
| 41 | // ------------------------------------------------------------------------ |
| 42 | /** |
| 43 | * The Action ID for zooming in. |
| 44 | */ |
| 45 | public final static String ZOOM_IN_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ZoomInCoolBar"; //$NON-NLS-1$ |
| 46 | /** |
| 47 | * The Action ID for zooming out. |
| 48 | */ |
| 49 | public final static String ZOOM_OUT_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ZoomOutCoolBar"; //$NON-NLS-1$ |
| 50 | /** |
| 51 | * The Action ID for reset zooming. |
| 52 | */ |
| 53 | public final static String RESET_ZOOM_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ResetZoom"; //$NON-NLS-1$ |
| 54 | /** |
| 55 | * The Action ID for no zoominf. |
| 56 | */ |
| 57 | public final static String NO_ZOOM_ID = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.NoZoom"; //$NON-NLS-1$ |
| 58 | |
| 59 | // ------------------------------------------------------------------------ |
| 60 | // Attributes |
| 61 | // ------------------------------------------------------------------------ |
| 62 | /** |
| 63 | * The sequence diagram view reference |
| 64 | */ |
| 65 | protected SDView fView = null; |
| 66 | /** |
| 67 | * Flag to indicate last zoom in. |
| 68 | */ |
| 69 | protected boolean fLastZoomIn = false; |
| 70 | /** |
| 71 | * Flag to indicate last zoom out. |
| 72 | */ |
| 73 | protected boolean fLastZoomOut = false; |
| 74 | /** |
| 75 | * Flag to indicate last zoom. |
| 76 | */ |
| 77 | protected boolean fLastZoom = true; |
| 78 | /** |
| 79 | * The cursor used when zooming in. |
| 80 | */ |
| 81 | private final Cursor fZoomInCursor; |
| 82 | /** |
| 83 | * The cursor used when zooming out. |
| 84 | */ |
| 85 | private final Cursor fZoomOutCursor; |
| 86 | |
| 87 | /** |
| 88 | * The different zoom actions |
| 89 | */ |
| 90 | public static enum ZoomType { |
| 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 | |
| 101 | // ------------------------------------------------------------------------ |
| 102 | // Constructors |
| 103 | // ------------------------------------------------------------------------ |
| 104 | |
| 105 | /** |
| 106 | * Constructor |
| 107 | * @param view The view reference |
| 108 | * @param type The type of zoom. |
| 109 | */ |
| 110 | public Zoom(SDView view, ZoomType type) { |
| 111 | super("", AS_RADIO_BUTTON);//$NON-NLS-1$ |
| 112 | |
| 113 | fView = view; |
| 114 | |
| 115 | // Pre-create zooming cursors |
| 116 | fZoomInCursor = new Cursor(Display.getCurrent(), |
| 117 | Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_IN).getImageData(), |
| 118 | Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0); |
| 119 | |
| 120 | fZoomOutCursor = new Cursor(Display.getCurrent(), |
| 121 | Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM_OUT).getImageData(), |
| 122 | Activator.getDefault().getImageFromImageRegistry(ITmfImageConstants.IMG_UI_ZOOM).getImageData(), 0, 0); |
| 123 | |
| 124 | switch (type) { |
| 125 | case ZOOM_IN: |
| 126 | setText(SDMessages._47); |
| 127 | setToolTipText(SDMessages._48); |
| 128 | setId(ZOOM_IN_ID); |
| 129 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_IN_MENU)); |
| 130 | break; |
| 131 | |
| 132 | case ZOOM_OUT: |
| 133 | setText(SDMessages._51); |
| 134 | setToolTipText(SDMessages._52); |
| 135 | setId(ZOOM_OUT_ID); |
| 136 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_OUT_MENU)); |
| 137 | break; |
| 138 | |
| 139 | case ZOOM_RESET: |
| 140 | setText(SDMessages._49); |
| 141 | setToolTipText(SDMessages._50); |
| 142 | setId(RESET_ZOOM_ID); |
| 143 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_HOME_MENU)); |
| 144 | break; |
| 145 | |
| 146 | case ZOOM_NONE: |
| 147 | default: |
| 148 | setText(SDMessages._53); |
| 149 | setToolTipText(SDMessages._54); |
| 150 | setId(NO_ZOOM_ID); |
| 151 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SELECT_MENU)); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // ------------------------------------------------------------------------ |
| 157 | // Methods |
| 158 | // ------------------------------------------------------------------------ |
| 159 | /* |
| 160 | * (non-Javadoc) |
| 161 | * @see org.eclipse.jface.action.Action#run() |
| 162 | */ |
| 163 | @Override |
| 164 | public void run() { |
| 165 | |
| 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. |
| 223 | * |
| 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); |
| 238 | if (nextPage instanceof ActionContributionItem) { |
| 239 | IAction action = ((ActionContributionItem) nextPage).getAction(); |
| 240 | if (action != null) { |
| 241 | action.setChecked(checked); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |