72011c729963faa1cd0707a8a4468744d1c1ce52
[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.ui.ConsoleView;
27 import com.intellij.execution.ui.ConsoleViewContentType;
28 import com.intellij.execution.ui.ExecutionConsole;
29 import com.intellij.execution.ui.RunContentDescriptor;
30 import com.intellij.execution.ui.actions.CloseAction;
31 import com.intellij.openapi.Disposable;
32 import com.intellij.openapi.actionSystem.*;
33 import com.intellij.openapi.application.ApplicationManager;
34 import com.intellij.openapi.command.CommandProcessor;
35 import com.intellij.openapi.editor.Document;
36 import com.intellij.openapi.editor.Editor;
37 import com.intellij.openapi.editor.EditorFactory;
38 import com.intellij.openapi.editor.EditorSettings;
39 import com.intellij.openapi.extensions.ExtensionPointName;
40 import com.intellij.openapi.extensions.Extensions;
41 import com.intellij.openapi.ide.CopyPasteManager;
42 import com.intellij.openapi.project.Project;
43 import com.intellij.openapi.util.Condition;
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     console.getEditor().getSettings().setCaretRowShown(true);
114     toolbarActions.add(new AnnotateStackTraceAction(console.getEditor(), console.getHyperlinks()));
115     toolbarActions.add(new CloseAction(executor, descriptor, project));
116     ExecutionManager.getInstance(project).getContentManager().showRunContent(executor, descriptor);
117     consoleView.allowHeavyFilters();
118     if (consoleFactory == null) {
119       printStacktrace(consoleView, text);
120     }
121     return descriptor;
122   }
123
124   private static final class MyConsolePanel extends JPanel {
125     public MyConsolePanel(ExecutionConsole consoleView, ActionGroup toolbarActions) {
126       super(new BorderLayout());
127       JPanel toolbarPanel = new JPanel(new BorderLayout());
128       toolbarPanel.add(ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, toolbarActions,false).getComponent());
129       add(toolbarPanel, BorderLayout.WEST);
130       add(consoleView.getComponent(), BorderLayout.CENTER);
131     }
132   }
133
134   public static StacktraceEditorPanel createEditorPanel(Project project, @NotNull Disposable parentDisposable) {
135     EditorFactory editorFactory = EditorFactory.getInstance();
136     Document document = editorFactory.createDocument("");
137     Editor editor = editorFactory.createEditor(document, project);
138     EditorSettings settings = editor.getSettings();
139     settings.setFoldingOutlineShown(false);
140     settings.setLineMarkerAreaShown(false);
141     settings.setIndentGuidesShown(false);
142     settings.setLineNumbersShown(false);
143     settings.setRightMarginShown(false);
144
145     StacktraceEditorPanel editorPanel = new StacktraceEditorPanel(project, editor);
146     editorPanel.setPreferredSize(JBUI.size(600, 400));
147     Disposer.register(parentDisposable, editorPanel);
148     return editorPanel;
149   }
150
151   public static final class StacktraceEditorPanel extends JPanel implements DataProvider, Disposable {
152     private final Project myProject;
153     private final Editor myEditor;
154
155     public StacktraceEditorPanel(Project project, Editor editor) {
156       super(new BorderLayout());
157       myProject = project;
158       myEditor = editor;
159       add(myEditor.getComponent());
160     }
161
162     @Override
163     public Object getData(String dataId) {
164       if (CommonDataKeys.EDITOR.is(dataId)) {
165         return myEditor;
166       }
167       return null;
168     }
169
170     public Editor getEditor() {
171       return myEditor;
172     }
173
174     public final void setText(@NotNull final String text) {
175       Runnable runnable = new Runnable() {
176         @Override
177         public void run() {
178           ApplicationManager.getApplication().runWriteAction(new Runnable() {
179             @Override
180             public void run() {
181               final Document document = myEditor.getDocument();
182               document.replaceString(0, document.getTextLength(), StringUtil.convertLineSeparators(text));
183             }
184           });
185         }
186       };
187       CommandProcessor.getInstance().executeCommand(myProject, runnable, "", this);
188     }
189
190     public void pasteTextFromClipboard() {
191       String text = getTextInClipboard();
192       if (text != null) {
193         setText(text);
194       }
195
196     }
197
198     @Override
199     public void dispose() {
200       EditorFactory.getInstance().releaseEditor(myEditor);
201     }
202
203     public String getText() {
204       return myEditor.getDocument().getText();
205     }
206
207     public JComponent getEditorComponent() {
208       return myEditor.getContentComponent();
209     }
210   }
211 }