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.map.hash.TObjectIntHashMap;
\r
17 import org.simantics.browsing.ui.BuiltinKeys;
\r
18 import org.simantics.browsing.ui.GraphExplorer;
\r
19 import org.simantics.browsing.ui.NodeContext;
\r
20 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;
\r
21 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
\r
22 import org.simantics.browsing.ui.common.processors.AbstractPrimitiveQueryProcessor;
\r
23 import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;
\r
24 import org.simantics.browsing.ui.common.processors.ShowMaxChildrenProcessor;
\r
27 * @author Tuukka Lehtonen
\r
29 public class DefaultShowMaxChildrenProcessor extends AbstractPrimitiveQueryProcessor<Integer> implements
\r
30 ShowMaxChildrenProcessor, ProcessorLifecycle {
\r
32 private final Integer MAX_INT = Integer.MAX_VALUE;
\r
35 * The set of currently expanded node contexts.
\r
37 private final TObjectIntHashMap<NodeContext> showMaxChildren = new TObjectIntHashMap<NodeContext>();
\r
38 private final THashMap<NodeContext, PrimitiveQueryUpdater> showMaxChildrenQueries = new THashMap<NodeContext, PrimitiveQueryUpdater>();
\r
40 public DefaultShowMaxChildrenProcessor() {
\r
44 public Object getIdentifier() {
\r
45 return BuiltinKeys.SHOW_MAX_CHILDREN;
\r
49 public String toString() {
\r
50 return "ShowMaxChildrenProcessor";
\r
54 public Integer query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey<Integer> key) {
\r
55 int maxChildren = showMaxChildren.get(context);
\r
56 //System.out.println("maxChildren(" + updater + ", " + context + "): " + maxChildren);
\r
57 showMaxChildrenQueries.put(context, updater);
\r
58 if (maxChildren == 0)
\r
60 return Integer.valueOf(maxChildren);
\r
64 public boolean setShowMaxChildren(NodeContext context, int maxChildren) {
\r
65 return _setShowMaxChildren(context, maxChildren);
\r
69 public boolean replaceShowMaxChildren(NodeContext context, int maxChildren) {
\r
70 return nodeStatusChanged(context, maxChildren);
\r
73 private boolean _setShowMaxChildren(NodeContext context, int maxChildren) {
\r
74 if (maxChildren > 0) {
\r
75 return this.showMaxChildren.put(context, maxChildren) != maxChildren;
\r
77 return this.showMaxChildren.remove(context) != 0;
\r
81 protected boolean nodeStatusChanged(NodeContext context, int maxChildren) {
\r
82 boolean result = _setShowMaxChildren(context, maxChildren);
\r
83 PrimitiveQueryUpdater updater = showMaxChildrenQueries.get(context);
\r
84 if (updater != null) {
\r
85 Integer newValue = null;
\r
86 if (maxChildren > 0) {
\r
87 if (maxChildren != Integer.MAX_VALUE)
\r
88 newValue = Integer.valueOf(maxChildren);
\r
92 updater.scheduleReplace(context, BuiltinKeys.SHOW_MAX_CHILDREN, newValue);
\r
98 public void attached(GraphExplorer explorer) {
\r
102 public void detached(GraphExplorer explorer) {
\r
107 public void clear() {
\r
108 showMaxChildren.clear();
\r
109 showMaxChildrenQueries.clear();
\r