]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/UpdateRunner.java
Use RuntimeEnvironmentRequest2 where applicable
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / UpdateRunner.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 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 java.util.HashSet;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.TreeItem;
19 import org.simantics.browsing.ui.BuiltinKeys;
20 import org.simantics.browsing.ui.CheckedState;
21 import org.simantics.browsing.ui.NodeContext;
22 import org.simantics.browsing.ui.NodeQueryManager;
23 import org.simantics.browsing.ui.common.internal.GENodeQueryManager;
24 import org.simantics.browsing.ui.common.internal.IGraphExplorerContext;
25 import org.simantics.browsing.ui.content.PrunedChildrenResult;
26
27 /**
28  * This class is responsible for updating the {@link TreeItem}s contained by the
29  * SWT Tree in {@link GraphExplorerImpl}.
30  * 
31  * It will reschedule itself for as long as there are UI items to update in the
32  * explorer. This class is not responsible for performing any throttling of
33  * <code>UpdateRunner</code> executions.
34  * 
35  * @author Antti Villberg
36  */
37 class UpdateRunner implements Runnable {
38
39     final GraphExplorerImpl ge;
40     //final IGraphExplorerContext geContext;
41
42     UpdateRunner(GraphExplorerImpl ge, IGraphExplorerContext geContext) {
43         this.ge = ge;
44       //  this.geContext = geContext;
45     }
46
47     public void run() {
48         try {
49                 doRun();
50         } catch (Throwable t) {
51                 t.printStackTrace();
52         }
53         
54     }
55
56     public void doRun() {
57         
58         if (ge.isDisposed())
59             return;
60
61         HashSet<TreeItem> items = new HashSet<TreeItem>();
62         boolean doRoot;
63
64         synchronized (ge.pendingItems) {
65
66             doRoot = ge.pendingRoot;
67             
68             for (TreeItem item : ge.pendingItems) {
69                 //if(item.isDisposed()) continue;
70                 //if(item.getParentItem() == null) doRoot = true;
71                 //else items.add(item.getParentItem());
72                 if (item == null) {
73                     doRoot = true;
74                 } else {
75                     if(item.isDisposed()) continue;
76                     items.add(item);
77                 }
78             }
79
80             ge.pendingItems = new HashSet<TreeItem>();
81             ge.pendingRoot = false;
82
83         }
84
85         if (doRoot) {
86
87             NodeQueryManager manager = new GENodeQueryManager(ge.explorerContext, null, null, TreeItemReference.create(null));
88
89             PrunedChildrenResult children = manager.query(ge.rootContext, BuiltinKeys.PRUNED_CHILDREN);
90             int maxChildren = ge.getMaxChildren(manager, ge.rootContext);
91             int childCount = Math.min(children.getPrunedChildren().length, maxChildren);
92             ge.tree.clearAll(true);
93             ge.tree.setItemCount(childCount);
94
95         } else {
96
97             int itemIndex = 0;
98             for (TreeItem item : items) {
99                 if (item.isDisposed())
100                     continue;
101
102                 final NodeContext context = (NodeContext)item.getData();
103                 assert(context != null);
104
105                 NodeQueryManager manager = new GENodeQueryManager(ge.explorerContext, null, null, TreeItemReference.create(item));
106
107                 PrunedChildrenResult children = manager.query(context, BuiltinKeys.PRUNED_CHILDREN);
108
109                 int maxChildren = ge.getMaxChildren(manager, context);
110                 item.setItemCount(Math.min(children.getPrunedChildren().length, maxChildren));
111
112                 ge.setTextAndImage(item, manager, context, itemIndex);
113
114                 item.clearAll(true);
115
116                 boolean isExpanded = manager.query(context, BuiltinKeys.IS_EXPANDED);
117                 item.setExpanded(isExpanded);
118
119                 if ((((Control) ge.getControl()).getStyle() & SWT.CHECK) != 0) {
120                     CheckedState checked = manager.query(context, BuiltinKeys.IS_CHECKED);
121                     item.setChecked(CheckedState.CHECKED_STATES.contains(checked));
122                     item.setGrayed(CheckedState.GRAYED == checked);
123                 }
124
125                 ++itemIndex;
126             }
127         }
128
129         synchronized (ge.pendingItems) {
130             if (!ge.scheduleUpdater())
131                 ge.updating = false;
132         }
133     }
134
135 }