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