]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultShowMaxChildrenProcessor.java
Merge remote-tracking branch 'origin/svn' commit 'ccc1271c9d6657fb9dcf4cf3cb115fa0c8c...
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / DefaultShowMaxChildrenProcessor.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.map.hash.TObjectIntHashMap;\r
16 \r
17 import org.simantics.browsing.ui.BuiltinKeys;\r
18 import org.simantics.browsing.ui.GraphExplorer;\r
19 import org.simantics.browsing.ui.NodeContext;\r
20 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;\r
21 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
22 import org.simantics.browsing.ui.common.processors.AbstractPrimitiveQueryProcessor;\r
23 import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;\r
24 import org.simantics.browsing.ui.common.processors.ShowMaxChildrenProcessor;\r
25 \r
26 /**\r
27  * @author Tuukka Lehtonen\r
28  */\r
29 public class DefaultShowMaxChildrenProcessor extends AbstractPrimitiveQueryProcessor<Integer> implements\r
30         ShowMaxChildrenProcessor, ProcessorLifecycle {\r
31 \r
32     private final Integer MAX_INT = Integer.MAX_VALUE;\r
33 \r
34     /**\r
35      * The set of currently expanded node contexts.\r
36      */\r
37     private final TObjectIntHashMap<NodeContext>               showMaxChildren        = new TObjectIntHashMap<NodeContext>();\r
38     private final THashMap<NodeContext, PrimitiveQueryUpdater> showMaxChildrenQueries = new THashMap<NodeContext, PrimitiveQueryUpdater>();\r
39 \r
40     public DefaultShowMaxChildrenProcessor() {\r
41     }\r
42 \r
43     @Override\r
44     public Object getIdentifier() {\r
45         return BuiltinKeys.SHOW_MAX_CHILDREN;\r
46     }\r
47 \r
48     @Override\r
49     public String toString() {\r
50         return "ShowMaxChildrenProcessor";\r
51     }\r
52 \r
53     @Override\r
54     public Integer query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey<Integer> key) {\r
55         int maxChildren = showMaxChildren.get(context);\r
56         //System.out.println("maxChildren(" + updater + ", " + context + "): " + maxChildren);\r
57         showMaxChildrenQueries.put(context, updater);\r
58         if (maxChildren == 0)\r
59             return null;\r
60         return Integer.valueOf(maxChildren);\r
61     }\r
62 \r
63     @Override\r
64     public boolean setShowMaxChildren(NodeContext context, int maxChildren) {\r
65         return _setShowMaxChildren(context, maxChildren);\r
66     }\r
67 \r
68     @Override\r
69     public boolean replaceShowMaxChildren(NodeContext context, int maxChildren) {\r
70         return nodeStatusChanged(context, maxChildren);\r
71     }\r
72 \r
73     private boolean _setShowMaxChildren(NodeContext context, int maxChildren) {\r
74         if (maxChildren > 0) {\r
75             return this.showMaxChildren.put(context, maxChildren) != maxChildren;\r
76         } else {\r
77             return this.showMaxChildren.remove(context) != 0;\r
78         }\r
79     }\r
80 \r
81     protected boolean nodeStatusChanged(NodeContext context, int maxChildren) {\r
82         boolean result = _setShowMaxChildren(context, maxChildren);\r
83         PrimitiveQueryUpdater updater = showMaxChildrenQueries.get(context);\r
84         if (updater != null) {\r
85             Integer newValue = null;\r
86             if (maxChildren > 0) {\r
87                 if (maxChildren != Integer.MAX_VALUE)\r
88                     newValue = Integer.valueOf(maxChildren);\r
89                 else\r
90                     newValue = MAX_INT;\r
91             }\r
92             updater.scheduleReplace(context, BuiltinKeys.SHOW_MAX_CHILDREN, newValue);\r
93         }\r
94         return result;\r
95     }\r
96 \r
97     @Override\r
98     public void attached(GraphExplorer explorer) {\r
99     }\r
100 \r
101     @Override\r
102     public void detached(GraphExplorer explorer) {\r
103         clear();\r
104     }\r
105 \r
106     @Override\r
107     public void clear() {\r
108         showMaxChildren.clear();\r
109         showMaxChildrenQueries.clear();\r
110     }\r
111 \r
112 }