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