~seh999/jcog/proto3

« back to all changes in this revision

Viewing changes to spacetime/src.ui/opencog/spacetime/swing/plugin/experimental/ManagedContent.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
package opencog.spacetime.swing.plugin.experimental;
 
2
 
 
3
import java.util.HashMap;
 
4
import java.util.LinkedList;
 
5
import java.util.List;
 
6
import java.util.Map;
 
7
import java.util.Set;
 
8
 
 
9
import opencog.spacetime.control.Tool;
 
10
import opencog.spacetime.space.SpaceComponent;
 
11
import opencog.spacetime.swing.JRViewerUtility;
 
12
import opencog.spacetime.swing.plugin.basic.Content;
 
13
 
 
14
import de.jtem.jrworkspace.plugin.Controller;
 
15
import de.jtem.jrworkspace.plugin.Plugin;
 
16
import de.jtem.jrworkspace.plugin.PluginInfo;
 
17
 
 
18
/**
 
19
 * A managed content that handles its content with a context map.
 
20
 * @author Stefan Sechelmann
 
21
 */
 
22
public class ManagedContent extends Plugin {
 
23
 
 
24
        /**
 
25
         * 
 
26
         * @author Stefan Sechelmann
 
27
         */
 
28
        public static interface ContentListener {
 
29
                public void contentAdded(Class<?> context, SpaceComponent c);
 
30
                public void contentRemoved(Class<?> context, SpaceComponent c);
 
31
                public void contentRemoved(Class<?> context);
 
32
                public void contentCleared();
 
33
                public void toolAdded(Class<?> context, Tool t);
 
34
                public void toolRemoved(Class<?> context, Tool t);
 
35
        }
 
36
        
 
37
        public static class ContentAdapter implements ContentListener {
 
38
                public void contentAdded(Class<?> context, SpaceComponent c) { }
 
39
                public void contentRemoved(Class<?> context, SpaceComponent c) { }
 
40
                public void contentRemoved(Class<?> context) { }
 
41
                public void contentCleared() { }
 
42
                public void toolAdded(Class<?> context, Tool t) { }
 
43
                public void toolRemoved(Class<?> context, Tool t) { }
 
44
        }
 
45
        
 
46
        private Content
 
47
                content = null;
 
48
        private SpaceComponent
 
49
                contentRoot = new SpaceComponent("Managed Content Root");
 
50
        private Map<Class<?>, SpaceComponent>
 
51
                contextMap = new HashMap<Class<?>, SpaceComponent>();
 
52
        private List<ContentListener>
 
53
                contentListener = new LinkedList<ContentListener>();
 
54
        
 
55
        
 
56
        public SpaceComponent getContextRoot(Class<?> context) {
 
57
                if (contextMap.get(context) == null) {
 
58
                        SpaceComponent contextRoot = new SpaceComponent();
 
59
                        contextRoot.setName(context.getSimpleName());
 
60
                        contentRoot.addChild(contextRoot);
 
61
                        contextMap.put(context, contextRoot);
 
62
                }
 
63
                SpaceComponent contextRoot = contextMap.get(context);
 
64
                return contextRoot;
 
65
        }
 
66
        
 
67
        
 
68
        /**
 
69
         * Sets a content for a given context class. This is equivalent to
 
70
         * first invoking removeAll(context) and then addContent(context, c);
 
71
         * @param context
 
72
         * @param c
 
73
         */
 
74
        public void setContent(Class<?> context, SpaceComponent c) {
 
75
                removeAll(context);
 
76
                addContent(context, c);
 
77
        }
 
78
        
 
79
        
 
80
        /**
 
81
         * Adds a component to the scene graph under the given context.
 
82
         * @param context
 
83
         * @param c
 
84
         */
 
85
        public void addContent(Class<?> context, SpaceComponent c) {
 
86
                SpaceComponent contextRoot = getContextRoot(context);
 
87
                contextRoot.addChild(c);
 
88
                fireContentAdded(context, c);
 
89
        }
 
90
        
 
91
        
 
92
        /**
 
93
         * Adds a component to the scene graph if it is not already 
 
94
         * a child of the context root.
 
95
         * @param context
 
96
         * @param c
 
97
         */
 
98
        public void addContentUnique(Class<?> context, SpaceComponent c) {
 
99
                SpaceComponent contextRoot = getContextRoot(context);
 
100
                if (!contextRoot.getChildComponents().contains(c)) {
 
101
                        contextRoot.addChild(c);
 
102
                        fireContentAdded(context, c);
 
103
                }
 
104
        }
 
105
        
 
106
        
 
107
        /**
 
108
         * Removes a scene component from the scene graph and the given context
 
109
         * @param context
 
110
         * @param c
 
111
         */
 
112
        public void removeContent(Class<?> context, SpaceComponent c) {
 
113
                SpaceComponent contextRoot = contextMap.get(context);
 
114
                if (contextRoot == null) {
 
115
                        return;
 
116
                }
 
117
                if (contextRoot.getChildComponents().contains(c)) {
 
118
                        contextRoot.removeChild(c);
 
119
                        fireContentRemoved(context, c);
 
120
                }
 
121
        }
 
122
        
 
123
        /**
 
124
         * Removes all content components of a given context 
 
125
         * @param context
 
126
         */
 
127
        public void removeAll(Class<?> context) {
 
128
                SpaceComponent contextRoot = contextMap.get(context);
 
129
                if (contextRoot == null) {
 
130
                        return;
 
131
                }
 
132
                contextMap.remove(context);
 
133
                if (contentRoot.getChildComponents().contains(contextRoot)) {
 
134
                        contentRoot.removeChild(contextRoot);
 
135
                        fireContentRemoved(context);
 
136
                }
 
137
        }
 
138
        
 
139
        /**
 
140
         * Removes all content components of all contexts
 
141
         */
 
142
        public void clearContent() {
 
143
                contextMap.clear();
 
144
                contentRoot = new SpaceComponent();
 
145
                fireContentCleared();
 
146
        }
 
147
        
 
148
        
 
149
        /**
 
150
         * Adds a tool to the given context root
 
151
         * @param context
 
152
         * @param tool
 
153
         */
 
154
        public void addTool(Class<?> context, Tool tool) {
 
155
                SpaceComponent contextRoot = getContextRoot(context);
 
156
                contextRoot.addTool(tool);
 
157
                fireToolAdded(context, tool);
 
158
        }
 
159
        
 
160
        
 
161
        /**
 
162
         * Adds a tool to the given context root if it is not already 
 
163
         * a child of the context root
 
164
         * @param context
 
165
         * @param tool
 
166
         */
 
167
        public void addToolUnique(Class<?> context, Tool tool) {
 
168
                SpaceComponent contextRoot = getContextRoot(context);
 
169
                if (!contextRoot.getTools().contains(tool)) {
 
170
                        contextRoot.addTool(tool);
 
171
                        fireToolAdded(context, tool);
 
172
                }
 
173
        }
 
174
        
 
175
        /**
 
176
         * Removes a tool from the scene graph at the given context root.
 
177
         * @param context
 
178
         * @param tool
 
179
         */
 
180
        public void removeTool(Class<?> context, Tool tool) {
 
181
                SpaceComponent contextRoot = getContextRoot(context);
 
182
                if (contextRoot.getTools().contains(tool)) {
 
183
                        contextRoot.removeTool(tool);
 
184
                        fireToolRemoved(context, tool);
 
185
                }
 
186
        }
 
187
        
 
188
        
 
189
        public void update() {
 
190
                content.setContent(contentRoot);
 
191
        }
 
192
        
 
193
        
 
194
        /**
 
195
         * Returns all registered contexts
 
196
         * @return a set of context classes
 
197
         */
 
198
        protected Set<Class<?>> getContextSet() {
 
199
                return contextMap.keySet();
 
200
        }
 
201
        
 
202
        
 
203
        @Override
 
204
        public PluginInfo getPluginInfo() {
 
205
                return new PluginInfo("Managed Content", "Stefan Sechelmann");
 
206
        }
 
207
 
 
208
        
 
209
        @Override
 
210
        public void install(Controller c) throws Exception {
 
211
                super.install(c);
 
212
                content = JRViewerUtility.getContentPlugin(c);
 
213
        }
 
214
        
 
215
        public boolean addContentListener(ContentListener l) {
 
216
                return contentListener.add(l);
 
217
        }
 
218
        
 
219
        public boolean removeContentListener(ContentListener l) {
 
220
                return contentListener.remove(l);
 
221
        }
 
222
        
 
223
        public void removeAllContentListener() {
 
224
                contentListener.clear();
 
225
        }
 
226
        
 
227
        protected void fireContentAdded(Class<?> context, SpaceComponent c) {
 
228
                for (ContentListener l : contentListener) {
 
229
                        l.contentAdded(context, c);
 
230
                }
 
231
        }
 
232
        
 
233
        protected void fireContentRemoved(Class<?> context, SpaceComponent c) {
 
234
                for (ContentListener l : contentListener) {
 
235
                        l.contentRemoved(context, c);
 
236
                }
 
237
        }
 
238
        
 
239
        protected void fireContentRemoved(Class<?> context) {
 
240
                for (ContentListener l : contentListener) {
 
241
                        l.contentRemoved(context);
 
242
                }
 
243
        }
 
244
        
 
245
        protected void fireContentCleared() {
 
246
                for (ContentListener l : contentListener) {
 
247
                        l.contentCleared();
 
248
                }
 
249
        }
 
250
        
 
251
        protected void fireToolAdded(Class<?> context, Tool t) {
 
252
                for (ContentListener l : contentListener) {
 
253
                        l.toolAdded(context, t);
 
254
                }
 
255
        }
 
256
        
 
257
        protected void fireToolRemoved(Class<?> context, Tool t) {
 
258
                for (ContentListener l : contentListener) {
 
259
                        l.toolRemoved(context, t);
 
260
                }
 
261
        }
 
262
        
 
263
}