View Javadoc

1   /*
2    * @(#)$Id: LoadGestureSetAction.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Michele Croci, mcroci@gmail.com
5    *
6    * Purpose		:   Loads a gesture set
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Nov 15, 2007     crocimi     Initial Release
15   * Jan 15, 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.gui.action;
28  
29  import java.awt.event.ActionEvent;
30  import java.io.File;
31  
32  import javax.swing.JFileChooser;
33  import javax.swing.JOptionPane;
34  
35  import org.sigtec.graphix.widget.BasicAction;
36  import org.sigtec.util.MIME;
37  import org.ximtec.igesture.core.GestureSet;
38  import org.ximtec.igesture.geco.gui.MainView;
39  import org.ximtec.igesture.geco.util.Constant;
40  import org.ximtec.igesture.geco.util.ExtensionFileFilter;
41  import org.ximtec.igesture.geco.util.GuiBundleTool;
42  import org.ximtec.igesture.util.XMLTool;
43  
44  
45  /**
46   * Loads a gesture set
47   * 
48   * @version 0.9, Nov 20, 2007
49   * @author Michele Croci, mcroci@gmail.com
50   * @author Beat Signer, signer@inf.ethz.ch
51   */
52  public class LoadGestureSetAction extends BasicAction {
53  
54     /**
55      * The key used to retrieve action details from the resource bundle.
56      */
57     public static final String KEY = "LoadGestureSetAction";
58  
59     private MainView mainView;
60  
61  
62     public LoadGestureSetAction(MainView mainView) {
63        super(KEY, GuiBundleTool.getBundle());
64        this.mainView = mainView;
65     }
66  
67  
68     /**
69      * Opens a dialog to allow the user to choose a gesture set.
70      * 
71      * @param event the action event.
72      */
73     public void actionPerformed(ActionEvent event) {
74        // ask confirm
75        int n = 0;
76        if (!mainView.getModel().getMappings().isEmpty()) {
77           n = JOptionPane.showConfirmDialog(mainView,
78                 Constant.LOAD_GESTURE_SET_CONFIRMATION, Constant.EMPTY_STRING,
79                 JOptionPane.YES_NO_OPTION);
80        }
81        if (n == 0) {
82           JFileChooser fileChooser = new JFileChooser();
83           fileChooser.setFileFilter(new ExtensionFileFilter(MIME
84                 .getExtension(MIME.XML), new String[] { MIME
85                 .getExtension(MIME.XML) }));
86           int status = fileChooser.showOpenDialog(null);
87           if (status == JFileChooser.APPROVE_OPTION) {
88              File selectedFile = fileChooser.getSelectedFile();
89              if (selectedFile != null) {
90                 String ext = selectedFile.getName().substring(
91                       selectedFile.getName().length() - 3,
92                       selectedFile.getName().length());
93                 if (ext.equals(MIME.getExtension(MIME.XML))) {
94                    // update model
95                    mainView.getModel().clearData();
96                    mainView.getModel().loadGestureSet(
97                          loadGestureSet(selectedFile));
98                    mainView.getModel().setGestureSetFileName(
99                          selectedFile.getAbsolutePath());
100                   // update view
101                   mainView.updateGestureList();
102                   mainView.enableSaveButton();
103                }
104 
105             }
106 
107          }
108 
109       }
110 
111    } // actionPerformed
112 
113 
114    /**
115     * Loads the gesture set.
116     * 
117     * @param file the file containing the gesture set to be loaded.
118     */
119    public GestureSet loadGestureSet(File file) {
120       return XMLTool.importGestureSet(file);
121    } // loadGestureSet
122 
123 }