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 am the ActionMap
65 * @param im the InputMap
67 public static void registerKeyAction(ActionMap am, InputMap im) {
68 // register the actions to a unique action keyword
69 am.put(ZOOM_IN, new ZoomAction());
70 am.put(ZOOM_OUT, new ZoomAction());
72 // add custom key stroke for these action
73 final KeyStroke[] keystrokesIn;
74 final KeyStroke[] keystrokesOut;
75 if (OS.get() == MAC) {
76 // AZERTY for Mac has a non-supported classic layout
77 keystrokesIn = new KeyStroke[] {
78 KeyStroke.getKeyStroke('/', ACCELERATOR_KEY),
79 KeyStroke.getKeyStroke('/', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK)
81 keystrokesOut = new KeyStroke[] {
82 KeyStroke.getKeyStroke('=', ACCELERATOR_KEY),
83 KeyStroke.getKeyStroke('=', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK)
86 keystrokesIn = new KeyStroke[] {
87 KeyStroke.getKeyStroke('=', ACCELERATOR_KEY),
88 KeyStroke.getKeyStroke('=', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
89 KeyStroke.getKeyStroke('+', ACCELERATOR_KEY),
90 KeyStroke.getKeyStroke('+', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
91 KeyStroke.getKeyStroke(KeyEvent.VK_ADD, ACCELERATOR_KEY)
93 keystrokesOut = new KeyStroke[] {
94 KeyStroke.getKeyStroke('-', ACCELERATOR_KEY),
95 KeyStroke.getKeyStroke('-', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
96 KeyStroke.getKeyStroke('_', ACCELERATOR_KEY),
97 KeyStroke.getKeyStroke('_', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
98 KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, ACCELERATOR_KEY)
102 for (KeyStroke k : keystrokesIn) {
105 for (KeyStroke k : keystrokesOut) {
111 * Create the button 'zoom in'
114 public static JButton createButtonZoomIn() {
115 btnZoomIn = new JButton();
116 ScilabLAF.setDefaultProperties(btnZoomIn);
117 btnZoomIn.setIcon(new ImageIcon(ICON_ZOOMIN));
118 btnZoomIn.setToolTipText(LABEL_ZOOMIN);
119 btnZoomIn.addActionListener(getCallBack());
120 setEnabledZoomIn(true);
125 * Create the button 'zoom out'
128 public static JButton createButtonZoomOut() {
129 btnZoomOut = new JButton();
130 ScilabLAF.setDefaultProperties(btnZoomOut);
131 btnZoomOut.setIcon(new ImageIcon(ICON_ZOOMOUT));
132 btnZoomOut.setToolTipText(LABEL_ZOOMOUT);
133 btnZoomOut.addActionListener(getCallBack());
134 setEnabledZoomOut(true);
139 * Create a new class instance
140 * @return the instance
142 private static CommonCallBack getCallBack() {
143 CommonCallBack callback = null;
145 callback = ZoomAction.class.getConstructor().newInstance();
146 } catch (IllegalArgumentException e) {
148 } catch (SecurityException e) {
150 } catch (InstantiationException e) {
152 } catch (IllegalAccessException e) {
154 } catch (InvocationTargetException e) {
156 } catch (NoSuchMethodException e) {
164 * @param e ActionEvent
166 public void actionPerformed(ActionEvent e) {
167 String cmd = e.getActionCommand();
169 Object src = e.getSource();
170 if (btnZoomIn != null && src.equals(btnZoomIn)) {
171 PaletteManagerView.get().getPanel().zoomIn();
172 } else if (btnZoomOut != null && src.equals(btnZoomOut)) {
173 PaletteManagerView.get().getPanel().zoomOut();
175 } else if (OS.get() == MAC) {
176 if (cmd.equals("/")) {
177 PaletteManagerView.get().getPanel().zoomIn();
178 } else if (cmd.equals("=")) {
179 PaletteManagerView.get().getPanel().zoomOut();
182 if (cmd.equals("+") || cmd.equals("=")) {
183 PaletteManagerView.get().getPanel().zoomIn();
184 } else if (cmd.equals("-") || cmd.equals("_")) {
185 PaletteManagerView.get().getPanel().zoomOut();
191 * setEnabled property of the button 'zoomIn'
192 * @param enabled enabled
194 public static void setEnabledZoomIn(boolean enabled) {
195 btnZoomIn.setEnabled(enabled);
199 * setEnabled property of the button 'zoomOut'
200 * @param enabled enabled
202 public static void setEnabledZoomOut(boolean enabled) {
203 btnZoomOut.setEnabled(enabled);
207 public void callBack() {