NotNull, dispose
[fedora-idea.git] / platform / lang-impl / src / com / intellij / execution / impl / ConsoleState.java
blobef19d65e6da9d7611fb673abe7128407b1e04c53
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.
17 package com.intellij.execution.impl;
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.ConsoleViewContentType;
23 import com.intellij.execution.ExecutionBundle;
24 import com.intellij.openapi.util.Key;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.io.OutputStreamWriter;
29 import java.io.Writer;
31 public abstract class ConsoleState {
32 public static final ConsoleState NOT_STARTED = new ConsoleState(){
33 public ConsoleState attachTo(final ConsoleViewImpl console, final ProcessHandler processHandler) {
34 return new RunningState(console, processHandler);
38 public ConsoleState dispose() {
39 return NOT_STARTED;
42 public boolean isFinished() {
43 return false;
46 public boolean isRunning() {
47 return false;
50 public void sendUserInput(final String input) throws IOException {}
52 public abstract ConsoleState attachTo(ConsoleViewImpl console, ProcessHandler processHandler);
54 private static class RunningState extends ConsoleState {
55 private final ConsoleViewImpl myConsole;
56 private final ProcessAdapter myProcessListener = new ProcessAdapter() {
57 public void onTextAvailable(final ProcessEvent event, final Key outputType) {
58 myConsole.print(event.getText(), ConsoleViewContentType.getConsoleViewType(outputType));
61 private final ProcessHandler myProcessHandler;
62 private final Writer myUserInputWriter;
64 public RunningState(final ConsoleViewImpl console, final ProcessHandler processHandler) {
65 myConsole = console;
66 myProcessHandler = processHandler;
67 processHandler.addProcessListener(myProcessListener);
68 final OutputStream processInput = myProcessHandler.getProcessInput();
69 myUserInputWriter = processInput != null ? new OutputStreamWriter(processInput) : null;
72 public ConsoleState dispose() {
73 if (myProcessHandler != null) {
74 myProcessHandler.removeProcessListener(myProcessListener);
76 return NOT_STARTED;
79 public boolean isFinished() {
80 return myProcessHandler == null || myProcessHandler.isProcessTerminated();
83 public boolean isRunning() {
84 return myProcessHandler != null && !myProcessHandler.isProcessTerminated();
87 public void sendUserInput(final String input) throws IOException {
88 if (myUserInputWriter == null)
89 throw new IOException(ExecutionBundle.message("no.user.process.input.error.message"));
90 myUserInputWriter.write(input);
91 myUserInputWriter.flush();
94 public ConsoleState attachTo(final ConsoleViewImpl console, final ProcessHandler processHandler) {
95 return dispose().attachTo(console, processHandler);