]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultIsExpandedProcessor.java
Merge remote-tracking branch 'origin/svn' commit 'ccc1271c9d6657fb9dcf4cf3cb115fa0c8c...
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / DefaultIsExpandedProcessor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt;\r
13 \r
14 import gnu.trove.map.hash.THashMap;\r
15 import gnu.trove.set.hash.THashSet;\r
16 \r
17 import java.util.Collection;\r
18 import java.util.HashSet;\r
19 import java.util.Map;\r
20 import java.util.Set;\r
21 \r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.widgets.Event;\r
24 import org.eclipse.swt.widgets.Listener;\r
25 import org.eclipse.swt.widgets.Tree;\r
26 import org.simantics.browsing.ui.BuiltinKeys;\r
27 import org.simantics.browsing.ui.GraphExplorer;\r
28 import org.simantics.browsing.ui.NodeContext;\r
29 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;\r
30 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
31 import org.simantics.browsing.ui.common.processors.AbstractPrimitiveQueryProcessor;\r
32 import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;\r
33 import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;\r
34 \r
35 /**\r
36  * @author Tuukka Lehtonen\r
37  */\r
38 public class DefaultIsExpandedProcessor extends AbstractPrimitiveQueryProcessor<Boolean> implements\r
39 IsExpandedProcessor, ProcessorLifecycle {\r
40 \r
41     /**\r
42      * The set of currently expanded node contexts.\r
43      */\r
44     private final Set<NodeContext>                        expanded        = new THashSet<NodeContext>();\r
45     private final Map<NodeContext, PrimitiveQueryUpdater> expandedQueries = new THashMap<NodeContext, PrimitiveQueryUpdater>();\r
46 \r
47     private Tree tree;\r
48 \r
49     public DefaultIsExpandedProcessor() {\r
50     }\r
51 \r
52     @Override\r
53     public Object getIdentifier() {\r
54         return BuiltinKeys.IS_EXPANDED;\r
55     }\r
56 \r
57     @Override\r
58     public String toString() {\r
59         return "IsExpandedProcessor";\r
60     }\r
61 \r
62     @Override\r
63     public Boolean query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey<Boolean> key) {\r
64         boolean isExpanded = expanded.contains(context);\r
65         //System.out.println("isExpanded(" + updater + ", " + context + "): " + isExpanded);\r
66         expandedQueries.put(context, updater);\r
67         return Boolean.valueOf(isExpanded);\r
68     }\r
69 \r
70     @Override\r
71     public Collection<NodeContext> getExpanded() {\r
72         return new HashSet<NodeContext>(expanded);\r
73     }\r
74 \r
75     @Override\r
76     public boolean getExpanded(NodeContext context) {\r
77         return this.expanded.contains(context);\r
78     }\r
79 \r
80     @Override\r
81     public boolean setExpanded(NodeContext context, boolean expanded) {\r
82         return _setExpanded(context, expanded);\r
83     }\r
84 \r
85     @Override\r
86     public boolean replaceExpanded(NodeContext context, boolean expanded) {\r
87         return nodeStatusChanged(context, expanded);\r
88     }\r
89 \r
90     private boolean _setExpanded(NodeContext context, boolean expanded) {\r
91         if (expanded) {\r
92             return this.expanded.add(context);\r
93         } else {\r
94             return this.expanded.remove(context);\r
95         }\r
96     }\r
97 \r
98     Listener treeListener = new Listener() {\r
99         @Override\r
100         public void handleEvent(Event event) {\r
101             NodeContext context = (NodeContext) event.item.getData();\r
102             switch (event.type) {\r
103                 case SWT.Expand:\r
104                     nodeStatusChanged(context, true);\r
105                     break;\r
106                 case SWT.Collapse:\r
107                     nodeStatusChanged(context, false);\r
108                     break;\r
109             }\r
110         }\r
111     };\r
112 \r
113     protected boolean nodeStatusChanged(NodeContext context, boolean expanded) {\r
114         boolean result = _setExpanded(context, expanded);\r
115         PrimitiveQueryUpdater updater = expandedQueries.get(context);\r
116         if (updater != null)\r
117             updater.scheduleReplace(context, BuiltinKeys.IS_EXPANDED, expanded);\r
118         return result;\r
119     }\r
120 \r
121     @Override\r
122     public void attached(GraphExplorer explorer) {\r
123         Object control = explorer.getControl();\r
124         if (control instanceof Tree) {\r
125             this.tree = (Tree) control;\r
126             tree.addListener(SWT.Expand, treeListener);\r
127             tree.addListener(SWT.Collapse, treeListener);\r
128         } else {\r
129             System.out.println("WARNING: " + getClass().getSimpleName() + " attached to unsupported control: " + control);\r
130         }\r
131     }\r
132 \r
133     @Override\r
134     public void clear() {\r
135         expanded.clear();\r
136         expandedQueries.clear();\r
137     }\r
138 \r
139     @Override\r
140     public void detached(GraphExplorer explorer) {\r
141         clear();\r
142         if (tree != null) {\r
143             tree.removeListener(SWT.Expand, treeListener);\r
144             tree.removeListener(SWT.Collapse, treeListener);\r
145             tree = null;\r
146         }\r
147     }\r
148 \r
149 }