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.
16 package com
.intellij
.xdebugger
.impl
.ui
.tree
.nodes
;
18 import com
.intellij
.xdebugger
.evaluation
.XDebuggerEvaluator
;
19 import com
.intellij
.xdebugger
.frame
.XValue
;
20 import com
.intellij
.xdebugger
.impl
.frame
.WatchInplaceEditor
;
21 import com
.intellij
.xdebugger
.impl
.ui
.DebuggerUIUtil
;
22 import com
.intellij
.xdebugger
.impl
.ui
.tree
.XDebuggerTree
;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Nullable
;
26 import javax
.swing
.tree
.TreeNode
;
27 import java
.util
.ArrayList
;
28 import java
.util
.Collection
;
29 import java
.util
.Collections
;
30 import java
.util
.List
;
35 public class WatchesRootNode
extends XDebuggerTreeNode
{
36 private List
<WatchNode
> myChildren
;
37 private List
<XDebuggerTreeNode
> myLoadedChildren
;
38 private XDebuggerEvaluator myCurrentEvaluator
;
40 public WatchesRootNode(final XDebuggerTree tree
, String
[] watchExpressions
) {
41 super(tree
, null, false);
42 myChildren
= new ArrayList
<WatchNode
>();
43 for (String watchExpression
: watchExpressions
) {
44 myChildren
.add(WatchMessageNode
.createMessageNode(tree
, this, watchExpression
));
48 public void updateWatches(@Nullable XDebuggerEvaluator evaluator
) {
49 myCurrentEvaluator
= evaluator
;
50 List
<WatchNode
> newChildren
= new ArrayList
<WatchNode
>();
51 if (evaluator
!= null) {
52 for (WatchNode child
: myChildren
) {
53 final String expression
= child
.getExpression();
54 final WatchMessageNode evaluatingNode
= WatchMessageNode
.createEvaluatingNode(myTree
, this, expression
);
55 newChildren
.add(evaluatingNode
);
56 evaluator
.evaluate(expression
, new MyEvaluationCallback(evaluatingNode
), null);
60 for (WatchNode child
: myChildren
) {
61 final String expression
= child
.getExpression();
62 newChildren
.add(WatchMessageNode
.createMessageNode(myTree
, this, expression
));
65 myChildren
= newChildren
;
66 myLoadedChildren
= null;
67 fireNodeChildrenChanged();
70 protected List
<?
extends TreeNode
> getChildren() {
75 public List
<?
extends WatchNode
> getAllChildren() {
79 public List
<?
extends XDebuggerTreeNode
> getLoadedChildren() {
80 if (myLoadedChildren
== null) {
81 myLoadedChildren
= new ArrayList
<XDebuggerTreeNode
>();
82 for (WatchNode child
: myChildren
) {
83 if (child
instanceof WatchNodeImpl
) {
84 myLoadedChildren
.add((WatchNodeImpl
)child
);
88 return myLoadedChildren
;
92 public void clearChildren() {
93 updateWatches(myCurrentEvaluator
);
96 private void replaceNode(final WatchNode oldNode
, final WatchNode newNode
) {
97 for (int i
= 0; i
< myChildren
.size(); i
++) {
98 WatchNode child
= myChildren
.get(i
);
99 if (child
== oldNode
) {
100 myChildren
.set(i
, newNode
);
101 if (newNode
instanceof XValueContainerNode
<?
>) {
102 myLoadedChildren
= null;
103 fireNodeChildrenChanged();
104 myTree
.childrenLoaded(this, Collections
.<XValueContainerNode
<?
>>singletonList((XValueContainerNode
<?
>)newNode
), false);
107 fireNodeChildrenChanged();
114 public void addWatchExpression(final @NotNull XDebuggerEvaluator evaluator
, final @NotNull String expression
, int index
) {
115 WatchNode message
= WatchMessageNode
.createEvaluatingNode(myTree
, this, expression
);
117 myChildren
.add(message
);
120 myChildren
.add(index
, message
);
122 evaluator
.evaluate(expression
, new MyEvaluationCallback(message
), null);
123 fireNodeChildrenChanged();
126 public int removeChildNode(XDebuggerTreeNode node
) {
127 int index
= myChildren
.indexOf(node
);
128 myChildren
.remove(node
);
129 myLoadedChildren
= null;
130 fireNodeChildrenChanged();
134 public void removeChildren(Collection
<?
extends XDebuggerTreeNode
> nodes
) {
135 myChildren
.removeAll(nodes
);
136 myLoadedChildren
= null;
137 fireNodeChildrenChanged();
140 public void addNewWatch() {
144 public void editWatch(@Nullable WatchNode node
) {
145 WatchNode messageNode
= WatchMessageNode
.createMessageNode(myTree
, this, "");
146 int index
= node
!= null ? myChildren
.indexOf(node
) : -1;
148 myChildren
.add(messageNode
);
151 myChildren
.set(index
, messageNode
);
153 fireNodeChildrenChanged();
154 WatchInplaceEditor editor
= new WatchInplaceEditor(this, messageNode
, "watch", node
);
158 private class MyEvaluationCallback
extends XEvaluationCallbackBase
{
159 private final WatchNode myResultPlace
;
161 public MyEvaluationCallback(final @NotNull WatchNode resultPlace
) {
162 myResultPlace
= resultPlace
;
165 public void evaluated(@NotNull final XValue result
) {
166 DebuggerUIUtil
.invokeLater(new Runnable() {
168 replaceNode(myResultPlace
, new WatchNodeImpl(myTree
, WatchesRootNode
.this, result
, myResultPlace
.getExpression()));
173 public void errorOccurred(@NotNull final String errorMessage
) {
174 DebuggerUIUtil
.invokeLater(new Runnable() {
176 replaceNode(myResultPlace
, WatchMessageNode
.createErrorNode(myTree
, WatchesRootNode
.this, myResultPlace
.getExpression(), errorMessage
));