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