View Javadoc

1   /*
2    * @(#)$Id: OpenProjectAction.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Michele Croci, mcroci@gmail.com
5    *
6    * Purpose		:   Opens an existing gesture mapping project.
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  import java.net.URISyntaxException;
32  import java.util.List;
33  
34  import javax.swing.JFileChooser;
35  import javax.swing.JOptionPane;
36  
37  import org.sigtec.graphix.widget.BasicAction;
38  import org.sigtec.util.MIME;
39  import org.sigtec.util.ResourceTool;
40  import org.ximtec.igesture.geco.Configuration;
41  import org.ximtec.igesture.geco.Geco;
42  import org.ximtec.igesture.geco.gui.MainView;
43  import org.ximtec.igesture.geco.mapping.GestureToActionMapping;
44  import org.ximtec.igesture.geco.util.Constant;
45  import org.ximtec.igesture.geco.util.ExtensionFileFilter;
46  import org.ximtec.igesture.geco.util.GuiBundleTool;
47  import org.ximtec.igesture.geco.xml.XMLGeco;
48  import org.ximtec.igesture.geco.xml.XMLImportGeco;
49  
50  
51  /**
52   * Opens an existing gesture mapping project.
53   * 
54   * @version 0.9, Nov 2007
55   * @author Michele Croci, mcroci@gmail.com
56   * @author Beat Signer, signer@inf.ethz.ch
57   */
58  public class OpenProjectAction extends BasicAction {
59  
60     /**
61      * The key used to retrieve action details from the resource bundle.
62      */
63     public static final String KEY = "OpenProjectAction";
64  
65     private MainView mainView;
66  
67  
68     public OpenProjectAction(MainView mainView) {
69        super(KEY, GuiBundleTool.getBundle());
70        this.mainView = mainView;
71     }
72  
73  
74     /**
75      * Creates a new mapping
76      * 
77      * @param event the action event.
78      */
79     public void actionPerformed(ActionEvent event) {
80        // display save dialog, if needed
81  
82        if (mainView.getModel().needSave()) {
83           int n = JOptionPane.showConfirmDialog(mainView,
84                 Constant.SAVE_DIALOG_TITLE, Constant.EMPTY_STRING,
85                 JOptionPane.YES_NO_OPTION);
86           if (n == 0) {
87              (new SaveProjectAction(mainView)).save();
88           }
89        }
90  
91        // display file chooser
92        displayFileChooser();
93     } // actionPerformed
94  
95  
96     private void displayFileChooser() {
97        String filePath;
98        try {
99           filePath = new File(ClassLoader.getSystemResource(
100                ResourceTool.getProperty(Constant.SETTINGS,
101                      Constant.SETTINGS_ATTRIBUTE_MAPPINGS, GuiBundleTool
102                            .getBundle())).toURI()).getPath()
103                + Constant.BACKSLASH;
104          
105       }
106       catch (URISyntaxException e) {
107          filePath = new File(ClassLoader.getSystemResource(
108                ResourceTool.getProperty(Constant.SETTINGS,
109                      Constant.SETTINGS_ATTRIBUTE_MAPPINGS, GuiBundleTool
110                            .getBundle())).getPath()).getPath()
111                + Constant.BACKSLASH;
112          
113       }
114 
115       JFileChooser fileChooser = new JFileChooser();
116       fileChooser.setFileFilter(new ExtensionFileFilter(MIME
117             .getExtension(MIME.XML),
118             new String[] { MIME.getExtension(MIME.XML) }));
119       fileChooser.setCurrentDirectory(new File(filePath));
120       int status = fileChooser.showOpenDialog(null);
121 
122       if (status == JFileChooser.APPROVE_OPTION) {
123          File selectedFile = fileChooser.getSelectedFile();
124          if (selectedFile != null) {
125             String ext = "";
126             if (selectedFile.getName().length() >= 4) {
127                ext = selectedFile.getName().substring(
128                      selectedFile.getName().length() - 4,
129                      selectedFile.getName().length());
130             }
131             if (ext.equals(Constant.DOT + MIME.getExtension(MIME.XML))) {
132                openProject(selectedFile);
133                exportConfiguration();
134                // writeFilePath(selectedFile);
135             }
136 
137          }
138 
139       }
140    }
141 
142 
143    public void openProject(File selFile) {
144       File selectedFile = selFile;
145 
146       XMLImportGeco XMLImport = new XMLImportGeco(mainView);
147       XMLImport.importProject(selectedFile);
148 
149       if (!XMLImport.hasError()) {
150          String projectName = selectedFile.getName();
151          projectName = projectName.substring(0, projectName.length() - 4);
152 
153          // update model
154          mainView.getModel().clearData();
155 
156          if (XMLImport.getGestureSet() != null) {
157             mainView.getModel().initRecogniser(XMLImport.getGestureSet());
158             mainView.getModel().loadGestureSet(XMLImport.getGestureSet());
159             List<GestureToActionMapping> list = XMLImport.getMappings();
160 
161             for (GestureToActionMapping mapping : list) {
162                mainView.getModel().addMapping(mapping);
163             }
164 
165             mainView.getModel().setProjectFile(selectedFile);
166             mainView.getModel().setProjectName(projectName);
167             mainView.getModel().setGestureSetFileName(
168                   XMLImport.getGestureSetFileName());
169             mainView.getModel().setNeedSave(false);
170 
171             // update view
172             mainView.initProjectView(projectName);
173             mainView.updateGestureList();
174             mainView.disableSaveButton();
175          }
176       }
177    }
178 
179 
180    public void exportConfiguration() {
181       File file;
182       try {
183          file = new File(ClassLoader.getSystemResource(Geco.GECO_CONFIGURATION)
184                .toURI());
185       }
186       catch (URISyntaxException e) {
187          file = new File(ClassLoader.getSystemResource(Geco.GECO_CONFIGURATION)
188                .getPath());
189       }
190 
191       Configuration configuration = mainView.getModel()
192             .getGestureConfiguration();
193       List<String> devices = configuration.getInputDevices();
194       String s = configuration.getInputDeviceName();
195       boolean[] arr = new boolean[devices.size()];
196       for (int i = 0; i < devices.size(); i++) {
197          if (s.equals(devices.get(i))) {
198             arr[i] = true;
199          }
200       }
201       boolean min = configuration.getMinimize();
202       XMLGeco.exportGestureConfiguration(file, devices, arr, min, mainView
203             .getModel().getProjectFile().getPath());
204    }
205 }