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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt;
\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
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
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
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
46 * @author Tuukka Lehtonen
\r
48 * @see BuiltinKeys#IS_EXPANDED
\r
50 * @see PropertyComparator
\r
52 public class PropertyIsExpandedProcessor extends DefaultIsExpandedProcessor {
\r
54 private static final boolean DEBUG = false;
\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
60 public String toString() {
\r
61 return "PropertyIsExpandedProcessor";
\r
64 private PropertyComparable getProperty(NodeContext context) {
\r
65 return AdaptionUtils.adaptToSingle(context, PropertyComparable.class);
\r
69 public boolean setExpanded(NodeContext context, boolean expanded) {
\r
70 PropertyComparable prop = getProperty(context);
\r
72 return _setExpanded(prop, expanded);
\r
73 return super.setExpanded(context, expanded);
\r
76 private boolean _setExpanded(PropertyComparable prop, boolean expanded) {
\r
78 return expandedProperties.add(prop);
\r
80 return expandedProperties.remove(prop);
\r
85 public Boolean query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey<Boolean> key) {
\r
87 System.out.println("QUERY(" + context + ")");
\r
89 PropertyComparable prop = getProperty(context);
\r
92 System.out.println("expanded properties:\n" + EString.implode(expandedProperties, "\n\t"));
\r
94 boolean isExpanded = expandedProperties.contains(prop);
\r
97 System.out.println("property.isExpanded(" + updater + ", " + prop + "): " + isExpanded);
\r
99 expandedPropertyQueries.put(prop, updater);
\r
100 return Boolean.valueOf(isExpanded);
\r
103 return super.query(updater, context, key);
\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
112 System.out.println("nodeStatusChanged(" + prop + ", " + expanded + ")");
\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
119 result = super.nodeStatusChanged(context, expanded);
\r
125 public void clear() {
\r
126 expandedProperties.clear();
\r
127 expandedPropertyQueries.clear();
\r