summaryrefslogtreecommitdiff
path: root/java/debugger/impl/src/com/intellij/debugger/ui/impl/WatchDebuggerTree.java
blob: 12e5b1c7fa9b8c92525c893e3883279041f79154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.debugger.ui.impl;

import com.intellij.debugger.engine.evaluation.TextWithImports;
import com.intellij.debugger.impl.DebuggerContextImpl;
import com.intellij.debugger.ui.impl.watch.DebuggerTree;
import com.intellij.debugger.ui.impl.watch.DebuggerTreeNodeImpl;
import com.intellij.debugger.ui.impl.watch.WatchItemDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.xdebugger.XDebuggerBundle;
import org.jetbrains.annotations.Nullable;

import javax.swing.tree.TreePath;
import java.util.Enumeration;

public class WatchDebuggerTree extends DebuggerTree {
  private static final Logger LOG = Logger.getInstance("#com.intellij.debugger.ui.impl.WatchDebuggerTree");

  public WatchDebuggerTree(Project project) {
    super(project);
    getEmptyText().setText(XDebuggerBundle.message("debugger.no.watches"));
  }

  public DebuggerTreeNodeImpl[] getWatches() {
    DebuggerTreeNodeImpl root = (DebuggerTreeNodeImpl)getModel().getRoot();
    DebuggerTreeNodeImpl[] watches = new DebuggerTreeNodeImpl[root.getChildCount()];

    final Enumeration e = root.children();
    int i = 0;
    while(e.hasMoreElements()) {
      watches[i++] = (DebuggerTreeNodeImpl)e.nextElement();
    }

    return watches;
  }

  public int getWatchCount() {
    DebuggerTreeNodeImpl root = (DebuggerTreeNodeImpl) getModel().getRoot();
    return root != null ? root.getChildCount() : 0;
  }

  public DebuggerTreeNodeImpl addWatch(WatchItemDescriptor descriptor) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    final DebuggerTreeNodeImpl root = (DebuggerTreeNodeImpl) getModel().getRoot();
    WatchItemDescriptor watchDescriptor = new WatchItemDescriptor(getProject(), descriptor.getEvaluationText());
    watchDescriptor.displayAs(descriptor);

    final DebuggerTreeNodeImpl node = DebuggerTreeNodeImpl.createNodeNoUpdate(this, watchDescriptor);
    root.add(node);

    treeChanged();
    getSelectionModel().setSelectionPath(new TreePath(node.getPath()));

    //node.calcValue();

    return node;
  }

  public DebuggerTreeNodeImpl addWatch(TextWithImports text, @Nullable String customName) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    final DebuggerTreeNodeImpl root = (DebuggerTreeNodeImpl) getModel().getRoot();
    final WatchItemDescriptor descriptor = new WatchItemDescriptor(getProject(), text, customName);
    DebuggerTreeNodeImpl node = DebuggerTreeNodeImpl.createNodeNoUpdate(this, descriptor);
    root.add(node);

    treeChanged();
    final TreePath path = new TreePath(node.getPath());
    getSelectionModel().setSelectionPath(path);
    scrollPathToVisible(path);
    return node;
  }

  public void removeWatch(DebuggerTreeNodeImpl node) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    LOG.assertTrue(node.getDescriptor() instanceof WatchItemDescriptor);

    DebuggerTreeNodeImpl root          = (DebuggerTreeNodeImpl) getModel().getRoot();
    DebuggerTreeNodeImpl nodeToSelect  = (DebuggerTreeNodeImpl) node.getNextSibling();

    getMutableModel().removeNodeFromParent(node);
    treeChanged();

    if(nodeToSelect == null && root.getChildCount() > 0) {
      nodeToSelect = (DebuggerTreeNodeImpl) root.getChildAt(root.getChildCount() - 1);
    }

    if(nodeToSelect != null) {
      setSelectionPath(new TreePath(nodeToSelect.getPath()));
    }
  }

  protected void build(DebuggerContextImpl context) {
    for (DebuggerTreeNodeImpl node : getWatches()) {
      node.calcValue();
    }
  }

  public static void setWatchNodeText(final DebuggerTreeNodeImpl node, TextWithImports text) {
    ((WatchItemDescriptor)node.getDescriptor()).setEvaluationText(text);
    node.calcValue();
  }

}