ZoomAction must work when a block is selected on the palette browser
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / palette / actions / ZoomAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2015 - Marcos CARDINOT
4  *
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
10  *
11  */
12
13 package org.scilab.modules.xcos.palette.actions;
14
15 import static org.scilab.modules.commons.OS.MAC;
16
17 import java.awt.Toolkit;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.KeyEvent;
20 import java.lang.reflect.InvocationTargetException;
21
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;
27
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;
34
35 /**
36  * Zoom Management
37  * @author Marcos CARDINOT <mcardinot@gmail.com>
38  */
39 public class ZoomAction extends CommonCallBack {
40
41     private static final long serialVersionUID = 1L;
42     
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");
47
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";
51
52     private static JButton btnZoomIn;
53     private static JButton btnZoomOut;
54
55     /**
56      * Constructor
57      */
58     public ZoomAction() {
59         super("");
60     }
61
62     /**
63      * Register the key for the action
64      * @param am the ActionMap
65      * @param im the InputMap
66      */
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());
71
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)
80             };
81             keystrokesOut = new KeyStroke[] {
82                     KeyStroke.getKeyStroke('=', ACCELERATOR_KEY),
83                     KeyStroke.getKeyStroke('=', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK)
84             };
85         } else {
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)
92             };
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)
99             };
100         }
101
102         for (KeyStroke k : keystrokesIn) {
103             im.put(k, ZOOM_IN);
104         }
105         for (KeyStroke k : keystrokesOut) {
106             im.put(k, ZOOM_OUT);
107         }
108     }
109
110     /**
111      * Create the button 'zoom in'
112      * @return the button
113      */
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);
121         return btnZoomIn;
122     }
123
124     /**
125      * Create the button 'zoom out'
126      * @return the button
127      */
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);
135         return btnZoomOut;
136     }
137
138     /**
139      * Create a new class instance
140      * @return the instance
141      */
142     private static CommonCallBack getCallBack() {
143         CommonCallBack callback = null;
144         try {
145             callback = ZoomAction.class.getConstructor().newInstance();
146         } catch (IllegalArgumentException e) {
147             e.printStackTrace();
148         } catch (SecurityException e) {
149             e.printStackTrace();
150         } catch (InstantiationException e) {
151             e.printStackTrace();
152         } catch (IllegalAccessException e) {
153             e.printStackTrace();
154         } catch (InvocationTargetException e) {
155             e.printStackTrace();
156         } catch (NoSuchMethodException e) {
157             e.printStackTrace();
158         }
159         return callback;
160     }
161
162     /**
163      * Action
164      * @param e ActionEvent
165      */
166     public void actionPerformed(ActionEvent e) {
167         String cmd = e.getActionCommand();
168         if (cmd.isEmpty()) {
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();
174             }
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();
180             }
181         } else {
182             if (cmd.equals("+") || cmd.equals("=")) {
183                 PaletteManagerView.get().getPanel().zoomIn();
184             } else if (cmd.equals("-") || cmd.equals("_")) {
185                 PaletteManagerView.get().getPanel().zoomOut();
186             }
187         }
188     }
189
190     /**
191      * setEnabled property of the button 'zoomIn'
192      * @param enabled enabled 
193      */
194     public static void setEnabledZoomIn(boolean enabled) {
195         btnZoomIn.setEnabled(enabled);
196     }
197
198     /**
199      * setEnabled property of the button 'zoomOut'
200      * @param enabled enabled 
201      */
202     public static void setEnabledZoomOut(boolean enabled) {
203         btnZoomOut.setEnabled(enabled);
204     }
205
206     @Override
207     public void callBack() {
208     }
209 }