]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/UpdateRunner.java
80ef2bf3919ee245be32db9bf4249d288c2d8370
[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.content.PrunedChildrenResult;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * This class is responsible for updating the {@link TreeItem}s contained by the
30  * SWT Tree in {@link GraphExplorerImpl}.
31  * 
32  * It will reschedule itself for as long as there are UI items to update in the
33  * explorer. This class is not responsible for performing any throttling of
34  * <code>UpdateRunner</code> executions.
35  * 
36  * @author Antti Villberg
37  */
38 class UpdateRunner implements Runnable {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(UpdateRunner.class);
41
42     final GraphExplorerImpl ge;
43
44     UpdateRunner(GraphExplorerImpl ge) {
45         this.ge = ge;
46     }
47
48     public void run() {
49         try {
50                 doRun();
51         } catch (Throwable t) {
52                 LOGGER.error("", t);
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 }