2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.intellij
.ide
.util
.treeView
;
19 import com
.intellij
.ide
.projectView
.PresentationData
;
20 import com
.intellij
.openapi
.Disposable
;
21 import com
.intellij
.openapi
.application
.Application
;
22 import com
.intellij
.openapi
.application
.ApplicationManager
;
23 import com
.intellij
.openapi
.progress
.ProgressIndicator
;
24 import com
.intellij
.openapi
.util
.ActionCallback
;
25 import com
.intellij
.util
.containers
.HashSet
;
26 import com
.intellij
.util
.ui
.UIUtil
;
27 import com
.intellij
.util
.ui
.update
.MergingUpdateQueue
;
28 import org
.jetbrains
.annotations
.NotNull
;
29 import org
.jetbrains
.annotations
.Nullable
;
32 import javax
.swing
.tree
.DefaultMutableTreeNode
;
33 import javax
.swing
.tree
.DefaultTreeModel
;
34 import javax
.swing
.tree
.TreeNode
;
35 import javax
.swing
.tree
.TreePath
;
36 import java
.lang
.ref
.WeakReference
;
39 public class AbstractTreeBuilder
implements Disposable
{
40 private AbstractTreeUi myUi
;
41 private static final String TREE_BUILDER
= "TreeBuilder";
42 public static final boolean DEFAULT_UPDATE_INACTIVE
= true;
44 public AbstractTreeBuilder(JTree tree
,
45 DefaultTreeModel treeModel
,
46 AbstractTreeStructure treeStructure
,
47 @Nullable Comparator
<NodeDescriptor
> comparator
) {
48 this(tree
, treeModel
, treeStructure
, comparator
, DEFAULT_UPDATE_INACTIVE
);
50 public AbstractTreeBuilder(JTree tree
,
51 DefaultTreeModel treeModel
,
52 AbstractTreeStructure treeStructure
,
53 @Nullable Comparator
<NodeDescriptor
> comparator
,
54 boolean updateIfInactive
) {
55 init(tree
, treeModel
, treeStructure
, comparator
, updateIfInactive
);
58 protected AbstractTreeBuilder() {
63 protected void init(final JTree tree
, final DefaultTreeModel treeModel
, final AbstractTreeStructure treeStructure
, final @Nullable Comparator
<NodeDescriptor
> comparator
,
64 final boolean updateIfInactive
) {
66 tree
.putClientProperty(TREE_BUILDER
, new WeakReference(this));
69 getUi().init(this, tree
, treeModel
, treeStructure
, comparator
, updateIfInactive
);
71 setPassthroughMode(isUnitTestingMode());
74 protected AbstractTreeUi
createUi() {
75 return new AbstractTreeUi();
78 public final void select(final Object element
) {
79 getUi().userSelect(new Object
[] {element
}, null, false, true);
82 public final void select(final Object element
, @Nullable final Runnable onDone
) {
83 getUi().userSelect(new Object
[] {element
}, onDone
, false, true);
86 public final void select(final Object element
, @Nullable final Runnable onDone
, boolean addToSelection
) {
87 getUi().userSelect(new Object
[] {element
}, onDone
, addToSelection
, true);
90 public final void select(final Object
[] elements
, @Nullable final Runnable onDone
) {
91 getUi().userSelect(elements
, onDone
, false, true);
94 public final void select(final Object
[] elements
, @Nullable final Runnable onDone
, boolean addToSelection
) {
95 getUi().userSelect(elements
, onDone
, addToSelection
, true);
98 public final void expand(Object element
, @Nullable Runnable onDone
) {
99 getUi().expand(element
, onDone
);
102 public final void expand(Object
[] element
, @Nullable Runnable onDone
) {
103 getUi().expand(element
, onDone
);
106 public final void collapseChildren(Object element
, @Nullable Runnable onDone
) {
107 getUi().collapseChildren(element
, onDone
);
111 protected AbstractTreeNode
createSearchingTreeNodeWrapper() {
112 return new AbstractTreeNodeWrapper();
115 public final AbstractTreeBuilder
setClearOnHideDelay(final long clearOnHideDelay
) {
116 getUi().setClearOnHideDelay(clearOnHideDelay
);
120 protected AbstractTreeUpdater
createUpdater() {
121 AbstractTreeUpdater updater
= new AbstractTreeUpdater(this);
122 updater
.setModalityStateComponent(MergingUpdateQueue
.ANY_COMPONENT
);
126 protected final AbstractTreeUpdater
getUpdater() {
127 return getUi().getUpdater();
130 public final boolean addSubtreeToUpdateByElement(Object element
) {
131 return getUpdater().addSubtreeToUpdateByElement(element
);
134 public final void addSubtreeToUpdate(DefaultMutableTreeNode node
) {
135 getUi().addSubtreeToUpdate(node
);
138 public final void addSubtreeToUpdate(DefaultMutableTreeNode node
, Runnable afterUpdate
) {
139 getUi().addSubtreeToUpdate(node
, afterUpdate
);
142 public final DefaultMutableTreeNode
getRootNode() {
143 return getUi().getRootNode();
146 public final void setNodeDescriptorComparator(Comparator
<NodeDescriptor
> nodeDescriptorComparator
) {
147 getUi().setNodeDescriptorComparator(nodeDescriptorComparator
);
151 * node descriptor getElement contract is as follows:
152 * 1.TreeStructure always returns & recieves "treestructure" element returned by getTreeStructureElement
153 * 2.Paths contain "model" element returned by getElement
156 protected Object
getTreeStructureElement(NodeDescriptor nodeDescriptor
) {
157 return nodeDescriptor
.getElement();
161 protected void updateNode(final DefaultMutableTreeNode node
) {
162 getUi().doUpdateNode(node
);
165 protected boolean validateNode(final Object child
) {
169 protected boolean isDisposeOnCollapsing(NodeDescriptor nodeDescriptor
) {
173 public final JTree
getTree() {
174 return getUi().getTree();
177 public final AbstractTreeStructure
getTreeStructure() {
178 return getUi().getTreeStructure();
181 public final void setTreeStructure(final AbstractTreeStructure structure
) {
182 getUi().setTreeStructure(structure
);
186 * @deprecated @see queueUpdateFrom
188 public void updateFromRoot() {
193 * @deprecated @see queueUpdateFrom
195 protected ActionCallback
updateFromRootCB() {
196 return queueUpdate();
199 public void initRootNode() {
200 getUi().initRootNode();
203 public final ActionCallback
queueUpdate() {
204 return queueUpdateFrom(getTreeStructure().getRootElement(), true);
207 public final ActionCallback
queueUpdateFrom(final Object element
, final boolean forceResort
) {
209 getUi().incComparatorStamp();
212 return getUi().queueUpdate(element
);
219 public void buildNodeForElement(Object element
) {
220 getUi().buildNodeForElement(element
);
229 public DefaultMutableTreeNode
getNodeForElement(Object element
) {
230 return getUi().getNodeForElement(element
, false);
233 public void cleanUp() {
238 protected ProgressIndicator
createProgressIndicator() {
242 protected void expandNodeChildren(final DefaultMutableTreeNode node
) {
243 getUi().doExpandNodeChildren(node
);
246 protected boolean isAutoExpandNode(final NodeDescriptor nodeDescriptor
) {
247 return getTreeStructure().getRootElement() == getTreeStructureElement(nodeDescriptor
);
250 protected boolean isAlwaysShowPlus(final NodeDescriptor descriptor
) {
256 protected boolean isSmartExpand() {
260 public final boolean isDisposed() {
261 return getUi() == null || getUi().isReleased();
268 public final void updateSubtree(final DefaultMutableTreeNode node
) {
269 getUi().updateSubtree(node
, true);
272 public final boolean wasRootNodeInitialized() {
273 return getUi().wasRootNodeInitialized();
276 public final boolean isNodeBeingBuilt(final TreePath path
) {
277 return getUi().isNodeBeingBuilt(path
);
284 public final void buildNodeForPath(final Object
[] path
) {
285 getUi().buildNodeForPath(path
);
291 public final DefaultMutableTreeNode
getNodeForPath(final Object
[] path
) {
292 return getUi().getNodeForPath(path
);
295 protected Object
findNodeByElement(final Object element
) {
296 return getUi().findNodeByElement(element
);
299 public static boolean isLoadingNode(final DefaultMutableTreeNode node
) {
300 return AbstractTreeUi
.isLoadingNode(node
);
303 public boolean isChildrenResortingNeeded(NodeDescriptor descriptor
) {
307 protected void runOnYeildingDone(Runnable onDone
) {
308 if (myUi
.isPassthroughMode()) {
311 UIUtil
.invokeLaterIfNeeded(onDone
);
315 protected void yield(Runnable runnable
) {
316 if (myUi
.isPassthroughMode()) {
319 SwingUtilities
.invokeLater(runnable
);
323 public boolean isToYieldUpdateFor(DefaultMutableTreeNode node
) {
327 public boolean isToEnsureSelectionOnFocusGained() {
331 protected void runBackgroundLoading(final Runnable runnable
) {
332 final Application app
= ApplicationManager
.getApplication();
334 app
.runReadAction(new Runnable() {
344 protected void updateAfterLoadedInBackground(Runnable runnable
) {
345 if (myUi
.isPassthroughMode()) {
348 UIUtil
.invokeLaterIfNeeded(runnable
);
352 public final ActionCallback
getIntialized() {
353 return myUi
.getInitialized();
356 public final ActionCallback
getReady(Object requestor
) {
357 return myUi
.getReady(requestor
);
360 protected void sortChildren(Comparator
<TreeNode
> nodeComparator
, DefaultMutableTreeNode node
, ArrayList
<TreeNode
> children
) {
361 Collections
.sort(children
, nodeComparator
);
364 public void setPassthroughMode(boolean passthrough
) {
365 myUi
.setPassthroughMode(passthrough
);
368 public static class AbstractTreeNodeWrapper
extends AbstractTreeNode
<Object
> {
369 public AbstractTreeNodeWrapper() {
374 public Collection
<AbstractTreeNode
> getChildren() {
375 return Collections
.emptyList();
378 public void update(PresentationData presentation
) {
382 public final AbstractTreeUi
getUi() {
386 public void dispose() {
387 if (isDisposed()) return;
393 protected boolean updateNodeDescriptor(final NodeDescriptor descriptor
) {
394 return getUi().doUpdateNodeDescriptor(descriptor
);
397 public final DefaultTreeModel
getTreeModel() {
398 return (DefaultTreeModel
)getTree().getModel();
402 public final Set
<Object
> getSelectedElements() {
403 return getUi().getSelectedElements();
407 public final <T
> Set
<T
> getSelectedElements(Class
<T
> elementClass
) {
408 Set
<T
> result
= new HashSet
<T
>();
409 for (Object o
: getSelectedElements()) {
410 Object each
= transformElement(o
);
411 if (elementClass
.isInstance(each
)) {
412 //noinspection unchecked
413 result
.add((T
) each
);
419 protected Object
transformElement(Object object
) {
423 public final void setCanYieldUpdate(boolean yield
) {
424 getUi().setCanYield(yield
);
428 public static AbstractTreeBuilder
getBuilderFor(JTree tree
) {
429 final WeakReference ref
= (WeakReference
)tree
.getClientProperty(TREE_BUILDER
);
430 return ref
!= null ?
(AbstractTreeBuilder
)ref
.get() : null;
434 public final <T
> Object
accept(Class nodeClass
, TreeVisitor
<T
> visitor
) {
435 return accept(nodeClass
, getTreeStructure().getRootElement(), visitor
);
439 private <T
> Object
accept(Class nodeClass
, Object element
, TreeVisitor
<T
> visitor
) {
440 if (element
== null) return null;
442 if (nodeClass
.isAssignableFrom(element
.getClass())) {
443 if (visitor
.visit((T
)element
)) return element
;
446 final Object
[] children
= getTreeStructure().getChildElements(element
);
447 for (Object each
: children
) {
448 final Object childObject
= accept(nodeClass
, each
, visitor
);
449 if (childObject
!= null) return childObject
;
455 public <T
> boolean select(Class nodeClass
, TreeVisitor
<T
> visitor
, @Nullable Runnable onDone
, boolean addToSelection
) {
456 final Object element
= accept(nodeClass
, visitor
);
457 if (element
!= null) {
458 select(element
, onDone
, addToSelection
);
465 public void scrollSelectionToVisible(@Nullable Runnable onDone
, boolean shouldBeCentered
) {
466 myUi
.scrollSelectionToVisible(onDone
, shouldBeCentered
);
469 protected boolean isUnitTestingMode() {
470 Application app
= ApplicationManager
.getApplication();
471 return app
!= null && app
.isUnitTestMode();