View Javadoc

1   /*
2    * @(#)$Id: AlgorithmWrapper.java 692 2009-07-24 18:28:20Z kurmannu $
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *                  
6    *
7    * Purpose		: 
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date				Who			Reason
14   *
15   * 23.03.2008		ukurmann	Initial Release
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  package org.ximtec.igesture.tool.view.testbench.wrapper;
27  
28  import java.beans.PropertyChangeListener;
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.List;
32  
33  import org.ximtec.igesture.algorithm.Algorithm;
34  import org.ximtec.igesture.configuration.Configuration;
35  import org.ximtec.igesture.core.DataObject;
36  import org.ximtec.igesture.core.DataObjectWrapper;
37  import org.ximtec.igesture.core.DefaultPropertyChangeNotifier;
38  import org.ximtec.igesture.tool.view.MainModel;
39  
40  /**
41   * Comment
42   * 
43   * @version 1.0 23.03.2008
44   * @author Ueli Kurmann
45   */
46  public class AlgorithmWrapper extends DefaultPropertyChangeNotifier implements DataObjectWrapper {
47  
48    private static final String PROPERTY_CONFIGURATIONS = "configurations";
49  
50    Class<? extends Algorithm> algorithmClass;
51  
52    MainModel mainModel;
53  
54    List<Configuration> configurations;
55  
56    public AlgorithmWrapper(MainModel mainModel, Class<? extends Algorithm> algorithmClass) {
57      this.algorithmClass = algorithmClass;
58      this.mainModel = mainModel;
59      updateReference();
60    }
61  
62    public void addConfiguration(Configuration configuration) {
63      mainModel.getStorageManager().store(configuration);
64  
65      for (PropertyChangeListener listener : getListeners()) {
66        configuration.addPropertyChangeListener(listener);
67      }
68      updateReference();
69  
70      propertyChangeSupport.fireIndexedPropertyChange(PROPERTY_CONFIGURATIONS, 0, null, configuration);
71      
72    }
73  
74    public void removeConfiguration(Configuration configuration) {
75      propertyChangeSupport.fireIndexedPropertyChange(PROPERTY_CONFIGURATIONS, 0, configuration, null);
76      updateReference();
77    }
78  
79    public Class<? extends Algorithm> getAlgorithm() {
80      return algorithmClass;
81    }
82  
83    public String getName() {
84      return algorithmClass.getSimpleName();
85    }
86  
87    @Override
88    public String toString() {
89      return getName();
90    }
91  
92    @Override
93    public List<DataObject> getDataObjects() {
94      List<DataObject> result = new ArrayList<DataObject>();
95      result.addAll(configurations);
96      return result;
97    }
98  
99    private List<PropertyChangeListener> getListeners() {
100     return Arrays.asList(propertyChangeSupport.getPropertyChangeListeners());
101   }
102 
103   private synchronized void updateReference() {
104     if (configurations == null) {
105       configurations = new ArrayList<Configuration>();
106     }
107     configurations.clear();
108     for (Configuration configuration : mainModel.getConfigurations()) {
109       if (configuration.getAlgorithms().contains(algorithmClass.getName())) {
110         configurations.add(configuration);
111       }
112     }
113 
114     // FIXME find another solution to notify the main controller about the
115     // update.
116     propertyChangeSupport.firePropertyChange("nil", "not", "yes");
117   }
118 
119 }