1
package opencog.spacetime.swing.plugin.experimental;
3
import java.util.HashMap;
4
import java.util.LinkedList;
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;
14
import de.jtem.jrworkspace.plugin.Controller;
15
import de.jtem.jrworkspace.plugin.Plugin;
16
import de.jtem.jrworkspace.plugin.PluginInfo;
19
* A managed content that handles its content with a context map.
20
* @author Stefan Sechelmann
22
public class ManagedContent extends Plugin {
26
* @author Stefan Sechelmann
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);
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) { }
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>();
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);
63
SpaceComponent contextRoot = contextMap.get(context);
69
* Sets a content for a given context class. This is equivalent to
70
* first invoking removeAll(context) and then addContent(context, c);
74
public void setContent(Class<?> context, SpaceComponent c) {
76
addContent(context, c);
81
* Adds a component to the scene graph under the given context.
85
public void addContent(Class<?> context, SpaceComponent c) {
86
SpaceComponent contextRoot = getContextRoot(context);
87
contextRoot.addChild(c);
88
fireContentAdded(context, c);
93
* Adds a component to the scene graph if it is not already
94
* a child of the context root.
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);
108
* Removes a scene component from the scene graph and the given context
112
public void removeContent(Class<?> context, SpaceComponent c) {
113
SpaceComponent contextRoot = contextMap.get(context);
114
if (contextRoot == null) {
117
if (contextRoot.getChildComponents().contains(c)) {
118
contextRoot.removeChild(c);
119
fireContentRemoved(context, c);
124
* Removes all content components of a given context
127
public void removeAll(Class<?> context) {
128
SpaceComponent contextRoot = contextMap.get(context);
129
if (contextRoot == null) {
132
contextMap.remove(context);
133
if (contentRoot.getChildComponents().contains(contextRoot)) {
134
contentRoot.removeChild(contextRoot);
135
fireContentRemoved(context);
140
* Removes all content components of all contexts
142
public void clearContent() {
144
contentRoot = new SpaceComponent();
145
fireContentCleared();
150
* Adds a tool to the given context root
154
public void addTool(Class<?> context, Tool tool) {
155
SpaceComponent contextRoot = getContextRoot(context);
156
contextRoot.addTool(tool);
157
fireToolAdded(context, tool);
162
* Adds a tool to the given context root if it is not already
163
* a child of the context root
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);
176
* Removes a tool from the scene graph at the given context root.
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);
189
public void update() {
190
content.setContent(contentRoot);
195
* Returns all registered contexts
196
* @return a set of context classes
198
protected Set<Class<?>> getContextSet() {
199
return contextMap.keySet();
204
public PluginInfo getPluginInfo() {
205
return new PluginInfo("Managed Content", "Stefan Sechelmann");
210
public void install(Controller c) throws Exception {
212
content = JRViewerUtility.getContentPlugin(c);
215
public boolean addContentListener(ContentListener l) {
216
return contentListener.add(l);
219
public boolean removeContentListener(ContentListener l) {
220
return contentListener.remove(l);
223
public void removeAllContentListener() {
224
contentListener.clear();
227
protected void fireContentAdded(Class<?> context, SpaceComponent c) {
228
for (ContentListener l : contentListener) {
229
l.contentAdded(context, c);
233
protected void fireContentRemoved(Class<?> context, SpaceComponent c) {
234
for (ContentListener l : contentListener) {
235
l.contentRemoved(context, c);
239
protected void fireContentRemoved(Class<?> context) {
240
for (ContentListener l : contentListener) {
241
l.contentRemoved(context);
245
protected void fireContentCleared() {
246
for (ContentListener l : contentListener) {
251
protected void fireToolAdded(Class<?> context, Tool t) {
252
for (ContentListener l : contentListener) {
253
l.toolAdded(context, t);
257
protected void fireToolRemoved(Class<?> context, Tool t) {
258
for (ContentListener l : contentListener) {
259
l.toolRemoved(context, t);