summaryrefslogtreecommitdiff
path: root/java/debugger/impl/src/com/intellij/debugger/actions/DebuggerAction.java
blob: d2216191903ad8797b457c6dfd49d0ecbe4e00b1 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * 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.
 */

/*
 * Class DebuggerAction
 * @author Jeka
 */
package com.intellij.debugger.actions;


import com.intellij.debugger.DebuggerManagerEx;
import com.intellij.debugger.impl.DebuggerContextImpl;
import com.intellij.debugger.impl.DebuggerStateManager;
import com.intellij.debugger.ui.impl.DebuggerTreePanel;
import com.intellij.debugger.ui.impl.watch.DebuggerTree;
import com.intellij.debugger.ui.impl.watch.DebuggerTreeNodeImpl;
import com.intellij.ide.DataManager;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.Project;
import com.intellij.ui.DoubleClickListener;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public abstract class DebuggerAction extends AnAction {
  private static final DebuggerTreeNodeImpl[] EMPTY_TREE_NODE_ARRAY = new DebuggerTreeNodeImpl[0];

  @Nullable
  public static DebuggerTree getTree(DataContext dataContext){
    return DebuggerTree.DATA_KEY.getData(dataContext);
  }

  @Nullable
  public static DebuggerTreePanel getPanel(DataContext dataContext){
    return DebuggerTreePanel.DATA_KEY.getData(dataContext);
  }

  @Nullable
  public static DebuggerTreeNodeImpl getSelectedNode(DataContext dataContext) {
    DebuggerTree tree = getTree(dataContext);
    if(tree == null) return null;

    if (tree.getSelectionCount() != 1) {
      return null;
    }
    TreePath path = tree.getSelectionPath();
    if (path == null) {
      return null;
    }
    Object component = path.getLastPathComponent();
    if (!(component instanceof DebuggerTreeNodeImpl)) {
      return null;
    }
    return (DebuggerTreeNodeImpl)component;
  }

  @Nullable
  public static DebuggerTreeNodeImpl[] getSelectedNodes(DataContext dataContext) {
    DebuggerTree tree = getTree(dataContext);
    if(tree == null) return null;
    TreePath[] paths = tree.getSelectionPaths();
    if (paths == null || paths.length == 0) {
      return EMPTY_TREE_NODE_ARRAY;
    }
    List<DebuggerTreeNodeImpl> nodes = new ArrayList<DebuggerTreeNodeImpl>(paths.length);
    for (TreePath path : paths) {
      Object component = path.getLastPathComponent();
      if (component instanceof DebuggerTreeNodeImpl) {
        nodes.add((DebuggerTreeNodeImpl) component);
      }
    }
    return nodes.toArray(new DebuggerTreeNodeImpl[nodes.size()]);
  }

  public static DebuggerContextImpl getDebuggerContext(DataContext dataContext) {
    DebuggerTreePanel panel = getPanel(dataContext);
    if(panel != null) {
      return panel.getContext();
    } else {
      Project project = CommonDataKeys.PROJECT.getData(dataContext);
      return project != null ? (DebuggerManagerEx.getInstanceEx(project)).getContext() : DebuggerContextImpl.EMPTY_CONTEXT;
    }
  }

  @Nullable
  public static DebuggerStateManager getContextManager(DataContext dataContext) {
    DebuggerTreePanel panel = getPanel(dataContext);
    return panel == null ? null : panel.getContextManager();
  }

  public static boolean isContextView(AnActionEvent e) {
    return DebuggerActions.EVALUATION_DIALOG_POPUP.equals(e.getPlace()) ||
           DebuggerActions.FRAME_PANEL_POPUP.equals(e.getPlace()) ||
           DebuggerActions.WATCH_PANEL_POPUP.equals(e.getPlace()) ||
           DebuggerActions.INSPECT_PANEL_POPUP.equals(e.getPlace());
  }

  public static Disposable installEditAction(final JTree tree, String actionName) {
    final DoubleClickListener listener = new DoubleClickListener() {
      @Override
      protected boolean onDoubleClick(MouseEvent e) {
        if (tree.getPathForLocation(e.getX(), e.getY()) == null) return false;
        DataContext dataContext = DataManager.getInstance().getDataContext(tree);
        GotoFrameSourceAction.doAction(dataContext);
        return true;
      }
    };
    //listener.installOn(tree);

    final AnAction action = ActionManager.getInstance().getAction(actionName);
    action.registerCustomShortcutSet(CommonShortcuts.getEditSource(), tree);

    return new Disposable() {
      public void dispose() {
        //listener.uninstall(tree);
        action.unregisterCustomShortcutSet(tree);
      }
    };
  }

  public static boolean isFirstStart(final AnActionEvent event) {
    //noinspection HardCodedStringLiteral
    String key = "initalized";
    if(event.getPresentation().getClientProperty(key) != null) return false;

    event.getPresentation().putClientProperty(key, key);
    return true;
  }

  public static void enableAction(final AnActionEvent event, final boolean enable) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        event.getPresentation().setEnabled(enable);
        event.getPresentation().setVisible(true);
      }
    });
  }
}