1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
16 import java.util.TreeMap;
17 import java.util.TreeSet;
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;
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.
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.
46 * @author Tuukka Lehtonen
48 * @see BuiltinKeys#IS_EXPANDED
50 * @see PropertyComparator
52 public class PropertyIsExpandedProcessor extends DefaultIsExpandedProcessor {
54 private static final boolean DEBUG = false;
56 private final Set<PropertyComparable> expandedProperties = new TreeSet<PropertyComparable>(PropertyComparator.INSTANCE);
57 private final Map<PropertyComparable, PrimitiveQueryUpdater> expandedPropertyQueries = new TreeMap<PropertyComparable, PrimitiveQueryUpdater>(PropertyComparator.INSTANCE);
60 public String toString() {
61 return "PropertyIsExpandedProcessor";
64 private PropertyComparable getProperty(NodeContext context) {
65 return AdaptionUtils.adaptToSingle(context, PropertyComparable.class);
69 public boolean setExpanded(NodeContext context, boolean expanded) {
70 PropertyComparable prop = getProperty(context);
72 return _setExpanded(prop, expanded);
73 return super.setExpanded(context, expanded);
76 private boolean _setExpanded(PropertyComparable prop, boolean expanded) {
78 return expandedProperties.add(prop);
80 return expandedProperties.remove(prop);
85 public Boolean query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey<Boolean> key) {
87 System.out.println("QUERY(" + context + ")");
89 PropertyComparable prop = getProperty(context);
92 System.out.println("expanded properties:\n" + EString.implode(expandedProperties, "\n\t"));
94 boolean isExpanded = expandedProperties.contains(prop);
97 System.out.println("property.isExpanded(" + updater + ", " + prop + "): " + isExpanded);
99 expandedPropertyQueries.put(prop, updater);
100 return Boolean.valueOf(isExpanded);
103 return super.query(updater, context, key);
107 protected boolean nodeStatusChanged(NodeContext context, boolean expanded) {
108 boolean result = false;
109 PropertyComparable prop = getProperty(context);
112 System.out.println("nodeStatusChanged(" + prop + ", " + expanded + ")");
114 result = _setExpanded(prop, expanded);
115 PrimitiveQueryUpdater updater = expandedPropertyQueries.get(prop);
117 updater.scheduleReplace(context, BuiltinKeys.IS_EXPANDED, expanded);
119 result = super.nodeStatusChanged(context, expanded);
125 public void clear() {
126 expandedProperties.clear();
127 expandedPropertyQueries.clear();