~seh999/jcog/proto3

« back to all changes in this revision

Viewing changes to spacetime/src/opencog/spacetime/scene/Appearance.java

  • Committer: SeH
  • Date: 2009-09-19 22:59:48 UTC
  • Revision ID: seh999@gmail.com-20090919225948-q3ab80xa57i74mm6
start of major jReality refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *
3
 
 * This file is part of jReality. jReality is open source software, made
4
 
 * available under a BSD license:
5
 
 *
6
 
 * Copyright (c) 2003-2006, jReality Group: Charles Gunn, Tim Hoffmann, Markus
7
 
 * Schmies, Steffen Weissmann.
8
 
 *
9
 
 * All rights reserved.
10
 
 *
11
 
 * Redistribution and use in source and binary forms, with or without
12
 
 * modification, are permitted provided that the following conditions are met:
13
 
 *
14
 
 * - Redistributions of source code must retain the above copyright notice, this
15
 
 *   list of conditions and the following disclaimer.
16
 
 *
17
 
 * - Redistributions in binary form must reproduce the above copyright notice,
18
 
 *   this list of conditions and the following disclaimer in the documentation
19
 
 *   and/or other materials provided with the distribution.
20
 
 *
21
 
 * - Neither the name of jReality nor the names of its contributors nor the
22
 
 *   names of their associated organizations may be used to endorse or promote
23
 
 *   products derived from this software without specific prior written
24
 
 *   permission.
25
 
 *
26
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29
 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
 
 * POSSIBILITY OF SUCH DAMAGE.
37
 
 *
38
 
 */
39
 
 
40
 
 
41
 
package opencog.spacetime.scene;
42
 
 
43
 
import java.util.Collections;
44
 
import java.util.HashMap;
45
 
import java.util.Set;
46
 
import java.util.Map.Entry;
47
 
 
48
 
import opencog.spacetime.scene.data.AttributeEntity;
49
 
import opencog.spacetime.scene.event.AppearanceEvent;
50
 
import opencog.spacetime.scene.event.AppearanceEventMulticaster;
51
 
import opencog.spacetime.scene.event.AppearanceListener;
52
 
 
53
 
 
54
 
/**
55
 
 * The appearance node. Contains attributes of arbitrary type stored as <i>(key,value)</i>
56
 
 * pairs in a HashMap<String, Object>.  There are special methods for setting attributes
57
 
 * whose values are common built-in types: <code>double</code>, <code>float</code>,<code>int</code>,
58
 
 * <code>boolean</code>,and <code>char</code>.
59
 
 * <p>
60
 
 * You can query the state of the Appearance by using {@link #getAttribute(String)}. If
61
 
 * not attribute has been defined for this key, the special object {@link #INHERITED} is returned.
62
 
 * <p>
63
 
 * If you wish to remove an attribute value from the key <i>foo</i>, call
64
 
 * <code><pre>
65
 
 * setAttribute(foo, Appearance.INHERITED);
66
 
 * </code></pre>
67
 
 * <p>
68
 
 * Some wiser person will have to tell you when the special object {@link #DEFAULT} is returned.
69
 
 * <p>
70
 
 * TODO: fire ONE single event that reports all changed attributes
71
 
 * @author Unknown
72
 
 */
73
 
public class Appearance extends SceneGraphNode
74
 
{
75
 
  // steffen: i changed these to objects otherwise an effective appearance can never return them...
76
 
  // obsolete now: it would be convenient if these were no inner classes but just objects....
77
 
  public static final Object DEFAULT = new Object();
78
 
  public static final Object INHERITED = new Object();
79
 
 
80
 
  private transient AppearanceListener appearanceListener;
81
 
  private HashMap<String, Object> attributes=new HashMap<String, Object>();
82
 
  private Set<String> storedAttributes = Collections.unmodifiableSet(attributes.keySet());
83
 
  
84
 
  private transient HashMap<String, Object> changedAttributes=new HashMap<String, Object>();
85
 
  
86
 
  private static int UNNAMED_ID;
87
 
 
88
 
  public Appearance(String name) {
89
 
          super(name);
90
 
  }
91
 
  
92
 
  public Appearance() {
93
 
          super("app "+(UNNAMED_ID++));
94
 
  }
95
 
  
96
 
  public Object getAttribute(String key)
97
 
  {
98
 
    startReader();
99
 
    try {
100
 
      Object aa=attributes.get(key);
101
 
      return aa!=null? aa : INHERITED;
102
 
    } finally {
103
 
      finishReader();
104
 
    }
105
 
  }
106
 
 
107
 
  public Object getAttribute(String key, Class type)
108
 
  {
109
 
    startReader();
110
 
    try {
111
 
      Object val=getAttribute(key);
112
 
      if(val==DEFAULT||type.isInstance(val)) return val;
113
 
      return INHERITED;
114
 
    } finally {
115
 
      finishReader();
116
 
    }
117
 
  }
118
 
 
119
 
  public void setAttribute(String key, Object value)
120
 
  {
121
 
    setAttribute(key, value, value.getClass());
122
 
  }
123
 
 
124
 
  public void setAttribute(String key, Object value, Class declaredType)
125
 
  {
126
 
    checkReadOnly();
127
 
    startWriter();
128
 
    try {
129
 
      Object old=null;
130
 
      
131
 
      if(declaredType==null||value==null) throw new NullPointerException();
132
 
      if(value==INHERITED)
133
 
      {
134
 
        old=attributes.remove(key);
135
 
      }
136
 
      else
137
 
      {
138
 
        // TODO: is this check ok? (cheap enough?)
139
 
        if (AttributeEntity.class.isAssignableFrom(value.getClass()))
140
 
          throw new IllegalArgumentException("no proxies allowed");
141
 
        old=attributes.put(key, value);
142
 
      }
143
 
      // TODO: if (!new.equals(old)) ... ???
144
 
      if (old != value ) fireAppearanceChanged(key, old);
145
 
    } finally {
146
 
      finishWriter();
147
 
    }
148
 
  }
149
 
  public void setAttribute(String key, double value)
150
 
  {
151
 
    setAttribute(key, new Double(value));
152
 
  }
153
 
  public void setAttribute(String key, float value)
154
 
  {
155
 
    setAttribute(key, new Float(value));
156
 
  }
157
 
  public void setAttribute(String key, int value)
158
 
  {
159
 
    setAttribute(key, new Integer(value));
160
 
  }
161
 
  public void setAttribute(String key, long value)
162
 
  {
163
 
    setAttribute(key, new Long(value));
164
 
  }
165
 
  public void setAttribute(String key, boolean value)
166
 
  {
167
 
    setAttribute(key, Boolean.valueOf(value));
168
 
  }
169
 
  public void setAttribute(String key, char value)
170
 
  {
171
 
    setAttribute(key, new Character(value));
172
 
  }
173
 
 
174
 
  public void addAppearanceListener(AppearanceListener listener) {
175
 
     startReader();
176
 
           appearanceListener=AppearanceEventMulticaster.add(appearanceListener, listener);
177
 
     finishReader();
178
 
   }
179
 
   public void removeAppearanceListener(AppearanceListener listener) {
180
 
     startReader();
181
 
           appearanceListener=AppearanceEventMulticaster.remove(appearanceListener, listener);
182
 
     finishReader();
183
 
   }
184
 
 
185
 
 /**
186
 
        * Tell the outside world that this appearance has changed.
187
 
        */
188
 
   protected void writingFinished() {
189
 
     try {
190
 
       for (Entry<String, Object> e : changedAttributes.entrySet() ) {
191
 
         appearanceListener.appearanceChanged(new AppearanceEvent(this, e.getKey(), e.getValue()));
192
 
       }
193
 
     } finally {
194
 
       changedAttributes.clear();
195
 
     }
196
 
   };
197
 
   
198
 
   protected void fireAppearanceChanged(String key, Object old) {
199
 
                 if(appearanceListener != null) changedAttributes.put(key, old);
200
 
   }
201
 
 
202
 
   public Set getStoredAttributes() {
203
 
     return storedAttributes;
204
 
   }
205
 
   
206
 
  public void accept(SceneGraphVisitor v) {
207
 
    startReader();
208
 
    try {
209
 
      v.visit(this);
210
 
    } finally {
211
 
      finishReader();
212
 
    }
213
 
  }
214
 
  static void superAccept(Appearance a, SceneGraphVisitor v) {
215
 
    a.superAccept(v);
216
 
  }
217
 
  private void superAccept(SceneGraphVisitor v) {
218
 
    super.accept(v);
219
 
  }
220
 
 
221
 
}