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.ColumnKeys;
118 import org.simantics.browsing.ui.common.ErrorLogger;
119 import org.simantics.browsing.ui.common.NodeContextBuilder;
120 import org.simantics.browsing.ui.common.NodeContextUtil;
121 import org.simantics.browsing.ui.common.internal.GECache;
122 import org.simantics.browsing.ui.common.internal.GENodeQueryManager;
123 import org.simantics.browsing.ui.common.internal.IGECache;
124 import org.simantics.browsing.ui.common.internal.IGraphExplorerContext;
125 import org.simantics.browsing.ui.common.internal.UIElementReference;
126 import org.simantics.browsing.ui.common.processors.DefaultCheckedStateProcessor;
127 import org.simantics.browsing.ui.common.processors.DefaultComparableChildrenProcessor;
128 import org.simantics.browsing.ui.common.processors.DefaultFinalChildrenProcessor;
129 import org.simantics.browsing.ui.common.processors.DefaultImageDecoratorProcessor;
130 import org.simantics.browsing.ui.common.processors.DefaultImagerFactoriesProcessor;
131 import org.simantics.browsing.ui.common.processors.DefaultImagerProcessor;
132 import org.simantics.browsing.ui.common.processors.DefaultLabelDecoratorProcessor;
133 import org.simantics.browsing.ui.common.processors.DefaultLabelerFactoriesProcessor;
134 import org.simantics.browsing.ui.common.processors.DefaultLabelerProcessor;
135 import org.simantics.browsing.ui.common.processors.DefaultPrunedChildrenProcessor;
136 import org.simantics.browsing.ui.common.processors.DefaultSelectedImageDecoratorFactoriesProcessor;
137 import org.simantics.browsing.ui.common.processors.DefaultSelectedLabelDecoratorFactoriesProcessor;
138 import org.simantics.browsing.ui.common.processors.DefaultSelectedLabelerProcessor;
139 import org.simantics.browsing.ui.common.processors.DefaultSelectedViewpointFactoryProcessor;
140 import org.simantics.browsing.ui.common.processors.DefaultSelectedViewpointProcessor;
141 import org.simantics.browsing.ui.common.processors.DefaultViewpointContributionProcessor;
142 import org.simantics.browsing.ui.common.processors.DefaultViewpointContributionsProcessor;
143 import org.simantics.browsing.ui.common.processors.DefaultViewpointProcessor;
144 import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;
145 import org.simantics.browsing.ui.common.processors.NoSelectionRequestProcessor;
146 import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;
147 import org.simantics.browsing.ui.content.ImageDecorator;
148 import org.simantics.browsing.ui.content.Imager;
149 import org.simantics.browsing.ui.content.LabelDecorator;
150 import org.simantics.browsing.ui.content.Labeler;
151 import org.simantics.browsing.ui.content.Labeler.CustomModifier;
152 import org.simantics.browsing.ui.content.Labeler.DeniedModifier;
153 import org.simantics.browsing.ui.content.Labeler.DialogModifier;
154 import org.simantics.browsing.ui.content.Labeler.EnumerationModifier;
155 import org.simantics.browsing.ui.content.Labeler.FilteringModifier;
156 import org.simantics.browsing.ui.content.Labeler.LabelerListener;
157 import org.simantics.browsing.ui.content.Labeler.Modifier;
158 import org.simantics.browsing.ui.content.PrunedChildrenResult;
159 import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;
160 import org.simantics.browsing.ui.model.nodetypes.NodeType;
161 import org.simantics.browsing.ui.swt.internal.Threads;
162 import org.simantics.db.layer0.SelectionHints;
163 import org.simantics.utils.ObjectUtils;
164 import org.simantics.utils.datastructures.BijectionMap;
165 import org.simantics.utils.datastructures.disposable.AbstractDisposable;
166 import org.simantics.utils.datastructures.hints.IHintContext;
167 import org.simantics.utils.threads.IThreadWorkQueue;
168 import org.simantics.utils.threads.SWTThread;
169 import org.simantics.utils.threads.ThreadUtils;
170 import org.simantics.utils.ui.ISelectionUtils;
171 import org.simantics.utils.ui.jface.BasePostSelectionProvider;
172 import org.simantics.utils.ui.widgets.VetoingEventHandler;
173 import org.simantics.utils.ui.workbench.WorkbenchUtils;
175 import gnu.trove.map.hash.THashMap;
176 import gnu.trove.procedure.TObjectProcedure;
177 import gnu.trove.set.hash.THashSet;
180 * @see #getMaxChildren()
181 * @see #setMaxChildren(int)
182 * @see #getMaxChildren(NodeQueryManager, NodeContext)
184 class GraphExplorerImpl extends GraphExplorerImplBase implements Listener, GraphExplorer /*, IPostSelectionProvider*/ {
186 private static class GraphExplorerPostSelectionProvider implements IPostSelectionProvider {
188 private GraphExplorerImpl ge;
190 GraphExplorerPostSelectionProvider(GraphExplorerImpl ge) {
199 public void setSelection(final ISelection selection) {
200 if(ge == null) return;
201 ge.setSelection(selection, false);
206 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
207 if(ge == null) return;
208 if(ge.isDisposed()) {
209 if (DEBUG_SELECTION_LISTENERS)
210 System.out.println("GraphExplorerImpl is disposed in removeSelectionChangedListener: " + listener);
213 //assertNotDisposed();
214 //System.out.println("Remove selection changed listener: " + listener);
215 ge.selectionProvider.removeSelectionChangedListener(listener);
219 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
220 if(ge == null) return;
221 if (!ge.thread.currentThreadAccess())
222 throw new AssertionError(getClass().getSimpleName() + ".addPostSelectionChangedListener called from non SWT-thread: " + Thread.currentThread());
223 if(ge.isDisposed()) {
224 System.out.println("Client BUG: GraphExplorerImpl is disposed in addPostSelectionChangedListener: " + listener);
227 //System.out.println("Add POST selection changed listener: " + listener);
228 ge.selectionProvider.addPostSelectionChangedListener(listener);
232 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
233 if(ge == null) return;
234 if(ge.isDisposed()) {
235 if (DEBUG_SELECTION_LISTENERS)
236 System.out.println("GraphExplorerImpl is disposed in removePostSelectionChangedListener: " + listener);
239 // assertNotDisposed();
240 //System.out.println("Remove POST selection changed listener: " + listener);
241 ge.selectionProvider.removePostSelectionChangedListener(listener);
246 public void addSelectionChangedListener(ISelectionChangedListener listener) {
247 if(ge == null) return;
248 if (!ge.thread.currentThreadAccess())
249 throw new AssertionError(getClass().getSimpleName() + ".addSelectionChangedListener called from non SWT-thread: " + Thread.currentThread());
250 //System.out.println("Add selection changed listener: " + listener);
251 if (ge.tree.isDisposed() || ge.selectionProvider == null) {
252 System.out.println("Client BUG: GraphExplorerImpl is disposed in addSelectionChangedListener: " + listener);
256 ge.selectionProvider.addSelectionChangedListener(listener);
261 public ISelection getSelection() {
262 if(ge == null) return StructuredSelection.EMPTY;
263 if (!ge.thread.currentThreadAccess())
264 throw new AssertionError(getClass().getSimpleName() + ".getSelection called from non SWT-thread: " + Thread.currentThread());
265 if (ge.tree.isDisposed() || ge.selectionProvider == null)
266 return StructuredSelection.EMPTY;
267 return ge.selectionProvider.getSelection();
273 * If this explorer is running with an Eclipse workbench open, this
274 * Workbench UI context will be activated whenever inline editing is started
275 * through {@link #startEditing(TreeItem, int)} and deactivated when inline
278 * This context information can be used to for UI handler activity testing.
280 private static final String INLINE_EDITING_UI_CONTEXT = "org.simantics.browsing.ui.inlineEditing";
282 private static final String KEY_DRAG_COLUMN = "dragColumn";
284 private static final boolean DEBUG_SELECTION_LISTENERS = false;
286 private static final int DEFAULT_CONSECUTIVE_LABEL_REFRESH_DELAY = 200;
288 public static final int DEFAULT_MAX_CHILDREN = 1000;
290 private static final long POST_SELECTION_DELAY = 300;
293 * The time in milliseconds that must elapse between consecutive
294 * {@link Tree} {@link SelectionListener#widgetSelected(SelectionEvent)}
295 * invocations in order for this class to construct a new selection.
298 * This is done because selection construction can be very expensive as the
299 * selected set grows larger when the user is pressing shift+arrow keys.
300 * GraphExplorerImpl will naturally listen to all changes in the tree
301 * selection, but as an optimization will not construct new
302 * StructuredSelection instances for every selection change event. A new
303 * selection will be constructed and set only if the selection hasn't
304 * changed for the amount of milliseconds specified by this constant.
306 private static final long SELECTION_CHANGE_QUIET_TIME = 150;
308 private final IThreadWorkQueue thread;
311 * Local method for checking from whether resources are loaded in
314 private final LocalResourceManager localResourceManager;
317 * Local device resource manager that is safe to use in
318 * {@link ImageLoaderJob} for creating images in a non-UI thread.
320 private final ResourceManager resourceManager;
323 * Package visibility.
324 * TODO: Get rid of these.
328 @SuppressWarnings({ "rawtypes" })
329 final HashMap<CacheKey<?>, NodeQueryProcessor> processors = new HashMap<CacheKey<?>, NodeQueryProcessor>();
330 @SuppressWarnings({ "rawtypes" })
331 final HashMap<Object, PrimitiveQueryProcessor> primitiveProcessors = new HashMap<Object, PrimitiveQueryProcessor>();
332 @SuppressWarnings({ "rawtypes" })
333 final HashMap<Class, DataSource> dataSources = new HashMap<Class, DataSource>();
335 class GraphExplorerContext extends AbstractDisposable implements IGraphExplorerContext {
336 // This is for query debugging only.
339 GECache cache = new GECache();
340 AtomicBoolean propagating = new AtomicBoolean(false);
341 Object propagateList = new Object();
342 Object propagate = new Object();
343 List<Runnable> scheduleList = new ArrayList<Runnable>();
344 final Deque<Integer> activity = new LinkedList<Integer>();
348 * Stores the currently running query update runnable. If
349 * <code>null</code> there's nothing scheduled yet in which case
350 * scheduling can commence. Otherwise the update should be skipped.
352 AtomicReference<Runnable> currentQueryUpdater = new AtomicReference<Runnable>();
355 * Keeps track of nodes that have already been auto-expanded. After
356 * being inserted into this set, nodes will not be forced to stay in an
357 * expanded state after that. This makes it possible for the user to
358 * close auto-expanded nodes.
360 Map<NodeContext, Boolean> autoExpanded = new WeakHashMap<NodeContext, Boolean>();
364 protected void doDispose() {
366 autoExpanded.clear();
370 public IGECache getCache() {
375 public int queryIndent() {
380 public int queryIndent(int offset) {
381 queryIndent += offset;
386 @SuppressWarnings("unchecked")
387 public <T> NodeQueryProcessor<T> getProcessor(Object o) {
388 return processors.get(o);
392 @SuppressWarnings("unchecked")
393 public <T> PrimitiveQueryProcessor<T> getPrimitiveProcessor(Object o) {
394 return primitiveProcessors.get(o);
397 @SuppressWarnings("unchecked")
399 public <T> DataSource<T> getDataSource(Class<T> clazz) {
400 return dataSources.get(clazz);
404 public void update(UIElementReference ref) {
405 //System.out.println("GE.update " + ref);
406 TreeItemReference tiref = (TreeItemReference) ref;
407 TreeItem item = tiref.getItem();
408 // NOTE: must be called regardless of the the item value.
409 // A null item is currently used to indicate a tree root update.
410 GraphExplorerImpl.this.update(item);
414 public Object getPropagateLock() {
419 public Object getPropagateListLock() {
420 return propagateList;
424 public boolean isPropagating() {
425 return propagating.get();
429 public void setPropagating(boolean b) {
430 this.propagating.set(b);
434 public List<Runnable> getScheduleList() {
439 public void setScheduleList(List<Runnable> list) {
440 this.scheduleList = list;
444 public Deque<Integer> getActivity() {
449 public void setActivityInt(int i) {
450 this.activityInt = i;
454 public int getActivityInt() {
459 public void scheduleQueryUpdate(Runnable r) {
460 if (GraphExplorerImpl.this.isDisposed() || queryUpdateScheduler.isShutdown())
462 //System.out.println("Scheduling query update for runnable " + r);
463 if (currentQueryUpdater.compareAndSet(null, r)) {
464 //System.out.println("Scheduling query update for runnable " + r);
465 queryUpdateScheduler.execute(QUERY_UPDATE_SCHEDULER);
469 Runnable QUERY_UPDATE_SCHEDULER = new Runnable() {
472 Runnable r = currentQueryUpdater.getAndSet(null);
474 //System.out.println("Running query update runnable " + r);
481 GraphExplorerContext explorerContext = new GraphExplorerContext();
483 HashSet<TreeItem> pendingItems = new HashSet<TreeItem>();
484 boolean updating = false;
485 boolean pendingRoot = false;
487 @SuppressWarnings("deprecation")
488 ModificationContext modificationContext = null;
490 NodeContext rootContext;
492 StatePersistor persistor = null;
494 boolean editable = true;
497 * This is a reverse mapping from {@link NodeContext} tree objects back to
498 * their owner TreeItems.
501 * Access this map only in the SWT thread to keep it thread-safe.
504 BijectionMap<NodeContext, TreeItem> contextToItem = new BijectionMap<NodeContext, TreeItem>();
507 * Columns of the UI viewer. Use {@link #setColumns(Column[])} to
510 Column[] columns = new Column[0];
511 Map<String, Integer> columnKeyToIndex = new HashMap<String, Integer>();
512 boolean refreshingColumnSizes = false;
513 boolean columnsAreVisible = true;
516 * An array reused for invoking {@link TreeItem#setImage(Image[])} instead
517 * of constantly allocating new arrays for setting each TreeItems images.
518 * This works because {@link TreeItem#setImage(Image[])} does not take hold
519 * of the array itself, only the contents of the array.
521 * @see #setImage(NodeContext, TreeItem, Imager, Collection, int)
523 Image[] columnImageArray = { null };
526 * Used for collecting Image or ImageDescriptor instances for a single
527 * TreeItem when initially setting images for a TreeItem.
529 * @see #setImage(NodeContext, TreeItem, Imager, Collection, int)
531 Object[] columnDescOrImageArray = { null };
533 final ExecutorService queryUpdateScheduler = Threads.getExecutor();
534 final ScheduledExecutorService uiUpdateScheduler = ThreadUtils.getNonBlockingWorkExecutor();
536 /** Set to true when the Tree widget is disposed. */
537 private boolean disposed = false;
538 private final CopyOnWriteArrayList<FocusListener> focusListeners = new CopyOnWriteArrayList<FocusListener>();
539 private final CopyOnWriteArrayList<MouseListener> mouseListeners = new CopyOnWriteArrayList<MouseListener>();
540 private final CopyOnWriteArrayList<KeyListener> keyListeners = new CopyOnWriteArrayList<KeyListener>();
542 /** Selection provider */
543 private GraphExplorerPostSelectionProvider postSelectionProvider = new GraphExplorerPostSelectionProvider(this);
544 protected BasePostSelectionProvider selectionProvider = new BasePostSelectionProvider();
545 protected SelectionDataResolver selectionDataResolver;
546 protected SelectionFilter selectionFilter;
547 protected BiFunction<GraphExplorer, Object[], Object[]> selectionTransformation = new BiFunction<GraphExplorer, Object[], Object[]>() {
550 public Object[] apply(GraphExplorer explorer, Object[] objects) {
551 Object[] result = new Object[objects.length];
552 for (int i = 0; i < objects.length; i++) {
553 IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);
554 context.setHint(SelectionHints.KEY_MAIN, objects[i]);
561 protected FontDescriptor originalFont;
562 protected ColorDescriptor originalForeground;
563 protected ColorDescriptor originalBackground;
566 * The set of currently selected TreeItem instances. This set is needed
567 * because we need to know in {@link #setData(Event)} whether the updated
568 * item was a part of the current selection in which case the selection must
571 private final Map<TreeItem, NodeContext> selectedItems = new HashMap<TreeItem, NodeContext>();
574 * TODO: specify what this is for
576 private final Set<NodeContext> selectionRefreshContexts = new HashSet<NodeContext>();
579 * If this field is non-null, it means that if {@link #setData(Event)}
580 * encounters a NodeContext equal to this one, it must make the TreeItem
581 * assigned to that NodeContext the topmost item of the tree using
582 * {@link Tree#setTopItem(TreeItem)}. After this the field value is
586 * This is related to {@link #initializeState()}, i.e. explorer state
589 // private NodeContext[] topNodePath = NodeContext.NONE;
590 // private int[] topNodePath = {};
591 // private int currentTopNodePathIndex = -1;
594 * See {@link #setAutoExpandLevel(int)}
596 private int autoExpandLevel = 0;
599 * <code>null</code> if not explicitly set through
600 * {@link #setServiceLocator(IServiceLocator)}.
602 private IServiceLocator serviceLocator;
605 * The global workbench context service, if the workbench is available.
606 * Retrieved in the constructor.
608 private IContextService contextService = null;
611 * The global workbench IFocusService, if the workbench is available.
612 * Retrieved in the constructor.
614 private IFocusService focusService = null;
617 * A Workbench UI context activation that is activated when starting inline
618 * editing through {@link #startEditing(TreeItem, int)}.
620 * @see #activateEditingContext()
621 * @see #deactivateEditingContext()
623 private IContextActivation editingContext = null;
625 static class ImageTask {
628 Object[] descsOrImages;
629 public ImageTask(NodeContext node, TreeItem item, Object[] descsOrImages) {
632 this.descsOrImages = descsOrImages;
637 * The job that is used for off-loading image loading tasks (see
638 * {@link ImageTask} to a worker thread from the main UI thread.
640 * @see #setPendingImages(IProgressMonitor)
642 ImageLoaderJob imageLoaderJob;
645 * The set of currently gathered up image loading tasks for
646 * {@link #imageLoaderJob} to execute.
648 * @see #setPendingImages(IProgressMonitor)
650 Map<TreeItem, ImageTask> imageTasks = new THashMap<TreeItem, ImageTask>();
653 * A state flag indicating whether the vertical scroll bar was visible for
654 * {@link #tree} the last time it was checked. Since there is no listener
655 * that can provide this information, we check it in {@link #setData(Event)}
656 * every time any data for a TreeItem is updated. If the visibility changes,
657 * we will force re-layouting of the tree's parent composite.
659 * @see #setData(Event)
661 private boolean verticalBarVisible = false;
663 static class TransientStateImpl implements TransientExplorerState {
665 private Integer activeColumn = null;
668 public synchronized Integer getActiveColumn() {
672 public synchronized void setActiveColumn(Integer column) {
673 activeColumn = column;
678 private TransientStateImpl transientState = new TransientStateImpl();
680 boolean scheduleUpdater() {
682 if (tree.isDisposed())
685 if (pendingRoot == true || !pendingItems.isEmpty()) {
686 assert(!tree.isDisposed());
688 int activity = explorerContext.activityInt;
690 if (activity < 100) {
691 // System.out.println("Scheduling update immediately.");
692 } else if (activity < 1000) {
693 // System.out.println("Scheduling update after 500ms.");
696 // System.out.println("Scheduling update after 3000ms.");
702 //System.out.println("Scheduling UI update after " + delay + " ms.");
703 uiUpdateScheduler.schedule(new Runnable() {
707 if (tree.isDisposed())
710 if (updateCounter > 0) {
712 uiUpdateScheduler.schedule(this, 50, TimeUnit.MILLISECONDS);
714 tree.getDisplay().asyncExec(new UpdateRunner(GraphExplorerImpl.this, GraphExplorerImpl.this.explorerContext));
718 }, delay, TimeUnit.MILLISECONDS);
727 int updateCounter = 0;
729 void update(TreeItem item) {
731 synchronized(pendingItems) {
733 // System.out.println("update " + item);
737 if(item == null) pendingRoot = true;
738 else pendingItems.add(item);
740 if(updating == true) return;
748 private int maxChildren = DEFAULT_MAX_CHILDREN;
751 public int getMaxChildren() {
756 public int getMaxChildren(NodeQueryManager manager, NodeContext context) {
757 Integer result = manager.query(context, BuiltinKeys.SHOW_MAX_CHILDREN);
758 //System.out.println("getMaxChildren(" + manager + ", " + context + "): " + result);
759 if (result != null) {
761 throw new AssertionError("BuiltinKeys.SHOW_MAX_CHILDREN query must never return < 0, got " + result);
768 public void setMaxChildren(int maxChildren) {
769 this.maxChildren = maxChildren;
773 public void setModificationContext(@SuppressWarnings("deprecation") ModificationContext modificationContext) {
774 this.modificationContext = modificationContext;
778 * @param parent the parent SWT composite
780 public GraphExplorerImpl(Composite parent) {
781 this(parent, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
785 * Stores the node context and the modifier that is currently being
786 * modified. These are used internally to prevent duplicate edits from being
787 * initiated which should always be a sensible thing to do.
789 private Set<NodeContext> currentlyModifiedNodes = new THashSet<NodeContext>();
791 private final TreeEditor editor;
792 private Color invalidModificationColor = null;
795 * @param item the TreeItem to start editing
796 * @param columnIndex the index of the column to edit, starts counting from
798 * @return <code>true</code> if the editing was initiated successfully or
799 * <code>false</code> if editing could not be started due to lack of
800 * {@link Modifier} for the labeler in question.
802 private String startEditing(final TreeItem item, final int columnIndex, String columnKey) {
804 return "Rename not supported for selection";
806 GENodeQueryManager manager = new GENodeQueryManager(this.explorerContext, null, null, TreeItemReference.create(item.getParentItem()));
807 final NodeContext context = (NodeContext) item.getData();
808 Labeler labeler = manager.query(context, BuiltinKeys.SELECTED_LABELER);
810 return "Rename not supported for selection";
812 if(columnKey == null) columnKey = columns[columnIndex].getKey();
814 // columnKey might be prefixed with '#' to indicate
815 // textual editing is preferred. Try to get modifier
816 // for that first and only if it fails, try without
818 Modifier modifier = labeler.getModifier(modificationContext, columnKey);
819 if (modifier == null) {
820 if(columnKey.startsWith("#"))
821 modifier = labeler.getModifier(modificationContext, columnKey.substring(1));
822 if (modifier == null)
823 return "Rename not supported for selection";
825 if (modifier instanceof DeniedModifier) {
826 DeniedModifier dm = (DeniedModifier)modifier;
827 return dm.getMessage();
830 // Prevent editing of a single node context multiple times.
831 if (currentlyModifiedNodes.contains(context)) {
832 //System.out.println("discarding duplicate edit for context " + context);
833 return "Rename not supported for selection";
836 // Clean up any previous editor control
837 Control oldEditor = editor.getEditor();
838 if (oldEditor != null)
841 if (modifier instanceof DialogModifier) {
842 performDialogEditing(item, columnIndex, context, (DialogModifier) modifier);
843 } else if (modifier instanceof CustomModifier) {
844 startCustomEditing(item, columnIndex, context, (CustomModifier) modifier);
845 } else if (modifier instanceof EnumerationModifier) {
846 startEnumerationEditing(item, columnIndex, context, (EnumerationModifier) modifier);
848 startTextEditing(item, columnIndex, context, modifier);
860 void performDialogEditing(final TreeItem item, final int columnIndex, final NodeContext context,
861 final DialogModifier modifier) {
862 final AtomicBoolean disposed = new AtomicBoolean(false);
863 Consumer<String> callback = result -> {
866 String error = modifier.isValid(result);
868 modifier.modify(result);
869 // Item may be disposed if the tree gets reset after a previous editing.
870 if (!item.isDisposed()) {
871 item.setText(columnIndex, result);
872 queueSelectionRefresh(context);
877 currentlyModifiedNodes.add(context);
879 String status = modifier.query(tree, item, columnIndex, context, callback);
881 ErrorLogger.defaultLog( new Status(IStatus.INFO, Activator.PLUGIN_ID, status) );
883 currentlyModifiedNodes.remove(context);
888 private void reconfigureTreeEditor(TreeItem item, int columnIndex, Control control, int widthHint, int heightHint, int insetX, int insetY) {
889 Point size = control.computeSize(widthHint, heightHint);
890 editor.horizontalAlignment = SWT.LEFT;
891 Rectangle itemRect = item.getBounds(columnIndex),
892 rect = tree.getClientArea();
893 editor.minimumWidth = Math.max(size.x, itemRect.width) + insetX * 2;
894 int left = itemRect.x,
895 right = rect.x + rect.width;
896 editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
897 editor.minimumHeight = size.y + insetY * 2;
901 void reconfigureTreeEditorForText(TreeItem item, int columnIndex, Control control, String text, int heightHint, int insetX, int insetY) {
902 GC gc = new GC(control);
903 Point size = gc.textExtent(text);
905 reconfigureTreeEditor(item, columnIndex, control, size.x, SWT.DEFAULT, insetX, insetY);
914 void startCustomEditing(final TreeItem item, final int columnIndex, final NodeContext context,
915 final CustomModifier modifier) {
916 final Object obj = modifier.createControl(tree, item, columnIndex, context);
917 if (!(obj instanceof Control))
918 throw new UnsupportedOperationException("SWT control required, got " + obj + " from CustomModifier.createControl(Object)");
919 final Control control = (Control) obj;
921 // final int insetX = 0;
922 // final int insetY = 0;
923 // control.addListener(SWT.Resize, new Listener() {
925 // public void handleEvent(Event e) {
926 // Rectangle rect = control.getBounds();
927 // control.setBounds(rect.x + insetX, rect.y + insetY, rect.width - insetX * 2, rect.height - insetY * 2);
930 control.addListener(SWT.Dispose, new Listener() {
932 public void handleEvent(Event event) {
933 currentlyModifiedNodes.remove(context);
934 queueSelectionRefresh(context);
935 deactivateEditingContext();
939 if (!(control instanceof Shell)) {
940 editor.setEditor(control, item, columnIndex);
946 GraphExplorerImpl.this.reconfigureTreeEditor(item, columnIndex, control, SWT.DEFAULT, SWT.DEFAULT, 0, 0);
948 activateEditingContext(control);
950 // Removed in disposeListener above
951 currentlyModifiedNodes.add(context);
952 //System.out.println("START CUSTOM EDITING: " + item);
961 void startEnumerationEditing(final TreeItem item, final int columnIndex, final NodeContext context, final EnumerationModifier modifier) {
962 String initialText = modifier.getValue();
963 if (initialText == null)
964 throw new AssertionError("Labeler.Modifier.getValue() returned null");
966 List<String> values = modifier.getValues();
967 String selectedValue = modifier.getValue();
968 int selectedIndex = values.indexOf(selectedValue);
969 if (selectedIndex == -1)
970 throw new AssertionFailedException(modifier + " EnumerationModifier.getValue returned '" + selectedValue + "' which is not among the possible values returned by EnumerationModifier.getValues(): " + values);
972 final CCombo combo = new CCombo(tree, SWT.FLAT | SWT.BORDER | SWT.READ_ONLY | SWT.DROP_DOWN);
973 combo.setVisibleItemCount(10);
974 //combo.setEditable(false);
976 for (String value : values) {
979 combo.select(selectedIndex);
981 Listener comboListener = new Listener() {
982 boolean arrowTraverseUsed = false;
984 public void handleEvent(final Event e) {
985 //System.out.println("FOO: " + e);
988 if (e.character == SWT.CR) {
989 // Commit edit directly on ENTER press.
990 String text = combo.getText();
991 modifier.modify(text);
992 // Item may be disposed if the tree gets reset after a previous editing.
993 if (!item.isDisposed()) {
994 item.setText(columnIndex, text);
995 queueSelectionRefresh(context);
999 } else if (e.keyCode == SWT.ESC) {
1000 // Cancel editing immediately
1007 if (arrowTraverseUsed) {
1008 arrowTraverseUsed = false;
1012 String text = combo.getText();
1013 modifier.modify(text);
1015 // Item may be disposed if the tree gets reset after a previous editing.
1016 if (!item.isDisposed()) {
1017 item.setText(columnIndex, text);
1018 queueSelectionRefresh(context);
1023 case SWT.FocusOut: {
1024 String text = combo.getText();
1025 modifier.modify(text);
1027 // Item may be disposed if the tree gets reset after a previous editing.
1028 if (!item.isDisposed()) {
1029 item.setText(columnIndex, text);
1030 queueSelectionRefresh(context);
1035 case SWT.Traverse: {
1037 case SWT.TRAVERSE_RETURN:
1038 String text = combo.getText();
1039 modifier.modify(text);
1040 if (!item.isDisposed()) {
1041 item.setText(columnIndex, text);
1042 queueSelectionRefresh(context);
1044 arrowTraverseUsed = false;
1046 case SWT.TRAVERSE_ESCAPE:
1050 case SWT.TRAVERSE_ARROW_NEXT:
1051 case SWT.TRAVERSE_ARROW_PREVIOUS:
1052 arrowTraverseUsed = true;
1055 //System.out.println("unhandled traversal: " + e.detail);
1061 currentlyModifiedNodes.remove(context);
1062 deactivateEditingContext();
1067 combo.addListener(SWT.MouseWheel, VetoingEventHandler.INSTANCE);
1068 combo.addListener(SWT.KeyDown, comboListener);
1069 combo.addListener(SWT.FocusOut, comboListener);
1070 combo.addListener(SWT.Traverse, comboListener);
1071 combo.addListener(SWT.Selection, comboListener);
1072 combo.addListener(SWT.Dispose, comboListener);
1074 editor.setEditor(combo, item, columnIndex);
1077 combo.setListVisible(true);
1079 GraphExplorerImpl.this.reconfigureTreeEditorForText(item, columnIndex, combo, combo.getText(), SWT.DEFAULT, 0, 0);
1081 activateEditingContext(combo);
1083 // Removed in comboListener
1084 currentlyModifiedNodes.add(context);
1086 //System.out.println("START ENUMERATION EDITING: " + item);
1091 * @param columnIndex
1095 void startTextEditing(final TreeItem item, final int columnIndex, final NodeContext context, final Modifier modifier) {
1096 String initialText = modifier.getValue();
1097 if (initialText == null)
1098 throw new AssertionError("Labeler.Modifier.getValue() returned null, modifier=" + modifier);
1100 final Composite composite = new Composite(tree, SWT.NONE);
1101 //composite.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_RED));
1102 final Text text = new Text(composite, SWT.BORDER);
1103 final int insetX = 0;
1104 final int insetY = 0;
1105 composite.addListener(SWT.Resize, new Listener() {
1107 public void handleEvent(Event e) {
1108 Rectangle rect = composite.getClientArea();
1109 text.setBounds(rect.x + insetX, rect.y + insetY, rect.width - insetX * 2, rect.height
1113 final FilteringModifier filter = modifier instanceof FilteringModifier ? (FilteringModifier) modifier : null;
1114 Listener textListener = new Listener() {
1116 boolean modified = false;
1119 public void handleEvent(final Event e) {
1125 //System.out.println("FOCUS OUT " + item);
1126 newText = text.getText();
1127 error = modifier.isValid(newText);
1128 if (error == null) {
1129 modifier.modify(newText);
1131 // Item may be disposed if the tree gets reset after a previous editing.
1132 if (!item.isDisposed()) {
1133 item.setText(columnIndex, newText);
1134 queueSelectionRefresh(context);
1137 // System.out.println("validation error: " + error);
1140 composite.dispose();
1143 newText = text.getText();
1144 error = modifier.isValid(newText);
1145 if (error != null) {
1146 text.setBackground(invalidModificationColor);
1148 //System.out.println("validation error: " + error);
1150 text.setBackground(null);
1157 // Safety check since it seems that this may happen with
1159 if (item.isDisposed())
1162 // Filter input if necessary
1163 e.text = filter != null ? filter.filter(e.text) : e.text;
1165 newText = text.getText();
1166 String leftText = newText.substring(0, e.start);
1167 String rightText = newText.substring(e.end, newText.length());
1168 GraphExplorerImpl.this.reconfigureTreeEditorForText(
1169 item, columnIndex, text, leftText + e.text + rightText,
1170 SWT.DEFAULT, insetX, insetY);
1174 case SWT.TRAVERSE_RETURN:
1176 newText = text.getText();
1177 error = modifier.isValid(newText);
1178 if (error == null) {
1179 modifier.modify(newText);
1180 if (!item.isDisposed()) {
1181 item.setText(columnIndex, newText);
1182 queueSelectionRefresh(context);
1187 case SWT.TRAVERSE_ESCAPE:
1188 composite.dispose();
1192 //System.out.println("unhandled traversal: " + e.detail);
1198 currentlyModifiedNodes.remove(context);
1199 deactivateEditingContext();
1205 // Set the initial text before registering a listener. We do not want immediate modification!
1206 text.setText(initialText);
1207 text.addListener(SWT.FocusOut, textListener);
1208 text.addListener(SWT.Traverse, textListener);
1209 text.addListener(SWT.Verify, textListener);
1210 text.addListener(SWT.Modify, textListener);
1211 text.addListener(SWT.Dispose, textListener);
1212 editor.setEditor(composite, item, columnIndex);
1216 // Initialize TreeEditor properly.
1217 GraphExplorerImpl.this.reconfigureTreeEditorForText(
1218 item, columnIndex, text, initialText,
1219 SWT.DEFAULT, insetX, insetY);
1221 // Removed in textListener
1222 currentlyModifiedNodes.add(context);
1224 activateEditingContext(text);
1226 //System.out.println("START TEXT EDITING: " + item);
1229 protected void errorStatus(String error) {
1230 IStatusLineManager status = getStatusLineManager();
1231 if (status != null) {
1232 status.setErrorMessage(error);
1236 protected IStatusLineManager getStatusLineManager() {
1237 if (serviceLocator instanceof IWorkbenchPart) {
1238 return WorkbenchUtils.getStatusLine((IWorkbenchPart) serviceLocator);
1239 } else if (serviceLocator instanceof IWorkbenchSite) {
1240 return WorkbenchUtils.getStatusLine((IWorkbenchSite) serviceLocator);
1245 protected void activateEditingContext(Control control) {
1246 if (contextService != null) {
1247 editingContext = contextService.activateContext(INLINE_EDITING_UI_CONTEXT);
1249 if (control != null && focusService != null) {
1250 focusService.addFocusTracker(control, INLINE_EDITING_UI_CONTEXT);
1251 // No need to remove the control, it will be
1252 // removed automatically when it is disposed.
1256 protected void deactivateEditingContext() {
1257 IContextActivation a = editingContext;
1259 editingContext = null;
1260 contextService.deactivateContext(a);
1267 void queueSelectionRefresh(NodeContext forContext) {
1268 selectionRefreshContexts.add(forContext);
1272 public String startEditing(NodeContext context, String columnKey_) {
1273 assertNotDisposed();
1274 if (!thread.currentThreadAccess())
1275 throw new IllegalStateException("not in SWT display thread " + thread.getThread());
1277 String columnKey = columnKey_;
1278 if(columnKey.startsWith("#")) {
1279 columnKey = columnKey.substring(1);
1282 Integer columnIndex = columnKeyToIndex.get(columnKey);
1283 if (columnIndex == null)
1284 return "Rename not supported for selection";
1286 TreeItem item = contextToItem.getRight(context);
1288 return "Rename not supported for selection";
1290 return startEditing(item, columnIndex, columnKey_);
1295 public String startEditing(String columnKey) {
1297 ISelection selection = postSelectionProvider.getSelection();
1298 if(selection == null) return "Rename not supported for selection";
1299 NodeContext context = ISelectionUtils.filterSingleSelection(selection, NodeContext.class);
1300 if(context == null) return "Rename not supported for selection";
1302 return startEditing(context, columnKey);
1307 * @param site <code>null</code> if the explorer is detached from the workbench
1308 * @param parent the parent SWT composite
1309 * @param style the tree style to use, check the see tags for the available flags
1314 * @see SWT#FULL_SELECTION
1315 * @see SWT#NO_SCROLL
1319 public GraphExplorerImpl(Composite parent, int style) {
1321 setServiceLocator(null);
1323 this.localResourceManager = new LocalResourceManager(JFaceResources.getResources());
1324 this.resourceManager = new DeviceResourceManager(parent.getDisplay());
1326 this.imageLoaderJob = new ImageLoaderJob(this);
1327 this.imageLoaderJob.setPriority(Job.DECORATE);
1329 invalidModificationColor = (Color) localResourceManager.get( ColorDescriptor.createFrom( new RGB(255, 128, 128) ) );
1331 this.thread = SWTThread.getThreadAccess(parent);
1333 for(int i=0;i<10;i++) explorerContext.activity.push(0);
1335 tree = new Tree(parent, style);
1336 tree.addListener(SWT.SetData, this);
1337 tree.addListener(SWT.Expand, this);
1338 tree.addListener(SWT.Dispose, this);
1339 tree.addListener(SWT.Activate, this);
1341 tree.setData(KEY_GRAPH_EXPLORER, this);
1343 // These are both required for performing column resizing without flicker.
1344 // See SWT.Resize event handling in #handleEvent() for more explanations.
1345 parent.addListener(SWT.Resize, this);
1346 tree.addListener(SWT.Resize, this);
1348 originalFont = JFaceResources.getDefaultFontDescriptor();
1349 // originalBackground = JFaceResources.getColorRegistry().get(symbolicName);
1350 // originalForeground = tree.getForeground();
1352 tree.setFont((Font) localResourceManager.get(originalFont));
1354 columns = new Column[] { new Column(ColumnKeys.SINGLE) };
1355 columnKeyToIndex = Collections.singletonMap(ColumnKeys.SINGLE, 0);
1357 editor = new TreeEditor(tree);
1358 editor.horizontalAlignment = SWT.LEFT;
1359 editor.grabHorizontal = true;
1360 editor.minimumWidth = 50;
1362 setBasicListeners();
1363 setDefaultProcessors();
1365 this.toolTip = new GraphExplorerToolTip(explorerContext, tree);
1369 public IThreadWorkQueue getThread() {
1373 TreeItem previousSingleSelection = null;
1374 long focusGainedAt = Long.MIN_VALUE;
1376 protected GraphExplorerToolTip toolTip;
1378 protected void setBasicListeners() {
1379 // Keep track of the previous single selection to help
1380 // decide whether to start editing a tree node on mouse
1382 tree.addListener(SWT.Selection, new Listener() {
1384 public void handleEvent(Event event) {
1385 TreeItem[] selection = tree.getSelection();
1386 if (selection.length == 1) {
1387 //for (TreeItem item : selection)
1388 // System.out.println("selection: " + item);
1389 previousSingleSelection = selection[0];
1391 previousSingleSelection = null;
1396 // Try to start editing of tree column when clicked for the second time.
1397 Listener mouseEditListener = new Listener() {
1399 Future<?> startEdit = null;
1402 public void handleEvent(Event event) {
1403 if (event.type == SWT.DragDetect) {
1404 // Needed to prevent editing from being started when in fact
1405 // the user starts dragging an item.
1406 //System.out.println("DRAG DETECT: " + event);
1410 //System.out.println("mouse down: " + event);
1411 if (event.button == 1) {
1412 // Always ignore the first mouse button press that focuses
1413 // the control. Do not let it start in-line editing since
1414 // that is very annoying to users and not how the UI's that
1415 // people are used to behave.
1416 long eventTime = ((long) event.time) & 0xFFFFFFFFL;
1417 if ((eventTime - focusGainedAt) < 250L) {
1418 //System.out.println("ignore mouse down " + focusGainedAt + ", " + eventTime + " = " + (eventTime-focusGainedAt));
1421 //System.out.println("testing whether to start editing");
1423 final Point point = new Point(event.x, event.y);
1424 final TreeItem item = tree.getItem(point);
1427 //System.out.println("mouse down @ " + point + ": " + item + ", previous item: " + previousSingleSelection);
1429 // Only start editing if the item was already selected.
1430 if (!item.equals(previousSingleSelection)) {
1435 if (tree.getColumnCount() > 1) {
1436 // TODO: reconsider this logic, might not be good in general.
1437 for (int i = 0; i < tree.getColumnCount(); i++) {
1438 if (item.getBounds(i).contains(point)) {
1439 tryScheduleEdit(event, item, point, 100, i);
1444 //System.out.println("clicks: " + event.count);
1445 if (item.getBounds().contains(point)) {
1446 if (event.count == 1) {
1447 tryScheduleEdit(event, item, point, 500, 0);
1456 void tryScheduleEdit(Event event, final TreeItem item, Point point, long delayMs, final int column) {
1457 //System.out.println("\tCONTAINS: " + item);
1461 //System.out.println("\tScheduling edit: " + item);
1462 startEdit = ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
1465 ThreadUtils.asyncExec(thread, new Runnable() {
1468 if (item.isDisposed())
1470 startEditing(item, column, null);
1474 }, delayMs, TimeUnit.MILLISECONDS);
1477 boolean cancelEdit() {
1478 Future<?> f = startEdit;
1480 // Try to cancel the start edit task if it's not running yet.
1483 boolean ret = f.cancel(false);
1484 //System.out.println("\tCancelled edit: " + ret);
1488 //System.out.println("\tNo edit in progress to cancel");
1492 tree.addListener(SWT.MouseDown, mouseEditListener);
1493 tree.addListener(SWT.DragDetect, mouseEditListener);
1494 tree.addListener(SWT.DragDetect, new Listener() {
1496 public void handleEvent(Event event) {
1497 Point test = new Point(event.x, event.y);
1498 TreeItem item = tree.getItem(test);
1500 for(int i=0;i<tree.getColumnCount();i++) {
1501 Rectangle rect = item.getBounds(i);
1502 if(rect.contains(test)) {
1503 tree.setData(KEY_DRAG_COLUMN, i);
1508 tree.setData(KEY_DRAG_COLUMN, -1);
1511 tree.addListener(SWT.MouseMove, new Listener() {
1513 public void handleEvent(Event event) {
1514 Point test = new Point(event.x, event.y);
1515 TreeItem item = tree.getItem(test);
1517 for(int i=0;i<tree.getColumnCount();i++) {
1518 Rectangle rect = item.getBounds(i);
1519 if(rect.contains(test)) {
1520 transientState.setActiveColumn(i);
1525 transientState.setActiveColumn(null);
1529 // Add focus/mouse/key listeners for supporting the respective
1530 // add/remove listener methods in IGraphExplorer.
1531 tree.addFocusListener(new FocusListener() {
1533 public void focusGained(FocusEvent e) {
1534 focusGainedAt = ((long) e.time) & 0xFFFFFFFFL;
1535 for (FocusListener listener : focusListeners)
1536 listener.focusGained(e);
1539 public void focusLost(FocusEvent e) {
1540 for (FocusListener listener : focusListeners)
1541 listener.focusLost(e);
1544 tree.addMouseListener(new MouseListener() {
1546 public void mouseDoubleClick(MouseEvent e) {
1547 for (MouseListener listener : mouseListeners) {
1548 listener.mouseDoubleClick(e);
1552 public void mouseDown(MouseEvent e) {
1553 for (MouseListener listener : mouseListeners) {
1554 listener.mouseDown(e);
1558 public void mouseUp(MouseEvent e) {
1559 for (MouseListener listener : mouseListeners) {
1560 listener.mouseUp(e);
1564 tree.addKeyListener(new KeyListener() {
1566 public void keyPressed(KeyEvent e) {
1567 for (KeyListener listener : keyListeners) {
1568 listener.keyPressed(e);
1572 public void keyReleased(KeyEvent e) {
1573 for (KeyListener listener : keyListeners) {
1574 listener.keyReleased(e);
1579 // Add a tree selection listener for keeping the selection of
1580 // GraphExplorer's ISelectionProvider up-to-date.
1581 tree.addSelectionListener(new SelectionListener() {
1583 public void widgetDefaultSelected(SelectionEvent e) {
1587 public void widgetSelected(SelectionEvent e) {
1588 widgetSelectionChanged(false);
1592 // This listener takes care of updating the set of currently selected
1593 // TreeItem instances. This set is needed because we need to know in
1594 // #setData(Event) whether the updated item was a part of the current
1595 // selection in which case the selection must be updated.
1596 selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
1598 public void selectionChanged(SelectionChangedEvent event) {
1599 //System.out.println("selection changed: " + event.getSelection());
1600 Set<NodeContext> set = ISelectionUtils.filterSetSelection(event.getSelection(), NodeContext.class);
1601 selectedItems.clear();
1602 for (NodeContext nc : set) {
1603 TreeItem item = contextToItem.getRight(nc);
1605 selectedItems.put(item, nc);
1607 //System.out.println("newly selected items: " + selectedItems);
1613 * Mod count for delaying post selection changed events.
1615 int postSelectionModCount = 0;
1618 * Last tree selection modification time for implementing a quiet
1619 * time for selection changes.
1621 long lastSelectionModTime = System.currentTimeMillis() - 10000;
1624 * Current target time for the selection to be set. Calculated
1625 * according to the set quiet time and last selection modification
1628 long selectionSetTargetTime = 0;
1631 * <code>true</code> if delayed selection runnable is current scheduled or
1634 boolean delayedSelectionScheduled = false;
1636 Runnable SELECTION_DELAY = new Runnable() {
1639 if (tree.isDisposed())
1641 long now = System.currentTimeMillis();
1642 long waitTimeLeft = selectionSetTargetTime - now;
1643 if (waitTimeLeft > 0) {
1644 // Not enough quiet time, reschedule.
1645 delayedSelectionScheduled = true;
1646 tree.getDisplay().timerExec((int) waitTimeLeft, this);
1648 // Time to perform selection, stop rescheduling.
1649 delayedSelectionScheduled = false;
1655 private void widgetSelectionChanged(boolean forceSelectionChange) {
1656 long modTime = System.currentTimeMillis();
1657 long delta = modTime - lastSelectionModTime;
1658 lastSelectionModTime = modTime;
1659 if (!forceSelectionChange && delta < SELECTION_CHANGE_QUIET_TIME) {
1660 long msToWait = SELECTION_CHANGE_QUIET_TIME - delta;
1661 selectionSetTargetTime = modTime + msToWait;
1662 if (!delayedSelectionScheduled) {
1663 delayedSelectionScheduled = true;
1664 tree.getDisplay().timerExec((int) msToWait, SELECTION_DELAY);
1666 // Make sure that post selection change events do not fire.
1667 ++postSelectionModCount;
1671 // Immediate selection reconstruction.
1675 private void resetSelection() {
1676 final ISelection selection = getWidgetSelection();
1678 //System.out.println("resetSelection(" + postSelectionModCount + ")");
1679 //System.out.println(" provider selection: " + selectionProvider.getSelection());
1680 //System.out.println(" widget selection: " + selection);
1682 selectionProvider.setAndFireNonEqualSelection(selection);
1684 // Implement deferred firing of post selection events
1685 final int count = ++postSelectionModCount;
1686 //System.out.println("[" + System.currentTimeMillis() + "] scheduling postSelectionChanged " + count + ": " + selection);
1687 ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
1690 int newCount = postSelectionModCount;
1691 // Don't publish selection yet, there's another change incoming.
1692 //System.out.println("[" + System.currentTimeMillis() + "] checking post selection publish: " + count + " vs. " + newCount + ": " + selection);
1693 if (newCount != count)
1695 //System.out.println("[" + System.currentTimeMillis() + "] " + count + " count equals, firing post selection listeners: " + selection);
1697 if (tree.isDisposed())
1700 //System.out.println("scheduling fire post selection changed: " + selection);
1701 tree.getDisplay().asyncExec(new Runnable() {
1704 if (tree.isDisposed() || selectionProvider == null)
1706 //System.out.println("firing post selection changed: " + selection);
1707 selectionProvider.firePostSelection(selection);
1711 }, POST_SELECTION_DELAY, TimeUnit.MILLISECONDS);
1714 protected void setDefaultProcessors() {
1715 // Add a simple IMAGER query processor that always returns null.
1716 // With this processor no images will ever be shown.
1717 // setPrimitiveProcessor(new StaticImagerProcessor(null));
1719 setProcessor(new DefaultComparableChildrenProcessor());
1720 setProcessor(new DefaultLabelDecoratorsProcessor());
1721 setProcessor(new DefaultImageDecoratorsProcessor());
1722 setProcessor(new DefaultSelectedLabelerProcessor());
1723 setProcessor(new DefaultLabelerFactoriesProcessor());
1724 setProcessor(new DefaultSelectedImagerProcessor());
1725 setProcessor(new DefaultImagerFactoriesProcessor());
1726 setPrimitiveProcessor(new DefaultLabelerProcessor());
1727 setPrimitiveProcessor(new DefaultCheckedStateProcessor());
1728 setPrimitiveProcessor(new DefaultImagerProcessor());
1729 setPrimitiveProcessor(new DefaultLabelDecoratorProcessor());
1730 setPrimitiveProcessor(new DefaultImageDecoratorProcessor());
1731 setPrimitiveProcessor(new NoSelectionRequestProcessor());
1733 setProcessor(new DefaultFinalChildrenProcessor(this));
1735 setProcessor(new DefaultPrunedChildrenProcessor());
1736 setProcessor(new DefaultSelectedViewpointProcessor());
1737 setProcessor(new DefaultSelectedLabelDecoratorFactoriesProcessor());
1738 setProcessor(new DefaultSelectedImageDecoratorFactoriesProcessor());
1739 setProcessor(new DefaultViewpointContributionsProcessor());
1741 setPrimitiveProcessor(new DefaultViewpointProcessor());
1742 setPrimitiveProcessor(new DefaultViewpointContributionProcessor());
1743 setPrimitiveProcessor(new DefaultSelectedViewpointFactoryProcessor());
1744 setPrimitiveProcessor(new DefaultIsExpandedProcessor());
1745 setPrimitiveProcessor(new DefaultShowMaxChildrenProcessor());
1749 public <T> void setProcessor(NodeQueryProcessor<T> processor) {
1750 assertNotDisposed();
1751 if (processor == null)
1752 throw new IllegalArgumentException("null processor");
1754 processors.put(processor.getIdentifier(), processor);
1758 public <T> void setPrimitiveProcessor(PrimitiveQueryProcessor<T> processor) {
1759 assertNotDisposed();
1760 if (processor == null)
1761 throw new IllegalArgumentException("null processor");
1763 PrimitiveQueryProcessor<?> oldProcessor = primitiveProcessors.put(processor.getIdentifier(), processor);
1765 if (oldProcessor instanceof ProcessorLifecycle)
1766 ((ProcessorLifecycle) oldProcessor).detached(this);
1767 if (processor instanceof ProcessorLifecycle)
1768 ((ProcessorLifecycle) processor).attached(this);
1772 public <T> void setDataSource(DataSource<T> provider) {
1773 assertNotDisposed();
1774 if (provider == null)
1775 throw new IllegalArgumentException("null provider");
1776 dataSources.put(provider.getProvidedClass(), provider);
1779 @SuppressWarnings("unchecked")
1781 public <T> DataSource<T> removeDataSource(Class<T> forProvidedClass) {
1782 assertNotDisposed();
1783 if (forProvidedClass == null)
1784 throw new IllegalArgumentException("null class");
1785 return dataSources.remove(forProvidedClass);
1789 public void setPersistor(StatePersistor persistor) {
1790 this.persistor = persistor;
1794 public SelectionDataResolver getSelectionDataResolver() {
1795 return selectionDataResolver;
1799 public void setSelectionDataResolver(SelectionDataResolver r) {
1800 this.selectionDataResolver = r;
1804 public SelectionFilter getSelectionFilter() {
1805 return selectionFilter;
1809 public void setSelectionFilter(SelectionFilter f) {
1810 this.selectionFilter = f;
1811 // TODO: re-filter current selection?
1815 public void setSelectionTransformation(BiFunction<GraphExplorer, Object[], Object[]> f) {
1816 this.selectionTransformation = f;
1820 public <T> void addListener(T listener) {
1821 if(listener instanceof FocusListener) {
1822 focusListeners.add((FocusListener)listener);
1823 } else if(listener instanceof MouseListener) {
1824 mouseListeners.add((MouseListener)listener);
1825 } else if(listener instanceof KeyListener) {
1826 keyListeners.add((KeyListener)listener);
1831 public <T> void removeListener(T listener) {
1832 if(listener instanceof FocusListener) {
1833 focusListeners.remove(listener);
1834 } else if(listener instanceof MouseListener) {
1835 mouseListeners.remove(listener);
1836 } else if(listener instanceof KeyListener) {
1837 keyListeners.remove(listener);
1841 public void addSelectionListener(SelectionListener listener) {
1842 tree.addSelectionListener(listener);
1845 public void removeSelectionListener(SelectionListener listener) {
1846 tree.removeSelectionListener(listener);
1849 private Set<String> uiContexts;
1852 public void setUIContexts(Set<String> contexts) {
1853 this.uiContexts = contexts;
1857 public void setRoot(final Object root) {
1858 if(uiContexts != null && uiContexts.size() == 1)
1859 setRootContext0(NodeContextBuilder.buildWithData(BuiltinKeys.INPUT, root, BuiltinKeys.UI_CONTEXT, uiContexts.iterator().next()));
1861 setRootContext0(NodeContextBuilder.buildWithData(BuiltinKeys.INPUT, root));
1865 public void setRootContext(final NodeContext context) {
1866 setRootContext0(context);
1869 private void setRootContext0(final NodeContext context) {
1870 Assert.isNotNull(context, "root must not be null");
1871 if (isDisposed() || tree.isDisposed())
1873 Display display = tree.getDisplay();
1874 if (display.getThread() == Thread.currentThread()) {
1877 display.asyncExec(new Runnable() {
1886 private void initializeState() {
1887 if (persistor == null)
1890 ExplorerState state = persistor.deserialize(
1891 Platform.getStateLocation(Activator.getDefault().getBundle()).toFile(),
1894 // topNodeToSet will be processed by #setData when it encounters a
1895 // NodeContext that matches this one.
1896 // topNodePath = state.topNodePath;
1897 // topNodePathChildIndex = state.topNodePathChildIndex;
1898 // currentTopNodePathIndex = 0;
1900 Object processor = getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
1901 if (processor instanceof DefaultIsExpandedProcessor) {
1902 DefaultIsExpandedProcessor isExpandedProcessor = (DefaultIsExpandedProcessor)processor;
1903 for(NodeContext expanded : state.expandedNodes) {
1904 isExpandedProcessor.setExpanded(expanded, true);
1909 private void saveState() {
1910 if (persistor == null)
1913 NodeContext[] topNodePath = NodeContext.NONE;
1914 int[] topNodePathChildIndex = {};
1915 Collection<NodeContext> expandedNodes = Collections.emptyList();
1916 Map<String, Integer> columnWidths = Collections.<String, Integer> emptyMap();
1918 // Resolve top node path
1919 TreeItem topItem = tree.getTopItem();
1920 if (topItem != null) {
1921 NodeContext topNode = (NodeContext) topItem.getData();
1922 if (topNode != null) {
1923 topNodePath = getNodeContextPathSegments(topNode);
1924 topNodePathChildIndex = new int[topNodePath.length];
1925 for (int i = 0; i < topNodePath.length; ++i) {
1926 // TODO: get child indexes
1927 topNodePathChildIndex[i] = 0;
1932 // Resolve expanded nodes
1933 Object processor = getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
1934 if (processor instanceof IsExpandedProcessor) {
1935 IsExpandedProcessor isExpandedProcessor = (IsExpandedProcessor) processor;
1936 expandedNodes = isExpandedProcessor.getExpanded();
1940 TreeColumn[] columns = tree.getColumns();
1941 if (columns.length > 1) {
1942 columnWidths = new HashMap<String, Integer>();
1943 for (int i = 0; i < columns.length; ++i) {
1944 columnWidths.put(columns[i].getText(), columns[i].getWidth());
1948 persistor.serialize(
1949 Platform.getStateLocation(Activator.getDefault().getBundle()).toFile(),
1951 new ExplorerState(topNodePath, topNodePathChildIndex, expandedNodes, columnWidths));
1955 * Invoke only from SWT thread to reset the root of the graph explorer tree.
1959 private void doSetRoot(NodeContext root) {
1960 if (tree.isDisposed())
1962 if (root.getConstant(BuiltinKeys.INPUT) == null) {
1963 ErrorLogger.defaultLogError("root node context does not contain BuiltinKeys.INPUT key. Node = " + root, new Exception("trace"));
1967 // Empty caches, release queries.
1968 GraphExplorerContext oldContext = explorerContext;
1969 GraphExplorerContext newContext = new GraphExplorerContext();
1970 GENodeQueryManager manager = new GENodeQueryManager(newContext, null, null, TreeItemReference.create(null));
1971 this.explorerContext = newContext;
1972 oldContext.safeDispose();
1973 toolTip.setGraphExplorerContext(explorerContext);
1975 // Need to empty these or otherwise they won't be emptied until the
1976 // explorer is disposed which would mean that many unwanted references
1977 // will be held by this map.
1978 clearPrimitiveProcessors();
1980 this.rootContext = root.getConstant(BuiltinKeys.IS_ROOT) != null ? root
1981 : NodeContextUtil.withConstant(root, BuiltinKeys.IS_ROOT, Boolean.TRUE);
1983 explorerContext.getCache().incRef(this.rootContext);
1987 NodeContext[] contexts = manager.query(rootContext, BuiltinKeys.FINAL_CHILDREN);
1989 tree.setItemCount(contexts.length);
1991 select(rootContext);
1992 refreshColumnSizes();
1996 public NodeContext getRoot() {
2001 public NodeContext getParentContext(NodeContext context) {
2003 throw new IllegalStateException("disposed");
2004 if (!thread.currentThreadAccess())
2005 throw new IllegalStateException("not in SWT display thread " + thread.getThread());
2007 TreeItem item = contextToItem.getRight(context);
2008 if(item == null) return null;
2009 TreeItem parentItem = item.getParentItem();
2010 if(parentItem == null) return null;
2011 return (NodeContext)parentItem.getData();
2014 Point previousTreeSize;
2015 Point previousTreeParentSize;
2016 boolean activatedBefore = false;
2019 public void handleEvent(Event event) {
2020 //System.out.println("EVENT: " + event);
2021 switch(event.type) {
2023 //System.out.println("EXPAND: " + event.item);
2024 if ((tree.getStyle() & SWT.VIRTUAL) != 0) {
2025 expandVirtual(event);
2027 System.out.println("TODO: non-virtual tree item expand");
2031 // Only invoked for SWT.VIRTUAL trees
2033 // Happened for Hannu once during program startup.
2034 // java.lang.AssertionError
2035 // at org.simantics.browsing.ui.common.internal.GENodeQueryManager.query(GENodeQueryManager.java:190)
2036 // at org.simantics.browsing.ui.swt.GraphExplorerImpl.setData(GraphExplorerImpl.java:2315)
2037 // at org.simantics.browsing.ui.swt.GraphExplorerImpl.handleEvent(GraphExplorerImpl.java:2039)
2038 // I do not know whether SWT guarantees that SetData events
2039 // don't come after Dispose event has been issued, but I
2040 // think its better to have this check here just incase.
2045 // This ensures that column sizes are refreshed at
2046 // least once when the GE is first shown.
2047 if (!activatedBefore) {
2048 refreshColumnSizes();
2049 activatedBefore = true;
2053 //new Exception().printStackTrace();
2060 if (event.widget == tree) {
2061 // This case is meant for listening to tree width increase.
2062 // The column resizing must be performed only after the tree
2063 // itself as been resized.
2064 Point size = tree.getSize();
2066 if (previousTreeSize != null) {
2067 dx = size.x - previousTreeSize.x;
2069 previousTreeSize = size;
2070 //System.out.println("RESIZE: " + dx + " - size=" + size);
2073 tree.setRedraw(false);
2074 refreshColumnSizes(size);
2075 tree.setRedraw(true);
2077 } else if (event.widget == tree.getParent()) {
2078 // This case is meant for listening to tree width decrease.
2079 // The columns must be resized before the tree widget itself
2080 // is resized to prevent scroll bar flicker. This can be achieved
2081 // by listening to the resize events of the tree parent widget.
2082 Composite parent = tree.getParent();
2083 Point size = parent.getSize();
2085 // We must subtract the parent's border and possible
2086 // scroll bar width from the new target width of the columns.
2087 size.x -= tree.getParent().getBorderWidth() * 2;
2088 ScrollBar vBar = parent.getVerticalBar();
2089 if (vBar != null && vBar.isVisible())
2090 size.x -= vBar.getSize().x;
2093 if (previousTreeParentSize != null) {
2094 dx = size.x - previousTreeParentSize.x;
2096 previousTreeParentSize = size;
2097 //System.out.println("RESIZE: " + dx + " - size=" + size);
2100 tree.setRedraw(false);
2101 refreshColumnSizes(size);
2102 tree.setRedraw(true);
2112 protected void refreshColumnSizes() {
2113 // Composite treeParent = tree.getParent();
2114 // Point size = treeParent.getSize();
2115 // size.x -= treeParent.getBorderWidth() * 2;
2116 Point size = tree.getSize();
2117 refreshColumnSizes(size);
2118 tree.getParent().layout();
2122 * This has been disabled since the logic of handling column widths has been
2123 * externalized to parties creating {@link GraphExplorerImpl} instances.
2125 protected void refreshColumnSizes(Point toSize) {
2127 refreshingColumnSizes = true;
2129 int columnCount = tree.getColumnCount();
2130 if (columnCount > 0) {
2131 Point size = toSize;
2132 int targetWidth = size.x - tree.getBorderWidth() * 2;
2135 // Take the vertical scroll bar existence into to account when
2136 // calculating the overflow column width.
2137 ScrollBar vBar = tree.getVerticalBar();
2138 //if (vBar != null && vBar.isVisible())
2140 targetWidth -= vBar.getSize().x;
2142 List<TreeColumn> resizing = new ArrayList<TreeColumn>();
2144 int resizingWidth = 0;
2145 int totalWeight = 0;
2146 for (int i = 0; i < columnCount - 1; ++i) {
2147 TreeColumn col = tree.getColumn(i);
2148 //System.out.println(" " + col.getText() + ": " + col.getWidth());
2149 int width = col.getWidth();
2151 Column c = (Column) col.getData();
2154 resizingWidth += width;
2155 totalWeight += c.getWeight();
2159 int requiredWidthAdjustment = targetWidth - usedWidth;
2160 if (requiredWidthAdjustment < 0)
2161 requiredWidthAdjustment = Math.min(requiredWidthAdjustment, -resizing.size());
2162 double diff = requiredWidthAdjustment;
2163 //System.out.println("REQUIRED WIDTH ADJUSTMENT: " + requiredWidthAdjustment);
2165 // Decide how much to give space to / take space from each grabbing column
2166 double wrel = 1.0 / resizing.size();
2168 double[] weightedShares = new double[resizing.size()];
2169 for (int i = 0; i < resizing.size(); ++i) {
2170 TreeColumn col = resizing.get(i);
2171 Column c = (Column) col.getData();
2172 if (totalWeight == 0) {
2173 weightedShares[i] = wrel;
2175 weightedShares[i] = (double) c.getWeight() / (double) totalWeight;
2178 //System.out.println("grabbing columns:" + resizing);
2179 //System.out.println("weighted space distribution: " + Arrays.toString(weightedShares));
2181 // Always shrink the columns if necessary, but don't enlarge before
2182 // there is sufficient space to at least give all resizable columns
2184 if (diff < 0 || (diff > 0 && diff > resizing.size())) {
2185 // Need to either shrink or enlarge the resizable columns if possible.
2186 for (int i = 0; i < resizing.size(); ++i) {
2187 TreeColumn col = resizing.get(i);
2188 Column c = (Column) col.getData();
2189 int cw = col.getWidth();
2190 //double wrel = (double) cw / (double) resizingWidth;
2191 //int delta = Math.min((int) Math.round(wrel * diff), requiredWidthAdjustment);
2192 double ddelta = weightedShares[i] * diff;
2195 delta = (int) Math.floor(ddelta);
2197 delta = Math.min((int) Math.floor(ddelta), requiredWidthAdjustment);
2199 //System.out.println("size delta(" + col.getText() + "): " + ddelta + " => " + delta);
2200 //System.out.println("argh(" + col.getText() + "): " + c.getWidth() + " vs. " + col.getWidth() + " vs. " + (cw+delta));
2201 int newWidth = Math.max(c.getWidth(), cw + delta);
2202 requiredWidthAdjustment -= (newWidth - cw);
2203 col.setWidth(newWidth);
2207 //System.out.println("FILLER WIDTH LEFT: " + requiredWidthAdjustment);
2209 TreeColumn last = tree.getColumn(columnCount - 1);
2210 // HACK: see #setColumns for why this is here.
2211 if (FILLER.equals(last.getText())) {
2212 last.setWidth(Math.max(0, requiredWidthAdjustment));
2216 refreshingColumnSizes = false;
2221 private void doDispose() {
2222 explorerContext.dispose();
2224 // No longer necessary, the used executors are shared.
2225 //scheduler.shutdown();
2226 //scheduler2.shutdown();
2229 detachPrimitiveProcessors();
2230 primitiveProcessors.clear();
2231 dataSources.clear();
2233 pendingItems.clear();
2237 contextToItem.clear();
2239 mouseListeners.clear();
2241 selectionProvider.clearListeners();
2242 selectionProvider = null;
2243 selectionDataResolver = null;
2244 selectionRefreshContexts.clear();
2245 selectedItems.clear();
2246 originalFont = null;
2248 localResourceManager.dispose();
2250 // Must shutdown image loader job before disposing its ResourceManager
2251 imageLoaderJob.dispose();
2252 imageLoaderJob.cancel();
2254 imageLoaderJob.join();
2255 } catch (InterruptedException e) {
2256 ErrorLogger.defaultLogError(e);
2258 resourceManager.dispose();
2260 postSelectionProvider.dispose();
2264 private void expandVirtual(final Event event) {
2265 TreeItem item = (TreeItem) event.item;
2266 assert (item != null);
2267 NodeContext context = (NodeContext) item.getData();
2268 assert (context != null);
2270 GENodeQueryManager manager = new GENodeQueryManager(this.explorerContext, null, null, TreeItemReference.create(item));
2271 NodeContext[] children = manager.query(context, BuiltinKeys.FINAL_CHILDREN);
2272 int maxChildren = getMaxChildren(manager, context);
2273 item.setItemCount(children.length < maxChildren ? children.length : maxChildren);
2276 private NodeContext getNodeContext(TreeItem item) {
2277 assert(item != null);
2279 NodeContext context = (NodeContext)item.getData();
2280 assert(context != null);
2285 private NodeContext getParentContext(TreeItem item) {
2286 TreeItem parentItem = item.getParentItem();
2287 if(parentItem != null) {
2288 return getNodeContext(parentItem);
2294 private static final String LISTENER_SET_INDICATOR = "LSI";
2295 private static final String PENDING = "PENDING";
2296 private int contextSelectionChangeModCount = 0;
2299 * Only invoked for SWT.VIRTUAL widgets.
2303 private void setData(final Event event) {
2304 assert (event != null);
2305 TreeItem item = (TreeItem) event.item;
2306 assert (item != null);
2308 // Based on experience it seems to be possible that
2309 // SetData events are sent for disposed TreeItems.
2310 if (item.isDisposed() || item.getData(PENDING) != null)
2313 //System.out.println("GE.SetData " + item);
2315 GENodeQueryManager manager = new GENodeQueryManager(this.explorerContext, null, null, TreeItemReference.create(item.getParentItem()));
2317 NodeContext parentContext = getParentContext(item);
2318 assert (parentContext != null);
2320 NodeContext[] parentChildren = manager.query(parentContext, BuiltinKeys.FINAL_CHILDREN);
2322 // Some children have disappeared since counting
2323 if (event.index < 0) {
2324 ErrorLogger.defaultLogError("GraphExplorer.setData: how can event.index be < 0: " + event.index + " ??", new Exception());
2327 if (event.index >= parentChildren.length)
2330 NodeContext context = parentChildren[event.index];
2331 assert (context != null);
2332 item.setData(context);
2334 // Manage NodeContext -> TreeItem mappings
2335 contextToItem.map(context, item);
2336 if (item.getData(LISTENER_SET_INDICATOR) == null) {
2337 // This "if" exists because setData will get called many
2338 // times for the same (NodeContext, TreeItem) pairs.
2339 // Each TreeItem only needs one listener, but this
2340 // is needed to tell whether it already has a listener
2342 item.setData(LISTENER_SET_INDICATOR, LISTENER_SET_INDICATOR);
2343 item.addListener(SWT.Dispose, itemDisposeListener);
2346 boolean isExpanded = manager.query(context, BuiltinKeys.IS_EXPANDED);
2348 PrunedChildrenResult children = manager.query(context, BuiltinKeys.PRUNED_CHILDREN);
2349 int maxChildren = getMaxChildren(manager, context);
2350 //item.setItemCount(children.getPrunedChildren().length < maxChildren ? children.getPrunedChildren().length : maxChildren);
2352 NodeContext[] pruned = children.getPrunedChildren();
2353 int count = Math.min(pruned.length, maxChildren);
2355 if (isExpanded || item.getItemCount() > 1) {
2356 item.setItemCount(count);
2357 TreeItem[] childItems = item.getItems();
2358 for(int i=0;i<count;i++)
2359 contextToItem.map(pruned[i], childItems[i]);
2361 if (children.getPrunedChildren().length == 0) {
2362 item.setItemCount(0);
2364 // item.setItemCount(1);
2365 item.setItemCount(count);
2366 TreeItem[] childItems = item.getItems();
2367 for(int i=0;i<count;i++)
2368 contextToItem.map(pruned[i], childItems[i]);
2369 // item.getItem(0).setData(PENDING, PENDING);
2370 // item.getItem(0).setItemCount(o);
2374 setTextAndImage(item, manager, context, event.index);
2376 // Check if the node should be auto-expanded?
2377 if ((autoExpandLevel == ALL_LEVELS || autoExpandLevel > 1) && !isExpanded) {
2378 //System.out.println("NOT EXPANDED(" +context + ", " + item + ")");
2379 int level = getTreeItemLevel(item);
2380 if ((autoExpandLevel == ALL_LEVELS || level <= autoExpandLevel)
2381 && !explorerContext.autoExpanded.containsKey(context))
2383 //System.out.println("AUTO-EXPANDING(" + context + ", " + item + ")");
2384 explorerContext.autoExpanded.put(context, Boolean.TRUE);
2385 setExpanded(context, true);
2389 item.setExpanded(isExpanded);
2391 if ((tree.getStyle() & SWT.CHECK) != 0) {
2392 CheckedState checked = manager.query(context, BuiltinKeys.IS_CHECKED);
2393 item.setChecked(CheckedState.CHECKED_STATES.contains(checked));
2394 item.setGrayed(CheckedState.GRAYED == checked);
2397 //System.out.println("GE.SetData completed " + item);
2399 // This test makes sure that selectionProvider holds the correct
2400 // selection with respect to the actual selection stored by the virtual
2402 // The data items shown below the items occupied by the selected and now removed data
2403 // will be squeezed to use the tree items previously used for the now
2404 // removed data. When this happens, the NodeContext items stored by the
2405 // tree items will be different from what the GraphExplorer's
2406 // ISelectionProvider thinks the selection currently is. To compensate,
2407 // 1. Recognize the situation
2408 // 2. ASAP set the selection provider selection to what is actually
2409 // offered by the tree widget.
2410 NodeContext selectedContext = selectedItems.get(item);
2411 // System.out.println("selectedContext(" + item + "): " + selectedContext);
2412 if (selectedContext != null && !selectedContext.equals(context)) {
2413 final int modCount = ++contextSelectionChangeModCount;
2414 // System.out.println("SELECTION MUST BE UPDATED (modCount=" + modCount + "): " + item);
2415 // System.out.println(" old context: " + selectedContext);
2416 // System.out.println(" new context: " + context);
2417 // System.out.println(" provider selection: " + selectionProvider.getSelection());
2418 // System.out.println(" widget selection: " + getWidgetSelection());
2419 ThreadUtils.asyncExec(thread, new Runnable() {
2424 int count = contextSelectionChangeModCount;
2425 // System.out.println("MODCOUNT: " + modCount + " vs. " + count);
2426 if (modCount != count)
2428 widgetSelectionChanged(true);
2433 // This must be done to keep the visible tree selection properly
2434 // in sync with the selectionProvider JFace proxy of this class in
2435 // cases where an in-line editor was previously active for the node
2437 if (selectionRefreshContexts.remove(context)) {
2438 final ISelection currentSelection = selectionProvider.getSelection();
2439 // asyncExec is here to prevent ui glitches that
2440 // seem to occur if the selection setting is done
2441 // directly here in between setData invocations.
2442 ThreadUtils.asyncExec(thread, new Runnable() {
2447 // System.out.println("REFRESHING SELECTION: " + currentSelection);
2448 // System.out.println("BEFORE setSelection: " + Arrays.toString(tree.getSelection()));
2449 // System.out.println("BEFORE setSelection: " + selectionProvider.getSelection());
2450 setSelection(currentSelection, true);
2451 // System.out.println("AFTER setSelection: " + Arrays.toString(tree.getSelection()));
2452 // System.out.println("AFTER setSelection: " + selectionProvider.getSelection());
2457 // TODO: doesn't work if any part of the node path that should be
2458 // revealed is out of view.
2459 // Disabled until a better solution is devised.
2460 // Suggestion: include item indexes into the stored node context path
2461 // to make it possible for this method to know whether the current
2462 // node path segment is currently out of view based on event.index.
2463 // If out of view, this code needs to scroll the view programmatically
2465 // if (currentTopNodePathIndex >= 0 && topNodePath.length > 0) {
2466 // NodeContext topNode = topNodePath[currentTopNodePathIndex];
2467 // if (topNode.equals(context)) {
2468 // final TreeItem topItem = item;
2469 // ++currentTopNodePathIndex;
2470 // if (currentTopNodePathIndex >= topNodePath.length) {
2471 // // Mission accomplished. End search for top node here.
2472 // topNodePath = NodeContext.NONE;
2473 // currentTopNodePathIndex = -1;
2475 // ThreadUtils.asyncExec(thread, new Runnable() {
2477 // public void run() {
2478 // if (isDisposed())
2480 // tree.setTopItem(topItem);
2486 // Check if vertical scroll bar has become visible and refresh layout.
2487 ScrollBar verticalBar = tree.getVerticalBar();
2488 if(verticalBar != null) {
2489 boolean currentlyVerticalBarVisible = verticalBar.isVisible();
2490 if (verticalBarVisible != currentlyVerticalBarVisible) {
2491 verticalBarVisible = currentlyVerticalBarVisible;
2492 Composite parent = tree.getParent();
2500 * @return see {@link GraphExplorer#setAutoExpandLevel(int)} for how the
2501 * return value is calculated. Items without parents have level=2,
2502 * their children level=3, etc. Returns 0 for invalid items
2504 private int getTreeItemLevel(TreeItem item) {
2508 for (TreeItem parent = item; parent != null; parent = parent.getParentItem(), ++level);
2509 //System.out.println("\tgetTreeItemLevel(" + parent + ")");
2510 //System.out.println("level(" + item + "): " + level);
2518 private NodeContext[] getNodeContextPathSegments(NodeContext node) {
2519 TreeItem item = contextToItem.getRight(node);
2521 return NodeContext.NONE;
2522 int level = getTreeItemLevel(item);
2524 return NodeContext.NONE;
2525 // Exclude root from the saved node path.
2527 NodeContext[] segments = new NodeContext[level];
2528 for (TreeItem parent = item; parent != null; parent = parent.getParentItem(), --level) {
2529 NodeContext ctx = (NodeContext) item.getData();
2531 return NodeContext.NONE;
2532 segments[level-1] = ctx;
2541 @SuppressWarnings("unused")
2542 private NodeContextPath getNodeContextPath(NodeContext node) {
2543 NodeContext[] path = getNodeContextPathSegments(node);
2544 return new NodeContextPath(path);
2547 void setImage(NodeContext node, TreeItem item, Imager imager, Collection<ImageDecorator> decorators, int itemIndex) {
2548 Image[] images = columnImageArray;
2549 Arrays.fill(images, null);
2550 if (imager == null) {
2551 item.setImage(images);
2555 Object[] descOrImage = columnDescOrImageArray;
2556 Arrays.fill(descOrImage, null);
2557 boolean finishLoadingInJob = false;
2559 for (Column column : columns) {
2560 String key = column.getKey();
2561 ImageDescriptor desc = imager.getImage(key);
2563 // Attempt to decorate the label
2564 if (!decorators.isEmpty()) {
2565 for (ImageDecorator id : decorators) {
2566 ImageDescriptor ds = id.decorateImage(desc, key, itemIndex);
2572 // Try resolving only cached images here and now
2573 Object img = localResourceManager.find(desc);
2575 img = resourceManager.find(desc);
2577 images[index] = img != null ? (Image) img : null;
2578 descOrImage[index] = img == null ? desc : img;
2579 finishLoadingInJob |= img == null;
2584 // Finish loading the final image in the image loader job if necessary.
2585 if (finishLoadingInJob) {
2586 // Prevent UI from flashing unnecessarily by reusing the old image
2587 // in the item if it exists.
2588 for (int c = 0; c < columns.length; ++c) {
2589 Image img = item.getImage(c);
2593 item.setImage(images);
2595 // Schedule loading to another thread to refrain from blocking
2596 // the UI with database operations.
2597 queueImageTask(item, new ImageTask(
2600 Arrays.copyOf(descOrImage, descOrImage.length)));
2602 // Set any images that were resolved.
2603 item.setImage(images);
2607 private void queueImageTask(TreeItem item, ImageTask task) {
2608 synchronized (imageTasks) {
2609 imageTasks.put(item, task);
2611 imageLoaderJob.scheduleIfNecessary(100);
2615 * Invoked in a job worker thread.
2618 * @see ImageLoaderJob
2621 protected IStatus setPendingImages(IProgressMonitor monitor) {
2622 ImageTask[] tasks = null;
2623 synchronized (imageTasks) {
2624 tasks = imageTasks.values().toArray(new ImageTask[imageTasks.size()]);
2627 if (tasks.length == 0)
2628 return Status.OK_STATUS;
2630 MultiStatus status = null;
2632 // Load missing images
2633 for (ImageTask task : tasks) {
2634 Object[] descs = task.descsOrImages;
2635 for (int i = 0; i < descs.length; ++i) {
2636 Object obj = descs[i];
2637 if (obj instanceof ImageDescriptor) {
2638 ImageDescriptor desc = (ImageDescriptor) obj;
2640 descs[i] = resourceManager.get((ImageDescriptor) desc);
2641 } catch (DeviceResourceException e) {
2643 status = new MultiStatus(Activator.PLUGIN_ID, 0, "Problems loading images:", null);
2644 status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Image descriptor loading failed: " + desc, e));
2650 // Perform final UI updates in the UI thread.
2651 final ImageTask[] _tasks = tasks;
2652 thread.asyncExec(new Runnable() {
2655 if (!tree.isDisposed()) {
2656 tree.setRedraw(false);
2658 tree.setRedraw(true);
2663 return status != null ? status : Status.OK_STATUS;
2667 * Invoked in the UI thread only.
2671 void setImages(ImageTask[] tasks) {
2672 for (ImageTask task : tasks)
2678 * Invoked in the UI thread only.
2682 void setImage(ImageTask task) {
2683 // Be sure not to process disposed items.
2684 if (task.item.isDisposed())
2686 // Discard this task if the TreeItem has switched owning NodeContext.
2687 if (!contextToItem.contains(task.node, task.item))
2690 Object[] descs = task.descsOrImages;
2691 Image[] images = columnImageArray;
2692 Arrays.fill(images, null);
2693 for (int i = 0; i < descs.length; ++i) {
2694 Object desc = descs[i];
2695 if (desc instanceof Image) {
2696 images[i] = (Image) desc;
2699 task.item.setImage(images);
2702 void setText(TreeItem item, Labeler labeler, Collection<LabelDecorator> decorators, int itemIndex) {
2703 if (labeler != null) {
2704 String[] texts = new String[columns.length];
2706 Map<String, String> labels = labeler.getLabels();
2707 Map<String, String> runtimeLabels = labeler.getRuntimeLabels();
2708 for (Column column : columns) {
2709 String key = column.getKey();
2711 if (runtimeLabels != null) s = runtimeLabels.get(key);
2712 if (s == null) s = labels.get(key);
2714 FontDescriptor font = originalFont;
2715 ColorDescriptor bg = originalBackground;
2716 ColorDescriptor fg = originalForeground;
2718 // Attempt to decorate the label
2719 if (!decorators.isEmpty()) {
2720 for (LabelDecorator ld : decorators) {
2721 String ds = ld.decorateLabel(s, key, itemIndex);
2725 FontDescriptor dfont = ld.decorateFont(font, key, itemIndex);
2729 ColorDescriptor dbg = ld.decorateBackground(bg, key, itemIndex);
2733 ColorDescriptor dfg = ld.decorateForeground(fg, key, itemIndex);
2739 if (font != originalFont) {
2740 //System.out.println("set font: " + index + ": " + font);
2741 item.setFont(index, (Font) localResourceManager.get(font));
2743 if (bg != originalBackground)
2744 item.setBackground(index, (Color) localResourceManager.get(bg));
2745 if (fg != originalForeground)
2746 item.setForeground(index, (Color) localResourceManager.get(fg));
2752 item.setText(texts);
2754 item.setText(Labeler.NO_LABEL);
2758 void setTextAndImage(TreeItem item, NodeQueryManager manager, NodeContext context, int itemIndex) {
2759 Labeler labeler = manager.query(context, BuiltinKeys.SELECTED_LABELER);
2760 if (labeler != null) {
2761 labeler.setListener(labelListener);
2763 Imager imager = manager.query(context, BuiltinKeys.SELECTED_IMAGER);
2764 Collection<LabelDecorator> labelDecorators = manager.query(context, BuiltinKeys.LABEL_DECORATORS);
2765 Collection<ImageDecorator> imageDecorators = manager.query(context, BuiltinKeys.IMAGE_DECORATORS);
2767 setText(item, labeler, labelDecorators, itemIndex);
2768 setImage(context, item, imager, imageDecorators, itemIndex);
2772 public void setFocus() {
2777 public <T> T query(NodeContext context, CacheKey<T> key) {
2778 return this.explorerContext.cache.get(context, key);
2782 public boolean isDisposed() {
2786 protected void assertNotDisposed() {
2788 throw new IllegalStateException("disposed");
2795 * @param forceControlUpdate
2798 public void setSelection(final ISelection selection, boolean forceControlUpdate) {
2799 assertNotDisposed();
2800 boolean equalsOld = selectionProvider.selectionEquals(selection);
2801 if (equalsOld && !forceControlUpdate) {
2802 // Just set the selection object instance, fire no events nor update
2803 // the viewer selection.
2804 selectionProvider.setSelection(selection);
2806 // Schedule viewer and selection update if necessary.
2807 if (tree.isDisposed())
2809 Display d = tree.getDisplay();
2810 if (d.getThread() == Thread.currentThread()) {
2811 updateSelectionToControl(selection);
2813 d.asyncExec(new Runnable() {
2816 if (tree.isDisposed())
2818 updateSelectionToControl(selection);
2826 /* Contains the best currently found tree item and its priority
2828 private static class SelectionResolutionStatus {
2829 int bestPriority = Integer.MAX_VALUE;
2837 private void updateSelectionToControl(ISelection selection) {
2838 if (selectionDataResolver == null)
2840 if (!(selection instanceof IStructuredSelection))
2843 // Initialize selection resolution status map
2844 IStructuredSelection iss = (IStructuredSelection) selection;
2845 final THashMap<Object,SelectionResolutionStatus> statusMap =
2846 new THashMap<Object,SelectionResolutionStatus>(iss.size());
2847 for(Iterator<?> it = iss.iterator(); it.hasNext();) {
2848 Object selectionElement = it.next();
2849 Object resolvedElement = selectionDataResolver.resolve(selectionElement);
2852 new SelectionResolutionStatus());
2855 // Iterate all tree items and try to match them to the selection
2856 iterateTreeItems(new TObjectProcedure<TreeItem>() {
2858 public boolean execute(TreeItem treeItem) {
2859 NodeContext nodeContext = (NodeContext)treeItem.getData();
2860 if(nodeContext == null)
2862 SelectionResolutionStatus status = statusMap.get(nodeContext);
2863 if(status != null) {
2864 status.bestPriority = 0; // best possible match
2865 status.bestItem = treeItem;
2869 Object input = nodeContext.getConstant(BuiltinKeys.INPUT);
2870 status = statusMap.get(input);
2871 if(status != null) {
2872 NodeType nodeType = nodeContext.getConstant(NodeType.TYPE);
2873 int curPriority = nodeType instanceof EntityNodeType
2874 ? 1 // Prefer EntityNodeType matches to other node types
2876 if(curPriority < status.bestPriority) {
2877 status.bestPriority = curPriority;
2878 status.bestItem = treeItem;
2886 ArrayList<TreeItem> items = new ArrayList<TreeItem>(statusMap.size());
2887 for(SelectionResolutionStatus status : statusMap.values())
2888 if(status.bestItem != null)
2889 items.add(status.bestItem);
2890 select(items.toArray(new TreeItem[items.size()]));
2896 public ISelection getWidgetSelection() {
2897 TreeItem[] items = tree.getSelection();
2898 if (items.length == 0)
2899 return StructuredSelection.EMPTY;
2901 List<NodeContext> nodes = new ArrayList<NodeContext>(items.length);
2903 // Caches for resolving node contexts the hard way if necessary.
2904 GENodeQueryManager manager = null;
2905 NodeContext lastParentContext = null;
2906 NodeContext[] lastChildren = null;
2908 for (int i = 0; i < items.length; i++) {
2909 TreeItem item = items[i];
2910 NodeContext ctx = (NodeContext) item.getData();
2911 // It may happen due to the virtual nature of the tree control
2912 // that it contains TreeItems which have not yet been ran through
2917 TreeItem parentItem = item.getParentItem();
2918 NodeContext parentContext = parentItem != null ? getNodeContext(parentItem) : rootContext;
2919 if (parentContext != null) {
2920 NodeContext[] children = lastChildren;
2921 if (parentContext != lastParentContext) {
2922 if (manager == null)
2923 manager = new GENodeQueryManager(this.explorerContext, null, null, null);
2924 lastChildren = children = manager.query(parentContext, BuiltinKeys.FINAL_CHILDREN);
2925 lastParentContext = parentContext;
2927 int index = parentItem != null ? parentItem.indexOf(item) : tree.indexOf(item);
2928 if (index >= 0 && index < children.length) {
2929 NodeContext child = children[index];
2930 if (child != null) {
2932 // Cache NodeContext in TreeItem for faster access
2933 item.setData(child);
2939 //System.out.println("widget selection " + items.length + " items / " + nodes.size() + " node contexts");
2940 ISelection selection = constructSelection(nodes.toArray(NodeContext.NONE));
2945 public TransientExplorerState getTransientState() {
2946 if (!thread.currentThreadAccess())
2947 throw new AssertionError(getClass().getSimpleName() + ".getActiveColumn called from non SWT-thread: " + Thread.currentThread());
2948 return transientState;
2955 private void select(TreeItem item) {
2956 tree.setSelection(item);
2957 tree.showSelection();
2958 selectionProvider.setAndFireNonEqualSelection(constructSelection((NodeContext) item.getData()));
2965 private void select(TreeItem[] items) {
2966 //System.out.println("Select: " + Arrays.toString(items));
2967 tree.setSelection(items);
2968 tree.showSelection();
2969 NodeContext[] data = new NodeContext[items.length];
2970 for (int i = 0; i < data.length; i++) {
2971 data[i] = (NodeContext) items[i].getData();
2973 selectionProvider.setAndFireNonEqualSelection(constructSelection(data));
2976 private void iterateTreeItems(TObjectProcedure<TreeItem> procedure) {
2977 for(TreeItem item : tree.getItems())
2978 if(!iterateTreeItems(item, procedure))
2982 private boolean iterateTreeItems(TreeItem item,
2983 TObjectProcedure<TreeItem> procedure) {
2984 if(!procedure.execute(item))
2986 if(item.getExpanded())
2987 for(TreeItem child : item.getItems())
2988 if(!iterateTreeItems(child, procedure))
2998 private boolean trySelect(TreeItem item, Object input) {
2999 NodeContext itemCtx = (NodeContext) item.getData();
3000 if (itemCtx != null) {
3001 if (input.equals(itemCtx.getConstant(BuiltinKeys.INPUT))) {
3006 if (item.getExpanded()) {
3007 for (TreeItem child : item.getItems()) {
3008 if (trySelect(child, input))
3015 private boolean equalsEnough(NodeContext c1, NodeContext c2) {
3017 Object input1 = c1.getConstant(BuiltinKeys.INPUT);
3018 Object input2 = c2.getConstant(BuiltinKeys.INPUT);
3019 if(!ObjectUtils.objectEquals(input1, input2))
3022 Object type1 = c1.getConstant(NodeType.TYPE);
3023 Object type2 = c2.getConstant(NodeType.TYPE);
3024 if(!ObjectUtils.objectEquals(type1, type2))
3031 private NodeContext tryFind(NodeContext context) {
3032 for (TreeItem item : tree.getItems()) {
3033 NodeContext found = tryFind(item, context);
3034 if(found != null) return found;
3039 private NodeContext tryFind(TreeItem item, NodeContext context) {
3040 NodeContext itemCtx = (NodeContext) item.getData();
3041 if (itemCtx != null) {
3042 if (equalsEnough(context, itemCtx)) {
3046 if (item.getExpanded()) {
3047 for (TreeItem child : item.getItems()) {
3048 NodeContext found = tryFind(child, context);
3049 if(found != null) return found;
3056 public boolean select(NodeContext context) {
3058 assertNotDisposed();
3060 if (context == null || context.equals(rootContext)) {
3062 selectionProvider.setAndFireNonEqualSelection(TreeSelection.EMPTY);
3066 // if (context.equals(rootContext)) {
3067 // tree.deselectAll();
3068 // selectionProvider.setAndFireNonEqualSelection(constructSelection(context));
3072 Object input = context.getConstant(BuiltinKeys.INPUT);
3074 for (TreeItem item : tree.getItems()) {
3075 if (trySelect(item, input))
3083 private NodeContext tryFind2(NodeContext context) {
3084 Set<NodeContext> ctxs = contextToItem.getLeftSet();
3085 for(NodeContext c : ctxs)
3086 if(equalsEnough(c, context))
3091 private boolean waitVisible(NodeContext parent, NodeContext context) {
3092 long start = System.nanoTime();
3094 TreeItem parentItem = contextToItem.getRight(parent);
3096 if(parentItem == null)
3100 NodeContext target = tryFind2(context);
3101 if(target != null) {
3102 TreeItem item = contextToItem.getRight(target);
3103 if (!(item.getParentItem().equals(parentItem)))
3105 tree.setTopItem(item);
3109 Display.getCurrent().readAndDispatch();
3110 long duration = System.nanoTime() - start;
3116 private boolean selectPathInternal(NodeContext[] contexts, int position) {
3117 //System.out.println("NodeContext path : " + contexts);
3119 NodeContext head = tryFind(contexts[position]);
3121 if(position == contexts.length-1) {
3122 return select(head);
3126 //setExpanded(head, true);
3128 if(!waitVisible(head, contexts[position+1]))
3131 setExpanded(head, true);
3133 return selectPathInternal(contexts, position+1);
3138 public boolean selectPath(Collection<NodeContext> contexts) {
3140 if(contexts == null) throw new IllegalArgumentException("Null list is not allowed");
3141 if(contexts.isEmpty()) throw new IllegalArgumentException("Empty list is not allowed");
3143 return selectPathInternal(contexts.toArray(new NodeContext[contexts.size()]), 0);
3148 public boolean isVisible(NodeContext context) {
3150 for (TreeItem item : tree.getItems()) {
3151 NodeContext found = tryFind(item, context);
3160 protected ISelection constructSelection(NodeContext... contexts) {
3161 if (contexts == null)
3162 throw new IllegalArgumentException("null contexts");
3163 if (contexts.length == 0)
3164 return StructuredSelection.EMPTY;
3165 if (selectionFilter == null)
3166 return new StructuredSelection(transformSelection(contexts));
3167 return new StructuredSelection( transformSelection(filter(selectionFilter, contexts)) );
3170 protected Object[] transformSelection(Object[] objects) {
3171 return selectionTransformation.apply(this, objects);
3174 protected static Object[] filter(SelectionFilter filter, NodeContext[] contexts) {
3175 int len = contexts.length;
3176 Object[] objects = new Object[len];
3177 for (int i = 0; i < len; ++i)
3178 objects[i] = filter.filter(contexts[i]);
3183 public void setExpanded(final NodeContext context, final boolean expanded) {
3184 assertNotDisposed();
3185 ThreadUtils.asyncExec(thread, new Runnable() {
3189 doSetExpanded(context, expanded);
3194 private void doSetExpanded(NodeContext context, boolean expanded) {
3195 //System.out.println("doSetExpanded(" + context + ", " + expanded + ")");
3196 TreeItem item = contextToItem.getRight(context);
3198 item.setExpanded(expanded);
3200 PrimitiveQueryProcessor<?> pqp = explorerContext.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
3201 if (pqp instanceof IsExpandedProcessor) {
3202 IsExpandedProcessor iep = (IsExpandedProcessor) pqp;
3203 iep.replaceExpanded(context, expanded);
3208 public void setColumnsVisible(boolean visible) {
3209 columnsAreVisible = visible;
3210 if(tree != null) tree.setHeaderVisible(columnsAreVisible);
3214 public void setColumns(final Column[] columns) {
3215 setColumns(columns, null);
3219 public void setColumns(final Column[] columns, Consumer<Map<Column, Object>> callback) {
3220 assertNotDisposed();
3221 checkUniqueColumnKeys(columns);
3223 Display d = tree.getDisplay();
3224 if (d.getThread() == Thread.currentThread())
3225 doSetColumns(columns, callback);
3228 if (tree.isDisposed())
3230 doSetColumns(columns, callback);
3234 private void checkUniqueColumnKeys(Column[] cols) {
3235 Set<String> usedColumnKeys = new HashSet<String>();
3236 List<Column> duplicateColumns = new ArrayList<Column>();
3237 for (Column c : cols) {
3238 if (!usedColumnKeys.add(c.getKey()))
3239 duplicateColumns.add(c);
3241 if (!duplicateColumns.isEmpty()) {
3242 throw new IllegalArgumentException("All columns do not have unique keys: " + cols + ", overlapping: " + duplicateColumns);
3247 * Only meant to be invoked from the SWT UI thread.
3251 private void doSetColumns(Column[] cols, Consumer<Map<Column, Object>> callback) {
3252 // Attempt to keep previous column widths.
3253 Map<String, Integer> prevWidths = new HashMap<String, Integer>();
3254 for (TreeColumn column : tree.getColumns()) {
3255 prevWidths.put(column.getText(), column.getWidth());
3259 HashMap<String, Integer> keyToIndex = new HashMap<String, Integer>();
3260 for (int i = 0; i < cols.length; ++i) {
3261 keyToIndex.put(cols[i].getKey(), i);
3264 this.columns = Arrays.copyOf(cols, cols.length);
3265 //this.columns[cols.length] = FILLER_COLUMN;
3266 this.columnKeyToIndex = keyToIndex;
3267 this.columnImageArray = new Image[cols.length];
3268 this.columnDescOrImageArray = new Object[cols.length];
3270 Map<Column, Object> map = new HashMap<Column, Object>();
3272 tree.setHeaderVisible(columnsAreVisible);
3273 for (Column column : columns) {
3274 TreeColumn c = new TreeColumn(tree, toSWT(column.getAlignment()));
3277 c.setText(column.getLabel());
3278 c.setToolTipText(column.getTooltip());
3280 int cw = column.getWidth();
3282 // Try to keep previous widths
3283 Integer w = prevWidths.get(column);
3286 else if (cw != Column.DEFAULT_CONTROL_WIDTH)
3289 // Go for some kind of default settings then...
3290 if (ColumnKeys.PROPERTY.equals(column.getKey()))
3296 // if (!column.hasGrab() && !FILLER.equals(column.getKey())) {
3297 // c.addListener(SWT.Resize, resizeListener);
3298 // c.setResizable(true);
3300 // //c.setResizable(false);
3305 if(callback != null) callback.accept(map);
3307 // Make sure the explorer fits the columns properly after initialization.
3308 tree.getDisplay().asyncExec(new Runnable() {
3311 if (tree.isDisposed())
3313 refreshColumnSizes();
3318 int toSWT(Align alignment) {
3319 switch (alignment) {
3320 case LEFT: return SWT.LEFT;
3321 case CENTER: return SWT.CENTER;
3322 case RIGHT: return SWT.RIGHT;
3323 default: throw new Error("unhandled alignment: " + alignment);
3328 public Column[] getColumns() {
3329 return Arrays.copyOf(columns, columns.length);
3332 private void detachPrimitiveProcessors() {
3333 for (PrimitiveQueryProcessor<?> p : primitiveProcessors.values()) {
3334 if (p instanceof ProcessorLifecycle) {
3335 ((ProcessorLifecycle) p).detached(this);
3340 private void clearPrimitiveProcessors() {
3341 for (PrimitiveQueryProcessor<?> p : primitiveProcessors.values()) {
3342 if (p instanceof ProcessorLifecycle) {
3343 ((ProcessorLifecycle) p).clear();
3348 Listener resizeListener = new Listener() {
3350 public void handleEvent(Event event) {
3351 // Prevent infinite recursion.
3352 if (refreshingColumnSizes)
3354 //TreeColumn column = (TreeColumn) event.widget;
3355 //Column c = (Column) column.getData();
3356 refreshColumnSizes();
3360 Listener itemDisposeListener = new Listener() {
3362 public void handleEvent(Event event) {
3363 if (event.type == SWT.Dispose) {
3364 if (event.widget instanceof TreeItem) {
3365 TreeItem ti = (TreeItem) event.widget;
3366 //NodeContext ctx = (NodeContext) ti.getData();
3367 // System.out.println("DISPOSE CONTEXT TO ITEM: " + ctx + " -> " + System.identityHashCode(ti));
3368 // System.out.println(" map size BEFORE: " + contextToItem.size());
3369 @SuppressWarnings("unused")
3370 NodeContext removed = contextToItem.removeWithRight(ti);
3371 // System.out.println(" REMOVED: " + removed);
3372 // System.out.println(" map size AFTER: " + contextToItem.size());
3381 LabelerListener labelListener = new LabelerListener() {
3383 public boolean columnModified(final NodeContext context, final String key, final String newLabel) {
3384 //System.out.println("column " + key + " modified for " + context + " to " + newLabel);
3385 if (tree.isDisposed())
3388 synchronized (labelRefreshRunnables) {
3389 Runnable refresher = new Runnable() {
3392 // Tree is guaranteed to be non-disposed if this is invoked.
3394 // contextToItem should be accessed only in the SWT thread to keep things thread-safe.
3395 final TreeItem item = contextToItem.getRight(context);
3396 if (item == null || item.isDisposed())
3399 final Integer index = columnKeyToIndex.get(key);
3403 //System.out.println(" found index: " + index);
3404 //System.out.println(" found item: " + item);
3406 GENodeQueryManager manager = new GENodeQueryManager(explorerContext, null, null, null);
3408 // FIXME: indexOf is quadratic
3410 TreeItem parentItem = item.getParentItem();
3411 if (parentItem == null) {
3412 itemIndex = tree.indexOf(item);
3413 //tree.clear(parentIndex, false);
3415 itemIndex = parentItem.indexOf(item);
3416 //item.clear(parentIndex, false);
3418 setTextAndImage(item, manager, context, itemIndex);
3419 } catch (SWTException e) {
3420 ErrorLogger.defaultLogError(e);
3424 //System.out.println(System.currentTimeMillis() + " queueing label refresher: " + refresher);
3425 labelRefreshRunnables.put(context, refresher);
3427 if (!refreshIsQueued) {
3428 refreshIsQueued = true;
3430 long now = System.currentTimeMillis();
3431 long elapsed = now - lastLabelRefreshScheduled;
3432 if (elapsed < DEFAULT_CONSECUTIVE_LABEL_REFRESH_DELAY)
3433 delay = DEFAULT_CONSECUTIVE_LABEL_REFRESH_DELAY - elapsed;
3434 //System.out.println("scheduling with delay: " + delay + " (" + lastLabelRefreshScheduled + " -> " + now + " = " + elapsed + ")");
3436 ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
3439 scheduleImmediateLabelRefresh();
3441 }, delay, TimeUnit.MILLISECONDS);
3443 scheduleImmediateLabelRefresh();
3445 lastLabelRefreshScheduled = now;
3452 public boolean columnsModified(final NodeContext context, final Map<String, String> columns) {
3453 System.out.println("TODO: implement GraphExplorerImpl.labelListener.columnsModified");
3458 private void scheduleImmediateLabelRefresh() {
3459 Runnable[] runnables = null;
3460 synchronized (labelRefreshRunnables) {
3461 if (labelRefreshRunnables.isEmpty())
3464 runnables = labelRefreshRunnables.values().toArray(new Runnable[labelRefreshRunnables.size()]);
3465 labelRefreshRunnables.clear();
3466 refreshIsQueued = false;
3468 final Runnable[] rs = runnables;
3470 if (tree.isDisposed())
3472 tree.getDisplay().asyncExec(new Runnable() {
3475 if (tree.isDisposed())
3477 //System.out.println(System.currentTimeMillis() + " EXECUTING " + rs.length + " label refresh runnables");
3478 tree.setRedraw(false);
3479 for (Runnable r : rs) {
3482 tree.setRedraw(true);
3487 long lastLabelRefreshScheduled = 0;
3488 boolean refreshIsQueued = false;
3489 Map<NodeContext, Runnable> labelRefreshRunnables = new HashMap<NodeContext, Runnable>();
3491 @SuppressWarnings("unchecked")
3493 public <T> T getAdapter(Class<T> adapter) {
3494 if(ISelectionProvider.class == adapter) return (T) postSelectionProvider;
3495 else if(IPostSelectionProvider.class == adapter) return (T) postSelectionProvider;
3499 @SuppressWarnings("unchecked")
3501 public <T> T getControl() {
3506 * @see org.simantics.browsing.ui.GraphExplorer#setAutoExpandLevel(int)
3509 public void setAutoExpandLevel(int level) {
3510 this.autoExpandLevel = level;
3514 public <T> NodeQueryProcessor<T> getProcessor(QueryKey<T> key) {
3515 return explorerContext.getProcessor(key);
3519 public <T> PrimitiveQueryProcessor<T> getPrimitiveProcessor(PrimitiveQueryKey<T> key) {
3520 return explorerContext.getPrimitiveProcessor(key);
3524 public boolean isEditable() {
3529 public void setEditable(boolean editable) {
3530 if (!thread.currentThreadAccess())
3531 throw new IllegalStateException("not in SWT display thread " + thread.getThread());
3533 this.editable = editable;
3534 Display display = tree.getDisplay();
3535 tree.setBackground(editable ? null : display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
3539 * For setting a more local service locator for the explorer than the global
3540 * workbench service locator. Sometimes required to give this implementation
3541 * access to local workbench services like IFocusService.
3544 * Must be invoked during right after construction.
3546 * @param serviceLocator
3547 * a specific service locator or <code>null</code> to use the
3548 * workbench global service locator
3550 public void setServiceLocator(IServiceLocator serviceLocator) {
3551 if (serviceLocator == null && PlatformUI.isWorkbenchRunning())
3552 serviceLocator = PlatformUI.getWorkbench();
3553 this.serviceLocator = serviceLocator;
3554 if (serviceLocator != null) {
3555 this.contextService = (IContextService) serviceLocator.getService(IContextService.class);
3556 this.focusService = (IFocusService) serviceLocator.getService(IFocusService.class);
3561 public Object getClicked(Object event) {
3562 MouseEvent e = (MouseEvent)event;
3563 final Tree tree = (Tree) e.getSource();
3564 Point point = new Point(e.x, e.y);
3565 TreeItem item = tree.getItem(point);
3567 // No selectable item at point?
3571 Object data = item.getData();