View Javadoc

1   /*
2    * @(#)$Id: NewProjectDialog.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Michele Croci, mcroci@gmail.com
5    *
6    * Purpose		:   New project dialog.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Dec 03, 2007		crocimi		Initial Release
15   * Jan 20, 2008     bsigner     Redesign 
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.GridBagConstraints;
31  import java.awt.GridBagLayout;
32  import java.awt.GridLayout;
33  import java.awt.Insets;
34  import java.awt.Point;
35  import java.awt.event.ActionEvent;
36  import java.awt.event.ActionListener;
37  import java.awt.event.KeyEvent;
38  import java.awt.event.KeyListener;
39  import java.io.File;
40  import java.net.URISyntaxException;
41  
42  import javax.swing.JButton;
43  import javax.swing.JFileChooser;
44  import javax.swing.JOptionPane;
45  import javax.swing.JPanel;
46  import javax.swing.border.BevelBorder;
47  import javax.swing.border.TitledBorder;
48  import javax.swing.event.DocumentEvent;
49  import javax.swing.event.DocumentListener;
50  
51  import org.sigtec.graphix.GuiTool;
52  import org.sigtec.graphix.widget.BasicDialog;
53  import org.sigtec.graphix.widget.BasicLabel;
54  import org.sigtec.graphix.widget.BasicTextField;
55  import org.sigtec.util.MIME;
56  import org.sigtec.util.ResourceTool;
57  import org.ximtec.igesture.core.GestureSet;
58  import org.ximtec.igesture.geco.gui.MainModel;
59  import org.ximtec.igesture.geco.gui.MainView;
60  import org.ximtec.igesture.geco.gui.action.SaveProjectAction;
61  import org.ximtec.igesture.geco.util.Constant;
62  import org.ximtec.igesture.geco.util.GuiBundleTool;
63  import org.ximtec.igesture.util.XMLTool;
64  
65  
66  /**
67   * New project dialog.
68   * 
69   * @version 0.9, Dec 2007
70   * @author Michele Croci, mcroci@gmail.com
71   * @author Beat Signer, signer@inf.ethz.ch
72   */
73  public class NewProjectDialog extends BasicDialog {
74  
75     /**
76      * The key used to retrieve dialog details from the resource bundle.
77      */
78     public static final String KEY = "NewProjectDialog";
79  
80     private MainView view;
81  
82     private BasicTextField fileTextField;
83     private BasicTextField projectTextField;
84     private JButton createButton;
85     private String filePath = Constant.EMPTY_STRING;
86  
87  
88     public NewProjectDialog(MainView view) {
89        super(KEY, GuiBundleTool.getBundle());
90        this.view = view;
91        setModal(true);
92        init();
93     }// NewProjectDialog
94  
95  
96     /**
97      * Initialises the dialog.
98      */
99     private void init() {
100       try {
101          filePath = new File(ClassLoader.getSystemResource(
102                ResourceTool.getProperty(Constant.SETTINGS,
103                      Constant.SETTINGS_ATTRIBUTE_MAPPINGS, GuiBundleTool
104                            .getBundle())).toURI()).getPath()
105                + Constant.BACKSLASH;
106          
107       }
108       catch (URISyntaxException e) {
109          filePath = new File(ClassLoader.getSystemResource(
110                ResourceTool.getProperty(Constant.SETTINGS,
111                      Constant.SETTINGS_ATTRIBUTE_MAPPINGS, GuiBundleTool
112                            .getBundle())).getPath()).getPath()
113                + Constant.BACKSLASH;
114          
115       }
116 
117       JPanel mainPanel = new JPanel();
118       mainPanel.setBorder(new TitledBorder(new BevelBorder(0, Color.gray,
119             Color.gray), GuiBundleTool.getBundle().getName(
120             Constant.PROJECT_PROPERTIES_STRING)));
121       mainPanel.setLayout(new GridBagLayout());
122       setLayout(new GridBagLayout());
123       Point p = new Point(view.getLocation().x + 50, view.getLocation().y + 100);
124       setLocation(p);
125       mainPanel.add(new BasicLabel(Constant.PROJECT_NAME, GuiBundleTool
126             .getBundle()), new GridBagConstraints(0, 0, 1, 1, 0, 0,
127             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10,
128                   10, 10, 10), 0, 0));
129 
130       projectTextField = new BasicTextField(
131             Constant.PROJECT_PROPERTIES_TEXT_FIELD, GuiBundleTool.getBundle());
132       projectTextField.getDocument().addDocumentListener(
133             new MyDocumentListener());
134       projectTextField.addKeyListener(new MyKeyListener());
135 
136       mainPanel.add(projectTextField, new GridBagConstraints(1, 0, 1, 1, 1, 1,
137             GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
138             new Insets(10, 10, 10, 10), 0, 0));
139 
140       mainPanel.add(new BasicLabel(Constant.PROJECT_LOCATION, GuiBundleTool
141             .getBundle()), new GridBagConstraints(0, 1, 1, 1, 0, 0,
142             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10,
143                   10, 10, 10), 0, 0));
144 
145       fileTextField = GuiTool.createTextField(Constant.PROJECT_FILE_TEXT_FIELD,
146             GuiBundleTool.getBundle());
147       fileTextField.setEditable(false);
148       fileTextField.setText(filePath);
149       mainPanel.add(fileTextField, new GridBagConstraints(1, 1, 1, 1, 1, 1,
150             GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
151             new Insets(10, 10, 10, 10), 0, 0));
152 
153       JButton browseButton = GuiTool.createButton(Constant.BROWSE, GuiBundleTool
154             .getBundle());
155       browseButton.addActionListener(new BrowseListener());
156       mainPanel.add(browseButton, new GridBagConstraints(2, 1, 1, 1, 0, 0,
157             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10,
158                   10, 10, 10), 0, 0));
159 
160       createButton = GuiTool.createButton(Constant.CREATE_PROJECT_BUTTON,
161             GuiBundleTool.getBundle());
162       createButton.setEnabled(false);
163       createButton.addActionListener(new CreateListener());
164       JButton cancelButton = GuiTool.createButton(Constant.CANCEL, GuiBundleTool
165             .getBundle());
166       cancelButton.addActionListener(new CancelListener());
167 
168       JPanel buttonPanel = new JPanel();
169       buttonPanel.setLayout(new GridLayout(1, 1, 20, 0));
170       buttonPanel.add(createButton);
171       buttonPanel.add(cancelButton);
172       mainPanel.add(buttonPanel, new GridBagConstraints(0, 3, 3, 1, 1, 1,
173             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10,
174                   10, 10, 10), 0, 0));
175 
176       this.add(mainPanel, new GridBagConstraints(0, 3, 3, 1, 1, 1,
177             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(20,
178                   20, 20, 20), 0, 0));
179       this.pack();
180    }// init
181 
182 
183    /**
184     * Shows the dialog.
185     */
186    public void showDialog() {
187       reset();
188       setVisible(true);
189    }// showDialog
190 
191 
192    /**
193     * Resets the dialog.
194     */
195    public void reset() {
196       fileTextField.setText(filePath);
197       projectTextField.setText(Constant.EMPTY_STRING);
198    }// reset
199 
200    /**
201     * Listener for browse event.
202     */
203    private class BrowseListener implements ActionListener {
204 
205       public void actionPerformed(ActionEvent e) {
206          // display file chooser dialog
207 
208          JFileChooser fileChooser = new JFileChooser();
209          fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
210          // fileChooser.setCurrentDirectory(new File(filePath));
211          int status = fileChooser.showDialog(null, "Open");
212          if (status == JFileChooser.APPROVE_OPTION) {
213             File selectedFile = fileChooser.getSelectedFile();
214             if (selectedFile != null) {
215 
216                String fileName = Constant.EMPTY_STRING;
217                if (selectedFile.getAbsolutePath().charAt(
218                      selectedFile.getAbsolutePath().length() - 1) != '\\') {
219                   fileName = selectedFile.getAbsolutePath() + Constant.BACKSLASH
220                         + NewProjectDialog.this.projectTextField.getText();
221                   if (!NewProjectDialog.this.projectTextField.getText().equals(
222                         Constant.EMPTY_STRING)) {
223                      fileName += Constant.DOT + MIME.getExtension(MIME.XML);
224                   }
225                   filePath = selectedFile.getAbsolutePath() + Constant.BACKSLASH;
226                }
227                else {
228                   fileName = selectedFile.getAbsolutePath()
229                         + NewProjectDialog.this.projectTextField.getText();
230                   if (!NewProjectDialog.this.projectTextField.getText().equals(
231                         Constant.EMPTY_STRING)) {
232                      fileName += org.sigtec.util.Constant.DOT
233                            + MIME.getExtension(MIME.XML);
234                   }
235                   filePath = selectedFile.getAbsolutePath();
236                }
237                fileTextField.setText(fileName);
238             }
239          }
240          else if (status == JFileChooser.CANCEL_OPTION) {
241 
242          }
243 
244       }
245    }
246 
247    /**
248     * Listener for create project event.
249     */
250    private class CreateListener implements ActionListener {
251 
252       public void actionPerformed(ActionEvent e) {
253 
254          createProject();
255       }// actionPerformed
256 
257    }
258 
259    /**
260     * Listener for cancel event.
261     */
262    private class CancelListener implements ActionListener {
263 
264       public void actionPerformed(ActionEvent e) {
265          NewProjectDialog.this.dispose();
266       }// actionPerformed
267 
268    }
269 
270    /**
271     * Listener for project name edit event.
272     */
273    private class MyDocumentListener implements DocumentListener {
274 
275       public void insertUpdate(DocumentEvent e) {
276          update();
277       }
278 
279 
280       public void removeUpdate(DocumentEvent e) {
281          update();
282       }
283 
284 
285       public void changedUpdate(DocumentEvent e) {
286          // Plain text components do not fire these events
287       }
288 
289 
290       public void update() {
291          if ((!projectTextField.getText().equals(Constant.EMPTY_STRING))) {
292             NewProjectDialog.this.createButton.setEnabled(true);
293             NewProjectDialog.this.fileTextField
294                   .setText(NewProjectDialog.this.filePath
295                         + NewProjectDialog.this.projectTextField.getText()
296                         + org.sigtec.util.Constant.DOT
297                         + MIME.getExtension(MIME.XML));
298          }
299          else {
300             NewProjectDialog.this.createButton.setEnabled(false);
301             NewProjectDialog.this.fileTextField
302                   .setText(NewProjectDialog.this.filePath
303                         + NewProjectDialog.this.projectTextField.getText());
304          }
305       }// update
306    }
307 
308    /**
309     * Listener for key event.
310     */
311 
312    private class MyKeyListener implements KeyListener {
313 
314       public void keyPressed(KeyEvent e) {
315          if (e.getKeyCode() == KeyEvent.VK_ENTER) {
316             createProject();
317          }
318       }
319 
320 
321       public void keyReleased(KeyEvent e) {
322       }
323 
324 
325       public void keyTyped(KeyEvent e) {
326       }
327    }
328 
329 
330    public void createProject() {
331       if ((!fileTextField.getText().equals(Constant.EMPTY_STRING))
332             && (!projectTextField.getText().equals(Constant.EMPTY_STRING))) {
333          String fileName = fileTextField.getText();
334 
335          boolean ok = true;
336          File temp = new File(fileName);
337          if (temp.exists()) {
338             int n = JOptionPane.showConfirmDialog(NewProjectDialog.this,
339                   Constant.OVERWRITE_FILE, Constant.EMPTY_STRING,
340                   JOptionPane.YES_NO_OPTION);
341             if (n != 0) {
342                ok = false;
343             }
344          }
345 
346          if (ok) {
347             // close dialog
348             NewProjectDialog.this.dispose();
349 
350             GestureSet gestureSet = XMLTool.importGestureSet(ClassLoader
351                   .getSystemResourceAsStream(MainModel.GESTURE_SET));
352 
353             view.getModel().clearData();
354             view.getModel().initRecogniser(gestureSet);
355             view.getModel().loadGestureSet(gestureSet);
356 
357             view.getModel().setProjectFile(temp);
358             view.getModel().setGestureSetFileName(MainModel.GESTURE_SET);
359             view.getModel().setProjectName(projectTextField.getText());
360 
361             // update view
362             view.initProjectView(projectTextField.getText());
363             view.enableMenuItem();
364             view.updateGestureList();
365 
366             // save
367             (new SaveProjectAction(view)).save();
368          }
369       }
370 
371    }
372 }