View Javadoc

1   /*
2    * @(#)$Id: OptionsDialog.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Michele Croci, mcroci@gmail.com
5    *
6    * Purpose      :   Options dialog.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Jan 10, 2008     crocimi     Initial Release
15   * Jan 20. 2008     bsigner     Adapted to new BasicDialog functionality
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.GridBagConstraints;
30  import java.awt.GridBagLayout;
31  import java.awt.GridLayout;
32  import java.awt.Insets;
33  import java.awt.Point;
34  import java.awt.event.ActionEvent;
35  import java.awt.event.ActionListener;
36  import java.awt.event.KeyEvent;
37  import java.io.File;
38  import java.net.URISyntaxException;
39  import java.util.Hashtable;
40  import java.util.List;
41  import java.util.Map;
42  
43  import javax.swing.ButtonGroup;
44  import javax.swing.JButton;
45  import javax.swing.JCheckBox;
46  import javax.swing.JPanel;
47  import javax.swing.JRadioButton;
48  import javax.swing.JTabbedPane;
49  
50  import org.sigtec.graphix.GuiTool;
51  import org.sigtec.graphix.widget.BasicDialog;
52  import org.ximtec.igesture.geco.Configuration;
53  import org.ximtec.igesture.geco.Geco;
54  import org.ximtec.igesture.geco.gui.MainView;
55  import org.ximtec.igesture.geco.util.Constant;
56  import org.ximtec.igesture.geco.util.GuiBundleTool;
57  import org.ximtec.igesture.geco.xml.XMLGeco;
58  
59  
60  /**
61   * Options dialog.
62   * 
63   * @version 0.9, Jan 2008
64   * @author Michele Croci, mcroci@gmail.com
65   */
66  public class OptionsDialog extends BasicDialog {
67  
68     private static String OPTIONS_DIALOG_KEY = "OptionsDialog";
69  
70     private MainView view;
71     private Configuration configuration;
72  
73     private int INPUTDEVICE = 0;
74     private int STARTUP = 0;
75  
76     private Map<String, JRadioButton> buttons = new Hashtable<String, JRadioButton>();
77   
78     private ButtonGroup group = new ButtonGroup();
79     private JCheckBox startupBox = new JCheckBox(Constant.MINIMIZE_STARTUP);
80     // GUI elements
81     private JTabbedPane tabbedPane = new JTabbedPane();
82  
83  
84     public OptionsDialog(MainView view) {
85        super(OPTIONS_DIALOG_KEY, GuiBundleTool.getBundle());
86        this.view = view;
87        setModal(true);
88        init();
89     }// NewProjectDialog
90  
91  
92     /**
93      * Initialises the dialog.
94      */
95     private void init() {
96        this.setLayout(new GridBagLayout());
97        this.setLayout(new GridBagLayout());
98        Point p = new Point(view.getLocation().x + 100, view.getLocation().y + 100);
99        this.setLocation(p);
100       addFirstTab();
101       addSecondTab();
102 
103       this.add(tabbedPane, new GridBagConstraints(0, 0, 1, 1, 1, 1,
104             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(30,
105                   30, 30, 15), 0, 0));
106 
107       JButton saveButton = GuiTool.createButton(Constant.SAVE, GuiBundleTool
108             .getBundle());
109       saveButton.addActionListener(new OkListener());
110       JButton cancelButton = GuiTool.createButton(Constant.CANCEL, GuiBundleTool
111             .getBundle());
112       cancelButton.addActionListener(new CancelListener());
113 
114       JPanel buttonPanel = new JPanel();
115       buttonPanel.setLayout(new GridLayout(1, 1, 20, 0));
116       buttonPanel.add(saveButton);
117       buttonPanel.add(cancelButton);
118       this.add(buttonPanel, new GridBagConstraints(0, 2, 1, 1, 0, 0,
119             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10,
120                   10, 10, 10), 0, 0));
121       pack();
122    }// init
123 
124 
125    /**
126     * Shows the dialog.
127     */
128    public void showDialog() {
129       reset();
130       setVisible(true);
131    }// showDialog
132 
133 
134    /**
135     * Resets the dialog.
136     */
137    public void reset() {
138       configuration = view.getModel().getGestureConfiguration();
139       JRadioButton button = buttons.get(configuration.getInputDeviceName());
140       button.setSelected(true);
141       startupBox.setSelected(configuration.getMinimize());
142    }// reset
143 
144 
145    /**
146     * Add the first tab to the dialog
147     * 
148     */
149    private void addFirstTab() {
150       JPanel aPanel = new JPanel();
151       aPanel.setLayout(new GridLayout(1, 1));
152 
153       // Creates the radio buttons.
154       JPanel radioButtonPanel = new JPanel();
155       radioButtonPanel.setLayout(new GridBagLayout());
156       List<String> devices = view.getModel().getGestureConfiguration()
157             .getInputDevices();
158       for (int i = 0; i < devices.size(); i++) {
159          JRadioButton button = new JRadioButton(devices.get(i));
160          button.setActionCommand(devices.get(i));
161          group.add(button);
162          radioButtonPanel.add(button, i);
163 
164          radioButtonPanel.add(button, new GridBagConstraints(0, i, 1, 1, 1, 0,
165                GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
166                      50, 0, 0, 50), 0, 0));
167          buttons.put(devices.get(i), button);
168 
169       }
170 
171       aPanel.add(radioButtonPanel);
172 
173       tabbedPane.addTab(Constant.INPUT_DEVICE, aPanel);
174       tabbedPane.setMnemonicAt(INPUTDEVICE, KeyEvent.VK_1);
175 
176    } // addFirstTab
177 
178 
179    /**
180     * Add the first tab to the dialog
181     * 
182     */
183    private void addSecondTab() {
184       JPanel aPanel = new JPanel();
185       aPanel.setLayout(new GridBagLayout());
186 
187       // Creates the radio buttons.
188       aPanel.add(startupBox, new GridBagConstraints(0, 0, 1, 1, 1, 1,
189             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(50,
190                   50, 50, 50), 0, 0));
191 
192       tabbedPane.addTab(Constant.STARTUP, aPanel);
193       tabbedPane.setMnemonicAt(STARTUP, KeyEvent.VK_2);
194 
195    } // addFirstTab
196 
197    /**
198     * Listener for save options event.
199     */
200    private class OkListener implements ActionListener {
201 
202       public void actionPerformed(ActionEvent e) {
203          String newSelDevice = group.getSelection().getActionCommand();
204          boolean needUpdate = (!newSelDevice.equals(configuration
205                .getInputDeviceName()) || configuration.getMinimize() != startupBox
206                .isSelected());
207 
208          if (needUpdate) {
209             List<String> devices = configuration.getInputDevices();
210             boolean arr[] = new boolean[devices.size()];
211             for (int i = 0; i < devices.size(); i++) {
212                if (newSelDevice.equals(devices.get(i)))
213                   arr[i] = true;
214                else
215                   arr[i] = false;
216             }
217             File confFile;
218 
219             try {
220                confFile = new File(ClassLoader.getSystemResource(
221                      Geco.GECO_CONFIGURATION).toURI());
222             }
223             catch (URISyntaxException ex) {
224                confFile = new File(ClassLoader.getSystemResource(
225                      Geco.GECO_CONFIGURATION).getPath());
226             }
227             XMLGeco.exportGestureConfiguration(confFile, devices, arr,
228                   startupBox.isSelected(), view.getModel().getProjectFile()
229                         .getPath());
230 
231             view.getModel().resetInputDevice();
232             view.getModel().setGestureConfiguration(
233                   new Configuration(Geco.GECO_CONFIGURATION));
234             view.getModel().configureInputDevice();
235          }
236 
237          // view.getModel().setGestureConfiguration();
238          OptionsDialog.this.dispose();
239       }
240    }
241 
242    /**
243     * Listener for cancel event.
244     */
245    private class CancelListener implements ActionListener {
246 
247       public void actionPerformed(ActionEvent e) {
248          OptionsDialog.this.dispose();
249       }// actionPerformed
250 
251    }
252 
253 }