View Javadoc

1   /*
2    * @(#)$Id: HotKeyView.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Michele Croci, mcroci@gmail.com
5    *
6    * Purpose		:   View for the choosed hotkey
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Dec 11, 2007		crocimi		Initial Release
15   *
16   * -----------------------------------------------------------------------
17   *
18   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
19   *
20   * This software is the proprietary information of ETH Zurich.
21   * Use is subject to license terms.
22   * 
23   */
24  
25  
26  package org.ximtec.igesture.geco.gui;
27  
28  import java.awt.Color;
29  import java.awt.GridBagConstraints;
30  import java.awt.GridBagLayout;
31  import java.awt.Insets;
32  import java.awt.event.ActionEvent;
33  import java.awt.event.ActionListener;
34  import java.awt.event.KeyEvent;
35  import java.awt.event.KeyListener;
36  
37  import javax.swing.JCheckBox;
38  import javax.swing.JComboBox;
39  import javax.swing.JLabel;
40  import javax.swing.JPanel;
41  import javax.swing.JTextField;
42  
43  import org.sigtec.graphix.GuiTool;
44  import org.ximtec.igesture.geco.dialog.MappingDialog;
45  import org.ximtec.igesture.geco.util.Constant;
46  import org.ximtec.igesture.geco.util.GuiBundleTool;
47  
48  
49  /**
50   * View for the choosed hotkey
51   * @version 0.9, Dec 11, 2007
52   * @author Michele Croci, mcroci@gmail.com
53   */
54  public class HotKeyView extends JPanel {
55  
56     private String keys = Constant.EMPTY_STRING;
57     private MappingDialog view;
58  
59     private final String ALT = "ALT";
60     private final String CTRL = "CTRL";
61     private final String SHIFT = "SHIFT";
62  
63     // GUI elements
64     private JCheckBox ctrlCheckBox = new JCheckBox(CTRL);
65     private JCheckBox shiftCheckBox = new JCheckBox(SHIFT);
66     private JCheckBox altCheckBox = new JCheckBox(ALT);
67     private JComboBox comboBox;
68     private JTextField buttonLabel;
69  
70     private boolean update = true;
71  
72  
73     public HotKeyView(MappingDialog view) {
74        this.view = view;
75        populateView();
76  
77        // elements must be non focusable otherwise text field
78        // doesn't get key events!
79        ctrlCheckBox.setFocusable(false);
80        shiftCheckBox.setFocusable(false);
81        altCheckBox.setFocusable(false);
82        comboBox.setFocusable(false);
83  
84        buttonLabel.addKeyListener(new ButtonLabelKeyListener());
85        initView();
86     }
87  
88  
89     /**
90      * Returns the choosed keys.
91      * 
92      * @return the key selected from the user
93      */
94     public String getAllKeys() {
95        return keys;
96     }
97  
98  
99     /**
100     * Update the view
101     * 
102     * @param keys the current choosed keys
103     */
104    public void updateView(String keys) {
105       this.keys = keys;
106       updateBoxElements();
107       updateTextFieldElement();
108    }
109 
110 
111    /**
112     * Reset the view. No key has been choosed.
113     */
114    public void initView() {
115       keys = Constant.EMPTY_STRING;
116       ctrlCheckBox.setSelected(false);
117       altCheckBox.setSelected(false);
118       shiftCheckBox.setSelected(false);
119       comboBox.setSelectedIndex(0);
120       buttonLabel.setText(Constant.EMPTY_STRING);
121    }
122 
123 
124    /**
125     * Update the checkboxes and the combobox,
126     */
127    private void updateBoxElements() {
128       if (keys.contains(CTRL)) {
129          ctrlCheckBox.setSelected(true);
130       }
131       else {
132          ctrlCheckBox.setSelected(false);
133       }
134 
135       if (keys.contains(SHIFT)) {
136          shiftCheckBox.setSelected(true);
137       }
138       else {
139          shiftCheckBox.setSelected(false);
140       }
141 
142       if (keys.contains(ALT)) {
143          altCheckBox.setSelected(true);
144       }
145       else {
146          altCheckBox.setSelected(false);
147       }
148       // get key
149       String s = keys.split("\\+")[keys.split("\\+").length - 1];
150       if (s.equals(CTRL) || s.equals(SHIFT) || s.equals(ALT)) {
151          comboBox.setSelectedIndex(0);
152          update = false;
153       }
154       else {
155          if (!s.equals(Constant.EMPTY_STRING)) {
156             comboBox.setSelectedItem(s);
157             if (!comboBox.getSelectedItem().equals(s)) {
158                comboBox.setSelectedItem("Invalid Key");
159                view.setAddButtonEnabled(false);
160             }
161             else {
162                view.setAddButtonEnabled(true);
163             }
164          }
165          else {
166             comboBox.setSelectedIndex(0);
167          }
168       }
169    }
170 
171 
172    /**
173     * Update the text field displaying the keys
174     */
175    private void updateTextFieldElement() {
176       String text = Constant.EMPTY_STRING;
177       if (ctrlCheckBox.isSelected()) {
178          text += "CTRL";
179       }
180       if (shiftCheckBox.isSelected()) {
181          if (ctrlCheckBox.isSelected()) {
182             text += "+";
183          }
184          text += "SHIFT";
185       }
186       if (altCheckBox.isSelected()) {
187          if (shiftCheckBox.isSelected() || ctrlCheckBox.isSelected()) {
188             text += "+";
189          }
190          text += "ALT";
191       }
192       if (ctrlCheckBox.isSelected() || shiftCheckBox.isSelected()
193             || altCheckBox.isSelected()) {
194          if (!comboBox.getSelectedItem().equals(Constant.EMPTY_STRING)) {
195             text += "+";
196          }
197       }
198       text += comboBox.getSelectedItem();
199       keys = text;
200       buttonLabel.setText(text);
201    }
202 
203    private class ButtonLabelKeyListener implements KeyListener {
204 
205       public void keyPressed(KeyEvent e) {
206 
207       }
208       String text = Constant.EMPTY_STRING;
209       boolean notLastKey = false;
210 
211 
212       public void keyReleased(KeyEvent e) {
213 
214          if (!text.equals(Constant.EMPTY_STRING))
215             text = "+" + text;
216 
217          text = KeyEvent.getKeyText(e.getKeyCode()).toUpperCase() + text;
218 
219          if (!e.isAltDown() && !e.isShiftDown() && !e.isControlDown())
220             notLastKey = false;
221          else
222             notLastKey = true;
223 
224          if (!notLastKey) {
225             buttonLabel.setText(text);
226             keys = text;
227             updateBoxElements();
228             text = Constant.EMPTY_STRING;
229          }
230 
231       }
232 
233 
234       public void keyTyped(KeyEvent e) {
235       }
236    }
237 
238 
239    private void populateView() {
240 
241       comboBox = new JComboBox(new String[] { "", "A", "B", "C", "D", "E", "F",
242             "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
243             "T", "U", "V", "W", "X", "Y", "Z", " ", "F1", "F2", "F3", "F4",
244             "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "0", "1", "2",
245             "3", "4", "5", "6", "7", "8", "9", "BACKSPACE", "DELETE", "SPACE",
246             "ENTER", "ESCAPE", "UP", "DOWN", "LEFT", "RIGHT", "TAB", "PAGE UP",
247             "PAGE DOWN", "END", "HOME", "Invalid Key" });
248 
249       JPanel bottomPanel = new JPanel();
250       this.setLayout(new GridBagLayout());
251       // gestureLabel.setText(gestureClass.getName());
252       bottomPanel.setLayout(new GridBagLayout());
253       JLabel plus = new JLabel("+");
254 
255       ctrlCheckBox.addActionListener(new ActionListener() {
256 
257          public void actionPerformed(ActionEvent e) {
258             updateTextFieldElement();
259          }
260       });
261       altCheckBox.addActionListener(new ActionListener() {
262 
263          public void actionPerformed(ActionEvent e) {
264             updateTextFieldElement();
265          }
266       });
267       shiftCheckBox.addActionListener(new ActionListener() {
268 
269          public void actionPerformed(ActionEvent e) {
270             updateTextFieldElement();
271          }
272       });
273       comboBox.addActionListener(new ActionListener() {
274 
275          public void actionPerformed(ActionEvent e) {
276             String s = Constant.EMPTY_STRING;
277             if (!keys.equals(Constant.EMPTY_STRING))
278                s = keys.split("\\+")[keys.split("\\+").length - 1];
279             if (!comboBox.getSelectedItem().equals(s)) {
280                if (update) {
281                   updateTextFieldElement();
282                }
283                else {
284                   update = true;
285                }
286             }
287 
288          }
289       });
290 
291       bottomPanel.add(ctrlCheckBox, new GridBagConstraints(1, 1, 1, 1, 1, 1,
292             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,
293                   20, 0, 0), 0, 0));
294       bottomPanel.add(plus, new GridBagConstraints(2, 1, 1, 1, 1, 1,
295             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
296                   0, 0), 0, 0));
297       bottomPanel.add(shiftCheckBox, new GridBagConstraints(3, 1, 1, 1, 1, 1,
298             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
299                   0, 0), 0, 0));
300       plus = new JLabel("+");
301       bottomPanel.add(plus, new GridBagConstraints(4, 1, 1, 1, 1, 1,
302             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
303                   0, 0), 0, 0));
304       bottomPanel.add(altCheckBox, new GridBagConstraints(5, 1, 1, 1, 1, 1,
305             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
306                   0, 0), 0, 0));
307       plus = new JLabel("+");
308       bottomPanel.add(plus, new GridBagConstraints(6, 1, 1, 1, 1, 1,
309             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
310                   0, 0), 0, 0));
311       bottomPanel.add(comboBox, new GridBagConstraints(7, 1, 1, 1, 1, 1,
312             GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
313             new Insets(0, 0, 0, 20), 0, 0));
314 
315       buttonLabel = GuiTool.createTextField("buttonTextField", GuiBundleTool
316             .getBundle());
317       buttonLabel.setEditable(false);
318       buttonLabel.setBackground(Color.WHITE);
319 
320       this.setLayout(new GridBagLayout());
321       this.add(bottomPanel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
322             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10,
323                   10, 10, 10), 0, 0));
324 
325       this.add(buttonLabel, new GridBagConstraints(0, 1, 1, 1, 1, 1,
326             GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
327             new Insets(10, 20, 10, 20), 0, 0));
328    }
329 
330 }