]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/base/SWTParentNode.java
Documented difference of RuntimeEnvironmentRequest and Runti...quest2
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / base / SWTParentNode.java
1 package org.simantics.views.swt.client.base;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Hashtable;
6 import java.util.Map;
7 import java.util.Set;
8
9 import org.eclipse.jface.resource.ResourceManager;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Control;
12 import org.eclipse.ui.IWorkbenchSite;
13 import org.simantics.scenegraph.INode;
14 import org.simantics.scenegraph.ParentNode;
15
16 abstract public class SWTParentNode extends ParentNode<ISWTViewNode> implements ISWTViewNode {
17
18         private boolean disposed = false;
19         
20         private static final long serialVersionUID = -3548136282051185971L;
21
22         class M implements Map<String, ISWTViewNode> {
23                 
24                 ArrayList<ISWTViewNode> list = new ArrayList<ISWTViewNode>();
25                 Hashtable<String, ISWTViewNode> table = new Hashtable<String, ISWTViewNode>();
26                 
27                 @Override
28                 public void clear() {
29                         table.clear();
30                         list.clear();
31                 }
32                 @Override
33                 public boolean containsKey(Object arg0) {
34                         return table.containsKey(arg0);
35                 }
36                 @Override
37                 public boolean containsValue(Object arg0) {
38                         return table.containsValue(arg0);
39                 }
40                 @Override
41                 public Set<java.util.Map.Entry<String, ISWTViewNode>> entrySet() {
42                         return table.entrySet();
43                 }
44                 @Override
45                 public ISWTViewNode get(Object arg0) {
46                         return table.get(arg0);
47                 }
48                 @Override
49                 public boolean isEmpty() {
50                         return table.isEmpty();
51                 }
52                 @Override
53                 public Set<String> keySet() {
54                         return table.keySet();
55                 }
56                 @Override
57                 public ISWTViewNode put(String arg0, ISWTViewNode arg1) {
58                         ISWTViewNode exist = table.put(arg0, arg1);
59                         if(exist != null) list.remove(exist);
60                         list.add(arg1);
61                         return exist;
62                 }
63                 @Override
64                 public void putAll(Map<? extends String, ? extends ISWTViewNode> arg0) {
65                         for(Map.Entry<? extends String, ? extends ISWTViewNode> entry : arg0.entrySet())
66                                 put(entry.getKey(), entry.getValue());
67                 }
68                 @Override
69                 public ISWTViewNode remove(Object arg0) {
70                         ISWTViewNode node = table.remove(arg0);
71                         if(node != null) list.remove(node);
72                         return node;
73                 }
74                 @Override
75                 public int size() {
76                         return table.size();
77                 }
78                 @Override
79                 public Collection<ISWTViewNode> values() {
80                         return list;
81                 }
82                 
83         }
84         
85         @Override
86         public SWTRoot getRootNode() {
87                 ParentNode<?> root = super.getRootNode();
88                 return (root instanceof SWTRoot) ? (SWTRoot) root : null;
89         }
90         
91         public ResourceManager getResourceManager() {
92                 SWTRoot root = getRootNode();
93                 if (root == null)
94                         throw new IllegalStateException(this + " not attached to SWTRoot root node");
95                 return root.getResourceManager();
96         }
97         
98         public ResourceManager peekResourceManager() {
99                 SWTRoot root = getRootNode();
100                 return root != null ? root.peekResourceManager() : null;
101         }
102
103         @Override
104         final public void asyncRemoveNode(INode node) {
105                 throw new Error();
106         }
107         
108         @Override
109         protected Map<String, ISWTViewNode> createChildMap() {
110                 return new M();
111         }
112         
113         protected void createChildComposites() {
114                 createChildComposites((Composite)getControl());
115         }
116
117         protected void createChildComposites(Composite composite) {
118                 for(ISWTViewNode node : children.values()) node.createControls(composite);
119         }
120         
121         @Override
122         public Control getControl() {
123                 return null;
124         }
125         
126         @Override
127         public IWorkbenchSite getSite() {
128                 
129                 ParentNode<?> parentNode = getParent();
130                 ISWTViewNode swtParent = (ISWTViewNode)parentNode;
131                 return swtParent.getSite();
132                 
133         }
134
135         public Collection<ISWTViewNode> getChildComposites() {
136                 return children.values();
137         }
138         
139         @Override
140         public boolean isNodeDisposed() {
141                 return disposed;
142         }
143         
144         public void dispose() {
145                 assert(!isNodeDisposed());
146                 for(ISWTViewNode child : getChildComposites())
147                         child.dispose();
148                 cleanup();
149                 disposed = true;
150         }
151         
152 }