enable in-place replace action for Analyze Stacktrace (IDEA-CR-9987)
[idea/community.git] / platform / lang-impl / src / com / intellij / unscramble / AnalyzeStacktraceUtil.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
17 package com.intellij.unscramble;
18
19 import com.intellij.execution.ExecutionManager;
20 import com.intellij.execution.Executor;
21 import com.intellij.execution.executors.DefaultRunExecutor;
22 import com.intellij.execution.filters.Filter;
23 import com.intellij.execution.filters.TextConsoleBuilder;
24 import com.intellij.execution.filters.TextConsoleBuilderFactory;
25 import com.intellij.execution.impl.ConsoleViewImpl;
26 import com.intellij.execution.impl.ConsoleViewUtil;
27 import com.intellij.execution.ui.ConsoleView;
28 import com.intellij.execution.ui.ConsoleViewContentType;
29 import com.intellij.execution.ui.ExecutionConsole;
30 import com.intellij.execution.ui.RunContentDescriptor;
31 import com.intellij.execution.ui.actions.CloseAction;
32 import com.intellij.openapi.Disposable;
33 import com.intellij.openapi.actionSystem.*;
34 import com.intellij.openapi.application.ApplicationManager;
35 import com.intellij.openapi.command.CommandProcessor;
36 import com.intellij.openapi.editor.Document;
37 import com.intellij.openapi.editor.Editor;
38 import com.intellij.openapi.editor.EditorFactory;
39 import com.intellij.openapi.editor.EditorSettings;
40 import com.intellij.openapi.extensions.ExtensionPointName;
41 import com.intellij.openapi.extensions.Extensions;
42 import com.intellij.openapi.ide.CopyPasteManager;
43 import com.intellij.openapi.project.Project;
44 import com.intellij.openapi.util.Disposer;
45 import com.intellij.openapi.util.text.StringUtil;
46 import com.intellij.util.ui.JBUI;
47 import org.jetbrains.annotations.NotNull;
48 import org.jetbrains.annotations.Nullable;
49
50 import javax.swing.*;
51 import java.awt.*;
52 import java.awt.datatransfer.DataFlavor;
53
54 /**
55  * @author yole
56  */
57 public class AnalyzeStacktraceUtil {
58   public static final ExtensionPointName<Filter> EP_NAME = ExtensionPointName.create("com.intellij.analyzeStacktraceFilter");
59
60   private AnalyzeStacktraceUtil() {
61   }
62
63   public static void printStacktrace(@NotNull ConsoleView consoleView, @NotNull String unscrambledTrace) {
64     ApplicationManager.getApplication().assertIsDispatchThread();
65     String text = unscrambledTrace + "\n";
66     String consoleText = ((ConsoleViewImpl)consoleView).getText();
67     if (!text.equals(consoleText)) {
68       consoleView.clear();
69       consoleView.print(text, ConsoleViewContentType.ERROR_OUTPUT);
70       consoleView.scrollTo(0);
71     }
72   }
73
74   @Nullable
75   public static String getTextInClipboard() {
76     return CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor);
77   }
78
79   public interface ConsoleFactory {
80     JComponent createConsoleComponent(ConsoleView consoleView, DefaultActionGroup toolbarActions);
81   }
82
83   public static void addConsole(Project project, @Nullable ConsoleFactory consoleFactory, final String tabTitle, String text) {
84     addConsole(project, consoleFactory, tabTitle, text, null);
85   }
86
87   public static RunContentDescriptor addConsole(Project project,
88                                                 @Nullable ConsoleFactory consoleFactory,
89                                                 final String tabTitle,
90                                                 String text,
91                                                 @Nullable Icon icon) {
92     final TextConsoleBuilder builder = TextConsoleBuilderFactory.getInstance().createBuilder(project);
93     builder.filters(Extensions.getExtensions(EP_NAME, project));
94     final ConsoleView consoleView = builder.getConsole();
95
96     final DefaultActionGroup toolbarActions = new DefaultActionGroup();
97     JComponent consoleComponent = consoleFactory != null
98                                   ? consoleFactory.createConsoleComponent(consoleView, toolbarActions)
99                                   : new MyConsolePanel(consoleView, toolbarActions);
100     final RunContentDescriptor descriptor =
101       new RunContentDescriptor(consoleView, null, consoleComponent, tabTitle, icon) {
102       @Override
103       public boolean isContentReuseProhibited() {
104         return true;
105       }
106     };
107
108     final Executor executor = DefaultRunExecutor.getRunExecutorInstance();
109     for (AnAction action: consoleView.createConsoleActions()) {
110       toolbarActions.add(action);
111     }
112     final ConsoleViewImpl console = (ConsoleViewImpl)consoleView;
113     ConsoleViewUtil.enableReplaceActionForConsoleViewEditor(console.getEditor());
114     console.getEditor().getSettings().setCaretRowShown(true);
115     toolbarActions.add(new AnnotateStackTraceAction(console.getEditor(), console.getHyperlinks()));
116     toolbarActions.add(new CloseAction(executor, descriptor, project));
117     ExecutionManager.getInstance(project).getContentManager().showRunContent(executor, descriptor);
118     consoleView.allowHeavyFilters();
119     if (consoleFactory == null) {
120       printStacktrace(consoleView, text);
121     }
122     return descriptor;
123   }
124
125   private static final class MyConsolePanel extends JPanel {
126     public MyConsolePanel(ExecutionConsole consoleView, ActionGroup toolbarActions) {
127       super(new BorderLayout());
128       JPanel toolbarPanel = new JPanel(new BorderLayout());
129       toolbarPanel.add(ActionManager.getInstance()
130                          .createActionToolbar(ActionPlaces.ANALYZE_STACKTRACE_PANEL_TOOLBAR, toolbarActions, false)
131                          .getComponent());
132       add(toolbarPanel, BorderLayout.WEST);
133       add(consoleView.getComponent(), BorderLayout.CENTER);
134     }
135   }
136
137   public static StacktraceEditorPanel createEditorPanel(Project project, @NotNull Disposable parentDisposable) {
138     EditorFactory editorFactory = EditorFactory.getInstance();
139     Document document = editorFactory.createDocument("");
140     Editor editor = editorFactory.createEditor(document, project);
141     EditorSettings settings = editor.getSettings();
142     settings.setFoldingOutlineShown(false);
143     settings.setLineMarkerAreaShown(false);
144     settings.setIndentGuidesShown(false);
145     settings.setLineNumbersShown(false);
146     settings.setRightMarginShown(false);
147
148     StacktraceEditorPanel editorPanel = new StacktraceEditorPanel(project, editor);
149     editorPanel.setPreferredSize(JBUI.size(600, 400));
150     Disposer.register(parentDisposable, editorPanel);
151     return editorPanel;
152   }
153
154   public static final class StacktraceEditorPanel extends JPanel implements DataProvider, Disposable {
155     private final Project myProject;
156     private final Editor myEditor;
157
158     public StacktraceEditorPanel(Project project, Editor editor) {
159       super(new BorderLayout());
160       myProject = project;
161       myEditor = editor;
162       add(myEditor.getComponent());
163     }
164
165     @Override
166     public Object getData(String dataId) {
167       if (CommonDataKeys.EDITOR.is(dataId)) {
168         return myEditor;
169       }
170       return null;
171     }
172
173     public Editor getEditor() {
174       return myEditor;
175     }
176
177     public final void setText(@NotNull final String text) {
178       Runnable runnable = new Runnable() {
179         @Override
180         public void run() {
181           ApplicationManager.getApplication().runWriteAction(new Runnable() {
182             @Override
183             public void run() {
184               final Document document = myEditor.getDocument();
185               document.replaceString(0, document.getTextLength(), StringUtil.convertLineSeparators(text));
186             }
187           });
188         }
189       };
190       CommandProcessor.getInstance().executeCommand(myProject, runnable, "", this);
191     }
192
193     public void pasteTextFromClipboard() {
194       String text = getTextInClipboard();
195       if (text != null) {
196         setText(text);
197       }
198
199     }
200
201     @Override
202     public void dispose() {
203       EditorFactory.getInstance().releaseEditor(myEditor);
204     }
205
206     public String getText() {
207       return myEditor.getDocument().getText();
208     }
209
210     public JComponent getEditorComponent() {
211       return myEditor.getContentComponent();
212     }
213   }
214 }