IDEA-79921 Suspend one thread while debugging
[idea/community.git] / java / debugger / impl / src / com / intellij / debugger / actions / ResumeThreadAction.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.debugger.actions;
17
18 import com.intellij.debugger.DebuggerBundle;
19 import com.intellij.debugger.engine.DebugProcessImpl;
20 import com.intellij.debugger.engine.events.SuspendContextCommandImpl;
21 import com.intellij.debugger.impl.DebuggerContextImpl;
22 import com.intellij.debugger.jdi.ThreadReferenceProxyImpl;
23 import com.intellij.debugger.ui.impl.watch.DebuggerTreeNodeImpl;
24 import com.intellij.debugger.ui.impl.watch.NodeDescriptorImpl;
25 import com.intellij.debugger.ui.impl.watch.ThreadDescriptorImpl;
26 import com.intellij.openapi.actionSystem.AnActionEvent;
27 import com.intellij.openapi.actionSystem.Presentation;
28 import com.sun.jdi.request.EventRequest;
29
30 /**
31  * User: lex
32  * Date: Sep 26, 2003
33  * Time: 7:35:09 PM
34  */
35 public class ResumeThreadAction extends DebuggerAction{
36   public void actionPerformed(final AnActionEvent e) {
37     DebuggerTreeNodeImpl[] selectedNode = getSelectedNodes(e.getDataContext());
38     final DebuggerContextImpl debuggerContext = getDebuggerContext(e.getDataContext());
39     final DebugProcessImpl debugProcess = debuggerContext.getDebugProcess();
40
41     //noinspection ConstantConditions
42     for (final DebuggerTreeNodeImpl debuggerTreeNode : selectedNode) {
43       final ThreadDescriptorImpl threadDescriptor = ((ThreadDescriptorImpl)debuggerTreeNode.getDescriptor());
44
45       if (threadDescriptor.isSuspended()) {
46         final ThreadReferenceProxyImpl thread = threadDescriptor.getThreadReference();
47         debugProcess.getManagerThread().schedule(new SuspendContextCommandImpl(debuggerContext.getSuspendContext()) {
48           public void contextAction() throws Exception {
49             debugProcess.createResumeThreadCommand(getSuspendContext(), thread).run();
50             debuggerTreeNode.calcValue();
51           }
52         });
53       }
54     }
55   }
56
57   public void update(AnActionEvent e) {
58     DebuggerTreeNodeImpl[] selectedNodes = getSelectedNodes(e.getDataContext());
59
60     boolean visible = false;
61     boolean enabled = false;
62     String text = DebuggerBundle.message("action.resume.thread.text.resume");
63
64     if(selectedNodes != null && selectedNodes.length > 0){
65       visible = true;
66       enabled = true;
67       for (DebuggerTreeNodeImpl selectedNode : selectedNodes) {
68         final NodeDescriptorImpl threadDescriptor = selectedNode.getDescriptor();
69         if (!(threadDescriptor instanceof ThreadDescriptorImpl) || !((ThreadDescriptorImpl)threadDescriptor).isSuspended()) {
70           visible = false;
71           break;
72         }
73       }
74       if (visible) {
75         for (DebuggerTreeNodeImpl selectedNode : selectedNodes) {
76           final ThreadDescriptorImpl threadDescriptor = (ThreadDescriptorImpl)selectedNode.getDescriptor();
77           if (threadDescriptor.getSuspendContext().getSuspendPolicy() == EventRequest.SUSPEND_ALL && !threadDescriptor.isFrozen()) {
78             enabled = false;
79             break;
80           }
81           //else {
82           //  if (threadDescriptor.isFrozen()) {
83           //    text = DebuggerBundle.message("action.resume.thread.text.unfreeze");
84           //  }
85           //}
86         }
87       }
88     }
89     final Presentation presentation = e.getPresentation();
90     presentation.setText(text);
91     presentation.setVisible(visible);
92     presentation.setEnabled(enabled);
93   }
94 }