1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 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 java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Deque;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
26 import java.util.WeakHashMap;
27 import java.util.concurrent.CopyOnWriteArrayList;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Future;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicBoolean;
33 import java.util.concurrent.atomic.AtomicReference;
34 import java.util.function.BiFunction;
35 import java.util.function.Consumer;
37 import org.eclipse.core.runtime.Assert;
38 import org.eclipse.core.runtime.AssertionFailedException;
39 import org.eclipse.core.runtime.IProgressMonitor;
40 import org.eclipse.core.runtime.IStatus;
41 import org.eclipse.core.runtime.MultiStatus;
42 import org.eclipse.core.runtime.Platform;
43 import org.eclipse.core.runtime.Status;
44 import org.eclipse.core.runtime.jobs.Job;
45 import org.eclipse.jface.action.IStatusLineManager;
46 import org.eclipse.jface.resource.ColorDescriptor;
47 import org.eclipse.jface.resource.DeviceResourceException;
48 import org.eclipse.jface.resource.DeviceResourceManager;
49 import org.eclipse.jface.resource.FontDescriptor;
50 import org.eclipse.jface.resource.ImageDescriptor;
51 import org.eclipse.jface.resource.JFaceResources;
52 import org.eclipse.jface.resource.LocalResourceManager;
53 import org.eclipse.jface.resource.ResourceManager;
54 import org.eclipse.jface.viewers.IPostSelectionProvider;
55 import org.eclipse.jface.viewers.ISelection;
56 import org.eclipse.jface.viewers.ISelectionChangedListener;
57 import org.eclipse.jface.viewers.ISelectionProvider;
58 import org.eclipse.jface.viewers.IStructuredSelection;
59 import org.eclipse.jface.viewers.SelectionChangedEvent;
60 import org.eclipse.jface.viewers.StructuredSelection;
61 import org.eclipse.jface.viewers.TreeSelection;
62 import org.eclipse.swt.SWT;
63 import org.eclipse.swt.SWTException;
64 import org.eclipse.swt.custom.CCombo;
65 import org.eclipse.swt.custom.TreeEditor;
66 import org.eclipse.swt.events.FocusEvent;
67 import org.eclipse.swt.events.FocusListener;
68 import org.eclipse.swt.events.KeyEvent;
69 import org.eclipse.swt.events.KeyListener;
70 import org.eclipse.swt.events.MouseEvent;
71 import org.eclipse.swt.events.MouseListener;
72 import org.eclipse.swt.events.SelectionEvent;
73 import org.eclipse.swt.events.SelectionListener;
74 import org.eclipse.swt.graphics.Color;
75 import org.eclipse.swt.graphics.Font;
76 import org.eclipse.swt.graphics.GC;
77 import org.eclipse.swt.graphics.Image;
78 import org.eclipse.swt.graphics.Point;
79 import org.eclipse.swt.graphics.RGB;
80 import org.eclipse.swt.graphics.Rectangle;
81 import org.eclipse.swt.widgets.Composite;
82 import org.eclipse.swt.widgets.Control;
83 import org.eclipse.swt.widgets.Display;
84 import org.eclipse.swt.widgets.Event;
85 import org.eclipse.swt.widgets.Listener;
86 import org.eclipse.swt.widgets.ScrollBar;
87 import org.eclipse.swt.widgets.Shell;
88 import org.eclipse.swt.widgets.Text;
89 import org.eclipse.swt.widgets.Tree;
90 import org.eclipse.swt.widgets.TreeColumn;
91 import org.eclipse.swt.widgets.TreeItem;
92 import org.eclipse.ui.IWorkbenchPart;
93 import org.eclipse.ui.IWorkbenchSite;
94 import org.eclipse.ui.PlatformUI;
95 import org.eclipse.ui.contexts.IContextActivation;
96 import org.eclipse.ui.contexts.IContextService;
97 import org.eclipse.ui.services.IServiceLocator;
98 import org.eclipse.ui.swt.IFocusService;
99 import org.simantics.browsing.ui.BuiltinKeys;
100 import org.simantics.browsing.ui.CheckedState;
101 import org.simantics.browsing.ui.Column;
102 import org.simantics.browsing.ui.Column.Align;
103 import org.simantics.browsing.ui.DataSource;
104 import org.simantics.browsing.ui.ExplorerState;
105 import org.simantics.browsing.ui.GraphExplorer;
106 import org.simantics.browsing.ui.NodeContext;
107 import org.simantics.browsing.ui.NodeContext.CacheKey;
108 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;
109 import org.simantics.browsing.ui.NodeContext.QueryKey;
110 import org.simantics.browsing.ui.NodeContextPath;
111 import org.simantics.browsing.ui.NodeQueryManager;
112 import org.simantics.browsing.ui.NodeQueryProcessor;
113 import org.simantics.browsing.ui.PrimitiveQueryProcessor;
114 import org.simantics.browsing.ui.SelectionDataResolver;
115 import org.simantics.browsing.ui.SelectionFilter;
116 import org.simantics.browsing.ui.StatePersistor;
117 import org.simantics.browsing.ui.common.AdaptableHintContext;
118 import org.simantics.browsing.ui.common.ColumnKeys;
119 import org.simantics.browsing.ui.common.ErrorLogger;
120 import org.simantics.browsing.ui.common.NodeContextBuilder;
121 import org.simantics.browsing.ui.common.NodeContextUtil;
122 import org.simantics.browsing.ui.common.internal.GECache;
123 import org.simantics.browsing.ui.common.internal.GENodeQueryManager;
124 import org.simantics.browsing.ui.common.internal.IGECache;
125 import org.simantics.browsing.ui.common.internal.IGraphExplorerContext;
126 import org.simantics.browsing.ui.common.internal.UIElementReference;
127 import org.simantics.browsing.ui.common.processors.DefaultCheckedStateProcessor;
128 import org.simantics.browsing.ui.common.processors.DefaultComparableChildrenProcessor;
129 import org.simantics.browsing.ui.common.processors.DefaultFinalChildrenProcessor;
130 import org.simantics.browsing.ui.common.processors.DefaultImageDecoratorProcessor;
131 import org.simantics.browsing.ui.common.processors.DefaultImagerFactoriesProcessor;
132 import org.simantics.browsing.ui.common.processors.DefaultImagerProcessor;
133 import org.simantics.browsing.ui.common.processors.DefaultLabelDecoratorProcessor;
134 import org.simantics.browsing.ui.common.processors.DefaultLabelerFactoriesProcessor;
135 import org.simantics.browsing.ui.common.processors.DefaultLabelerProcessor;
136 import org.simantics.browsing.ui.common.processors.DefaultPrunedChildrenProcessor;
137 import org.simantics.browsing.ui.common.processors.DefaultSelectedImageDecoratorFactoriesProcessor;
138 import org.simantics.browsing.ui.common.processors.DefaultSelectedLabelDecoratorFactoriesProcessor;
139 import org.simantics.browsing.ui.common.processors.DefaultSelectedLabelerProcessor;
140 import org.simantics.browsing.ui.common.processors.DefaultSelectedViewpointFactoryProcessor;
141 import org.simantics.browsing.ui.common.processors.DefaultSelectedViewpointProcessor;
142 import org.simantics.browsing.ui.common.processors.DefaultViewpointContributionProcessor;
143 import org.simantics.browsing.ui.common.processors.DefaultViewpointContributionsProcessor;
144 import org.simantics.browsing.ui.common.processors.DefaultViewpointProcessor;
145 import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;
146 import org.simantics.browsing.ui.common.processors.NoSelectionRequestProcessor;
147 import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;
148 import org.simantics.browsing.ui.content.ImageDecorator;
149 import org.simantics.browsing.ui.content.Imager;
150 import org.simantics.browsing.ui.content.LabelDecorator;
151 import org.simantics.browsing.ui.content.Labeler;
152 import org.simantics.browsing.ui.content.Labeler.CustomModifier;
153 import org.simantics.browsing.ui.content.Labeler.DeniedModifier;
154 import org.simantics.browsing.ui.content.Labeler.DialogModifier;
155 import org.simantics.browsing.ui.content.Labeler.EnumerationModifier;
156 import org.simantics.browsing.ui.content.Labeler.FilteringModifier;
157 import org.simantics.browsing.ui.content.Labeler.LabelerListener;
158 import org.simantics.browsing.ui.content.Labeler.Modifier;
159 import org.simantics.browsing.ui.content.PrunedChildrenResult;
160 import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;
161 import org.simantics.browsing.ui.model.nodetypes.NodeType;
162 import org.simantics.browsing.ui.swt.internal.Threads;
163 import org.simantics.db.layer0.SelectionHints;
164 import org.simantics.utils.ObjectUtils;
165 import org.simantics.utils.datastructures.BijectionMap;
166 import org.simantics.utils.datastructures.disposable.AbstractDisposable;
167 import org.simantics.utils.datastructures.hints.IHintContext;
168 import org.simantics.utils.threads.IThreadWorkQueue;
169 import org.simantics.utils.threads.SWTThread;
170 import org.simantics.utils.threads.ThreadUtils;
171 import org.simantics.utils.ui.ISelectionUtils;
172 import org.simantics.utils.ui.jface.BasePostSelectionProvider;
173 import org.simantics.utils.ui.widgets.VetoingEventHandler;
174 import org.simantics.utils.ui.workbench.WorkbenchUtils;
176 import gnu.trove.map.hash.THashMap;
177 import gnu.trove.procedure.TObjectProcedure;
178 import gnu.trove.set.hash.THashSet;
181 * @see #getMaxChildren()
182 * @see #setMaxChildren(int)
183 * @see #getMaxChildren(NodeQueryManager, NodeContext)
185 class GraphExplorerImpl extends GraphExplorerImplBase implements Listener, GraphExplorer /*, IPostSelectionProvider*/ {
187 private static class GraphExplorerPostSelectionProvider implements IPostSelectionProvider {
189 private GraphExplorerImpl ge;
191 GraphExplorerPostSelectionProvider(GraphExplorerImpl ge) {
200 public void setSelection(final ISelection selection) {
201 if(ge == null) return;
202 ge.setSelection(selection, false);
207 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
208 if(ge == null) return;
209 if(ge.isDisposed()) {
210 if (DEBUG_SELECTION_LISTENERS)
211 System.out.println("GraphExplorerImpl is disposed in removeSelectionChangedListener: " + listener);
214 //assertNotDisposed();
215 //System.out.println("Remove selection changed listener: " + listener);
216 ge.selectionProvider.removeSelectionChangedListener(listener);
220 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
221 if(ge == null) return;
222 if (!ge.thread.currentThreadAccess())
223 throw new AssertionError(getClass().getSimpleName() + ".addPostSelectionChangedListener called from non SWT-thread: " + Thread.currentThread());
224 if(ge.isDisposed()) {
225 System.out.println("Client BUG: GraphExplorerImpl is disposed in addPostSelectionChangedListener: " + listener);
228 //System.out.println("Add POST selection changed listener: " + listener);
229 ge.selectionProvider.addPostSelectionChangedListener(listener);
233 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
234 if(ge == null) return;
235 if(ge.isDisposed()) {
236 if (DEBUG_SELECTION_LISTENERS)
237 System.out.println("GraphExplorerImpl is disposed in removePostSelectionChangedListener: " + listener);
240 // assertNotDisposed();
241 //System.out.println("Remove POST selection changed listener: " + listener);
242 ge.selectionProvider.removePostSelectionChangedListener(listener);
247 public void addSelectionChangedListener(ISelectionChangedListener listener) {
248 if(ge == null) return;
249 if (!ge.thread.currentThreadAccess())
250 throw new AssertionError(getClass().getSimpleName() + ".addSelectionChangedListener called from non SWT-thread: " + Thread.currentThread());
251 //System.out.println("Add selection changed listener: " + listener);
252 if (ge.tree.isDisposed() || ge.selectionProvider == null) {
253 System.out.println("Client BUG: GraphExplorerImpl is disposed in addSelectionChangedListener: " + listener);
257 ge.selectionProvider.addSelectionChangedListener(listener);
262 public ISelection getSelection() {
263 if(ge == null) return StructuredSelection.EMPTY;
264 if (!ge.thread.currentThreadAccess())
265 throw new AssertionError(getClass().getSimpleName() + ".getSelection called from non SWT-thread: " + Thread.currentThread());
266 if (ge.tree.isDisposed() || ge.selectionProvider == null)
267 return StructuredSelection.EMPTY;
268 return ge.selectionProvider.getSelection();
274 * If this explorer is running with an Eclipse workbench open, this
275 * Workbench UI context will be activated whenever inline editing is started
276 * through {@link #startEditing(TreeItem, int)} and deactivated when inline
279 * This context information can be used to for UI handler activity testing.
281 private static final String INLINE_EDITING_UI_CONTEXT = "org.simantics.browsing.ui.inlineEditing";
283 private static final String KEY_DRAG_COLUMN = "dragColumn";
285 private static final boolean DEBUG_SELECTION_LISTENERS = false;
287 private static final int DEFAULT_CONSECUTIVE_LABEL_REFRESH_DELAY = 200;
289 public static final int DEFAULT_MAX_CHILDREN = 1000;
291 private static final long POST_SELECTION_DELAY = 300;
294 * The time in milliseconds that must elapse between consecutive
295 * {@link Tree} {@link SelectionListener#widgetSelected(SelectionEvent)}
296 * invocations in order for this class to construct a new selection.
299 * This is done because selection construction can be very expensive as the
300 * selected set grows larger when the user is pressing shift+arrow keys.
301 * GraphExplorerImpl will naturally listen to all changes in the tree
302 * selection, but as an optimization will not construct new
303 * StructuredSelection instances for every selection change event. A new
304 * selection will be constructed and set only if the selection hasn't
305 * changed for the amount of milliseconds specified by this constant.
307 private static final long SELECTION_CHANGE_QUIET_TIME = 150;
309 private final IThreadWorkQueue thread;
312 * Local method for checking from whether resources are loaded in
315 private final LocalResourceManager localResourceManager;
318 * Local device resource manager that is safe to use in
319 * {@link ImageLoaderJob} for creating images in a non-UI thread.
321 private final ResourceManager resourceManager;
324 * Package visibility.
325 * TODO: Get rid of these.
329 @SuppressWarnings({ "rawtypes" })
330 final HashMap<CacheKey<?>, NodeQueryProcessor> processors = new HashMap<CacheKey<?>, NodeQueryProcessor>();
331 @SuppressWarnings({ "rawtypes" })
332 final HashMap<Object, PrimitiveQueryProcessor> primitiveProcessors = new HashMap<Object, PrimitiveQueryProcessor>();
333 @SuppressWarnings({ "rawtypes" })
334 final HashMap<Class, DataSource> dataSources = new HashMap<Class, DataSource>();
336 class GraphExplorerContext extends AbstractDisposable implements IGraphExplorerContext {
337 // This is for query debugging only.
340 GECache cache = new GECache();
341 AtomicBoolean propagating = new AtomicBoolean(false);
342 Object propagateList = new Object();
343 Object propagate = new Object();
344 List<Runnable> scheduleList = new ArrayList<Runnable>();
345 final Deque<Integer> activity = new LinkedList<Integer>();
349 * Stores the currently running query update runnable. If
350 * <code>null</code> there's nothing scheduled yet in which case
351 * scheduling can commence. Otherwise the update should be skipped.
353 AtomicReference<Runnable> currentQueryUpdater = new AtomicReference<Runnable>();
356 * Keeps track of nodes that have already been auto-expanded. After
357 * being inserted into this set, nodes will not be forced to stay in an
358 * expanded state after that. This makes it possible for the user to
359 * close auto-expanded nodes.
361 Map<NodeContext, Boolean> autoExpanded = new WeakHashMap<NodeContext, Boolean>();
365 protected void doDispose() {
367 autoExpanded.clear();
371 public IGECache getCache() {
376 public int queryIndent() {
381 public int queryIndent(int offset) {
382 queryIndent += offset;
387 @SuppressWarnings("unchecked")
388 public <T> NodeQueryProcessor<T> getProcessor(Object o) {
389 return processors.get(o);
393 @SuppressWarnings("unchecked")
394 public <T> PrimitiveQueryProcessor<T> getPrimitiveProcessor(Object o) {
395 return primitiveProcessors.get(o);
398 @SuppressWarnings("unchecked")
400 public <T> DataSource<T> getDataSource(Class<T> clazz) {
401 return dataSources.get(clazz);
405 public void update(UIElementReference ref) {
406 //System.out.println("GE.update " + ref);
407 TreeItemReference tiref = (TreeItemReference) ref;
408 TreeItem item = tiref.getItem();
409 // NOTE: must be called regardless of the the item value.
410 // A null item is currently used to indicate a tree root update.
411 GraphExplorerImpl.this.update(item);
415 public Object getPropagateLock() {
420 public Object getPropagateListLock() {
421 return propagateList;
425 public boolean isPropagating() {
426 return propagating.get();
430 public void setPropagating(boolean b) {
431 this.propagating.set(b);
435 public List<Runnable> getScheduleList() {
440 public void setScheduleList(List<Runnable> list) {
441 this.scheduleList = list;
445 public Deque<Integer> getActivity() {
450 public void setActivityInt(int i) {
451 this.activityInt = i;
455 public int getActivityInt() {
460 public void scheduleQueryUpdate(Runnable r) {
461 if (GraphExplorerImpl.this.isDisposed() || queryUpdateScheduler.isShutdown())
463 //System.out.println("Scheduling query update for runnable " + r);
464 if (currentQueryUpdater.compareAndSet(null, r)) {
465 //System.out.println("Scheduling query update for runnable " + r);
466 queryUpdateScheduler.execute(QUERY_UPDATE_SCHEDULER);
470 Runnable QUERY_UPDATE_SCHEDULER = new Runnable() {
473 Runnable r = currentQueryUpdater.getAndSet(null);
475 //System.out.println("Running query update runnable " + r);
482 GraphExplorerContext explorerContext = new GraphExplorerContext();
484 HashSet<TreeItem> pendingItems = new HashSet<TreeItem>();
485 boolean updating = false;
486 boolean pendingRoot = false;
488 @SuppressWarnings("deprecation")
489 ModificationContext modificationContext = null;
491 NodeContext rootContext;
493 StatePersistor persistor = null;
495 boolean editable = true;
498 * This is a reverse mapping from {@link NodeContext} tree objects back to
499 * their owner TreeItems.
502 * Access this map only in the SWT thread to keep it thread-safe.
505 BijectionMap<NodeContext, TreeItem> contextToItem = new BijectionMap<NodeContext, TreeItem>();
508 * Columns of the UI viewer. Use {@link #setColumns(Column[])} to
511 Column[] columns = new Column[0];
512 Map<String, Integer> columnKeyToIndex = new HashMap<String, Integer>();
513 boolean refreshingColumnSizes = false;
514 boolean columnsAreVisible = true;
517 * An array reused for invoking {@link TreeItem#setImage(Image[])} instead
518 * of constantly allocating new arrays for setting each TreeItems images.
519 * This works because {@link TreeItem#setImage(Image[])} does not take hold
520 * of the array itself, only the contents of the array.
522 * @see #setImage(NodeContext, TreeItem, Imager, Collection, int)
524 Image[] columnImageArray = { null };
527 * Used for collecting Image or ImageDescriptor instances for a single
528 * TreeItem when initially setting images for a TreeItem.
530 * @see #setImage(NodeContext, TreeItem, Imager, Collection, int)
532 Object[] columnDescOrImageArray = { null };
534 final ExecutorService queryUpdateScheduler = Threads.getExecutor();
535 final ScheduledExecutorService uiUpdateScheduler = ThreadUtils.getNonBlockingWorkExecutor();
537 /** Set to true when the Tree widget is disposed. */
538 private boolean disposed = false;
539 private final CopyOnWriteArrayList<FocusListener> focusListeners = new CopyOnWriteArrayList<FocusListener>();
540 private final CopyOnWriteArrayList<MouseListener> mouseListeners = new CopyOnWriteArrayList<MouseListener>();
541 private final CopyOnWriteArrayList<KeyListener> keyListeners = new CopyOnWriteArrayList<KeyListener>();
543 /** Selection provider */
544 private GraphExplorerPostSelectionProvider postSelectionProvider = new GraphExplorerPostSelectionProvider(this);
545 protected BasePostSelectionProvider selectionProvider = new BasePostSelectionProvider();
546 protected SelectionDataResolver selectionDataResolver;
547 protected SelectionFilter selectionFilter;
548 protected BiFunction<GraphExplorer, Object[], Object[]> selectionTransformation = new BiFunction<GraphExplorer, Object[], Object[]>() {
551 public Object[] apply(GraphExplorer explorer, Object[] objects) {
552 Object[] result = new Object[objects.length];
553 for (int i = 0; i < objects.length; i++) {
554 IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);
555 context.setHint(SelectionHints.KEY_MAIN, objects[i]);
562 protected FontDescriptor originalFont;
563 protected ColorDescriptor originalForeground;
564 protected ColorDescriptor originalBackground;
567 * The set of currently selected TreeItem instances. This set is needed
568 * because we need to know in {@link #setData(Event)} whether the updated
569 * item was a part of the current selection in which case the selection must
572 private final Map<TreeItem, NodeContext> selectedItems = new HashMap<TreeItem, NodeContext>();
575 * TODO: specify what this is for
577 private final Set<NodeContext> selectionRefreshContexts = new HashSet<NodeContext>();
580 * If this field is non-null, it means that if {@link #setData(Event)}
581 * encounters a NodeContext equal to this one, it must make the TreeItem
582 * assigned to that NodeContext the topmost item of the tree using
583 * {@link Tree#setTopItem(TreeItem)}. After this the field value is
587 * This is related to {@link #initializeState()}, i.e. explorer state
590 // private NodeContext[] topNodePath = NodeContext.NONE;
591 // private int[] topNodePath = {};
592 // private int currentTopNodePathIndex = -1;
595 * See {@link #setAutoExpandLevel(int)}
597 private int autoExpandLevel = 0;
600 * <code>null</code> if not explicitly set through
601 * {@link #setServiceLocator(IServiceLocator)}.
603 private IServiceLocator serviceLocator;
606 * The global workbench context service, if the workbench is available.
607 * Retrieved in the constructor.
609 private IContextService contextService = null;
612 * The global workbench IFocusService, if the workbench is available.
613 * Retrieved in the constructor.
615 private IFocusService focusService = null;
618 * A Workbench UI context activation that is activated when starting inline
619 * editing through {@link #startEditing(TreeItem, int)}.
621 * @see #activateEditingContext()
622 * @see #deactivateEditingContext()
624 private IContextActivation editingContext = null;
626 static class ImageTask {
629 Object[] descsOrImages;
630 public ImageTask(NodeContext node, TreeItem item, Object[] descsOrImages) {
633 this.descsOrImages = descsOrImages;
638 * The job that is used for off-loading image loading tasks (see
639 * {@link ImageTask} to a worker thread from the main UI thread.
641 * @see #setPendingImages(IProgressMonitor)
643 ImageLoaderJob imageLoaderJob;
646 * The set of currently gathered up image loading tasks for
647 * {@link #imageLoaderJob} to execute.
649 * @see #setPendingImages(IProgressMonitor)
651 Map<TreeItem, ImageTask> imageTasks = new THashMap<TreeItem, ImageTask>();
654 * A state flag indicating whether the vertical scroll bar was visible for
655 * {@link #tree} the last time it was checked. Since there is no listener
656 * that can provide this information, we check it in {@link #setData(Event)}
657 * every time any data for a TreeItem is updated. If the visibility changes,
658 * we will force re-layouting of the tree's parent composite.
660 * @see #setData(Event)
662 private boolean verticalBarVisible = false;
664 static class TransientStateImpl implements TransientExplorerState {
666 private Integer activeColumn = null;
669 public synchronized Integer getActiveColumn() {
673 public synchronized void setActiveColumn(Integer column) {
674 activeColumn = column;
679 private TransientStateImpl transientState = new TransientStateImpl();
681 boolean scheduleUpdater() {
683 if (tree.isDisposed())
686 if (pendingRoot == true || !pendingItems.isEmpty()) {
687 assert(!tree.isDisposed());
689 int activity = explorerContext.activityInt;
691 if (activity < 100) {
692 // System.out.println("Scheduling update immediately.");
693 } else if (activity < 1000) {
694 // System.out.println("Scheduling update after 500ms.");
697 // System.out.println("Scheduling update after 3000ms.");
703 //System.out.println("Scheduling UI update after " + delay + " ms.");
704 uiUpdateScheduler.schedule(new Runnable() {
708 if (tree.isDisposed())
711 if (updateCounter > 0) {
713 uiUpdateScheduler.schedule(this, 50, TimeUnit.MILLISECONDS);
715 tree.getDisplay().asyncExec(new UpdateRunner(GraphExplorerImpl.this, GraphExplorerImpl.this.explorerContext));
719 }, delay, TimeUnit.MILLISECONDS);
728 int updateCounter = 0;
730 void update(TreeItem item) {
732 synchronized(pendingItems) {
734 // System.out.println("update " + item);
738 if(item == null) pendingRoot = true;
739 else pendingItems.add(item);
741 if(updating == true) return;
749 private int maxChildren = DEFAULT_MAX_CHILDREN;
752 public int getMaxChildren() {
757 public int getMaxChildren(NodeQueryManager manager, NodeContext context) {
758 Integer result = manager.query(context, BuiltinKeys.SHOW_MAX_CHILDREN);
759 //System.out.println("getMaxChildren(" + manager + ", " + context + "): " + result);
760 if (result != null) {
762 throw new AssertionError("BuiltinKeys.SHOW_MAX_CHILDREN query must never return < 0, got " + result);
769 public void setMaxChildren(int maxChildren) {
770 this.maxChildren = maxChildren;
774 public void setModificationContext(@SuppressWarnings("deprecation") ModificationContext modificationContext) {
775 this.modificationContext = modificationContext;
779 * @param parent the parent SWT composite
781 public GraphExplorerImpl(Composite parent) {
782 this(parent, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
786 * Stores the node context and the modifier that is currently being
787 * modified. These are used internally to prevent duplicate edits from being
788 * initiated which should always be a sensible thing to do.
790 private Set<NodeContext> currentlyModifiedNodes = new THashSet<NodeContext>();
792 private final TreeEditor editor;
793 private Color invalidModificationColor = null;
796 * @param item the TreeItem to start editing
797 * @param columnIndex the index of the column to edit, starts counting from
799 * @return <code>true</code> if the editing was initiated successfully or
800 * <code>false</code> if editing could not be started due to lack of
801 * {@link Modifier} for the labeler in question.
803 private String startEditing(final TreeItem item, final int columnIndex, String columnKey) {
805 return "Rename not supported for selection";
807 GENodeQueryManager manager = new GENodeQueryManager(this.explorerContext, null, null, TreeItemReference.create(item.getParentItem()));
808 final NodeContext context = (NodeContext) item.getData();
809 Labeler labeler = manager.query(context, BuiltinKeys.SELECTED_LABELER);
811 return "Rename not supported for selection";
813 if(columnKey == null) columnKey = columns[columnIndex].getKey();
815 // columnKey might be prefixed with '#' to indicate
816 // textual editing is preferred. Try to get modifier
817 // for that first and only if it fails, try without
819 Modifier modifier = labeler.getModifier(modificationContext, columnKey);
820 if (modifier == null) {
821 if(columnKey.startsWith("#"))
822 modifier = labeler.getModifier(modificationContext, columnKey.substring(1));
823 if (modifier == null)
824 return "Rename not supported for selection";
826 if (modifier instanceof DeniedModifier) {
827 DeniedModifier dm = (DeniedModifier)modifier;
828 return dm.getMessage();
831 // Prevent editing of a single node context multiple times.
832 if (currentlyModifiedNodes.contains(context)) {
833 //System.out.println("discarding duplicate edit for context " + context);
834 return "Rename not supported for selection";
837 // Clean up any previous editor control
838 Control oldEditor = editor.getEditor();
839 if (oldEditor != null)
842 if (modifier instanceof DialogModifier) {
843 performDialogEditing(item, columnIndex, context, (DialogModifier) modifier);
844 } else if (modifier instanceof CustomModifier) {
845 startCustomEditing(item, columnIndex, context, (CustomModifier) modifier);
846 } else if (modifier instanceof EnumerationModifier) {
847 startEnumerationEditing(item, columnIndex, context, (EnumerationModifier) modifier);
849 startTextEditing(item, columnIndex, context, modifier);
861 void performDialogEditing(final TreeItem item, final int columnIndex, final NodeContext context,
862 final DialogModifier modifier) {
863 final AtomicBoolean disposed = new AtomicBoolean(false);
864 Consumer<String> callback = result -> {
867 String error = modifier.isValid(result);
869 modifier.modify(result);
870 // Item may be disposed if the tree gets reset after a previous editing.
871 if (!item.isDisposed()) {
872 item.setText(columnIndex, result);
873 queueSelectionRefresh(context);
878 currentlyModifiedNodes.add(context);
880 String status = modifier.query(tree, item, columnIndex, context, callback);
882 ErrorLogger.defaultLog( new Status(IStatus.INFO, Activator.PLUGIN_ID, status) );
884 currentlyModifiedNodes.remove(context);
889 private void reconfigureTreeEditor(TreeItem item, int columnIndex, Control control, int widthHint, int heightHint, int insetX, int insetY) {
890 Point size = control.computeSize(widthHint, heightHint);
891 editor.horizontalAlignment = SWT.LEFT;
892 Rectangle itemRect = item.getBounds(columnIndex),
893 rect = tree.getClientArea();
894 editor.minimumWidth = Math.max(size.x, itemRect.width) + insetX * 2;
895 int left = itemRect.x,
896 right = rect.x + rect.width;
897 editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
898 editor.minimumHeight = size.y + insetY * 2;
902 void reconfigureTreeEditorForText(TreeItem item, int columnIndex, Control control, String text, int heightHint, int insetX, int insetY) {
903 GC gc = new GC(control);
904 Point size = gc.textExtent(text);
906 reconfigureTreeEditor(item, columnIndex, control, size.x, SWT.DEFAULT, insetX, insetY);
915 void startCustomEditing(final TreeItem item, final int columnIndex, final NodeContext context,
916 final CustomModifier modifier) {
917 final Object obj = modifier.createControl(tree, item, columnIndex, context);
918 if (!(obj instanceof Control))
919 throw new UnsupportedOperationException("SWT control required, got " + obj + " from CustomModifier.createControl(Object)");
920 final Control control = (Control) obj;
922 // final int insetX = 0;
923 // final int insetY = 0;
924 // control.addListener(SWT.Resize, new Listener() {
926 // public void handleEvent(Event e) {
927 // Rectangle rect = control.getBounds();
928 // control.setBounds(rect.x + insetX, rect.y + insetY, rect.width - insetX * 2, rect.height - insetY * 2);
931 control.addListener(SWT.Dispose, new Listener() {
933 public void handleEvent(Event event) {
934 currentlyModifiedNodes.remove(context);
935 queueSelectionRefresh(context);
936 deactivateEditingContext();
940 if (!(control instanceof Shell)) {
941 editor.setEditor(control, item, columnIndex);
947 GraphExplorerImpl.this.reconfigureTreeEditor(item, columnIndex, control, SWT.DEFAULT, SWT.DEFAULT, 0, 0);
949 activateEditingContext(control);
951 // Removed in disposeListener above
952 currentlyModifiedNodes.add(context);
953 //System.out.println("START CUSTOM EDITING: " + item);
962 void startEnumerationEditing(final TreeItem item, final int columnIndex, final NodeContext context, final EnumerationModifier modifier) {
963 String initialText = modifier.getValue();
964 if (initialText == null)
965 throw new AssertionError("Labeler.Modifier.getValue() returned null");
967 List<String> values = modifier.getValues();
968 String selectedValue = modifier.getValue();
969 int selectedIndex = values.indexOf(selectedValue);
970 if (selectedIndex == -1)
971 throw new AssertionFailedException(modifier + " EnumerationModifier.getValue returned '" + selectedValue + "' which is not among the possible values returned by EnumerationModifier.getValues(): " + values);
973 final CCombo combo = new CCombo(tree, SWT.FLAT | SWT.BORDER | SWT.READ_ONLY | SWT.DROP_DOWN);
974 combo.setVisibleItemCount(10);
975 //combo.setEditable(false);
977 for (String value : values) {
980 combo.select(selectedIndex);
982 Listener comboListener = new Listener() {
983 boolean arrowTraverseUsed = false;
985 public void handleEvent(final Event e) {
986 //System.out.println("FOO: " + e);
989 if (e.character == SWT.CR) {
990 // Commit edit directly on ENTER press.
991 String text = combo.getText();
992 modifier.modify(text);
993 // Item may be disposed if the tree gets reset after a previous editing.
994 if (!item.isDisposed()) {
995 item.setText(columnIndex, text);
996 queueSelectionRefresh(context);
1000 } else if (e.keyCode == SWT.ESC) {
1001 // Cancel editing immediately
1008 if (arrowTraverseUsed) {
1009 arrowTraverseUsed = false;
1013 String text = combo.getText();
1014 modifier.modify(text);
1016 // Item may be disposed if the tree gets reset after a previous editing.
1017 if (!item.isDisposed()) {
1018 item.setText(columnIndex, text);
1019 queueSelectionRefresh(context);
1024 case SWT.FocusOut: {
1025 String text = combo.getText();
1026 modifier.modify(text);
1028 // Item may be disposed if the tree gets reset after a previous editing.
1029 if (!item.isDisposed()) {
1030 item.setText(columnIndex, text);
1031 queueSelectionRefresh(context);
1036 case SWT.Traverse: {
1038 case SWT.TRAVERSE_RETURN:
1039 String text = combo.getText();
1040 modifier.modify(text);
1041 if (!item.isDisposed()) {
1042 item.setText(columnIndex, text);
1043 queueSelectionRefresh(context);
1045 arrowTraverseUsed = false;
1047 case SWT.TRAVERSE_ESCAPE:
1051 case SWT.TRAVERSE_ARROW_NEXT:
1052 case SWT.TRAVERSE_ARROW_PREVIOUS:
1053 arrowTraverseUsed = true;
1056 //System.out.println("unhandled traversal: " + e.detail);
1062 currentlyModifiedNodes.remove(context);
1063 deactivateEditingContext();
1068 combo.addListener(SWT.MouseWheel, VetoingEventHandler.INSTANCE);
1069 combo.addListener(SWT.KeyDown, comboListener);
1070 combo.addListener(SWT.FocusOut, comboListener);
1071 combo.addListener(SWT.Traverse, comboListener);
1072 combo.addListener(SWT.Selection, comboListener);
1073 combo.addListener(SWT.Dispose, comboListener);
1075 editor.setEditor(combo, item, columnIndex);
1078 combo.setListVisible(true);
1080 GraphExplorerImpl.this.reconfigureTreeEditorForText(item, columnIndex, combo, combo.getText(), SWT.DEFAULT, 0, 0);
1082 activateEditingContext(combo);
1084 // Removed in comboListener
1085 currentlyModifiedNodes.add(context);
1087 //System.out.println("START ENUMERATION EDITING: " + item);
1092 * @param columnIndex
1096 void startTextEditing(final TreeItem item, final int columnIndex, final NodeContext context, final Modifier modifier) {
1097 String initialText = modifier.getValue();
1098 if (initialText == null)
1099 throw new AssertionError("Labeler.Modifier.getValue() returned null, modifier=" + modifier);
1101 final Composite composite = new Composite(tree, SWT.NONE);
1102 //composite.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_RED));
1103 final Text text = new Text(composite, SWT.BORDER);
1104 final int insetX = 0;
1105 final int insetY = 0;
1106 composite.addListener(SWT.Resize, new Listener() {
1108 public void handleEvent(Event e) {
1109 Rectangle rect = composite.getClientArea();
1110 text.setBounds(rect.x + insetX, rect.y + insetY, rect.width - insetX * 2, rect.height
1114 final FilteringModifier filter = modifier instanceof FilteringModifier ? (FilteringModifier) modifier : null;
1115 Listener textListener = new Listener() {
1117 boolean modified = false;
1120 public void handleEvent(final Event e) {
1126 //System.out.println("FOCUS OUT " + item);
1127 newText = text.getText();
1128 error = modifier.isValid(newText);
1129 if (error == null) {
1130 modifier.modify(newText);
1132 // Item may be disposed if the tree gets reset after a previous editing.
1133 if (!item.isDisposed()) {
1134 item.setText(columnIndex, newText);
1135 queueSelectionRefresh(context);
1138 // System.out.println("validation error: " + error);
1141 composite.dispose();
1144 newText = text.getText();
1145 error = modifier.isValid(newText);
1146 if (error != null) {
1147 text.setBackground(invalidModificationColor);
1149 //System.out.println("validation error: " + error);
1151 text.setBackground(null);
1158 // Safety check since it seems that this may happen with
1160 if (item.isDisposed())
1163 // Filter input if necessary
1164 e.text = filter != null ? filter.filter(e.text) : e.text;
1166 newText = text.getText();
1167 String leftText = newText.substring(0, e.start);
1168 String rightText = newText.substring(e.end, newText.length());
1169 GraphExplorerImpl.this.reconfigureTreeEditorForText(
1170 item, columnIndex, text, leftText + e.text + rightText,
1171 SWT.DEFAULT, insetX, insetY);
1175 case SWT.TRAVERSE_RETURN:
1177 newText = text.getText();
1178 error = modifier.isValid(newText);
1179 if (error == null) {
1180 modifier.modify(newText);
1181 if (!item.isDisposed()) {
1182 item.setText(columnIndex, newText);
1183 queueSelectionRefresh(context);
1188 case SWT.TRAVERSE_ESCAPE:
1189 composite.dispose();
1193 //System.out.println("unhandled traversal: " + e.detail);
1199 currentlyModifiedNodes.remove(context);
1200 deactivateEditingContext();
1206 // Set the initial text before registering a listener. We do not want immediate modification!
1207 text.setText(initialText);
1208 text.addListener(SWT.FocusOut, textListener);
1209 text.addListener(SWT.Traverse, textListener);
1210 text.addListener(SWT.Verify, textListener);
1211 text.addListener(SWT.Modify, textListener);
1212 text.addListener(SWT.Dispose, textListener);
1213 editor.setEditor(composite, item, columnIndex);
1217 // Initialize TreeEditor properly.
1218 GraphExplorerImpl.this.reconfigureTreeEditorForText(
1219 item, columnIndex, text, initialText,
1220 SWT.DEFAULT, insetX, insetY);
1222 // Removed in textListener
1223 currentlyModifiedNodes.add(context);
1225 activateEditingContext(text);
1227 //System.out.println("START TEXT EDITING: " + item);
1230 protected void errorStatus(String error) {
1231 IStatusLineManager status = getStatusLineManager();
1232 if (status != null) {
1233 status.setErrorMessage(error);
1237 protected IStatusLineManager getStatusLineManager() {
1238 if (serviceLocator instanceof IWorkbenchPart) {
1239 return WorkbenchUtils.getStatusLine((IWorkbenchPart) serviceLocator);
1240 } else if (serviceLocator instanceof IWorkbenchSite) {
1241 return WorkbenchUtils.getStatusLine((IWorkbenchSite) serviceLocator);
1246 protected void activateEditingContext(Control control) {
1247 if (contextService != null) {
1248 editingContext = contextService.activateContext(INLINE_EDITING_UI_CONTEXT);
1250 if (control != null && focusService != null) {
1251 focusService.addFocusTracker(control, INLINE_EDITING_UI_CONTEXT);
1252 // No need to remove the control, it will be
1253 // removed automatically when it is disposed.
1257 protected void deactivateEditingContext() {
1258 IContextActivation a = editingContext;
1260 editingContext = null;
1261 contextService.deactivateContext(a);
1268 void queueSelectionRefresh(NodeContext forContext) {
1269 selectionRefreshContexts.add(forContext);
1273 public String startEditing(NodeContext context, String columnKey_) {
1274 assertNotDisposed();
1275 if (!thread.currentThreadAccess())
1276 throw new IllegalStateException("not in SWT display thread " + thread.getThread());
1278 String columnKey = columnKey_;
1279 if(columnKey.startsWith("#")) {
1280 columnKey = columnKey.substring(1);
1283 Integer columnIndex = columnKeyToIndex.get(columnKey);
1284 if (columnIndex == null)
1285 return "Rename not supported for selection";
1287 TreeItem item = contextToItem.getRight(context);
1289 return "Rename not supported for selection";
1291 return startEditing(item, columnIndex, columnKey_);
1296 public String startEditing(String columnKey) {
1298 ISelection selection = postSelectionProvider.getSelection();
1299 if(selection == null) return "Rename not supported for selection";
1300 NodeContext context = ISelectionUtils.filterSingleSelection(selection, NodeContext.class);
1301 if(context == null) return "Rename not supported for selection";
1303 return startEditing(context, columnKey);
1308 * @param site <code>null</code> if the explorer is detached from the workbench
1309 * @param parent the parent SWT composite
1310 * @param style the tree style to use, check the see tags for the available flags
1315 * @see SWT#FULL_SELECTION
1316 * @see SWT#NO_SCROLL
1320 public GraphExplorerImpl(Composite parent, int style) {
1322 setServiceLocator(null);
1324 this.localResourceManager = new LocalResourceManager(JFaceResources.getResources());
1325 this.resourceManager = new DeviceResourceManager(parent.getDisplay());
1327 this.imageLoaderJob = new ImageLoaderJob(this);
1328 this.imageLoaderJob.setPriority(Job.DECORATE);
1330 invalidModificationColor = (Color) localResourceManager.get( ColorDescriptor.createFrom( new RGB(255, 128, 128) ) );
1332 this.thread = SWTThread.getThreadAccess(parent);
1334 for(int i=0;i<10;i++) explorerContext.activity.push(0);
1336 tree = new Tree(parent, style);
1337 tree.addListener(SWT.SetData, this);
1338 tree.addListener(SWT.Expand, this);
1339 tree.addListener(SWT.Dispose, this);
1340 tree.addListener(SWT.Activate, this);
1342 tree.setData(KEY_GRAPH_EXPLORER, this);
1344 // These are both required for performing column resizing without flicker.
1345 // See SWT.Resize event handling in #handleEvent() for more explanations.
1346 parent.addListener(SWT.Resize, this);
1347 tree.addListener(SWT.Resize, this);
1349 originalFont = JFaceResources.getDefaultFontDescriptor();
1350 // originalBackground = JFaceResources.getColorRegistry().get(symbolicName);
1351 // originalForeground = tree.getForeground();
1353 tree.setFont((Font) localResourceManager.get(originalFont));
1355 columns = new Column[] { new Column(ColumnKeys.SINGLE) };
1356 columnKeyToIndex = Collections.singletonMap(ColumnKeys.SINGLE, 0);
1358 editor = new TreeEditor(tree);
1359 editor.horizontalAlignment = SWT.LEFT;
1360 editor.grabHorizontal = true;
1361 editor.minimumWidth = 50;
1363 setBasicListeners();
1364 setDefaultProcessors();
1366 this.toolTip = new GraphExplorerToolTip(explorerContext, tree);
1370 public IThreadWorkQueue getThread() {
1374 TreeItem previousSingleSelection = null;
1375 long focusGainedAt = Long.MIN_VALUE;
1377 protected GraphExplorerToolTip toolTip;
1379 protected void setBasicListeners() {
1380 // Keep track of the previous single selection to help
1381 // decide whether to start editing a tree node on mouse
1383 tree.addListener(SWT.Selection, new Listener() {
1385 public void handleEvent(Event event) {
1386 TreeItem[] selection = tree.getSelection();
1387 if (selection.length == 1) {
1388 //for (TreeItem item : selection)
1389 // System.out.println("selection: " + item);
1390 previousSingleSelection = selection[0];
1392 previousSingleSelection = null;
1397 // Try to start editing of tree column when clicked for the second time.
1398 Listener mouseEditListener = new Listener() {
1400 Future<?> startEdit = null;
1403 public void handleEvent(Event event) {
1404 if (event.type == SWT.DragDetect) {
1405 // Needed to prevent editing from being started when in fact
1406 // the user starts dragging an item.
1407 //System.out.println("DRAG DETECT: " + event);
1411 //System.out.println("mouse down: " + event);
1412 if (event.button == 1) {
1413 // Always ignore the first mouse button press that focuses
1414 // the control. Do not let it start in-line editing since
1415 // that is very annoying to users and not how the UI's that
1416 // people are used to behave.
1417 long eventTime = ((long) event.time) & 0xFFFFFFFFL;
1418 if ((eventTime - focusGainedAt) < 250L) {
1419 //System.out.println("ignore mouse down " + focusGainedAt + ", " + eventTime + " = " + (eventTime-focusGainedAt));
1422 //System.out.println("testing whether to start editing");
1424 final Point point = new Point(event.x, event.y);
1425 final TreeItem item = tree.getItem(point);
1428 //System.out.println("mouse down @ " + point + ": " + item + ", previous item: " + previousSingleSelection);
1430 // Only start editing if the item was already selected.
1431 if (!item.equals(previousSingleSelection)) {
1436 if (tree.getColumnCount() > 1) {
1437 // TODO: reconsider this logic, might not be good in general.
1438 for (int i = 0; i < tree.getColumnCount(); i++) {
1439 if (item.getBounds(i).contains(point)) {
1440 tryScheduleEdit(event, item, point, 100, i);
1445 //System.out.println("clicks: " + event.count);
1446 if (item.getBounds().contains(point)) {
1447 if (event.count == 1) {
1448 tryScheduleEdit(event, item, point, 500, 0);
1457 void tryScheduleEdit(Event event, final TreeItem item, Point point, long delayMs, final int column) {
1458 //System.out.println("\tCONTAINS: " + item);
1462 //System.out.println("\tScheduling edit: " + item);
1463 startEdit = ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
1466 ThreadUtils.asyncExec(thread, new Runnable() {
1469 if (item.isDisposed())
1471 startEditing(item, column, null);
1475 }, delayMs, TimeUnit.MILLISECONDS);
1478 boolean cancelEdit() {
1479 Future<?> f = startEdit;
1481 // Try to cancel the start edit task if it's not running yet.
1484 boolean ret = f.cancel(false);
1485 //System.out.println("\tCancelled edit: " + ret);
1489 //System.out.println("\tNo edit in progress to cancel");
1493 tree.addListener(SWT.MouseDown, mouseEditListener);
1494 tree.addListener(SWT.DragDetect, mouseEditListener);
1495 tree.addListener(SWT.DragDetect, new Listener() {
1497 public void handleEvent(Event event) {
1498 Point test = new Point(event.x, event.y);
1499 TreeItem item = tree.getItem(test);
1501 for(int i=0;i<tree.getColumnCount();i++) {
1502 Rectangle rect = item.getBounds(i);
1503 if(rect.contains(test)) {
1504 tree.setData(KEY_DRAG_COLUMN, i);
1509 tree.setData(KEY_DRAG_COLUMN, -1);
1512 tree.addListener(SWT.MouseMove, new Listener() {
1514 public void handleEvent(Event event) {
1515 Point test = new Point(event.x, event.y);
1516 TreeItem item = tree.getItem(test);
1518 for(int i=0;i<tree.getColumnCount();i++) {
1519 Rectangle rect = item.getBounds(i);
1520 if(rect.contains(test)) {
1521 transientState.setActiveColumn(i);
1526 transientState.setActiveColumn(null);
1530 // Add focus/mouse/key listeners for supporting the respective
1531 // add/remove listener methods in IGraphExplorer.
1532 tree.addFocusListener(new FocusListener() {
1534 public void focusGained(FocusEvent e) {
1535 focusGainedAt = ((long) e.time) & 0xFFFFFFFFL;
1536 for (FocusListener listener : focusListeners)
1537 listener.focusGained(e);
1540 public void focusLost(FocusEvent e) {
1541 for (FocusListener listener : focusListeners)
1542 listener.focusLost(e);
1545 tree.addMouseListener(new MouseListener() {
1547 public void mouseDoubleClick(MouseEvent e) {
1548 for (MouseListener listener : mouseListeners) {
1549 listener.mouseDoubleClick(e);
1553 public void mouseDown(MouseEvent e) {
1554 for (MouseListener listener : mouseListeners) {
1555 listener.mouseDown(e);
1559 public void mouseUp(MouseEvent e) {
1560 for (MouseListener listener : mouseListeners) {
1561 listener.mouseUp(e);
1565 tree.addKeyListener(new KeyListener() {
1567 public void keyPressed(KeyEvent e) {
1568 for (KeyListener listener : keyListeners) {
1569 listener.keyPressed(e);
1573 public void keyReleased(KeyEvent e) {
1574 for (KeyListener listener : keyListeners) {
1575 listener.keyReleased(e);
1580 // Add a tree selection listener for keeping the selection of
1581 // GraphExplorer's ISelectionProvider up-to-date.
1582 tree.addSelectionListener(new SelectionListener() {
1584 public void widgetDefaultSelected(SelectionEvent e) {
1588 public void widgetSelected(SelectionEvent e) {
1589 widgetSelectionChanged(false);
1593 // This listener takes care of updating the set of currently selected
1594 // TreeItem instances. This set is needed because we need to know in
1595 // #setData(Event) whether the updated item was a part of the current
1596 // selection in which case the selection must be updated.
1597 selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
1599 public void selectionChanged(SelectionChangedEvent event) {
1600 //System.out.println("selection changed: " + event.getSelection());
1601 Set<NodeContext> set = ISelectionUtils.filterSetSelection(event.getSelection(), NodeContext.class);
1602 selectedItems.clear();
1603 for (NodeContext nc : set) {
1604 TreeItem item = contextToItem.getRight(nc);
1606 selectedItems.put(item, nc);
1608 //System.out.println("newly selected items: " + selectedItems);
1614 * Mod count for delaying post selection changed events.
1616 int postSelectionModCount = 0;
1619 * Last tree selection modification time for implementing a quiet
1620 * time for selection changes.
1622 long lastSelectionModTime = System.currentTimeMillis() - 10000;
1625 * Current target time for the selection to be set. Calculated
1626 * according to the set quiet time and last selection modification
1629 long selectionSetTargetTime = 0;
1632 * <code>true</code> if delayed selection runnable is current scheduled or
1635 boolean delayedSelectionScheduled = false;
1637 Runnable SELECTION_DELAY = new Runnable() {
1640 if (tree.isDisposed())
1642 long now = System.currentTimeMillis();
1643 long waitTimeLeft = selectionSetTargetTime - now;
1644 if (waitTimeLeft > 0) {
1645 // Not enough quiet time, reschedule.
1646 delayedSelectionScheduled = true;
1647 tree.getDisplay().timerExec((int) waitTimeLeft, this);
1649 // Time to perform selection, stop rescheduling.
1650 delayedSelectionScheduled = false;
1656 private void widgetSelectionChanged(boolean forceSelectionChange) {
1657 long modTime = System.currentTimeMillis();
1658 long delta = modTime - lastSelectionModTime;
1659 lastSelectionModTime = modTime;
1660 if (!forceSelectionChange && delta < SELECTION_CHANGE_QUIET_TIME) {
1661 long msToWait = SELECTION_CHANGE_QUIET_TIME - delta;
1662 selectionSetTargetTime = modTime + msToWait;
1663 if (!delayedSelectionScheduled) {
1664 delayedSelectionScheduled = true;
1665 tree.getDisplay().timerExec((int) msToWait, SELECTION_DELAY);
1667 // Make sure that post selection change events do not fire.
1668 ++postSelectionModCount;
1672 // Immediate selection reconstruction.
1676 private void resetSelection() {
1677 final ISelection selection = getWidgetSelection();
1679 //System.out.println("resetSelection(" + postSelectionModCount + ")");
1680 //System.out.println(" provider selection: " + selectionProvider.getSelection());
1681 //System.out.println(" widget selection: " + selection);
1683 selectionProvider.setAndFireNonEqualSelection(selection);
1685 // Implement deferred firing of post selection events
1686 final int count = ++postSelectionModCount;
1687 //System.out.println("[" + System.currentTimeMillis() + "] scheduling postSelectionChanged " + count + ": " + selection);
1688 ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
1691 int newCount = postSelectionModCount;
1692 // Don't publish selection yet, there's another change incoming.
1693 //System.out.println("[" + System.currentTimeMillis() + "] checking post selection publish: " + count + " vs. " + newCount + ": " + selection);
1694 if (newCount != count)
1696 //System.out.println("[" + System.currentTimeMillis() + "] " + count + " count equals, firing post selection listeners: " + selection);
1698 if (tree.isDisposed())
1701 //System.out.println("scheduling fire post selection changed: " + selection);
1702 tree.getDisplay().asyncExec(new Runnable() {
1705 if (tree.isDisposed() || selectionProvider == null)
1707 //System.out.println("firing post selection changed: " + selection);
1708 selectionProvider.firePostSelection(selection);
1712 }, POST_SELECTION_DELAY, TimeUnit.MILLISECONDS);
1715 protected void setDefaultProcessors() {
1716 // Add a simple IMAGER query processor that always returns null.
1717 // With this processor no images will ever be shown.
1718 // setPrimitiveProcessor(new StaticImagerProcessor(null));
1720 setProcessor(new DefaultComparableChildrenProcessor());
1721 setProcessor(new DefaultLabelDecoratorsProcessor());
1722 setProcessor(new DefaultImageDecoratorsProcessor());
1723 setProcessor(new DefaultSelectedLabelerProcessor());
1724 setProcessor(new DefaultLabelerFactoriesProcessor());
1725 setProcessor(new DefaultSelectedImagerProcessor());
1726 setProcessor(new DefaultImagerFactoriesProcessor());
1727 setPrimitiveProcessor(new DefaultLabelerProcessor());
1728 setPrimitiveProcessor(new DefaultCheckedStateProcessor());
1729 setPrimitiveProcessor(new DefaultImagerProcessor());
1730 setPrimitiveProcessor(new DefaultLabelDecoratorProcessor());
1731 setPrimitiveProcessor(new DefaultImageDecoratorProcessor());
1732 setPrimitiveProcessor(new NoSelectionRequestProcessor());
1734 setProcessor(new DefaultFinalChildrenProcessor(this));
1736 setProcessor(new DefaultPrunedChildrenProcessor());
1737 setProcessor(new DefaultSelectedViewpointProcessor());
1738 setProcessor(new DefaultSelectedLabelDecoratorFactoriesProcessor());
1739 setProcessor(new DefaultSelectedImageDecoratorFactoriesProcessor());
1740 setProcessor(new DefaultViewpointContributionsProcessor());
1742 setPrimitiveProcessor(new DefaultViewpointProcessor());
1743 setPrimitiveProcessor(new DefaultViewpointContributionProcessor());
1744 setPrimitiveProcessor(new DefaultSelectedViewpointFactoryProcessor());
1745 setPrimitiveProcessor(new DefaultIsExpandedProcessor());
1746 setPrimitiveProcessor(new DefaultShowMaxChildrenProcessor());
1750 public <T> void setProcessor(NodeQueryProcessor<T> processor) {
1751 assertNotDisposed();
1752 if (processor == null)
1753 throw new IllegalArgumentException("null processor");
1755 processors.put(processor.getIdentifier(), processor);
1759 public <T> void setPrimitiveProcessor(PrimitiveQueryProcessor<T> processor) {
1760 assertNotDisposed();
1761 if (processor == null)
1762 throw new IllegalArgumentException("null processor");
1764 PrimitiveQueryProcessor<?> oldProcessor = primitiveProcessors.put(processor.getIdentifier(), processor);
1766 if (oldProcessor instanceof ProcessorLifecycle)
1767 ((ProcessorLifecycle) oldProcessor).detached(this);
1768 if (processor instanceof ProcessorLifecycle)
1769 ((ProcessorLifecycle) processor).attached(this);
1773 public <T> void setDataSource(DataSource<T> provider) {
1774 assertNotDisposed();
1775 if (provider == null)
1776 throw new IllegalArgumentException("null provider");
1777 dataSources.put(provider.getProvidedClass(), provider);
1780 @SuppressWarnings("unchecked")
1782 public <T> DataSource<T> removeDataSource(Class<T> forProvidedClass) {
1783 assertNotDisposed();
1784 if (forProvidedClass == null)
1785 throw new IllegalArgumentException("null class");
1786 return dataSources.remove(forProvidedClass);
1790 public void setPersistor(StatePersistor persistor) {
1791 this.persistor = persistor;
1795 public SelectionDataResolver getSelectionDataResolver() {
1796 return selectionDataResolver;
1800 public void setSelectionDataResolver(SelectionDataResolver r) {
1801 this.selectionDataResolver = r;
1805 public SelectionFilter getSelectionFilter() {
1806 return selectionFilter;
1810 public void setSelectionFilter(SelectionFilter f) {
1811 this.selectionFilter = f;
1812 // TODO: re-filter current selection?
1816 public void setSelectionTransformation(BiFunction<GraphExplorer, Object[], Object[]> f) {
1817 this.selectionTransformation = f;
1821 public <T> void addListener(T listener) {
1822 if(listener instanceof FocusListener) {
1823 focusListeners.add((FocusListener)listener);
1824 } else if(listener instanceof MouseListener) {
1825 mouseListeners.add((MouseListener)listener);
1826 } else if(listener instanceof KeyListener) {
1827 keyListeners.add((KeyListener)listener);
1832 public <T> void removeListener(T listener) {
1833 if(listener instanceof FocusListener) {
1834 focusListeners.remove(listener);
1835 } else if(listener instanceof MouseListener) {
1836 mouseListeners.remove(listener);
1837 } else if(listener instanceof KeyListener) {
1838 keyListeners.remove(listener);
1842 public void addSelectionListener(SelectionListener listener) {
1843 tree.addSelectionListener(listener);
1846 public void removeSelectionListener(SelectionListener listener) {
1847 tree.removeSelectionListener(listener);
1850 private Set<String> uiContexts;
1853 public void setUIContexts(Set<String> contexts) {
1854 this.uiContexts = contexts;
1858 public void setRoot(final Object root) {
1859 if(uiContexts != null && uiContexts.size() == 1)
1860 setRootContext0(NodeContextBuilder.buildWithData(BuiltinKeys.INPUT, root, BuiltinKeys.UI_CONTEXT, uiContexts.iterator().next()));
1862 setRootContext0(NodeContextBuilder.buildWithData(BuiltinKeys.INPUT, root));
1866 public void setRootContext(final NodeContext context) {
1867 setRootContext0(context);
1870 private void setRootContext0(final NodeContext context) {
1871 Assert.isNotNull(context, "root must not be null");
1872 if (isDisposed() || tree.isDisposed())
1874 Display display = tree.getDisplay();
1875 if (display.getThread() == Thread.currentThread()) {
1878 display.asyncExec(new Runnable() {
1887 private void initializeState() {
1888 if (persistor == null)
1891 ExplorerState state = persistor.deserialize(
1892 Platform.getStateLocation(Activator.getDefault().getBundle()).toFile(),
1895 // topNodeToSet will be processed by #setData when it encounters a
1896 // NodeContext that matches this one.
1897 // topNodePath = state.topNodePath;
1898 // topNodePathChildIndex = state.topNodePathChildIndex;
1899 // currentTopNodePathIndex = 0;
1901 Object processor = getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
1902 if (processor instanceof DefaultIsExpandedProcessor) {
1903 DefaultIsExpandedProcessor isExpandedProcessor = (DefaultIsExpandedProcessor)processor;
1904 for(NodeContext expanded : state.expandedNodes) {
1905 isExpandedProcessor.setExpanded(expanded, true);
1910 private void saveState() {
1911 if (persistor == null)
1914 NodeContext[] topNodePath = NodeContext.NONE;
1915 int[] topNodePathChildIndex = {};
1916 Collection<NodeContext> expandedNodes = Collections.emptyList();
1917 Map<String, Integer> columnWidths = Collections.<String, Integer> emptyMap();
1919 // Resolve top node path
1920 TreeItem topItem = tree.getTopItem();
1921 if (topItem != null) {
1922 NodeContext topNode = (NodeContext) topItem.getData();
1923 if (topNode != null) {
1924 topNodePath = getNodeContextPathSegments(topNode);
1925 topNodePathChildIndex = new int[topNodePath.length];
1926 for (int i = 0; i < topNodePath.length; ++i) {
1927 // TODO: get child indexes
1928 topNodePathChildIndex[i] = 0;
1933 // Resolve expanded nodes
1934 Object processor = getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
1935 if (processor instanceof IsExpandedProcessor) {
1936 IsExpandedProcessor isExpandedProcessor = (IsExpandedProcessor) processor;
1937 expandedNodes = isExpandedProcessor.getExpanded();
1941 TreeColumn[] columns = tree.getColumns();
1942 if (columns.length > 1) {
1943 columnWidths = new HashMap<String, Integer>();
1944 for (int i = 0; i < columns.length; ++i) {
1945 columnWidths.put(columns[i].getText(), columns[i].getWidth());
1949 persistor.serialize(
1950 Platform.getStateLocation(Activator.getDefault().getBundle()).toFile(),
1952 new ExplorerState(topNodePath, topNodePathChildIndex, expandedNodes, columnWidths));
1956 * Invoke only from SWT thread to reset the root of the graph explorer tree.
1960 private void doSetRoot(NodeContext root) {
1961 if (tree.isDisposed())
1963 if (root.getConstant(BuiltinKeys.INPUT) == null) {
1964 ErrorLogger.defaultLogError("root node context does not contain BuiltinKeys.INPUT key. Node = " + root, new Exception("trace"));
1968 // Empty caches, release queries.
1969 GraphExplorerContext oldContext = explorerContext;
1970 GraphExplorerContext newContext = new GraphExplorerContext();
1971 GENodeQueryManager manager = new GENodeQueryManager(newContext, null, null, TreeItemReference.create(null));
1972 this.explorerContext = newContext;
1973 oldContext.safeDispose();
1974 toolTip.setGraphExplorerContext(explorerContext);
1976 // Need to empty these or otherwise they won't be emptied until the
1977 // explorer is disposed which would mean that many unwanted references
1978 // will be held by this map.
1979 clearPrimitiveProcessors();
1981 this.rootContext = root.getConstant(BuiltinKeys.IS_ROOT) != null ? root
1982 : NodeContextUtil.withConstant(root, BuiltinKeys.IS_ROOT, Boolean.TRUE);
1984 explorerContext.getCache().incRef(this.rootContext);
1988 NodeContext[] contexts = manager.query(rootContext, BuiltinKeys.FINAL_CHILDREN);
1990 tree.setItemCount(contexts.length);
1992 select(rootContext);
1993 refreshColumnSizes();
1997 public NodeContext getRoot() {
2002 public NodeContext getParentContext(NodeContext context) {
2004 throw new IllegalStateException("disposed");
2005 if (!thread.currentThreadAccess())
2006 throw new IllegalStateException("not in SWT display thread " + thread.getThread());
2008 TreeItem item = contextToItem.getRight(context);
2009 if(item == null) return null;
2010 TreeItem parentItem = item.getParentItem();
2011 if(parentItem == null) return null;
2012 return (NodeContext)parentItem.getData();
2015 Point previousTreeSize;
2016 Point previousTreeParentSize;
2017 boolean activatedBefore = false;
2020 public void handleEvent(Event event) {
2021 //System.out.println("EVENT: " + event);
2022 switch(event.type) {
2024 //System.out.println("EXPAND: " + event.item);
2025 if ((tree.getStyle() & SWT.VIRTUAL) != 0) {
2026 expandVirtual(event);
2028 System.out.println("TODO: non-virtual tree item expand");
2032 // Only invoked for SWT.VIRTUAL trees
2034 // Happened for Hannu once during program startup.
2035 // java.lang.AssertionError
2036 // at org.simantics.browsing.ui.common.internal.GENodeQueryManager.query(GENodeQueryManager.java:190)
2037 // at org.simantics.browsing.ui.swt.GraphExplorerImpl.setData(GraphExplorerImpl.java:2315)
2038 // at org.simantics.browsing.ui.swt.GraphExplorerImpl.handleEvent(GraphExplorerImpl.java:2039)
2039 // I do not know whether SWT guarantees that SetData events
2040 // don't come after Dispose event has been issued, but I
2041 // think its better to have this check here just incase.
2046 // This ensures that column sizes are refreshed at
2047 // least once when the GE is first shown.
2048 if (!activatedBefore) {
2049 refreshColumnSizes();
2050 activatedBefore = true;
2054 //new Exception().printStackTrace();
2061 if (event.widget == tree) {
2062 // This case is meant for listening to tree width increase.
2063 // The column resizing must be performed only after the tree
2064 // itself as been resized.
2065 Point size = tree.getSize();
2067 if (previousTreeSize != null) {
2068 dx = size.x - previousTreeSize.x;
2070 previousTreeSize = size;
2071 //System.out.println("RESIZE: " + dx + " - size=" + size);
2074 tree.setRedraw(false);
2075 refreshColumnSizes(size);
2076 tree.setRedraw(true);
2078 } else if (event.widget == tree.getParent()) {
2079 // This case is meant for listening to tree width decrease.
2080 // The columns must be resized before the tree widget itself
2081 // is resized to prevent scroll bar flicker. This can be achieved
2082 // by listening to the resize events of the tree parent widget.
2083 Composite parent = tree.getParent();
2084 Point size = parent.getSize();
2086 // We must subtract the parent's border and possible
2087 // scroll bar width from the new target width of the columns.
2088 size.x -= tree.getParent().getBorderWidth() * 2;
2089 ScrollBar vBar = parent.getVerticalBar();
2090 if (vBar != null && vBar.isVisible())
2091 size.x -= vBar.getSize().x;
2094 if (previousTreeParentSize != null) {
2095 dx = size.x - previousTreeParentSize.x;
2097 previousTreeParentSize = size;
2098 //System.out.println("RESIZE: " + dx + " - size=" + size);
2101 tree.setRedraw(false);
2102 refreshColumnSizes(size);
2103 tree.setRedraw(true);
2113 protected void refreshColumnSizes() {
2114 // Composite treeParent = tree.getParent();
2115 // Point size = treeParent.getSize();
2116 // size.x -= treeParent.getBorderWidth() * 2;
2117 Point size = tree.getSize();
2118 refreshColumnSizes(size);
2119 tree.getParent().layout();
2123 * This has been disabled since the logic of handling column widths has been
2124 * externalized to parties creating {@link GraphExplorerImpl} instances.
2126 protected void refreshColumnSizes(Point toSize) {
2128 refreshingColumnSizes = true;
2130 int columnCount = tree.getColumnCount();
2131 if (columnCount > 0) {
2132 Point size = toSize;
2133 int targetWidth = size.x - tree.getBorderWidth() * 2;
2136 // Take the vertical scroll bar existence into to account when
2137 // calculating the overflow column width.
2138 ScrollBar vBar = tree.getVerticalBar();
2139 //if (vBar != null && vBar.isVisible())
2141 targetWidth -= vBar.getSize().x;
2143 List<TreeColumn> resizing = new ArrayList<TreeColumn>();
2145 int resizingWidth = 0;
2146 int totalWeight = 0;
2147 for (int i = 0; i < columnCount - 1; ++i) {
2148 TreeColumn col = tree.getColumn(i);
2149 //System.out.println(" " + col.getText() + ": " + col.getWidth());
2150 int width = col.getWidth();
2152 Column c = (Column) col.getData();
2155 resizingWidth += width;
2156 totalWeight += c.getWeight();
2160 int requiredWidthAdjustment = targetWidth - usedWidth;
2161 if (requiredWidthAdjustment < 0)
2162 requiredWidthAdjustment = Math.min(requiredWidthAdjustment, -resizing.size());
2163 double diff = requiredWidthAdjustment;
2164 //System.out.println("REQUIRED WIDTH ADJUSTMENT: " + requiredWidthAdjustment);
2166 // Decide how much to give space to / take space from each grabbing column
2167 double wrel = 1.0 / resizing.size();
2169 double[] weightedShares = new double[resizing.size()];
2170 for (int i = 0; i < resizing.size(); ++i) {
2171 TreeColumn col = resizing.get(i);
2172 Column c = (Column) col.getData();
2173 if (totalWeight == 0) {
2174 weightedShares[i] = wrel;
2176 weightedShares[i] = (double) c.getWeight() / (double) totalWeight;
2179 //System.out.println("grabbing columns:" + resizing);
2180 //System.out.println("weighted space distribution: " + Arrays.toString(weightedShares));
2182 // Always shrink the columns if necessary, but don't enlarge before
2183 // there is sufficient space to at least give all resizable columns
2185 if (diff < 0 || (diff > 0 && diff > resizing.size())) {
2186 // Need to either shrink or enlarge the resizable columns if possible.
2187 for (int i = 0; i < resizing.size(); ++i) {
2188 TreeColumn col = resizing.get(i);
2189 Column c = (Column) col.getData();
2190 int cw = col.getWidth();
2191 //double wrel = (double) cw / (double) resizingWidth;
2192 //int delta = Math.min((int) Math.round(wrel * diff), requiredWidthAdjustment);
2193 double ddelta = weightedShares[i] * diff;
2196 delta = (int) Math.floor(ddelta);
2198 delta = Math.min((int) Math.floor(ddelta), requiredWidthAdjustment);
2200 //System.out.println("size delta(" + col.getText() + "): " + ddelta + " => " + delta);
2201 //System.out.println("argh(" + col.getText() + "): " + c.getWidth() + " vs. " + col.getWidth() + " vs. " + (cw+delta));
2202 int newWidth = Math.max(c.getWidth(), cw + delta);
2203 requiredWidthAdjustment -= (newWidth - cw);
2204 col.setWidth(newWidth);
2208 //System.out.println("FILLER WIDTH LEFT: " + requiredWidthAdjustment);
2210 TreeColumn last = tree.getColumn(columnCount - 1);
2211 // HACK: see #setColumns for why this is here.
2212 if (FILLER.equals(last.getText())) {
2213 last.setWidth(Math.max(0, requiredWidthAdjustment));
2217 refreshingColumnSizes = false;
2222 private void doDispose() {
2223 explorerContext.dispose();
2225 // No longer necessary, the used executors are shared.
2226 //scheduler.shutdown();
2227 //scheduler2.shutdown();
2230 detachPrimitiveProcessors();
2231 primitiveProcessors.clear();
2232 dataSources.clear();
2234 pendingItems.clear();
2238 contextToItem.clear();
2240 mouseListeners.clear();
2242 selectionProvider.clearListeners();
2243 selectionProvider = null;
2244 selectionDataResolver = null;
2245 selectionRefreshContexts.clear();
2246 selectedItems.clear();
2247 originalFont = null;
2249 localResourceManager.dispose();
2251 // Must shutdown image loader job before disposing its ResourceManager
2252 imageLoaderJob.dispose();
2253 imageLoaderJob.cancel();
2255 imageLoaderJob.join();
2256 } catch (InterruptedException e) {
2257 ErrorLogger.defaultLogError(e);
2259 resourceManager.dispose();
2261 postSelectionProvider.dispose();
2265 private void expandVirtual(final Event event) {
2266 TreeItem item = (TreeItem) event.item;
2267 assert (item != null);
2268 NodeContext context = (NodeContext) item.getData();
2269 assert (context != null);
2271 GENodeQueryManager manager = new GENodeQueryManager(this.explorerContext, null, null, TreeItemReference.create(item));
2272 NodeContext[] children = manager.query(context, BuiltinKeys.FINAL_CHILDREN);
2273 int maxChildren = getMaxChildren(manager, context);
2274 item.setItemCount(children.length < maxChildren ? children.length : maxChildren);
2277 private NodeContext getNodeContext(TreeItem item) {
2278 assert(item != null);
2280 NodeContext context = (NodeContext)item.getData();
2281 assert(context != null);
2286 private NodeContext getParentContext(TreeItem item) {
2287 TreeItem parentItem = item.getParentItem();
2288 if(parentItem != null) {
2289 return getNodeContext(parentItem);
2295 private static final String LISTENER_SET_INDICATOR = "LSI";
2296 private static final String PENDING = "PENDING";
2297 private int contextSelectionChangeModCount = 0;
2300 * Only invoked for SWT.VIRTUAL widgets.
2304 private void setData(final Event event) {
2305 assert (event != null);
2306 TreeItem item = (TreeItem) event.item;
2307 assert (item != null);
2309 // Based on experience it seems to be possible that
2310 // SetData events are sent for disposed TreeItems.
2311 if (item.isDisposed() || item.getData(PENDING) != null)
2314 //System.out.println("GE.SetData " + item);
2316 GENodeQueryManager manager = new GENodeQueryManager(this.explorerContext, null, null, TreeItemReference.create(item.getParentItem()));
2318 NodeContext parentContext = getParentContext(item);
2319 assert (parentContext != null);
2321 NodeContext[] parentChildren = manager.query(parentContext, BuiltinKeys.FINAL_CHILDREN);
2323 // Some children have disappeared since counting
2324 if (event.index < 0) {
2325 ErrorLogger.defaultLogError("GraphExplorer.setData: how can event.index be < 0: " + event.index + " ??", new Exception());
2328 if (event.index >= parentChildren.length)
2331 NodeContext context = parentChildren[event.index];
2332 assert (context != null);
2333 item.setData(context);
2335 // Manage NodeContext -> TreeItem mappings
2336 contextToItem.map(context, item);
2337 if (item.getData(LISTENER_SET_INDICATOR) == null) {
2338 // This "if" exists because setData will get called many
2339 // times for the same (NodeContext, TreeItem) pairs.
2340 // Each TreeItem only needs one listener, but this
2341 // is needed to tell whether it already has a listener
2343 item.setData(LISTENER_SET_INDICATOR, LISTENER_SET_INDICATOR);
2344 item.addListener(SWT.Dispose, itemDisposeListener);
2347 boolean isExpanded = manager.query(context, BuiltinKeys.IS_EXPANDED);
2349 PrunedChildrenResult children = manager.query(context, BuiltinKeys.PRUNED_CHILDREN);
2350 int maxChildren = getMaxChildren(manager, context);
2351 //item.setItemCount(children.getPrunedChildren().length < maxChildren ? children.getPrunedChildren().length : maxChildren);
2353 NodeContext[] pruned = children.getPrunedChildren();
2354 int count = Math.min(pruned.length, maxChildren);
2356 if (isExpanded || item.getItemCount() > 1) {
2357 item.setItemCount(count);
2358 TreeItem[] childItems = item.getItems();
2359 for(int i=0;i<count;i++)
2360 contextToItem.map(pruned[i], childItems[i]);
2362 if (children.getPrunedChildren().length == 0) {
2363 item.setItemCount(0);
2365 // item.setItemCount(1);
2366 item.setItemCount(count);
2367 TreeItem[] childItems = item.getItems();
2368 for(int i=0;i<count;i++)
2369 contextToItem.map(pruned[i], childItems[i]);
2370 // item.getItem(0).setData(PENDING, PENDING);
2371 // item.getItem(0).setItemCount(o);
2375 setTextAndImage(item, manager, context, event.index);
2377 // Check if the node should be auto-expanded?
2378 if ((autoExpandLevel == ALL_LEVELS || autoExpandLevel > 1) && !isExpanded) {
2379 //System.out.println("NOT EXPANDED(" +context + ", " + item + ")");
2380 int level = getTreeItemLevel(item);
2381 if ((autoExpandLevel == ALL_LEVELS || level <= autoExpandLevel)
2382 && !explorerContext.autoExpanded.containsKey(context))
2384 //System.out.println("AUTO-EXPANDING(" + context + ", " + item + ")");
2385 explorerContext.autoExpanded.put(context, Boolean.TRUE);
2386 setExpanded(context, true);
2390 item.setExpanded(isExpanded);
2392 if ((tree.getStyle() & SWT.CHECK) != 0) {
2393 CheckedState checked = manager.query(context, BuiltinKeys.IS_CHECKED);
2394 item.setChecked(CheckedState.CHECKED_STATES.contains(checked));
2395 item.setGrayed(CheckedState.GRAYED == checked);
2398 //System.out.println("GE.SetData completed " + item);
2400 // This test makes sure that selectionProvider holds the correct
2401 // selection with respect to the actual selection stored by the virtual
2403 // The data items shown below the items occupied by the selected and now removed data
2404 // will be squeezed to use the tree items previously used for the now
2405 // removed data. When this happens, the NodeContext items stored by the
2406 // tree items will be different from what the GraphExplorer's
2407 // ISelectionProvider thinks the selection currently is. To compensate,
2408 // 1. Recognize the situation
2409 // 2. ASAP set the selection provider selection to what is actually
2410 // offered by the tree widget.
2411 NodeContext selectedContext = selectedItems.get(item);
2412 // System.out.println("selectedContext(" + item + "): " + selectedContext);
2413 if (selectedContext != null && !selectedContext.equals(context)) {
2414 final int modCount = ++contextSelectionChangeModCount;
2415 // System.out.println("SELECTION MUST BE UPDATED (modCount=" + modCount + "): " + item);
2416 // System.out.println(" old context: " + selectedContext);
2417 // System.out.println(" new context: " + context);
2418 // System.out.println(" provider selection: " + selectionProvider.getSelection());
2419 // System.out.println(" widget selection: " + getWidgetSelection());
2420 ThreadUtils.asyncExec(thread, new Runnable() {
2425 int count = contextSelectionChangeModCount;
2426 // System.out.println("MODCOUNT: " + modCount + " vs. " + count);
2427 if (modCount != count)
2429 widgetSelectionChanged(true);
2434 // This must be done to keep the visible tree selection properly
2435 // in sync with the selectionProvider JFace proxy of this class in
2436 // cases where an in-line editor was previously active for the node
2438 if (selectionRefreshContexts.remove(context)) {
2439 final ISelection currentSelection = selectionProvider.getSelection();
2440 // asyncExec is here to prevent ui glitches that
2441 // seem to occur if the selection setting is done
2442 // directly here in between setData invocations.
2443 ThreadUtils.asyncExec(thread, new Runnable() {
2448 // System.out.println("REFRESHING SELECTION: " + currentSelection);
2449 // System.out.println("BEFORE setSelection: " + Arrays.toString(tree.getSelection()));
2450 // System.out.println("BEFORE setSelection: " + selectionProvider.getSelection());
2451 setSelection(currentSelection, true);
2452 // System.out.println("AFTER setSelection: " + Arrays.toString(tree.getSelection()));
2453 // System.out.println("AFTER setSelection: " + selectionProvider.getSelection());
2458 // TODO: doesn't work if any part of the node path that should be
2459 // revealed is out of view.
2460 // Disabled until a better solution is devised.
2461 // Suggestion: include item indexes into the stored node context path
2462 // to make it possible for this method to know whether the current
2463 // node path segment is currently out of view based on event.index.
2464 // If out of view, this code needs to scroll the view programmatically
2466 // if (currentTopNodePathIndex >= 0 && topNodePath.length > 0) {
2467 // NodeContext topNode = topNodePath[currentTopNodePathIndex];
2468 // if (topNode.equals(context)) {
2469 // final TreeItem topItem = item;
2470 // ++currentTopNodePathIndex;
2471 // if (currentTopNodePathIndex >= topNodePath.length) {
2472 // // Mission accomplished. End search for top node here.
2473 // topNodePath = NodeContext.NONE;
2474 // currentTopNodePathIndex = -1;
2476 // ThreadUtils.asyncExec(thread, new Runnable() {
2478 // public void run() {
2479 // if (isDisposed())
2481 // tree.setTopItem(topItem);
2487 // Check if vertical scroll bar has become visible and refresh layout.
2488 ScrollBar verticalBar = tree.getVerticalBar();
2489 if(verticalBar != null) {
2490 boolean currentlyVerticalBarVisible = verticalBar.isVisible();
2491 if (verticalBarVisible != currentlyVerticalBarVisible) {
2492 verticalBarVisible = currentlyVerticalBarVisible;
2493 Composite parent = tree.getParent();
2501 * @return see {@link GraphExplorer#setAutoExpandLevel(int)} for how the
2502 * return value is calculated. Items without parents have level=2,
2503 * their children level=3, etc. Returns 0 for invalid items
2505 private int getTreeItemLevel(TreeItem item) {
2509 for (TreeItem parent = item; parent != null; parent = parent.getParentItem(), ++level);
2510 //System.out.println("\tgetTreeItemLevel(" + parent + ")");
2511 //System.out.println("level(" + item + "): " + level);
2519 private NodeContext[] getNodeContextPathSegments(NodeContext node) {
2520 TreeItem item = contextToItem.getRight(node);
2522 return NodeContext.NONE;
2523 int level = getTreeItemLevel(item);
2525 return NodeContext.NONE;
2526 // Exclude root from the saved node path.
2528 NodeContext[] segments = new NodeContext[level];
2529 for (TreeItem parent = item; parent != null; parent = parent.getParentItem(), --level) {
2530 NodeContext ctx = (NodeContext) item.getData();
2532 return NodeContext.NONE;
2533 segments[level-1] = ctx;
2542 @SuppressWarnings("unused")
2543 private NodeContextPath getNodeContextPath(NodeContext node) {
2544 NodeContext[] path = getNodeContextPathSegments(node);
2545 return new NodeContextPath(path);
2548 void setImage(NodeContext node, TreeItem item, Imager imager, Collection<ImageDecorator> decorators, int itemIndex) {
2549 Image[] images = columnImageArray;
2550 Arrays.fill(images, null);
2551 if (imager == null) {
2552 item.setImage(images);
2556 Object[] descOrImage = columnDescOrImageArray;
2557 Arrays.fill(descOrImage, null);
2558 boolean finishLoadingInJob = false;
2560 for (Column column : columns) {
2561 String key = column.getKey();
2562 ImageDescriptor desc = imager.getImage(key);
2564 // Attempt to decorate the label
2565 if (!decorators.isEmpty()) {
2566 for (ImageDecorator id : decorators) {
2567 ImageDescriptor ds = id.decorateImage(desc, key, itemIndex);
2573 // Try resolving only cached images here and now
2574 Object img = localResourceManager.find(desc);
2576 img = resourceManager.find(desc);
2578 images[index] = img != null ? (Image) img : null;
2579 descOrImage[index] = img == null ? desc : img;
2580 finishLoadingInJob |= img == null;
2585 // Finish loading the final image in the image loader job if necessary.
2586 if (finishLoadingInJob) {
2587 // Prevent UI from flashing unnecessarily by reusing the old image
2588 // in the item if it exists.
2589 for (int c = 0; c < columns.length; ++c) {
2590 Image img = item.getImage(c);
2594 item.setImage(images);
2596 // Schedule loading to another thread to refrain from blocking
2597 // the UI with database operations.
2598 queueImageTask(item, new ImageTask(
2601 Arrays.copyOf(descOrImage, descOrImage.length)));
2603 // Set any images that were resolved.
2604 item.setImage(images);
2608 private void queueImageTask(TreeItem item, ImageTask task) {
2609 synchronized (imageTasks) {
2610 imageTasks.put(item, task);
2612 imageLoaderJob.scheduleIfNecessary(100);
2616 * Invoked in a job worker thread.
2619 * @see ImageLoaderJob
2622 protected IStatus setPendingImages(IProgressMonitor monitor) {
2623 ImageTask[] tasks = null;
2624 synchronized (imageTasks) {
2625 tasks = imageTasks.values().toArray(new ImageTask[imageTasks.size()]);
2628 if (tasks.length == 0)
2629 return Status.OK_STATUS;
2631 MultiStatus status = null;
2633 // Load missing images
2634 for (ImageTask task : tasks) {
2635 Object[] descs = task.descsOrImages;
2636 for (int i = 0; i < descs.length; ++i) {
2637 Object obj = descs[i];
2638 if (obj instanceof ImageDescriptor) {
2639 ImageDescriptor desc = (ImageDescriptor) obj;
2641 descs[i] = resourceManager.get((ImageDescriptor) desc);
2642 } catch (DeviceResourceException e) {
2644 status = new MultiStatus(Activator.PLUGIN_ID, 0, "Problems loading images:", null);
2645 status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Image descriptor loading failed: " + desc, e));
2651 // Perform final UI updates in the UI thread.
2652 final ImageTask[] _tasks = tasks;
2653 thread.asyncExec(new Runnable() {
2656 if (!tree.isDisposed()) {
2657 tree.setRedraw(false);
2659 tree.setRedraw(true);
2664 return status != null ? status : Status.OK_STATUS;
2668 * Invoked in the UI thread only.
2672 void setImages(ImageTask[] tasks) {
2673 for (ImageTask task : tasks)
2679 * Invoked in the UI thread only.
2683 void setImage(ImageTask task) {
2684 // Be sure not to process disposed items.
2685 if (task.item.isDisposed())
2687 // Discard this task if the TreeItem has switched owning NodeContext.
2688 if (!contextToItem.contains(task.node, task.item))
2691 Object[] descs = task.descsOrImages;
2692 Image[] images = columnImageArray;
2693 Arrays.fill(images, null);
2694 for (int i = 0; i < descs.length; ++i) {
2695 Object desc = descs[i];
2696 if (desc instanceof Image) {
2697 images[i] = (Image) desc;
2700 task.item.setImage(images);
2703 void setText(TreeItem item, Labeler labeler, Collection<LabelDecorator> decorators, int itemIndex) {
2704 if (labeler != null) {
2705 String[] texts = new String[columns.length];
2707 Map<String, String> labels = labeler.getLabels();
2708 Map<String, String> runtimeLabels = labeler.getRuntimeLabels();
2709 for (Column column : columns) {
2710 String key = column.getKey();
2712 if (runtimeLabels != null) s = runtimeLabels.get(key);
2713 if (s == null) s = labels.get(key);
2715 FontDescriptor font = originalFont;
2716 ColorDescriptor bg = originalBackground;
2717 ColorDescriptor fg = originalForeground;
2719 // Attempt to decorate the label
2720 if (!decorators.isEmpty()) {
2721 for (LabelDecorator ld : decorators) {
2722 String ds = ld.decorateLabel(s, key, itemIndex);
2726 FontDescriptor dfont = ld.decorateFont(font, key, itemIndex);
2730 ColorDescriptor dbg = ld.decorateBackground(bg, key, itemIndex);
2734 ColorDescriptor dfg = ld.decorateForeground(fg, key, itemIndex);
2740 if (font != originalFont) {
2741 //System.out.println("set font: " + index + ": " + font);
2742 item.setFont(index, (Font) localResourceManager.get(font));
2744 if (bg != originalBackground)
2745 item.setBackground(index, (Color) localResourceManager.get(bg));
2746 if (fg != originalForeground)
2747 item.setForeground(index, (Color) localResourceManager.get(fg));
2753 item.setText(texts);
2755 item.setText(Labeler.NO_LABEL);
2759 void setTextAndImage(TreeItem item, NodeQueryManager manager, NodeContext context, int itemIndex) {
2760 Labeler labeler = manager.query(context, BuiltinKeys.SELECTED_LABELER);
2761 if (labeler != null) {
2762 labeler.setListener(labelListener);
2764 Imager imager = manager.query(context, BuiltinKeys.SELECTED_IMAGER);
2765 Collection<LabelDecorator> labelDecorators = manager.query(context, BuiltinKeys.LABEL_DECORATORS);
2766 Collection<ImageDecorator> imageDecorators = manager.query(context, BuiltinKeys.IMAGE_DECORATORS);
2768 setText(item, labeler, labelDecorators, itemIndex);
2769 setImage(context, item, imager, imageDecorators, itemIndex);
2773 public void setFocus() {
2778 public <T> T query(NodeContext context, CacheKey<T> key) {
2779 return this.explorerContext.cache.get(context, key);
2783 public boolean isDisposed() {
2787 protected void assertNotDisposed() {
2789 throw new IllegalStateException("disposed");
2796 * @param forceControlUpdate
2799 public void setSelection(final ISelection selection, boolean forceControlUpdate) {
2800 assertNotDisposed();
2801 boolean equalsOld = selectionProvider.selectionEquals(selection);
2802 if (equalsOld && !forceControlUpdate) {
2803 // Just set the selection object instance, fire no events nor update
2804 // the viewer selection.
2805 selectionProvider.setSelection(selection);
2807 // Schedule viewer and selection update if necessary.
2808 if (tree.isDisposed())
2810 Display d = tree.getDisplay();
2811 if (d.getThread() == Thread.currentThread()) {
2812 updateSelectionToControl(selection);
2814 d.asyncExec(new Runnable() {
2817 if (tree.isDisposed())
2819 updateSelectionToControl(selection);
2827 /* Contains the best currently found tree item and its priority
2829 private static class SelectionResolutionStatus {
2830 int bestPriority = Integer.MAX_VALUE;
2838 private void updateSelectionToControl(ISelection selection) {
2839 if (selectionDataResolver == null)
2841 if (!(selection instanceof IStructuredSelection))
2844 // Initialize selection resolution status map
2845 IStructuredSelection iss = (IStructuredSelection) selection;
2846 final THashMap<Object,SelectionResolutionStatus> statusMap =
2847 new THashMap<Object,SelectionResolutionStatus>(iss.size());
2848 for(Iterator<?> it = iss.iterator(); it.hasNext();) {
2849 Object selectionElement = it.next();
2850 Object resolvedElement = selectionDataResolver.resolve(selectionElement);
2853 new SelectionResolutionStatus());
2856 // Iterate all tree items and try to match them to the selection
2857 iterateTreeItems(new TObjectProcedure<TreeItem>() {
2859 public boolean execute(TreeItem treeItem) {
2860 NodeContext nodeContext = (NodeContext)treeItem.getData();
2861 if(nodeContext == null)
2863 SelectionResolutionStatus status = statusMap.get(nodeContext);
2864 if(status != null) {
2865 status.bestPriority = 0; // best possible match
2866 status.bestItem = treeItem;
2870 Object input = nodeContext.getConstant(BuiltinKeys.INPUT);
2871 status = statusMap.get(input);
2872 if(status != null) {
2873 NodeType nodeType = nodeContext.getConstant(NodeType.TYPE);
2874 int curPriority = nodeType instanceof EntityNodeType
2875 ? 1 // Prefer EntityNodeType matches to other node types
2877 if(curPriority < status.bestPriority) {
2878 status.bestPriority = curPriority;
2879 status.bestItem = treeItem;
2887 ArrayList<TreeItem> items = new ArrayList<TreeItem>(statusMap.size());
2888 for(SelectionResolutionStatus status : statusMap.values())
2889 if(status.bestItem != null)
2890 items.add(status.bestItem);
2891 select(items.toArray(new TreeItem[items.size()]));
2897 public ISelection getWidgetSelection() {
2898 TreeItem[] items = tree.getSelection();
2899 if (items.length == 0)
2900 return StructuredSelection.EMPTY;
2902 List<NodeContext> nodes = new ArrayList<NodeContext>(items.length);
2904 // Caches for resolving node contexts the hard way if necessary.
2905 GENodeQueryManager manager = null;
2906 NodeContext lastParentContext = null;
2907 NodeContext[] lastChildren = null;
2909 for (int i = 0; i < items.length; i++) {
2910 TreeItem item = items[i];
2911 NodeContext ctx = (NodeContext) item.getData();
2912 // It may happen due to the virtual nature of the tree control
2913 // that it contains TreeItems which have not yet been ran through
2918 TreeItem parentItem = item.getParentItem();
2919 NodeContext parentContext = parentItem != null ? getNodeContext(parentItem) : rootContext;
2920 if (parentContext != null) {
2921 NodeContext[] children = lastChildren;
2922 if (parentContext != lastParentContext) {
2923 if (manager == null)
2924 manager = new GENodeQueryManager(this.explorerContext, null, null, null);
2925 lastChildren = children = manager.query(parentContext, BuiltinKeys.FINAL_CHILDREN);
2926 lastParentContext = parentContext;
2928 int index = parentItem != null ? parentItem.indexOf(item) : tree.indexOf(item);
2929 if (index >= 0 && index < children.length) {
2930 NodeContext child = children[index];
2931 if (child != null) {
2933 // Cache NodeContext in TreeItem for faster access
2934 item.setData(child);
2940 //System.out.println("widget selection " + items.length + " items / " + nodes.size() + " node contexts");
2941 ISelection selection = constructSelection(nodes.toArray(NodeContext.NONE));
2946 public TransientExplorerState getTransientState() {
2947 if (!thread.currentThreadAccess())
2948 throw new AssertionError(getClass().getSimpleName() + ".getActiveColumn called from non SWT-thread: " + Thread.currentThread());
2949 return transientState;
2956 private void select(TreeItem item) {
2957 tree.setSelection(item);
2958 tree.showSelection();
2959 selectionProvider.setAndFireNonEqualSelection(constructSelection((NodeContext) item.getData()));
2966 private void select(TreeItem[] items) {
2967 //System.out.println("Select: " + Arrays.toString(items));
2968 tree.setSelection(items);
2969 tree.showSelection();
2970 NodeContext[] data = new NodeContext[items.length];
2971 for (int i = 0; i < data.length; i++) {
2972 data[i] = (NodeContext) items[i].getData();
2974 selectionProvider.setAndFireNonEqualSelection(constructSelection(data));
2977 private void iterateTreeItems(TObjectProcedure<TreeItem> procedure) {
2978 for(TreeItem item : tree.getItems())
2979 if(!iterateTreeItems(item, procedure))
2983 private boolean iterateTreeItems(TreeItem item,
2984 TObjectProcedure<TreeItem> procedure) {
2985 if(!procedure.execute(item))
2987 if(item.getExpanded())
2988 for(TreeItem child : item.getItems())
2989 if(!iterateTreeItems(child, procedure))
2999 private boolean trySelect(TreeItem item, Object input) {
3000 NodeContext itemCtx = (NodeContext) item.getData();
3001 if (itemCtx != null) {
3002 if (input.equals(itemCtx.getConstant(BuiltinKeys.INPUT))) {
3007 if (item.getExpanded()) {
3008 for (TreeItem child : item.getItems()) {
3009 if (trySelect(child, input))
3016 private boolean equalsEnough(NodeContext c1, NodeContext c2) {
3018 Object input1 = c1.getConstant(BuiltinKeys.INPUT);
3019 Object input2 = c2.getConstant(BuiltinKeys.INPUT);
3020 if(!ObjectUtils.objectEquals(input1, input2))
3023 Object type1 = c1.getConstant(NodeType.TYPE);
3024 Object type2 = c2.getConstant(NodeType.TYPE);
3025 if(!ObjectUtils.objectEquals(type1, type2))
3032 private NodeContext tryFind(NodeContext context) {
3033 for (TreeItem item : tree.getItems()) {
3034 NodeContext found = tryFind(item, context);
3035 if(found != null) return found;
3040 private NodeContext tryFind(TreeItem item, NodeContext context) {
3041 NodeContext itemCtx = (NodeContext) item.getData();
3042 if (itemCtx != null) {
3043 if (equalsEnough(context, itemCtx)) {
3047 if (item.getExpanded()) {
3048 for (TreeItem child : item.getItems()) {
3049 NodeContext found = tryFind(child, context);
3050 if(found != null) return found;
3057 public boolean select(NodeContext context) {
3059 assertNotDisposed();
3061 if (context == null || context.equals(rootContext)) {
3063 selectionProvider.setAndFireNonEqualSelection(TreeSelection.EMPTY);
3067 // if (context.equals(rootContext)) {
3068 // tree.deselectAll();
3069 // selectionProvider.setAndFireNonEqualSelection(constructSelection(context));
3073 Object input = context.getConstant(BuiltinKeys.INPUT);
3075 for (TreeItem item : tree.getItems()) {
3076 if (trySelect(item, input))
3084 private NodeContext tryFind2(NodeContext context) {
3085 Set<NodeContext> ctxs = contextToItem.getLeftSet();
3086 for(NodeContext c : ctxs)
3087 if(equalsEnough(c, context))
3092 private boolean waitVisible(NodeContext parent, NodeContext context) {
3093 long start = System.nanoTime();
3095 TreeItem parentItem = contextToItem.getRight(parent);
3097 if(parentItem == null)
3101 NodeContext target = tryFind2(context);
3102 if(target != null) {
3103 TreeItem item = contextToItem.getRight(target);
3104 if (!(item.getParentItem().equals(parentItem)))
3106 tree.setTopItem(item);
3110 Display.getCurrent().readAndDispatch();
3111 long duration = System.nanoTime() - start;
3117 private boolean selectPathInternal(NodeContext[] contexts, int position) {
3118 //System.out.println("NodeContext path : " + contexts);
3120 NodeContext head = tryFind(contexts[position]);
3121 // tryFind may return null for positions, that actually have NodeContext.
3125 if(position == contexts.length-1) {
3126 return select(head);
3130 //setExpanded(head, true);
3132 if(!waitVisible(head, contexts[position+1]))
3135 setExpanded(head, true);
3137 return selectPathInternal(contexts, position+1);
3142 public boolean selectPath(Collection<NodeContext> contexts) {
3144 if(contexts == null) throw new IllegalArgumentException("Null list is not allowed");
3145 if(contexts.isEmpty()) throw new IllegalArgumentException("Empty list is not allowed");
3147 return selectPathInternal(contexts.toArray(new NodeContext[contexts.size()]), 0);
3152 public boolean isVisible(NodeContext context) {
3154 for (TreeItem item : tree.getItems()) {
3155 NodeContext found = tryFind(item, context);
3164 protected ISelection constructSelection(NodeContext... contexts) {
3165 if (contexts == null)
3166 throw new IllegalArgumentException("null contexts");
3167 if (contexts.length == 0)
3168 return StructuredSelection.EMPTY;
3169 if (selectionFilter == null)
3170 return new StructuredSelection(transformSelection(contexts));
3171 return new StructuredSelection( transformSelection(filter(selectionFilter, contexts)) );
3174 protected Object[] transformSelection(Object[] objects) {
3175 return selectionTransformation.apply(this, objects);
3178 protected static Object[] filter(SelectionFilter filter, NodeContext[] contexts) {
3179 int len = contexts.length;
3180 Object[] objects = new Object[len];
3181 for (int i = 0; i < len; ++i)
3182 objects[i] = filter.filter(contexts[i]);
3187 public void setExpanded(final NodeContext context, final boolean expanded) {
3188 assertNotDisposed();
3189 ThreadUtils.asyncExec(thread, new Runnable() {
3193 doSetExpanded(context, expanded);
3198 private void doSetExpanded(NodeContext context, boolean expanded) {
3199 //System.out.println("doSetExpanded(" + context + ", " + expanded + ")");
3200 TreeItem item = contextToItem.getRight(context);
3202 item.setExpanded(expanded);
3204 PrimitiveQueryProcessor<?> pqp = explorerContext.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
3205 if (pqp instanceof IsExpandedProcessor) {
3206 IsExpandedProcessor iep = (IsExpandedProcessor) pqp;
3207 iep.replaceExpanded(context, expanded);
3212 public void setColumnsVisible(boolean visible) {
3213 columnsAreVisible = visible;
3214 if(tree != null) tree.setHeaderVisible(columnsAreVisible);
3218 public void setColumns(final Column[] columns) {
3219 setColumns(columns, null);
3223 public void setColumns(final Column[] columns, Consumer<Map<Column, Object>> callback) {
3224 assertNotDisposed();
3225 checkUniqueColumnKeys(columns);
3227 Display d = tree.getDisplay();
3228 if (d.getThread() == Thread.currentThread())
3229 doSetColumns(columns, callback);
3232 if (tree.isDisposed())
3234 doSetColumns(columns, callback);
3238 private void checkUniqueColumnKeys(Column[] cols) {
3239 Set<String> usedColumnKeys = new HashSet<String>();
3240 List<Column> duplicateColumns = new ArrayList<Column>();
3241 for (Column c : cols) {
3242 if (!usedColumnKeys.add(c.getKey()))
3243 duplicateColumns.add(c);
3245 if (!duplicateColumns.isEmpty()) {
3246 throw new IllegalArgumentException("All columns do not have unique keys: " + cols + ", overlapping: " + duplicateColumns);
3251 * Only meant to be invoked from the SWT UI thread.
3255 private void doSetColumns(Column[] cols, Consumer<Map<Column, Object>> callback) {
3256 // Attempt to keep previous column widths.
3257 Map<String, Integer> prevWidths = new HashMap<String, Integer>();
3258 for (TreeColumn column : tree.getColumns()) {
3259 prevWidths.put(column.getText(), column.getWidth());
3263 HashMap<String, Integer> keyToIndex = new HashMap<String, Integer>();
3264 for (int i = 0; i < cols.length; ++i) {
3265 keyToIndex.put(cols[i].getKey(), i);
3268 this.columns = Arrays.copyOf(cols, cols.length);
3269 //this.columns[cols.length] = FILLER_COLUMN;
3270 this.columnKeyToIndex = keyToIndex;
3271 this.columnImageArray = new Image[cols.length];
3272 this.columnDescOrImageArray = new Object[cols.length];
3274 Map<Column, Object> map = new HashMap<Column, Object>();
3276 tree.setHeaderVisible(columnsAreVisible);
3277 for (Column column : columns) {
3278 TreeColumn c = new TreeColumn(tree, toSWT(column.getAlignment()));
3281 c.setText(column.getLabel());
3282 c.setToolTipText(column.getTooltip());
3284 int cw = column.getWidth();
3286 // Try to keep previous widths
3287 Integer w = prevWidths.get(column);
3290 else if (cw != Column.DEFAULT_CONTROL_WIDTH)
3293 // Go for some kind of default settings then...
3294 if (ColumnKeys.PROPERTY.equals(column.getKey()))
3300 // if (!column.hasGrab() && !FILLER.equals(column.getKey())) {
3301 // c.addListener(SWT.Resize, resizeListener);
3302 // c.setResizable(true);
3304 // //c.setResizable(false);
3309 if(callback != null) callback.accept(map);
3311 // Make sure the explorer fits the columns properly after initialization.
3312 tree.getDisplay().asyncExec(new Runnable() {
3315 if (tree.isDisposed())
3317 refreshColumnSizes();
3322 int toSWT(Align alignment) {
3323 switch (alignment) {
3324 case LEFT: return SWT.LEFT;
3325 case CENTER: return SWT.CENTER;
3326 case RIGHT: return SWT.RIGHT;
3327 default: throw new Error("unhandled alignment: " + alignment);
3332 public Column[] getColumns() {
3333 return Arrays.copyOf(columns, columns.length);
3336 private void detachPrimitiveProcessors() {
3337 for (PrimitiveQueryProcessor<?> p : primitiveProcessors.values()) {
3338 if (p instanceof ProcessorLifecycle) {
3339 ((ProcessorLifecycle) p).detached(this);
3344 private void clearPrimitiveProcessors() {
3345 for (PrimitiveQueryProcessor<?> p : primitiveProcessors.values()) {
3346 if (p instanceof ProcessorLifecycle) {
3347 ((ProcessorLifecycle) p).clear();
3352 Listener resizeListener = new Listener() {
3354 public void handleEvent(Event event) {
3355 // Prevent infinite recursion.
3356 if (refreshingColumnSizes)
3358 //TreeColumn column = (TreeColumn) event.widget;
3359 //Column c = (Column) column.getData();
3360 refreshColumnSizes();
3364 Listener itemDisposeListener = new Listener() {
3366 public void handleEvent(Event event) {
3367 if (event.type == SWT.Dispose) {
3368 if (event.widget instanceof TreeItem) {
3369 TreeItem ti = (TreeItem) event.widget;
3370 //NodeContext ctx = (NodeContext) ti.getData();
3371 // System.out.println("DISPOSE CONTEXT TO ITEM: " + ctx + " -> " + System.identityHashCode(ti));
3372 // System.out.println(" map size BEFORE: " + contextToItem.size());
3373 @SuppressWarnings("unused")
3374 NodeContext removed = contextToItem.removeWithRight(ti);
3375 // System.out.println(" REMOVED: " + removed);
3376 // System.out.println(" map size AFTER: " + contextToItem.size());
3385 LabelerListener labelListener = new LabelerListener() {
3387 public boolean columnModified(final NodeContext context, final String key, final String newLabel) {
3388 //System.out.println("column " + key + " modified for " + context + " to " + newLabel);
3389 if (tree.isDisposed())
3392 synchronized (labelRefreshRunnables) {
3393 Runnable refresher = new Runnable() {
3396 // Tree is guaranteed to be non-disposed if this is invoked.
3398 // contextToItem should be accessed only in the SWT thread to keep things thread-safe.
3399 final TreeItem item = contextToItem.getRight(context);
3400 if (item == null || item.isDisposed())
3403 final Integer index = columnKeyToIndex.get(key);
3407 //System.out.println(" found index: " + index);
3408 //System.out.println(" found item: " + item);
3410 GENodeQueryManager manager = new GENodeQueryManager(explorerContext, null, null, null);
3412 // FIXME: indexOf is quadratic
3414 TreeItem parentItem = item.getParentItem();
3415 if (parentItem == null) {
3416 itemIndex = tree.indexOf(item);
3417 //tree.clear(parentIndex, false);
3419 itemIndex = parentItem.indexOf(item);
3420 //item.clear(parentIndex, false);
3422 setTextAndImage(item, manager, context, itemIndex);
3423 } catch (SWTException e) {
3424 ErrorLogger.defaultLogError(e);
3428 //System.out.println(System.currentTimeMillis() + " queueing label refresher: " + refresher);
3429 labelRefreshRunnables.put(context, refresher);
3431 if (!refreshIsQueued) {
3432 refreshIsQueued = true;
3434 long now = System.currentTimeMillis();
3435 long elapsed = now - lastLabelRefreshScheduled;
3436 if (elapsed < DEFAULT_CONSECUTIVE_LABEL_REFRESH_DELAY)
3437 delay = DEFAULT_CONSECUTIVE_LABEL_REFRESH_DELAY - elapsed;
3438 //System.out.println("scheduling with delay: " + delay + " (" + lastLabelRefreshScheduled + " -> " + now + " = " + elapsed + ")");
3440 ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
3443 scheduleImmediateLabelRefresh();
3445 }, delay, TimeUnit.MILLISECONDS);
3447 scheduleImmediateLabelRefresh();
3449 lastLabelRefreshScheduled = now;
3456 public boolean columnsModified(final NodeContext context, final Map<String, String> columns) {
3457 System.out.println("TODO: implement GraphExplorerImpl.labelListener.columnsModified");
3462 private void scheduleImmediateLabelRefresh() {
3463 Runnable[] runnables = null;
3464 synchronized (labelRefreshRunnables) {
3465 if (labelRefreshRunnables.isEmpty())
3468 runnables = labelRefreshRunnables.values().toArray(new Runnable[labelRefreshRunnables.size()]);
3469 labelRefreshRunnables.clear();
3470 refreshIsQueued = false;
3472 final Runnable[] rs = runnables;
3474 if (tree.isDisposed())
3476 tree.getDisplay().asyncExec(new Runnable() {
3479 if (tree.isDisposed())
3481 //System.out.println(System.currentTimeMillis() + " EXECUTING " + rs.length + " label refresh runnables");
3482 tree.setRedraw(false);
3483 for (Runnable r : rs) {
3486 tree.setRedraw(true);
3491 long lastLabelRefreshScheduled = 0;
3492 boolean refreshIsQueued = false;
3493 Map<NodeContext, Runnable> labelRefreshRunnables = new HashMap<NodeContext, Runnable>();
3495 @SuppressWarnings("unchecked")
3497 public <T> T getAdapter(Class<T> adapter) {
3498 if(ISelectionProvider.class == adapter) return (T) postSelectionProvider;
3499 else if(IPostSelectionProvider.class == adapter) return (T) postSelectionProvider;
3503 @SuppressWarnings("unchecked")
3505 public <T> T getControl() {
3510 * @see org.simantics.browsing.ui.GraphExplorer#setAutoExpandLevel(int)
3513 public void setAutoExpandLevel(int level) {
3514 this.autoExpandLevel = level;
3518 public <T> NodeQueryProcessor<T> getProcessor(QueryKey<T> key) {
3519 return explorerContext.getProcessor(key);
3523 public <T> PrimitiveQueryProcessor<T> getPrimitiveProcessor(PrimitiveQueryKey<T> key) {
3524 return explorerContext.getPrimitiveProcessor(key);
3528 public boolean isEditable() {
3533 public void setEditable(boolean editable) {
3534 if (!thread.currentThreadAccess())
3535 throw new IllegalStateException("not in SWT display thread " + thread.getThread());
3537 this.editable = editable;
3538 Display display = tree.getDisplay();
3539 tree.setBackground(editable ? null : display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
3543 * For setting a more local service locator for the explorer than the global
3544 * workbench service locator. Sometimes required to give this implementation
3545 * access to local workbench services like IFocusService.
3548 * Must be invoked during right after construction.
3550 * @param serviceLocator
3551 * a specific service locator or <code>null</code> to use the
3552 * workbench global service locator
3554 public void setServiceLocator(IServiceLocator serviceLocator) {
3555 if (serviceLocator == null && PlatformUI.isWorkbenchRunning())
3556 serviceLocator = PlatformUI.getWorkbench();
3557 this.serviceLocator = serviceLocator;
3558 if (serviceLocator != null) {
3559 this.contextService = (IContextService) serviceLocator.getService(IContextService.class);
3560 this.focusService = (IFocusService) serviceLocator.getService(IFocusService.class);
3565 public Object getClicked(Object event) {
3566 MouseEvent e = (MouseEvent)event;
3567 final Tree tree = (Tree) e.getSource();
3568 Point point = new Point(e.x, e.y);
3569 TreeItem item = tree.getItem(point);
3571 // No selectable item at point?
3575 Object data = item.getData();