View Javadoc

1   /*
2    * @(#)$Id: MainModel.java 787 2010-03-27 15:06:04Z bpuype $
3    *
4    * Author		:	Michele Croci, mcroci@gmail.com
5    *
6    * Purpose		:   Model for MainView class 
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Nov 20, 2007		crocimi		Initial Release
15   *
16   * -----------------------------------------------------------------------
17   *
18   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
19   *
20   * This software is the proprietary information of ETH Zurich.
21   * Use is subject to license terms.
22   * 
23   */
24  
25  
26  package org.ximtec.igesture.geco.gui;
27  
28  import java.io.File;
29  import java.util.Comparator;
30  import java.util.Hashtable;
31  import java.util.List;
32  
33  import org.sigtec.ink.Note;
34  import org.sigtec.ink.Point;
35  import org.ximtec.igesture.Recogniser;
36  import org.ximtec.igesture.core.Gesture;
37  import org.ximtec.igesture.core.GestureClass;
38  import org.ximtec.igesture.core.GestureSet;
39  import org.ximtec.igesture.event.GestureActionManager;
40  import org.ximtec.igesture.geco.Configuration;
41  import org.ximtec.igesture.geco.mapping.GestureToActionMapping;
42  import org.ximtec.igesture.geco.util.SortedListModel;
43  import org.ximtec.igesture.io.AbstractGestureDevice;
44  import org.ximtec.igesture.io.GestureDevice;
45  import org.ximtec.igesture.io.GestureEventListener;
46  import org.ximtec.igesture.io.mouseclient.MouseReader;
47  import org.ximtec.igesture.io.mouseclient.SwingMouseReader;
48  import org.ximtec.igesture.storage.StorageEngine;
49  import org.ximtec.igesture.storage.StorageManager;
50  
51  
52  /**
53   * Model for MainView class
54   * @version 0.9, Nov 20, 2007
55   * @author Michele Croci, mcroci@gmail.com
56   */
57  public class MainModel implements GestureEventListener {
58  
59     private GestureSet gestureSet;
60  
61     private SortedListModel<GestureClass> gestureListModel;
62  
63     private SortedListModel<GestureToActionMapping> mappingListModel;
64  
65     private GestureActionManager eventManager = new GestureActionManager();
66  
67     private Hashtable<GestureClass, GestureToActionMapping> mappings = new Hashtable<GestureClass, GestureToActionMapping>();
68  
69     public Hashtable<String, GestureClass> gestureClassesTable = new Hashtable<String, GestureClass>();
70  
71     private File projectFile;
72  
73     private String gestureSetFileName;
74  
75     private String projectName;
76  
77     private org.ximtec.igesture.configuration.Configuration configuration;
78  
79     private Configuration gestureConfiguration;
80  
81     private boolean toBeSaved;
82  
83     private StorageManager storageManager;
84  
85     public static final String GESTURE_SET = "gestureSet/msApplicationGestures.xml";
86  
87     private Recogniser recogniser;
88  
89     private SwingMouseReader client;
90  
91     private boolean minimize;
92  
93  
94     /**
95      * Constructs a new main model.
96      * 
97      * @param engine the storage engine used by the storage manager.
98      */
99     public MainModel(org.ximtec.igesture.configuration.Configuration conf,
100          Configuration gestConf) {
101       this.configuration = conf;
102       this.gestureConfiguration = gestConf;
103       configureInputDevice();
104       minimize = gestureConfiguration.getMinimize();
105 
106       Comparator<GestureClass> c1 = new Comparator<GestureClass>() {
107 
108          public int compare(GestureClass a, GestureClass b) {
109             return a.getName().compareTo(b.getName());
110          }
111       };
112       Comparator<GestureToActionMapping> c2 = new Comparator<GestureToActionMapping>() {
113 
114          public int compare(GestureToActionMapping a, GestureToActionMapping b) {
115             return a.getGestureClass().getName().compareTo(
116                   b.getGestureClass().getName());
117          }
118       };
119       gestureListModel = new SortedListModel<GestureClass>(c1);
120       mappingListModel = new SortedListModel<GestureToActionMapping>(c2);
121    }// GecoMainModel
122 
123 
124    /**
125     * Loads the data.
126     * 
127     * @param engine the storage engine used by the storage manager.
128     */
129 
130    public void loadData(StorageEngine engine) {
131       if (storageManager != null) {
132          storageManager.dispose();
133       }
134 
135       storageManager = new StorageManager(engine);
136       // loadData();
137    } // loadData
138 
139 
140    /**
141     * Loads the data from the database. All elements are available in memory and
142     * write operations are forwarded to the database.
143     */
144    /*
145     * private void loadData() { gestureClasses = new ArrayList<GestureClass>();
146     * 
147     * for (GestureClass dataObject : storageManager.load(GestureClass.class)) {
148     * gestureClasses.add(dataObject); }
149     * 
150     * for (GestureSet dataObject : storageManager.load(GestureSet.class)) {
151     * gestureSets.add(dataObject); } } // loadData
152     */
153 
154    /**
155     * Adds the gesture set to the gesture main model
156     * 
157     * @param gestureSet the gesture set to be added.
158     */
159    public void loadGestureSet(GestureSet gestureSet) {
160       if (!configuration.getGestureSets().contains(gestureSet))
161          configuration.addGestureSet(gestureSet);
162 
163       this.gestureSet = gestureSet;
164       gestureClassesTable.clear();
165 
166       for (GestureClass gc : gestureSet.getGestureClasses()) {
167          gestureListModel.add(gc);
168          gestureClassesTable.put(gc.getName(), gc);
169 
170       }
171 
172       toBeSaved = true;
173    } // loadGestureSet
174 
175 
176    /**
177     * Clear the list models and the mapping tables
178     */
179    public void clearData() {
180       gestureClassesTable.clear();
181       mappingListModel.clear();
182       gestureListModel.clear();
183       mappings.clear();
184    }// clearData
185 
186 
187    /**
188     * Set the project file
189     * 
190     * @param f the xml file in which the project is saved
191     */
192    public void setProjectFile(File f) {
193       projectFile = f;
194    }// setProjectFile
195 
196 
197    /**
198     * Set the project name
199     * 
200     * @param name the name of the project
201     */
202    public void setProjectName(String name) {
203       projectName = name;
204       toBeSaved = true;
205    }// setProjectName
206 
207 
208    /**
209     * Returns the project file
210     * 
211     * @return the xml file in which the project is saved
212     */
213    public File getProjectFile() {
214       return projectFile;
215    }// getProjectFile
216 
217 
218    /**
219     * Set the project name
220     * 
221     * @param name the name of the project
222     */
223    public void setGestureSetFileName(String name) {
224       gestureSetFileName = name;
225       toBeSaved = true;
226    }// setProjectName
227 
228 
229    /**
230     * Returns the project file
231     * 
232     * @return the xml file in which the project is saved
233     */
234    public String getGestureSetFileName() {
235       return gestureSetFileName;
236    }// getProjectFile
237 
238 
239    /**
240     * Adds the gesture set to the gesture main model
241     * 
242     * @param gestureSet the gesture set to be added.
243     */
244    public String getProjectName() {
245       return projectName;
246    }// getProjectName
247 
248 
249    /**
250     * Returns the gesture set
251     * 
252     * @return the current GestureSet
253     */
254    public GestureSet getGestureSet() {
255       return gestureSet;
256    } // getGestureSets
257 
258 
259    public Hashtable<GestureClass, GestureToActionMapping> getMappings() {
260       return mappings;
261    } // getMappings
262 
263 
264    /**
265     * Adds a gesture-action mapping
266     * 
267     * @param gm the mapping to be added
268     */
269    public void addMapping(GestureToActionMapping gm) {
270       gestureListModel.removeElement(gm.getGestureClass());
271       eventManager.unRegisterEventHandler(gm.getGestureClass().getName());
272       eventManager.registerEventHandler(gm.getGestureClass().getName(), gm
273             .getAction());
274       mappingListModel.removeElement(gm);
275       mappingListModel.add(gm);
276       mappings.put(gm.getGestureClass(), gm);
277       toBeSaved = true;
278    }// addMapping
279 
280 
281    /**
282     * Removes a gesture-action mapping
283     * 
284     * @param the mapping to be removed
285     */
286    public void removeMapping(GestureToActionMapping gm) {
287       mappings.remove(gm.getGestureClass());
288       eventManager.unRegisterEventHandler(gm.getGestureClass().getName());
289       mappingListModel.removeElement(gm);
290       gestureListModel.add(gm.getGestureClass());
291       toBeSaved = true;
292    }// removeMapping
293 
294 
295    /**
296     * Returns the model for the gesture List
297     * 
298     * @return the model
299     */
300    public SortedListModel<GestureClass> getGestureListModel() {
301       return gestureListModel;
302    }// getGestureListModel
303 
304 
305    /**
306     * Returns the model for the mapping List
307     * 
308     * @return the model
309     */
310    public SortedListModel<GestureToActionMapping> getMappingListModel() {
311       return mappingListModel;
312    }// getMappingListModel
313 
314 
315    /**
316     * Returns the event manager
317     * 
318     * @return the event manager
319     */
320    public GestureActionManager getEventManager() {
321       return eventManager;
322    }// getEventManager
323 
324 
325    /**
326     * Returns the Configuration
327     * 
328     * @return the configuration
329     */
330    public org.ximtec.igesture.configuration.Configuration getConfiguration() {
331       return configuration;
332    }// getConfiguration
333 
334 
335    /**
336     * Set the configuration
337     * 
338     * @param c the Configuration to be set
339     */
340    public void setConfiguration(org.ximtec.igesture.configuration.Configuration c) {
341       configuration = c;
342    }// setConfiguration
343 
344 
345    /**
346     * Set toBeSaved flag
347     * 
348     * @param the new value of the flag
349     */
350    public void setNeedSave(boolean b) {
351       toBeSaved = b;
352    }// setNeedSave
353 
354 
355    /**
356     * Should project be saved?
357     * 
358     * @return
359     */
360    public boolean needSave() {
361       return toBeSaved;
362    }// needSave
363 
364 
365    public void initRecogniser(GestureSet gestureSet) {
366       if (recogniser == null) {
367 
368          try {
369 
370             configuration.addGestureSet(gestureSet);
371             recogniser = new Recogniser(configuration);
372             recogniser.addGestureHandler(eventManager);
373             client.addGestureHandler(this);
374          }
375          catch (Exception e) {
376             e.printStackTrace();
377          }
378 
379       }
380 
381    } // initRecogniser
382 
383 
384    /**
385     * Configure Input Client
386     */
387    public void resetInputDevice() {
388       if (client != null) {
389          client.clear();
390       }
391    }
392 
393 
394    public void configureInputDevice() {
395       
396       client = new SwingMouseReader();
397       client.init();
398       // client = new InputDeviceClient(gestureConfiguration.getInputDevice(),
399       // gestureConfiguration.getInputDeviceEventListener());
400       // client.addButtonDeviceEventListener(this);
401       if (recogniser != null) {
402          client.addGestureHandler(this);
403          
404       }
405    }
406 
407 
408    /**
409     * Get GestureConfiguration
410     */
411    public Configuration getGestureConfiguration() {
412       return gestureConfiguration;
413    }
414 
415 
416    /**
417     * Set GestureConfiguration
418     */
419    public void setGestureConfiguration(Configuration conf) {
420       gestureConfiguration = conf;
421    }
422 
423 
424    /**
425     * Application should be minimized
426     */
427    public boolean minimizeAsStartup() {
428       return minimize;
429    }
430 
431 
432    @Override
433    public void handleChunks(List< ? > chunks) {
434       // TODO Auto-generated method stub
435 
436    }
437 
438 
439    @Override
440    public void handleGesture(Gesture< ? > gesture) {
441 
442       if (client.getGesture() != null) {
443          Note note = client.getGesture().getGesture();
444          if (note.getPoints().size() > 5) {
445             recogniser.recognise(note);
446          }
447       }
448 
449    }
450 }