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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt;
\r
14 import gnu.trove.map.hash.THashMap;
\r
15 import gnu.trove.set.hash.THashSet;
\r
17 import java.util.Collection;
\r
18 import java.util.HashSet;
\r
19 import java.util.Map;
\r
20 import java.util.Set;
\r
22 import org.eclipse.swt.SWT;
\r
23 import org.eclipse.swt.widgets.Event;
\r
24 import org.eclipse.swt.widgets.Listener;
\r
25 import org.eclipse.swt.widgets.Tree;
\r
26 import org.simantics.browsing.ui.BuiltinKeys;
\r
27 import org.simantics.browsing.ui.GraphExplorer;
\r
28 import org.simantics.browsing.ui.NodeContext;
\r
29 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;
\r
30 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
\r
31 import org.simantics.browsing.ui.common.processors.AbstractPrimitiveQueryProcessor;
\r
32 import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;
\r
33 import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;
\r
36 * @author Tuukka Lehtonen
\r
38 public class DefaultIsExpandedProcessor extends AbstractPrimitiveQueryProcessor<Boolean> implements
\r
39 IsExpandedProcessor, ProcessorLifecycle {
\r
42 * The set of currently expanded node contexts.
\r
44 private final Set<NodeContext> expanded = new THashSet<NodeContext>();
\r
45 private final Map<NodeContext, PrimitiveQueryUpdater> expandedQueries = new THashMap<NodeContext, PrimitiveQueryUpdater>();
\r
49 public DefaultIsExpandedProcessor() {
\r
53 public Object getIdentifier() {
\r
54 return BuiltinKeys.IS_EXPANDED;
\r
58 public String toString() {
\r
59 return "IsExpandedProcessor";
\r
63 public Boolean query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey<Boolean> key) {
\r
64 boolean isExpanded = expanded.contains(context);
\r
65 //System.out.println("isExpanded(" + updater + ", " + context + "): " + isExpanded);
\r
66 expandedQueries.put(context, updater);
\r
67 return Boolean.valueOf(isExpanded);
\r
71 public Collection<NodeContext> getExpanded() {
\r
72 return new HashSet<NodeContext>(expanded);
\r
76 public boolean getExpanded(NodeContext context) {
\r
77 return this.expanded.contains(context);
\r
81 public boolean setExpanded(NodeContext context, boolean expanded) {
\r
82 return _setExpanded(context, expanded);
\r
86 public boolean replaceExpanded(NodeContext context, boolean expanded) {
\r
87 return nodeStatusChanged(context, expanded);
\r
90 private boolean _setExpanded(NodeContext context, boolean expanded) {
\r
92 return this.expanded.add(context);
\r
94 return this.expanded.remove(context);
\r
98 Listener treeListener = new Listener() {
\r
100 public void handleEvent(Event event) {
\r
101 NodeContext context = (NodeContext) event.item.getData();
\r
102 switch (event.type) {
\r
104 nodeStatusChanged(context, true);
\r
107 nodeStatusChanged(context, false);
\r
113 protected boolean nodeStatusChanged(NodeContext context, boolean expanded) {
\r
114 boolean result = _setExpanded(context, expanded);
\r
115 PrimitiveQueryUpdater updater = expandedQueries.get(context);
\r
116 if (updater != null)
\r
117 updater.scheduleReplace(context, BuiltinKeys.IS_EXPANDED, expanded);
\r
122 public void attached(GraphExplorer explorer) {
\r
123 Object control = explorer.getControl();
\r
124 if (control instanceof Tree) {
\r
125 this.tree = (Tree) control;
\r
126 tree.addListener(SWT.Expand, treeListener);
\r
127 tree.addListener(SWT.Collapse, treeListener);
\r
129 System.out.println("WARNING: " + getClass().getSimpleName() + " attached to unsupported control: " + control);
\r
134 public void clear() {
\r
136 expandedQueries.clear();
\r
140 public void detached(GraphExplorer explorer) {
\r
142 if (tree != null) {
\r
143 tree.removeListener(SWT.Expand, treeListener);
\r
144 tree.removeListener(SWT.Collapse, treeListener);
\r