]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/PropertyIsExpandedProcessor.java
Merge remote-tracking branch 'origin/svn' commit 'ccc1271c9d6657fb9dcf4cf3cb115fa0c8c...
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / PropertyIsExpandedProcessor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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 java.util.Map;\r
15 import java.util.Set;\r
16 import java.util.TreeMap;\r
17 import java.util.TreeSet;\r
18 \r
19 import org.simantics.browsing.ui.BuiltinKeys;\r
20 import org.simantics.browsing.ui.NodeContext;\r
21 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;\r
22 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
23 import org.simantics.browsing.ui.common.property.IProperty;\r
24 import org.simantics.browsing.ui.common.property.PropertyComparable;\r
25 import org.simantics.browsing.ui.common.property.PropertyComparator;\r
26 import org.simantics.utils.strings.EString;\r
27 import org.simantics.utils.ui.AdaptionUtils;\r
28 \r
29 /**\r
30  * A custom processor implementation for the {@link BuiltinKeys#IS_EXPANDED}\r
31  * primitive query that specially handles all {@link NodeContext}s that contain\r
32  * {@link PropertyComparable} inputs.\r
33  * \r
34  * <p>\r
35  * This class is needed because default values for properties are generally\r
36  * asserted and adding instance properties for subjects with default values\r
37  * causes the data contained by IProperty instances to change. For this reason\r
38  * {@link PropertyComparable} contains two methods:\r
39  * {@link IProperty#propertyHashCode()} and\r
40  * {@link PropertyComparable#propertyEquals(Object)} for comparing whether\r
41  * two {@link PropertyComparable} instances represent the same property of the\r
42  * same subject, regardless of the property value. This allows the UI to\r
43  * correctly expand tree nodes for record-type and array-valued properties when\r
44  * the value changes from asserted to direct or vice versa.\r
45  * \r
46  * @author Tuukka Lehtonen\r
47  * \r
48  * @see BuiltinKeys#IS_EXPANDED\r
49  * @see IProperty\r
50  * @see PropertyComparator\r
51  */\r
52 public class PropertyIsExpandedProcessor extends DefaultIsExpandedProcessor {\r
53 \r
54     private static final boolean DEBUG = false;\r
55 \r
56     private final Set<PropertyComparable>                        expandedProperties      = new TreeSet<PropertyComparable>(PropertyComparator.INSTANCE);\r
57     private final Map<PropertyComparable, PrimitiveQueryUpdater> expandedPropertyQueries = new TreeMap<PropertyComparable, PrimitiveQueryUpdater>(PropertyComparator.INSTANCE);\r
58 \r
59     @Override\r
60     public String toString() {\r
61         return "PropertyIsExpandedProcessor";\r
62     }\r
63 \r
64     private PropertyComparable getProperty(NodeContext context) {\r
65         return AdaptionUtils.adaptToSingle(context, PropertyComparable.class);\r
66     }\r
67 \r
68     @Override\r
69     public boolean setExpanded(NodeContext context, boolean expanded) {\r
70         PropertyComparable prop = getProperty(context);\r
71         if (prop != null)\r
72             return _setExpanded(prop, expanded);\r
73         return super.setExpanded(context, expanded);\r
74     }\r
75 \r
76     private boolean _setExpanded(PropertyComparable prop, boolean expanded) {\r
77         if (expanded) {\r
78             return expandedProperties.add(prop);\r
79         } else {\r
80             return expandedProperties.remove(prop);\r
81         }\r
82     }\r
83 \r
84     @Override\r
85     public Boolean query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey<Boolean> key) {\r
86         if (DEBUG)\r
87             System.out.println("QUERY(" + context + ")");\r
88 \r
89         PropertyComparable prop = getProperty(context);\r
90         if (prop != null) {\r
91             if (DEBUG)\r
92                 System.out.println("expanded properties:\n" + EString.implode(expandedProperties, "\n\t"));\r
93 \r
94             boolean isExpanded = expandedProperties.contains(prop);\r
95 \r
96             if (DEBUG)\r
97                 System.out.println("property.isExpanded(" + updater + ", " + prop + "): " + isExpanded);\r
98 \r
99             expandedPropertyQueries.put(prop, updater);\r
100             return Boolean.valueOf(isExpanded);\r
101         }\r
102 \r
103         return super.query(updater, context, key);\r
104     }\r
105 \r
106     @Override\r
107     protected boolean nodeStatusChanged(NodeContext context, boolean expanded) {\r
108         boolean result = false;\r
109         PropertyComparable prop = getProperty(context);\r
110         if (prop != null) {\r
111             if (DEBUG)\r
112                 System.out.println("nodeStatusChanged(" + prop + ", " + expanded + ")");\r
113 \r
114             result = _setExpanded(prop, expanded);\r
115             PrimitiveQueryUpdater updater = expandedPropertyQueries.get(prop);\r
116             if (updater != null)\r
117                 updater.scheduleReplace(context, BuiltinKeys.IS_EXPANDED, expanded);\r
118         } else {\r
119             result = super.nodeStatusChanged(context, expanded);\r
120         }\r
121         return result;\r
122     }\r
123 \r
124     @Override\r
125     public void clear() {\r
126         expandedProperties.clear();\r
127         expandedPropertyQueries.clear();\r
128         super.clear();\r
129     }\r
130 \r
131 }