2 * Copyright 2000-2010 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 com.intellij.execution.runners;
18 import com.intellij.execution.console.LanguageConsoleImpl;
19 import com.intellij.execution.process.ConsoleHistoryModel;
20 import com.intellij.execution.process.ProcessHandler;
21 import com.intellij.openapi.editor.Document;
22 import com.intellij.openapi.util.TextRange;
24 import java.io.IOException;
25 import java.io.OutputStream;
30 public class ConsoleExecuteActionHandler {
31 private final ProcessHandler myProcessHandler;
32 private final boolean myPreserveMarkup;
33 private boolean myAddCurrentToHistory = true;
34 private ConsoleHistoryModel myConsoleHistoryModel;
36 public ConsoleExecuteActionHandler(ProcessHandler processHandler, boolean preserveMarkup) {
37 myProcessHandler = processHandler;
38 myConsoleHistoryModel = new ConsoleHistoryModel();
39 myPreserveMarkup = preserveMarkup;
42 public void setConsoleHistoryModel(ConsoleHistoryModel consoleHistoryModel) {
43 myConsoleHistoryModel = consoleHistoryModel;
46 public ConsoleHistoryModel getConsoleHistoryModel() {
47 return myConsoleHistoryModel;
50 public void runExecuteAction(LanguageConsoleImpl languageConsole) {
52 // Process input and add to history
53 final Document document = languageConsole.getCurrentEditor().getDocument();
54 final String text = document.getText();
55 final TextRange range = new TextRange(0, document.getTextLength());
57 languageConsole.getCurrentEditor().getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
58 if (myAddCurrentToHistory) {
59 languageConsole.addCurrentToHistory(range, false, myPreserveMarkup);
61 languageConsole.setInputText("");
63 myConsoleHistoryModel.addToHistory(text);
64 // Send to interpreter / server
69 public void processLine(String line) {
70 sendText(line + "\n");
73 public void sendText(String line) {
74 //final Charset charset = myProcessHandler.getCharset();
75 final OutputStream outputStream = myProcessHandler.getProcessInput();
77 //byte[] bytes = (line + "\n").getBytes(charset.name());
78 byte[] bytes = line.getBytes();
79 outputStream.write(bytes);
82 catch (IOException e) {
87 public void setAddCurrentToHistory(boolean addCurrentToHistory) {
88 myAddCurrentToHistory = addCurrentToHistory;
91 public void finishExecution() {