]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultShowMaxChildrenProcessor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / DefaultShowMaxChildrenProcessor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import gnu.trove.map.hash.THashMap;
15 import gnu.trove.map.hash.TObjectIntHashMap;
16
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;
25
26 /**
27  * @author Tuukka Lehtonen
28  */
29 public class DefaultShowMaxChildrenProcessor extends AbstractPrimitiveQueryProcessor<Integer> implements
30         ShowMaxChildrenProcessor, ProcessorLifecycle {
31
32     private final Integer MAX_INT = Integer.MAX_VALUE;
33
34     /**
35      * The set of currently expanded node contexts.
36      */
37     private final TObjectIntHashMap<NodeContext>               showMaxChildren        = new TObjectIntHashMap<NodeContext>();
38     private final THashMap<NodeContext, PrimitiveQueryUpdater> showMaxChildrenQueries = new THashMap<NodeContext, PrimitiveQueryUpdater>();
39
40     public DefaultShowMaxChildrenProcessor() {
41     }
42
43     @Override
44     public Object getIdentifier() {
45         return BuiltinKeys.SHOW_MAX_CHILDREN;
46     }
47
48     @Override
49     public String toString() {
50         return "ShowMaxChildrenProcessor";
51     }
52
53     @Override
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);
58         if (maxChildren == 0)
59             return null;
60         return Integer.valueOf(maxChildren);
61     }
62
63     @Override
64     public boolean setShowMaxChildren(NodeContext context, int maxChildren) {
65         return _setShowMaxChildren(context, maxChildren);
66     }
67
68     @Override
69     public boolean replaceShowMaxChildren(NodeContext context, int maxChildren) {
70         return nodeStatusChanged(context, maxChildren);
71     }
72
73     private boolean _setShowMaxChildren(NodeContext context, int maxChildren) {
74         if (maxChildren > 0) {
75             return this.showMaxChildren.put(context, maxChildren) != maxChildren;
76         } else {
77             return this.showMaxChildren.remove(context) != 0;
78         }
79     }
80
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);
89                 else
90                     newValue = MAX_INT;
91             }
92             updater.scheduleReplace(context, BuiltinKeys.SHOW_MAX_CHILDREN, newValue);
93         }
94         return result;
95     }
96
97     @Override
98     public void attached(GraphExplorer explorer) {
99     }
100
101     @Override
102     public void detached(GraphExplorer explorer) {
103         clear();
104     }
105
106     @Override
107     public void clear() {
108         showMaxChildren.clear();
109         showMaxChildrenQueries.clear();
110     }
111
112 }