View Javadoc

1   /*
2    * @(#)$Id: MainModel.java 819 2010-05-24 21:00:23Z kurmannu $
3    *
4    * Author       :   Ueli Kurmann, igesture@uelikurmann.ch
5    *                                   
6    *                                   
7    * Purpose      :   The main model.
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date             Who         Reason
14   *
15   * 23.03.2008       ukurmann    Initial Release
16   * 29.10.2008       bsigner     Cleanup
17   *
18   * -----------------------------------------------------------------------
19   *
20   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
21   *
22   * This software is the proprietary information of ETH Zurich.
23   * Use is subject to license terms.
24   * 
25   */
26  
27  package org.ximtec.igesture.tool.view;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Properties;
32  
33  import org.ximtec.igesture.algorithm.Algorithm;
34  import org.ximtec.igesture.algorithm.rubine.RubineAlgorithm;
35  import org.ximtec.igesture.algorithm.rubine3d.Rubine3DAlgorithm;
36  import org.ximtec.igesture.algorithm.rubinebd.RubineAlgorithmBigDecimal;
37  import org.ximtec.igesture.algorithm.siger.SigerAlgorithm;
38  import org.ximtec.igesture.algorithm.signature.SiGridAlgorithm;
39  import org.ximtec.igesture.configuration.Configuration;
40  import org.ximtec.igesture.core.GestureSet;
41  import org.ximtec.igesture.core.TestSet;
42  import org.ximtec.igesture.storage.IStorageManager;
43  import org.ximtec.igesture.storage.StorageEngine;
44  import org.ximtec.igesture.storage.StorageManager;
45  import org.ximtec.igesture.tool.locator.RunnableService;
46  import org.ximtec.igesture.tool.util.PropertyChangeVisitor;
47  import org.ximtec.igesture.tool.util.StorageManagerProxy;
48  import org.ximtec.igesture.tool.view.admin.wrapper.GestureSetList;
49  import org.ximtec.igesture.tool.view.testbench.wrapper.AlgorithmList;
50  import org.ximtec.igesture.tool.view.testbench.wrapper.AlgorithmWrapper;
51  import org.ximtec.igesture.tool.view.testset.wrapper.TestSetList;
52  
53  /**
54   * The main model.
55   * 
56   * @version 1.0, Mar 2008
57   * @author Ueli Kurmann, igesture@uelikurmann.ch
58   * @author Beat Signer, signer@inf.ethz.ch
59   */
60  public class MainModel implements RunnableService {
61  
62    public static final String IDENTIFIER = "MainModel";
63  
64    private IStorageManager storageManager;
65  
66    private MainController mainController;
67  
68    private Properties properties;
69  
70    private AlgorithmList algorithmList;
71    
72    private boolean isActive;
73    
74    private String projectName;
75  
76    /**
77     * Constructs a new main model.
78     * 
79     * @param engine
80     *          the storage engine to be used.
81     * @param mainController
82     *          the main controller of the application.
83     * @param proeprties
84     *          the properties
85     */
86    public MainModel(StorageEngine engine, MainController mainController, Properties properties) {
87      this.mainController = mainController;
88      this.properties = properties;
89      setStorageEngine(engine);
90    }
91  
92    /**
93     * Returns a list with all gesture sets.
94     * 
95     * @return a list with all gesture sets.
96     */
97    public List<GestureSet> getGestureSets() {
98      return storageManager.load(GestureSet.class);
99    } // getGestureSets
100 
101   /**
102    * Returns a list with all test sets.
103    * 
104    * @return a list with all test sets.
105    */
106   public List<TestSet> getTestSets() {
107     return storageManager.load(TestSet.class);
108   } // getTestSets
109 
110   /**
111    * Returns a list of all available algorithms.
112    * 
113    * TODO: The list of algorithm should be configurable in a property file.
114    * 
115    * @return a list of all available algorithms.
116    */
117   public List<Class<? extends Algorithm>> getAlgorithms() {
118     List<Class<? extends Algorithm>> algorithms = new ArrayList<Class<? extends Algorithm>>();
119     algorithms.add(RubineAlgorithm.class);
120     algorithms.add(SigerAlgorithm.class);
121     algorithms.add(SiGridAlgorithm.class);
122     algorithms.add(Rubine3DAlgorithm.class);
123     algorithms.add(RubineAlgorithmBigDecimal.class);
124     return algorithms;
125   } // getAlgorithms
126 
127   /**
128    * Returns a list of configurations.
129    * 
130    * @return a list of configurations.
131    */
132   public List<Configuration> getConfigurations() {
133     return storageManager.load(Configuration.class);
134   } // getConfigurations
135 
136   /**
137    * Returns a gesture set list (all gesture sets are wrapped in a
138    * GestureSetList)
139    * 
140    * @return a gesture set list.
141    */
142   public GestureSetList getGestureSetList() {
143     GestureSetList rootSet = new GestureSetList(this);
144     rootSet.addPropertyChangeListener(mainController);
145     return rootSet;
146   } // getGestureSetList
147 
148   public TestSetList getTestSetList() {
149     TestSetList testSetList = new TestSetList(this);
150     testSetList.addPropertyChangeListener(mainController);
151     return testSetList;
152   } // getTestSetList
153 
154   /**
155    * Returns an algorithm list (all algorithms are wrapped in an AlgorithmList)
156    * 
157    * Attention: Implementation depends on the assumption that the list of algorithm
158    * will not change during runtime.
159    * 
160    * TODO: Merge new/removed algorithms in the list during runtime.
161    * 
162    * @return an algorithm list (all algorithms are wrapped in an AlgorithmList)
163    */
164   public synchronized AlgorithmList getAlgorithmList() {
165     if (algorithmList == null) {
166       algorithmList = new AlgorithmList(this);
167 
168       for (Class<? extends Algorithm> algorithmClass : getAlgorithms()) {
169         AlgorithmWrapper algorithmWrapper = new AlgorithmWrapper(this, algorithmClass);
170         algorithmWrapper.addPropertyChangeListener(mainController);
171         algorithmList.addAlgorithm(algorithmWrapper);
172       }
173 
174       algorithmList.addPropertyChangeListener(mainController);
175     }
176     return algorithmList;
177   } // getAlgorithmList
178 
179   @Override
180   public String getIdentifier() {
181     return IDENTIFIER;
182   } // getIdentifier
183 
184   @Override
185   public void reset() {
186     // TODO Auto-generated method stub
187   }
188 
189   @Override
190   public void start() {
191     // TODO Auto-generated method stub
192   }
193 
194   @Override
195   public void stop() {
196     storageManager.dispose();
197   }
198 
199   /**
200    * Returns the Storage Manager TODO: Investigate design! Should be
201    * encapsulated. (UK)
202    * 
203    * @return
204    */
205   public IStorageManager getStorageManager() {
206     return storageManager;
207   }
208 
209   /**
210    * Sets the storage manager. This method is used to change the main model's
211    * data source (the storage manager is wrapped in a dynamic proxy to register
212    * a PropertyChangeListener).
213    * 
214    * @param storageEngine
215    */
216   public void setStorageEngine(StorageEngine storageEngine) {
217     PropertyChangeVisitor visitor = new PropertyChangeVisitor(mainController);
218     this.storageManager = StorageManagerProxy.newInstance(new StorageManager(storageEngine), visitor);
219     isActive = storageEngine != null;
220   } // setStorageEngine
221 
222   /**
223    * Add / Set a property. Properties are key/value pairs and made persistent
224    * after shutting down the application.
225    * 
226    * @param key
227    * @param value
228    */
229   public void setProperty(String key, String value) {
230     properties.setProperty(key, value);
231   }
232 
233   /**
234    * Returns the property value of the given key.
235    * 
236    * @param key
237    *          the key of the property.
238    * @return the value of the property.
239    */
240   public String getProperty(String key) {
241     return properties.getProperty(key);
242   }
243 
244   /**
245    * Removes a property.
246    * 
247    * @param key
248    */
249   public void removeProperty(String key) {
250     properties.remove(key);
251   }
252 
253   public Properties getProperties() {
254     return properties;
255   }
256 
257   public boolean isActive() {
258     return isActive;
259   }
260 
261   public void setProjectName(String projectName) {
262     this.projectName = projectName;
263   }
264 
265   public String getProjectName() {
266     return projectName;
267   }
268 
269 }