View Javadoc

1   /*
2    * @(#)$Id: MappingDialog.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Michele Croci, mcroci@gmail.com  
5    *
6    * Purpose      :   Dialog for mapping a gesture to an action.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Nov 26, 2007     crocimi     Initial Release
15   * Jan 18, 2008     bsigner     Cleanup
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.geco.dialog;
28  
29  import java.awt.Color;
30  import java.awt.Dimension;
31  import java.awt.GridBagConstraints;
32  import java.awt.GridBagLayout;
33  import java.awt.GridLayout;
34  import java.awt.Insets;
35  import java.awt.Point;
36  import java.awt.event.ActionEvent;
37  import java.awt.event.ActionListener;
38  import java.awt.event.KeyEvent;
39  
40  import javax.swing.JButton;
41  import javax.swing.JFileChooser;
42  import javax.swing.JLabel;
43  import javax.swing.JPanel;
44  import javax.swing.JTabbedPane;
45  import javax.swing.JTextField;
46  import javax.swing.border.BevelBorder;
47  import javax.swing.border.TitledBorder;
48  
49  import org.sigtec.graphix.GuiTool;
50  import org.sigtec.graphix.widget.BasicDialog;
51  import org.ximtec.igesture.core.GestureClass;
52  import org.ximtec.igesture.geco.action.CommandExecutor;
53  import org.ximtec.igesture.geco.action.KeyboardSimulation;
54  import org.ximtec.igesture.geco.gui.CommandView;
55  import org.ximtec.igesture.geco.gui.HotKeyView;
56  import org.ximtec.igesture.geco.gui.MainView;
57  import org.ximtec.igesture.geco.mapping.GestureToActionMapping;
58  import org.ximtec.igesture.geco.util.Constant;
59  import org.ximtec.igesture.geco.util.GuiBundleTool;
60  
61  
62  /**
63   * Dialog for mapping a gesture to an action
64   * 
65   * @version 0.9, Nov 26, 2007
66   * @author Michele Croci, mcroci@gmail.com
67   * @author Beat Signer, signer@inf.ethz.ch
68   */
69  public class MappingDialog extends BasicDialog {
70  
71     private GestureClass gestureClass;
72     private GestureToActionMapping gestureMapping;
73     private MainView view;
74  
75     // GUI elements
76     private JTabbedPane tabbedPane = new JTabbedPane();
77     private JLabel gestureLabel = new JLabel();
78     private JButton addButton;
79     private JTextField textField = GuiTool.createTextField(Constant.EMPTY_STRING,
80           GuiBundleTool.getBundle());
81     private int DIALOG_WIDTH = 450;
82     private int DIALOG_HEIGHT = 500;
83  
84     // views
85     private HotKeyView hotkeyView;
86     private CommandView commandView;
87  
88     // private JPanel labelPanel;
89  
90     // constants
91     private final int HOTKEY = 0;
92     private final int COMMAND = 1;
93     private final int OPEN_FILE = 2;
94  
95  
96     // models
97     // private HotKeyModel hotkeyModel = new HotKeyModel();
98  
99     public MappingDialog(MainView gmv) {
100       view = gmv;
101       setFocusable(false);
102       setModal(true);
103       initDialog();
104       tabbedPane.setFocusable(false);
105 
106       // this.addFocusListener(this);
107    }// MappingDialog
108 
109 
110    /**
111     * Shows the dialog.
112     * 
113     * @param gc the gesture to be mapped.
114     */
115    public void showDialog(GestureClass gc) {
116       gestureMapping = view.getModel().getMappings().get(gc);
117       gestureClass = gc;
118       initValues();
119       this.setVisible(true);
120    }// showDialog
121 
122 
123    /**
124     * Hides the dialog.
125     */
126    public void hideDialog() {
127       this.setVisible(false);
128    }// hideDialog
129 
130 
131    /**
132     * Initialises the state of the buttons.
133     */
134    private void initValues() {
135       gestureLabel.setText(gestureClass.getName());
136       if (gestureMapping == null) {
137 
138          commandView.init();
139          hotkeyView.initView();
140          tabbedPane.setSelectedIndex(COMMAND);
141          tabbedPane.setSelectedIndex(HOTKEY);
142       }
143       else {
144          if (gestureMapping.getAction() instanceof KeyboardSimulation) {
145             KeyboardSimulation ks = (KeyboardSimulation)gestureMapping
146                   .getAction();
147             hotkeyView.updateView(ks.toString());
148 
149          }
150          else if (gestureMapping.getAction() instanceof CommandExecutor) {
151             CommandExecutor cmd = (CommandExecutor)gestureMapping.getAction();
152             commandView.updateView(cmd.getCommand());
153             tabbedPane.setSelectedIndex(COMMAND);
154          }
155       }
156    }// initButtonsState
157 
158 
159    /**
160     * Init the Dialog
161     */
162    private void initDialog() {
163       this.setResizable(false);
164       this.setTitle(GuiBundleTool.getBundle().getName(
165             Constant.GESTURE_MAPPING_TITLE));
166       this.setLayout(new GridBagLayout());
167       this.add(tabbedPane, new GridBagConstraints(0, 1, 1, 1, 1, 1,
168             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(20,
169                   40, 20, 40), 0, 0));
170       Point p = new Point(view.getLocation().x + 200, view.getLocation().y + 100);
171       this.setLocation(p);
172       this.setSize(new Dimension(DIALOG_WIDTH, DIALOG_HEIGHT));
173 
174       showGesture();
175       addButtonPanel();
176       addFirstTab();
177       addSecondTab();
178       addThirdTab();
179       // tabbedPane.addChangeListener(hotkeyView.getChangeListener());
180    } // initDialog
181 
182 
183    private void showGesture() {
184       JPanel topPanel = new JPanel();
185       topPanel.setBorder(new TitledBorder(new BevelBorder(0, Color.gray,
186             Color.gray), Constant.GESTURE));
187       topPanel.add(gestureLabel);
188       this.add(topPanel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
189             GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
190             new Insets(10, 40, 10, 40), 0, 0));
191 
192    }
193 
194 
195    /**
196     * Add the buttons
197     * 
198     */
199    private void addButtonPanel() {
200       JPanel buttonPanel = new JPanel();
201       addButton = GuiTool.createButton(Constant.ADD, GuiBundleTool.getBundle());
202       JButton cancelButton = GuiTool.createButton(Constant.CANCEL, GuiBundleTool
203             .getBundle());
204       addButton.addActionListener(new ActionListener() {
205 
206          public void actionPerformed(ActionEvent event) {
207             // update model
208             addGestureMappingToTable();
209             view.getModel().addMapping(
210                   MappingDialog.this.view.getModel().getMappings().get(
211                         gestureClass));
212             // update view
213             view.updateLists();
214             view.enableSaveButton();
215 
216             MappingDialog.this.dispose();
217          }
218       });
219       cancelButton.addActionListener(new ActionListener() {
220 
221          public void actionPerformed(ActionEvent event) {
222             MappingDialog.this.dispose();
223          }
224       });
225       buttonPanel.add(addButton);
226       buttonPanel.add(cancelButton);
227       this.add(buttonPanel, new GridBagConstraints(0, 2, 1, 1, 0, 0,
228             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,
229                   20, 0, 20), 0, 0));
230    }
231 
232 
233    /**
234     * Add the first tab to the dialog
235     * 
236     */
237    private void addFirstTab() {
238       hotkeyView = new HotKeyView(this);
239 
240       JPanel aPanel = new JPanel();
241       aPanel.setBorder(new TitledBorder(new BevelBorder(0, Color.gray,
242             Color.gray), Constant.HOTKEY));
243 
244       aPanel.setLayout(new GridLayout(1, 1));
245       aPanel.add(hotkeyView);
246 
247       tabbedPane.addTab(Constant.HOTKEY, aPanel);
248       tabbedPane.setMnemonicAt(HOTKEY, KeyEvent.VK_1);
249 
250    } // addFirstTab
251 
252 
253    /**
254     * Add the second tab to the dialog
255     * 
256     */
257    private void addSecondTab() {
258 
259       commandView = new CommandView();
260       tabbedPane.addTab(Constant.COMMAND, commandView);
261       tabbedPane.setMnemonicAt(COMMAND, KeyEvent.VK_2);
262 
263    }
264 
265 
266    /**
267     * Add third tab to the dialog
268     * 
269     */
270    private void addThirdTab() {
271       JPanel aPanel = new JPanel();
272       aPanel.setLayout(new GridBagLayout());
273       tabbedPane.addTab(Constant.OPEN_FILE, aPanel);
274       tabbedPane.setMnemonicAt(OPEN_FILE, KeyEvent.VK_3);
275       JButton browse = GuiTool.createButton(Constant.BROWSE, GuiBundleTool
276             .getBundle());
277 
278       browse.addActionListener(
279 
280       new ActionListener() {
281 
282          public void actionPerformed(ActionEvent e) {
283             JFileChooser fileChooser = new JFileChooser();
284 
285             int status = fileChooser.showOpenDialog(null);
286 
287             if (status == JFileChooser.APPROVE_OPTION) {
288                textField.setText(fileChooser.getSelectedFile().getPath());
289             }
290          }
291       });
292 
293       aPanel.add(textField, new GridBagConstraints(0, 0, 1, 1, 1, 1,
294             GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
295             new Insets(50, 20, 50, 20), 0, 0));
296       aPanel.add(browse, new GridBagConstraints(0, 1, 1, 1, 1, 1,
297             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(50,
298                   50, 50, 50), 0, 0));
299 
300    }
301 
302 
303    /**
304     * Add a Gesture-action map to the table of mappings
305     * 
306     */
307    private void addGestureMappingToTable() {
308       if (tabbedPane.getSelectedIndex() == HOTKEY) {
309 
310          GestureToActionMapping mapping = new GestureToActionMapping(
311                gestureClass, new KeyboardSimulation(hotkeyView.getAllKeys()));
312          view.getModel().getMappings().put(gestureClass, mapping);
313       }
314       else if (tabbedPane.getSelectedIndex() == COMMAND) {
315          GestureToActionMapping mapping = new GestureToActionMapping(
316                gestureClass, new CommandExecutor(commandView.getCommand()));
317          view.getModel().getMappings().put(gestureClass, mapping);
318       }
319       else if (tabbedPane.getSelectedIndex() == OPEN_FILE) {
320          GestureToActionMapping mapping = new GestureToActionMapping(
321                gestureClass, new CommandExecutor(textField.getText()));
322          view.getModel().getMappings().put(gestureClass, mapping);
323       }
324    }// addGestureMappingToTable
325 
326 
327    public void setAddButtonEnabled(boolean b) {
328       addButton.setEnabled(b);
329    }
330 }