View Javadoc

1   /*
2    * @(#)$Id: GestureClass.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose      :   This class represents the concept of a specific
7    *                  gesture (e.g. circle, rectangle, triangle).
8    *                  The class provides a storage container for managing
9    *                  descriptors in the form of samples, character
10   *                  strings etc.
11   * 
12   * -----------------------------------------------------------------------
13   *
14   * Revision Information:
15   *
16   * Date             Who         Reason
17   *
18   * Dec 26, 2006     ukurmann    Initial Release
19   * Mar 22, 2007     bsigner     Cleanup
20   *
21   * -----------------------------------------------------------------------
22   *
23   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
24   *
25   * This software is the proprietary information of ETH Zurich.
26   * Use is subject to license terms.
27   * 
28   */
29  
30  
31  package org.ximtec.igesture.core;
32  
33  import java.util.ArrayList;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  
38  
39  /**
40   * This class represents the concept of a specific gesture (e.g. circle,
41   * rectangle, triangle). The class provides a storage container for managing
42   * descriptors in the form of samples, character strings etc.
43   * 
44   * @version 1.0, Dec 2006
45   * @author Ueli Kurmann, igesture@uelikurmann.ch
46   * @author Beat Signer, signer@inf.ethz.ch
47   */
48  public class GestureClass extends DefaultDataObject {
49  
50     public static final String PROPERTY_NAME = "name";
51  
52     public static final String PROPERTY_DESCRIPTORS = "descriptors";
53  
54     private Map<Class< ? extends Descriptor>, Descriptor> descriptors;
55  
56     /**
57      * The name of the gesture class (e.g. circle).
58      */
59     private String name;
60  
61  
62     /**
63      * Constructs a new gesture class instance.
64      * 
65      * @param name the name of the gesture class to be created.
66      */
67     public GestureClass(String name) {
68        super();
69        setName(name);
70        this.descriptors = new HashMap<Class< ? extends Descriptor>, Descriptor>();
71     }
72  
73  
74     /**
75      * Sets the name of the gesture class.
76      * 
77      * @param name the name of the gesture class.
78      */
79     public void setName(String name) {
80        String oldValue = this.name;
81        this.name = name;
82        propertyChangeSupport.firePropertyChange(PROPERTY_NAME, oldValue, name);
83     } // setName
84  
85  
86     /**
87      * Returns the name of the gesture class.
88      * 
89      * @return the name of the gesture class.
90      */
91     public String getName() {
92        return name;
93     } // getName
94  
95  
96     /**
97      * Returns the gesture class descriptor for a given classname.
98      * 
99      * @param classname the classname for which the gesture class descriptor has
100     *            to be returned.
101     * @return the gesture class descriptor.
102     */
103    @SuppressWarnings("unchecked")
104    public <T extends Descriptor> T getDescriptor(Class<T> classname) {
105       return (T)descriptors.get(classname);
106    } // getDescriptor
107 
108 
109    /**
110     * Returns all gesture class descriptors.
111     * 
112     * @return all gesture class descriptors.
113     */
114    public List<Descriptor> getDescriptors() {
115       return new ArrayList<Descriptor>(descriptors.values());
116    } // getDescriptors
117 
118 
119    /**
120     * Adds a descriptor.
121     * 
122     * @param descriptor the descriptor to be added.
123     */
124    public void addDescriptor(Descriptor descriptor) {
125       addDescriptor(descriptor.getType(), descriptor);
126    } // addDescriptor
127 
128 
129    /**
130     * Adds a descriptor.
131     * 
132     * @param classname the type of the descriptor.
133     * @param descriptor the descriptor to be added.
134     */
135    public void addDescriptor(Class< ? extends Descriptor> classname,
136          Descriptor descriptor) {
137       if (!descriptors.containsKey(classname)) {
138          descriptors.put(classname, descriptor);
139          propertyChangeSupport.fireIndexedPropertyChange(PROPERTY_DESCRIPTORS, 0, null, descriptor);
140       }
141 
142    } // addDescriptor
143 
144 
145    /**
146     * Removes the given descriptor.
147     * 
148     * @param descriptor the descriptor to be removed.
149     */
150    public void removeDescriptor(Class< ? extends Descriptor> descriptor) {
151       descriptors.remove(descriptor);
152       propertyChangeSupport.fireIndexedPropertyChange(PROPERTY_DESCRIPTORS, 0,
153             descriptor, null);
154    } // removeDescriptor
155 
156 
157    /**
158     * Returns true if the given descriptor exists.
159     * @param descriptor the descriptor to be checked for.
160     * @return true if the given descriptor exists.
161     */
162    public boolean hasDescriptor(Class< ? extends Descriptor> descriptor) {
163       return descriptors.containsKey(descriptor);
164    } // hasDescriptor
165 
166 
167    /**
168     * {@inheritDoc}
169     */
170    @Override
171    public void accept(Visitor visitor) {
172       visitor.visit(this);
173       
174       if (descriptors == null) {
175          descriptors = new HashMap<Class< ? extends Descriptor>, Descriptor>();
176       }
177       
178       for (Descriptor descriptor : descriptors.values()) {
179          
180          if (descriptor != null) {
181             descriptor.accept(visitor);
182          }
183       
184       }
185    
186    } // accept
187 
188 
189    @Override
190    public String toString() {
191       return this.name;
192    } // toString
193 
194 }