2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Marcos CARDINOT
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
13 package org.scilab.modules.xcos.palette.actions;
15 import static org.scilab.modules.commons.OS.MAC;
17 import java.awt.Toolkit;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.KeyEvent;
20 import java.lang.reflect.InvocationTargetException;
22 import javax.swing.ActionMap;
23 import javax.swing.ImageIcon;
24 import javax.swing.InputMap;
25 import javax.swing.JButton;
26 import javax.swing.KeyStroke;
28 import org.scilab.modules.commons.OS;
29 import org.scilab.modules.commons.gui.FindIconHelper;
30 import org.scilab.modules.commons.gui.ScilabLAF;
31 import org.scilab.modules.gui.events.callback.CommonCallBack;
32 import org.scilab.modules.xcos.palette.view.PaletteManagerView;
33 import org.scilab.modules.xcos.utils.XcosMessages;
37 * @author Marcos CARDINOT <mcardinot@gmail.com>
39 public class ZoomAction extends CommonCallBack {
41 private static final long serialVersionUID = 1L;
43 private static final String LABEL_ZOOMIN = XcosMessages.ZOOM_IN;
44 private static final String LABEL_ZOOMOUT = XcosMessages.ZOOM_OUT;
45 private static final String ICON_ZOOMIN = FindIconHelper.findIcon("zoom-in");
46 private static final String ICON_ZOOMOUT = FindIconHelper.findIcon("zoom-out");
48 private static final int ACCELERATOR_KEY = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
49 private static final String ZOOM_IN = "ZoomIn";
50 private static final String ZOOM_OUT = "ZoomOut";
52 private static JButton btnZoomIn;
53 private static JButton btnZoomOut;
63 * Register the key for the action
64 * @param view PaletteManagerPanel instance
66 public static void registerKeyAction(PaletteManagerView view) {
67 // Multi-shortcut action
68 ActionMap[] ams = new ActionMap[] {
69 view.getTree().getActionMap(),
70 view.getPanel().getActionMap()
72 InputMap[] maps = new InputMap[] {
73 view.getTree().getInputMap(),
74 view.getPanel().getInputMap()
77 // register the action to a unique action keyword
78 for (ActionMap am : ams) {
79 am.put(ZOOM_IN, new ZoomAction());
80 am.put(ZOOM_OUT, new ZoomAction());
83 // add custom key stroke for this action
84 final KeyStroke[] keystrokesIn;
85 final KeyStroke[] keystrokesOut;
86 if (OS.get() == MAC) {
87 // AZERTY for Mac has a non-supported classic layout
88 keystrokesIn = new KeyStroke[] {
89 KeyStroke.getKeyStroke('/', ACCELERATOR_KEY),
90 KeyStroke.getKeyStroke('/', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK)
92 keystrokesOut = new KeyStroke[] {
93 KeyStroke.getKeyStroke('=', ACCELERATOR_KEY),
94 KeyStroke.getKeyStroke('=', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK)
97 keystrokesIn = new KeyStroke[] {
98 KeyStroke.getKeyStroke('=', ACCELERATOR_KEY),
99 KeyStroke.getKeyStroke('=', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
100 KeyStroke.getKeyStroke('+', ACCELERATOR_KEY),
101 KeyStroke.getKeyStroke('+', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
102 KeyStroke.getKeyStroke(KeyEvent.VK_ADD, ACCELERATOR_KEY)
104 keystrokesOut = new KeyStroke[] {
105 KeyStroke.getKeyStroke('-', ACCELERATOR_KEY),
106 KeyStroke.getKeyStroke('-', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
107 KeyStroke.getKeyStroke('_', ACCELERATOR_KEY),
108 KeyStroke.getKeyStroke('_', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
109 KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, ACCELERATOR_KEY)
113 for (InputMap map : maps) {
114 for (KeyStroke k : keystrokesIn) {
117 for (KeyStroke k : keystrokesOut) {
118 map.put(k, ZOOM_OUT);
124 * Create the button 'zoom in'
127 public static JButton createButtonZoomIn() {
128 btnZoomIn = new JButton();
129 ScilabLAF.setDefaultProperties(btnZoomIn);
130 btnZoomIn.setIcon(new ImageIcon(ICON_ZOOMIN));
131 btnZoomIn.setToolTipText(LABEL_ZOOMIN);
132 btnZoomIn.addActionListener(getCallBack());
133 setEnabledZoomIn(true);
138 * Create the button 'zoom out'
141 public static JButton createButtonZoomOut() {
142 btnZoomOut = new JButton();
143 ScilabLAF.setDefaultProperties(btnZoomOut);
144 btnZoomOut.setIcon(new ImageIcon(ICON_ZOOMOUT));
145 btnZoomOut.setToolTipText(LABEL_ZOOMOUT);
146 btnZoomOut.addActionListener(getCallBack());
147 setEnabledZoomOut(true);
152 * Create a new class instance
153 * @return the instance
155 private static CommonCallBack getCallBack() {
156 CommonCallBack callback = null;
158 callback = ZoomAction.class.getConstructor().newInstance();
159 } catch (IllegalArgumentException e) {
161 } catch (SecurityException e) {
163 } catch (InstantiationException e) {
165 } catch (IllegalAccessException e) {
167 } catch (InvocationTargetException e) {
169 } catch (NoSuchMethodException e) {
177 * @param e ActionEvent
179 public void actionPerformed(ActionEvent e) {
180 String cmd = e.getActionCommand();
182 Object src = e.getSource();
183 if (btnZoomIn != null && src.equals(btnZoomIn)) {
184 PaletteManagerView.get().getPanel().zoomIn();
185 } else if (btnZoomOut != null && src.equals(btnZoomOut)) {
186 PaletteManagerView.get().getPanel().zoomOut();
188 } else if (OS.get() == MAC) {
189 if (cmd.equals("/")) {
190 PaletteManagerView.get().getPanel().zoomIn();
191 } else if (cmd.equals("=")) {
192 PaletteManagerView.get().getPanel().zoomOut();
195 if (cmd.equals("+") || cmd.equals("=")) {
196 PaletteManagerView.get().getPanel().zoomIn();
197 } else if (cmd.equals("-") || cmd.equals("_")) {
198 PaletteManagerView.get().getPanel().zoomOut();
204 * setEnabled property of the button 'zoomIn'
205 * @param enabled enabled
207 public static void setEnabledZoomIn(boolean enabled) {
208 btnZoomIn.setEnabled(enabled);
212 * setEnabled property of the button 'zoomOut'
213 * @param enabled enabled
215 public static void setEnabledZoomOut(boolean enabled) {
216 btnZoomOut.setEnabled(enabled);
220 public void callBack() {