ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / embedder / MavenConsoleImpl.java
blobf14e66faf49d95ae784b43178c1f95306c2fc184
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.jetbrains.idea.maven.embedder;
18 import com.intellij.execution.filters.*;
19 import com.intellij.execution.process.ProcessAdapter;
20 import com.intellij.execution.process.ProcessEvent;
21 import com.intellij.execution.process.ProcessHandler;
22 import com.intellij.execution.ui.ConsoleView;
23 import com.intellij.execution.ui.ConsoleViewContentType;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.util.Key;
26 import com.intellij.openapi.util.Pair;
27 import com.intellij.openapi.wm.ToolWindow;
28 import com.intellij.openapi.wm.ToolWindowId;
29 import com.intellij.openapi.wm.ToolWindowManager;
30 import com.intellij.ui.content.Content;
31 import com.intellij.ui.content.ContentFactory;
32 import com.intellij.ui.content.MessageView;
33 import org.jetbrains.idea.maven.project.MavenGeneralSettings;
34 import org.jetbrains.idea.maven.project.MavenProjectsManager;
35 import org.jetbrains.idea.maven.execution.MavenRunnerParameters;
36 import org.jetbrains.idea.maven.execution.MavenRunnerSettings;
37 import org.jetbrains.idea.maven.utils.MavenUtil;
39 import java.util.concurrent.atomic.AtomicBoolean;
41 public class MavenConsoleImpl extends MavenConsole {
42 private static final Key<MavenConsoleImpl> CONSOLE_KEY = Key.create("MAVEN_CONSOLE_KEY");
44 private static final String CONSOLE_FILTER_REGEXP =
45 RegexpFilter.FILE_PATH_MACROS + ":\\[" + RegexpFilter.LINE_MACROS + "," + RegexpFilter.COLUMN_MACROS + "]";
47 private final String myTitle;
48 private final Project myProject;
49 private final ConsoleView myConsoleView;
50 private final AtomicBoolean isOpen = new AtomicBoolean(false);
51 private final Pair<MavenRunnerParameters, MavenRunnerSettings> myParametersAndSettings;
53 public MavenConsoleImpl(String title, Project project) {
54 this(title, project, null);
57 public MavenConsoleImpl(String title,
58 Project project,
59 Pair<MavenRunnerParameters, MavenRunnerSettings> parametersAndSettings) {
60 super(getGeneralSettings(project).getLoggingLevel(), getGeneralSettings(project).isPrintErrorStackTraces());
61 myTitle = title;
62 myProject = project;
63 myConsoleView = createConsoleView();
64 myParametersAndSettings = parametersAndSettings;
67 private static MavenGeneralSettings getGeneralSettings(Project project) {
68 return MavenProjectsManager.getInstance(project).getGeneralSettings();
71 private ConsoleView createConsoleView() {
72 return createConsoleBuilder(myProject).getConsole();
75 public static TextConsoleBuilder createConsoleBuilder(Project project) {
76 TextConsoleBuilder builder = TextConsoleBuilderFactory.getInstance().createBuilder(project);
78 Filter[] filters = { new ExceptionFilter(project), new RegexpFilter(project, CONSOLE_FILTER_REGEXP)};
79 for (Filter filter : filters) {
80 builder.addFilter(filter);
82 return builder;
85 public boolean canPause() {
86 return myConsoleView.canPause();
89 public boolean isOutputPaused() {
90 return myConsoleView.isOutputPaused();
93 public void setOutputPaused(boolean outputPaused) {
94 myConsoleView.setOutputPaused(outputPaused);
97 public Pair<MavenRunnerParameters, MavenRunnerSettings> getParametersAndSettings() {
98 return myParametersAndSettings;
101 public void attachToProcess(ProcessHandler processHandler) {
102 myConsoleView.attachToProcess(processHandler);
103 processHandler.addProcessListener(new ProcessAdapter() {
104 @Override
105 public void onTextAvailable(ProcessEvent event, Key outputType) {
106 ensureAttachedToToolWindow();
111 protected void doPrint(String text, OutputType type) {
112 ensureAttachedToToolWindow();
114 ConsoleViewContentType contentType;
115 switch (type) {
116 case SYSTEM:
117 contentType = ConsoleViewContentType.SYSTEM_OUTPUT;
118 break;
119 case ERROR:
120 contentType = ConsoleViewContentType.ERROR_OUTPUT;
121 break;
122 case NORMAL:
123 default:
124 contentType = ConsoleViewContentType.NORMAL_OUTPUT;
126 myConsoleView.print(text, contentType);
129 private void ensureAttachedToToolWindow() {
130 if (!isOpen.compareAndSet(false, true)) return;
132 MavenUtil.invokeLater(myProject, new Runnable() {
133 public void run() {
134 MessageView messageView = MessageView.SERVICE.getInstance(myProject);
136 Content content = ContentFactory.SERVICE.getInstance().createContent(
137 myConsoleView.getComponent(), myTitle, true);
138 content.putUserData(CONSOLE_KEY, MavenConsoleImpl.this);
139 messageView.getContentManager().addContent(content);
140 messageView.getContentManager().setSelectedContent(content);
142 // remove unused tabs
143 for (Content each : messageView.getContentManager().getContents()) {
144 if (each.isPinned()) continue;
145 if (each == content) continue;
147 MavenConsoleImpl console = each.getUserData(CONSOLE_KEY);
148 if (console == null) continue;
150 if (!myTitle.equals(console.myTitle)) continue;
152 if (console.isFinished()) {
153 messageView.getContentManager().removeContent(each, false);
157 ToolWindow toolWindow = ToolWindowManager.getInstance(myProject).getToolWindow(ToolWindowId.MESSAGES_WINDOW);
158 if (!toolWindow.isActive()) {
159 toolWindow.activate(null, false);
165 public void close() {
166 MessageView messageView = MessageView.SERVICE.getInstance(myProject);
167 for (Content each : messageView.getContentManager().getContents()) {
168 MavenConsoleImpl console = each.getUserData(CONSOLE_KEY);
169 if (console != null) {
170 messageView.getContentManager().removeContent(each, true);
171 return;