net.openchrom.xxd.processor.supplier.rscripting.ui.code.ConsolePageParticipant.java Source code

Java tutorial

Introduction

Here is the source code for net.openchrom.xxd.processor.supplier.rscripting.ui.code.ConsolePageParticipant.java

Source

/*******************************************************************************
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * Contributors:
 *
 * Hitesh Gaur - initial API and implementation
 *******************************************************************************/
package net.openchrom.xxd.processor.supplier.rscripting.ui.code;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.IRegion;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsolePageParticipant;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.IOConsoleInputStream;
import org.eclipse.ui.console.TextConsolePage;
import org.eclipse.ui.console.TextConsoleViewer;
import org.eclipse.ui.part.IPageBookViewPage;
import org.eclipse.ui.part.IPageSite;
import org.osgi.framework.Bundle;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

import net.openchrom.xxd.processor.supplier.rscripting.ui.Activator;
import net.openchrom.xxd.processor.supplier.rscripting.ui.Starter;
import net.openchrom.xxd.processor.supplier.rscripting.ui.Util;
import net.openchrom.xxd.processor.supplier.rscripting.ui.dialogs.MsgDialog;
import net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RState;
import net.openchrom.xxd.processor.supplier.rscripting.ui.os.Pid;
import net.openchrom.xxd.processor.supplier.rscripting.ui.os.UnixProcessManager;

public class ConsolePageParticipant implements IConsolePageParticipant {

    public IPageBookViewPage page;
    private static IOConsole ioc;
    private Thread initializationThread;
    public static String interpreterSelection = "-";
    private String input = null;
    private InputStreamReader isr;
    private BufferedReader in;
    public StyledText styledText;
    private ArrayList<String> list;
    public int commandBack = 0;
    protected int position = -1;
    public boolean ignore = false;
    private Process shellProcess;
    private Process RProcess;
    public Process nativeShellProcess;
    public String consoleEncoding = "UTF-8";
    public Thread processThread;
    public Thread RprocessThread;
    public Thread nativeShellprocessThread;
    private IOConsoleInputStream iocinput;
    private boolean runThread;
    public IToolBarManager toolBarManager;
    public IActionBars actionBars;
    private Pid rPid;
    private Pid shellPid;
    private IContributionItem item;
    private static final char IAC = (char) 5;
    private static final char BRK = (char) 3;
    private static ConsolePageParticipant ConsolePageParticipantInstance;
    static boolean lineSeperatorConsole = true;
    static boolean addToHistoryConsole = true;
    public static String finalOutput;

    public ConsolePageParticipant() {
        ConsolePageParticipantInstance = this;
    }

    @Override
    public Object getAdapter(Class adapter) {

        // TODO Auto-generated method stub
        return null;
    }

    public static void pipeInputToConsole(String input, boolean lineSeperator, boolean addToHistory) {

        lineSeperatorConsole = lineSeperator;
        addToHistoryConsole = addToHistory;
        ioc.getInputStream().appendData(input);
        ioc.getInputStream().appendData(System.getProperty("line.separator"));
    }

    public static void pipeInputToConsole(String input) {

        lineSeperatorConsole = true;
        addToHistoryConsole = true;
        ioc.getInputStream().appendData(input);
        ioc.getInputStream().appendData(System.getProperty("line.separator"));
    }

    public static String pipeOutputToString() {

        finalOutput = ioc.getDocument().get();
        return finalOutput;
    }

    @Override
    public void init(IPageBookViewPage page, IConsole console) {

        // Activator.TestMessageOut("inside init of participant");
        this.page = page;
        if (console instanceof IOConsole) {
            ioc = (IOConsole) console;
        }
        iocinput = ioc.getInputStream();
        isr = new InputStreamReader(iocinput);
        initializationThread = new Thread(new Runnable() {

            public void run() {

                if (initializationThread.isInterrupted() == false) {
                    interpret();
                }
            }
        });
        runThread = true;
        initializationThread.start();
        list = new ArrayList<String>();
        styledText = (StyledText) page.getControl();
        IPreferenceStore store = Activator.getDefault().getPreferenceStore();
        FontData shellFont = PreferenceConverter.getFontData(store, "PluginShellFonts");
        if (shellFont != null) {
            styledText.setFont(new Font(Display.getDefault(), shellFont));
        }
        styledText.setKeyBinding(SWT.ARROW_UP, SWT.NULL);
        styledText.setKeyBinding(SWT.ARROW_DOWN, SWT.NULL);
        styledText.addKeyListener(new KeyAdapter() {

            public void keyReleased(KeyEvent event) {

                if (event.keyCode == SWT.ARROW_UP) {
                    styledText.setCaretOffset(styledText.getText().length());
                    commandBack--;
                    if (position == -1) {
                        position = styledText.getCaretOffset();
                        // commandBack = list.size() - 1;
                    }
                    if (commandBack == -1) {
                        commandBack = 0;
                    }
                    int range = styledText.getText().length() - position;
                    if (range > 0) {
                        if (list.size() > 0) {
                            styledText.replaceTextRange(position, range, list.get(commandBack));
                        }
                    } else if (range == 0) {
                        if (list.size() > 0) {
                            styledText.replaceTextRange(position, 0, list.get(commandBack));
                        }
                    } else {
                    }
                } else if (event.keyCode == SWT.ARROW_DOWN) {
                    styledText.setCaretOffset(styledText.getText().length());
                    commandBack++;
                    if (position == -1) {
                        position = styledText.getCaretOffset();
                        // commandBack=list.size()-1;
                    }
                    if (commandBack >= list.size() - 1) {
                        commandBack = list.size() - 1;
                    }
                    int range = styledText.getText().length() - position;
                    if (range > 0) {
                        if (list.size() > 0) {
                            styledText.replaceTextRange(position, range, list.get(commandBack));
                        }
                    } else if (range == 0) {
                        if (list.size() > 0) {
                            styledText.replaceTextRange(position, 0, list.get(commandBack));
                        }
                    } else {
                    }
                } else if (event.keyCode == SWT.F1) {
                    if (interpreterSelection.equals("R") && RServe.getConnection() != null) {
                        fastSaveAndReloadRWorkspace();
                        System.out.print("Rserve workspace data transferred to native R workspace!");
                        System.out.println();
                    } else {
                        MsgDialog.message("Please start Rserve and the \"Native R\" shell in the Plugin console!");
                    }
                }
                /* CTRL+c key event! */
                else if (event.stateMask == SWT.CTRL && event.keyCode == 'c') {
                    // Send OS signal
                    if (Util.getOS().equals("Windows")) {
                        Bundle bundle = Platform.getBundle("net.openchrom.xxd.processor.supplier.rscripting.ui");
                        URL locationUrl = FileLocator.find(bundle, new Path("/"), null);
                        URL fileUrl = null;
                        try {
                            fileUrl = FileLocator.toFileURL(locationUrl);
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                        File fi = new File(fileUrl.getPath());
                        String pathBundle = null;
                        if (Util.is64BitVM()) {
                            pathBundle = fi.toString() + "/win/64";
                        } else {
                            pathBundle = fi.toString() + "/win/32";
                        }
                        if (interpreterSelection.equals("R")) {
                            try {
                                Process p = Runtime.getRuntime()
                                        .exec(pathBundle + "/SendSignalCtrlC.exe " + rPid.getPidWindows(RProcess));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            // System.out.println(pathBundle +
                            // "/SendSignalCtrlC.exe " +
                            // rPid.getPidWindows(RProcess));
                        }
                    } else {
                        if (interpreterSelection.equals("R")) {
                            // Process p =
                            // Runtime.getRuntime().exec("kill -INT " +
                            // rPid.getPidUnix(RProcess));
                            if (RServe.getConnection() == null) {
                                UnixProcessManager.sendSigIntToProcessTree(RProcess);
                            } else {
                                System.out.print(
                                        "Please change to the native connection to interrupt a running R script!");
                            }
                            // System.out.println(rPid.getPidWindows(RProcess));
                        }
                    }
                }
                /* CTRL+x key event! */
                else if (event.stateMask == SWT.CTRL && event.keyCode == 'x') {
                } else {
                    commandBack = list.size();
                    position = -1;
                }
            }
        });
        IPageSite pageSite = page.getSite();
        IWorkbenchPage workbenchPage = pageSite.getPage();
        IViewPart viewPart = workbenchPage.findView(IConsoleConstants.ID_CONSOLE_VIEW);
        IViewSite viewSite = viewPart.getViewSite();
        actionBars = viewSite.getActionBars();
        toolBarManager = actionBars.getToolBarManager();
        IContributionItem[] items = toolBarManager.getItems();
        if (items != null && items.length > 0) {
            toolBarManager.remove(items[items.length - 1]);
        }
        // toolBarManager.removeAll();
        // At startup only output console is activated!
        styledText.setEditable(false);
        styledText.setText(
                "At startup only output console is activated! Please Press R Shell button to Start R process");
        // toolBarManager.add(new RShellCommands());
        toolBarManager.add(new ConsoleCommands(this));
        item = new PlaceholderLabel().getPlaceholderLabel();
        toolBarManager.add(new StartRServe());
        // toolBarManager.add(new WorkingDirectoryCommand());
        toolBarManager.add(new EditorCommands(this));
        toolBarManager.add(item);
        in = new BufferedReader(isr);
        ioc.clearConsole();
        styledText.setText(
                "At startup only output console is activated! Please Press R Shell button to Start R process");
    }

    public void processCommand() {

        IPreferenceStore store = Activator.getDefault().getPreferenceStore();
        consoleEncoding = store.getString("Console_Encoding");
        String shellArgs = store.getString("shell_arguments");
        try {
            if (Util.getOS().equals("Windows")) {
                // process = Runtime.getRuntime().exec("cmd");
                /*
                 * ProcessBuilder can redirect the error stream! No second
                 * thread needed!
                 */
                ProcessBuilder builder = new ProcessBuilder("cmd");
                // System.out.println(builder.environment());
                builder.redirectErrorStream(true);
                nativeShellProcess = builder.start();
                nativeShellprocessThread = new Thread(new NativeProcessGrabber());
                nativeShellprocessThread.start();
                /* Start shell with arguments! */
                ConsolePageParticipant.pipeInputToConsole(shellArgs, true, true);
                shellPid = new Pid();
                // System.out.println("Process Id is: " +
                // shellPid.getPidWindows(nativeShellProcess));
            } else if (Util.getOS().equals("Linux")) {
                // Some Useful commands: export TERM=xterm; top -b; ssh -tt
                // gksudo 'apt-get --yes install abiword'
                List<String> args = new ArrayList<String>();
                args.add("/bin/sh");
                args.add("-i");
                ProcessBuilder builder = new ProcessBuilder(args);
                builder.redirectErrorStream(true);
                nativeShellProcess = builder.start();
                nativeShellprocessThread = new Thread(new NativeProcessGrabber());
                nativeShellprocessThread.start();
                /* Start shell with arguments! */
                ConsolePageParticipant.pipeInputToConsole(shellArgs, true, true);
                shellPid = new Pid();
                // System.out.println("Process Id is: " +
                // shellPid.getPidUnix(nativeShellProcess));
            } else if (Util.getOS().equals("Mac")) {
                List<String> args = new ArrayList<String>();
                args.add("/bin/sh");
                args.add("-i");
                ProcessBuilder builder = new ProcessBuilder(args);
                builder.redirectErrorStream(true);
                nativeShellProcess = builder.start();
                nativeShellprocessThread = new Thread(new NativeProcessGrabber());
                nativeShellprocessThread.start();
                /* Start shell with arguments! */
                ConsolePageParticipant.pipeInputToConsole(shellArgs, true, false);
                shellPid = new Pid();
                // System.out.println("Process Id is: " +
                // shellPid.getPidUnix(nativeShellProcess));
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * If R native is started first we have to make some default settings from
     * the preferences!
     */
    public void rOptions() {

        pipeInputToConsole("options(max.print=5000)");
        /*
         * Set the default install location for the add on packages!
         */
        IPreferenceStore store = Activator.getDefault().getPreferenceStore();
        String rPackages = store.getString("InstallLocation");
        if (Util.getOS().equals("Windows")) {
            rPackages = rPackages.replace("\\", "/");
            pipeInputToConsole(".libPaths(\"" + rPackages + "\")");
        }
        /* For Linux and Mac if there is no path we will take the default defined! */
        else {
            if (rPackages.isEmpty() == false) {
                pipeInputToConsole(".libPaths(\"" + rPackages + "\")");
            }
        }
    }

    public void processRCommand() {

        IPreferenceStore store = Activator.getDefault().getPreferenceStore();
        consoleEncoding = store.getString("Console_Encoding");
        try {
            if (Util.getOS().equals("Windows")) {
                String rPath = store.getString(PreferenceConstants.PATH_R);
                List<String> args = new ArrayList<String>();
                if (Util.is64BitVM()) {
                    args.add(rPath + "/bin/x64/rterm");
                } else {
                    args.add(rPath + "/bin/i386/rterm");
                }
                args.add("--ess");
                ProcessBuilder builder = new ProcessBuilder(args);
                builder.redirectErrorStream(true);
                RProcess = builder.start();
                RprocessThread = new Thread(new RProcessGrabber());
                RprocessThread.start();
                rPid = new Pid();
                // System.out.println("Process Id is: " +
                // rPid.getPidWindows(RProcess));
                /*
                 * ProcessBuilder pb = new ProcessBuilder("cmd").directory(new File("C:"));
                 * pb.start();
                 * String path = store.getString(PreferenceConstants.PATH_R);
                 * List<String> args = new ArrayList<String>();
                 * if(Util.is64BitVM()) {
                 * args.add(path + "/bin/x64/rterm");
                 * } else {
                 * args.add(path + "/bin/i386/rterm");
                 * }
                 * args.add("--ess");
                 * args.add("--no-save");
                 * ProcessBuilder builder = new ProcessBuilder(args);
                 * // ProcessBuilder.Redirect redOut = builder.redirectOutput();
                 * // redOut.
                 * builder.redirectErrorStream(true);
                 * // builder.inheritIO();
                 * Process proc = builder.start();
                 * Thread procThread = new Thread(new RThread(proc));
                 * procThread.start();
                 * int pid = Util.getProcessId(proc, Util.getOS());
                 * System.out.println("Process Id is: " + pid);
                 */
                rOptions();
            } else if (Util.getOS().equals("Linux")) {
                String rPath = store.getString(PreferenceConstants.PATH_R);
                List<String> args = new ArrayList<String>();
                if (rPath.isEmpty() == false) {
                    args.add(rPath + "/bin/R");
                } else {
                    args.add("R");
                }
                args.add("--interactive");
                ProcessBuilder builder = new ProcessBuilder(args);
                builder.redirectErrorStream(true);
                RProcess = builder.start();
                RprocessThread = new Thread(new RProcessGrabber());
                RprocessThread.start();
                rPid = new Pid();
                // System.out.println("Process Id is: " +
                // rPid.getPidUnix(RProcess));
                rOptions();
            } else if (Util.getOS().equals("Mac")) {
                String rPath = store.getString(PreferenceConstants.PATH_R);
                List<String> args = new ArrayList<String>();
                if (rPath.isEmpty() == false) {
                    args.add(rPath + "/bin/R");
                } else {
                    args.add("R");
                }
                args.add("--interactive");
                ProcessBuilder builder = new ProcessBuilder(args);
                builder.redirectErrorStream(true);
                RProcess = builder.start();
                RprocessThread = new Thread(new RProcessGrabber());
                RprocessThread.start();
                rPid = new Pid();
                // System.out.println("Process Id is: " +
                // rPid.getPidUnix(RProcess));
                rOptions();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    class NativeProcessGrabber implements Runnable {

        final InputStream inp = nativeShellProcess.getInputStream();

        public void run() {

            // setPriority(Thread.MAX_PRIORITY);
            if (interpreterSelection.equals("shell")) {
                try {
                    InputStreamReader inr = new InputStreamReader(inp, Charset.forName(consoleEncoding));
                    int ch;
                    while ((ch = inr.read()) != -1) {
                        System.out.print((char) ch);
                    }
                    // consoleOutputChar.delete(0,
                    // consoleOutputChar.length()-1);
                } catch (IOException e) {
                    // e.printStackTrace();
                    System.out.println("Stream closed!");
                } finally {
                    if (inp != null) {
                        try {
                            inp.close();
                        } catch (IOException e) {
                        }
                    }
                }
            }
        }
    }

    class RProcessGrabber implements Runnable {

        final InputStream inp = RProcess.getInputStream();

        public void run() {

            // setPriority(Thread.MAX_PRIORITY);
            if (interpreterSelection.equals("R")) {
                try {
                    InputStreamReader inr = new InputStreamReader(inp, Charset.forName(consoleEncoding));
                    int ch;
                    while ((ch = inr.read()) != -1) {
                        if (Util.getOS().equals("Windows")) {
                            System.out.print((char) ch);
                        }
                        /*
                         * Under Linux and MacOSX commands are echoed in ASCII
                         * and evtl. ANSI control characters.We cannot avoid the
                         * echo but can delete some ASCII characters for a
                         * better output!
                         */
                        else {
                            if (ch != 8 && ch != 12 && ch != 13) {
                                System.out.print((char) ch);
                            }
                        }
                    }
                } catch (IOException e) {
                    // e.printStackTrace();
                    System.out.println("Stream closed!");
                } finally {
                    if (inp != null) {
                        try {
                            inp.close();
                        } catch (IOException e) {
                        }
                    }
                }
            }
        }
    }

    class ProcessGrabber implements Runnable {

        final InputStream inp = shellProcess.getInputStream();

        public void run() {
            // setPriority(Thread.MAX_PRIORITY);

            try {
                InputStreamReader inr = new InputStreamReader(inp, Charset.forName(consoleEncoding));
                int ch;
                while ((ch = inr.read()) != -1) {
                    System.out.print((char) ch);
                }
                // consoleOutputChar.delete(0, consoleOutputChar.length()-1);
            } catch (IOException e) {
                // e.printStackTrace();
                System.out.println("Stream closed!");
            } finally {
                if (inp != null) {
                    try {
                        inp.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
    }

    @Override
    public void dispose() {

        ioc.getInputStream().appendData(System.getProperty("line.separator"));
        interpreterSelection = "-";
        /*
         * Use a thread to ensure that the other threads will be destroyed -
         * maybee not necessary!
         */
        // new Thread() {
        // @Override
        // public void run() {
        if (shellProcess != null) {
            shellProcess.destroy();
        }
        if (RProcess != null) {
            RProcess.destroy();
        }
        if (nativeShellProcess != null) {
            nativeShellProcess.destroy();
        }
        if (initializationThread != null) {
            initializationThread.interrupt();
        }
        if (processThread != null) {
            processThread.interrupt();
        }
        if (RprocessThread != null) {
            RprocessThread.interrupt();
        }
        if (nativeShellprocessThread != null) {
            nativeShellprocessThread.interrupt();
        }
        in = null;
        // }
        // }.start();
        /* Destroy the IOConsole! */
        ioc.partitionerFinished();
        ioc.destroy();
    }

    @Override
    public void activated() {

        // Activator.TestMessageOut("inside activated of participant");
        ioc.getDocument().addDocumentListener(eobHandler);
    }

    @Override
    public void deactivated() {

        ioc.getDocument().removeDocumentListener(eobHandler);
    }

    private void interpret() {

        while (!Thread.interrupted() && runThread == true) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e1) {
                runThread = false;
                e1.printStackTrace();
            }
            switch (interpreterSelection) {
            case "shell":
                try {
                    if (in != null) {
                        if (Util.getOS().equals("Windows")) {
                            input = in.readLine();
                        } else {
                            input = in.readLine();
                        }
                    }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                if (input == null)
                    break;
                else {
                    if (ignore == false) {
                        if (input != null && input.equals("") == false) {
                            list.add(input);
                        }
                    }
                    if (nativeShellProcess != null) {
                        final OutputStream os = nativeShellProcess.getOutputStream();
                        final OutputStreamWriter osw = new OutputStreamWriter(os);
                        final BufferedWriter bw = new BufferedWriter(osw, 100);
                        try {
                            bw.write(input);
                            bw.newLine();
                            // If necessary: bw.write("\r\n");
                            os.flush();
                            bw.flush();
                            // bw.close();
                            System.out.flush();
                        } catch (IOException e) {
                            System.err.println("");
                        }
                    }
                    /*
                     * Process p = RConnectionJob.getProc(); // Write to the
                     * output!
                     */
                    ignore = false;
                    System.out.flush();
                }
                break;
            case "R":
                try {
                    if (in != null) {
                        // System.out.print("Shell>");
                        input = in.readLine();
                    }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                if (input == null)
                    break;
                else {
                    if (ignore == false) {
                        if (input != null && input.equals("") == false) {
                            if (addToHistoryConsole) {
                                list.add(input);
                            }
                        }
                    }
                    if (RProcess != null) {
                        final OutputStream os = RProcess.getOutputStream();
                        final OutputStreamWriter osw = new OutputStreamWriter(os);
                        final BufferedWriter bw = new BufferedWriter(osw, 100);
                        try {
                            // ConsolePageParticipant.getConsolePageParticipantInstance().ioc.get
                            bw.write(input);
                            if (lineSeperatorConsole) {
                                bw.newLine();
                            }
                            // If necessary: bw.write("\r\n");
                            os.flush();
                            bw.flush();
                            // bw.close();
                            System.out.flush();
                        } catch (IOException e) {
                            System.err.println("");
                        }
                    }
                    /*
                     * Process p = RConnectionJob.getProc(); // Write to the
                     * output!
                     */
                    ignore = false;
                    System.out.flush();
                }
                break;
            case "-":
                System.out.flush();
                break;
            default:
                System.out.flush();
                break;
            }
            // System.out.flush();
        }
    }

    private IDocumentListener eobHandler = new IDocumentListener() {

        public void documentAboutToBeChanged(DocumentEvent event) {

        }

        /**
         * Ensure that caret moves to End of Buffer when the REPL prints it
         * prompt
         */
        public void documentChanged(DocumentEvent event) {

            IDocument doc = event.getDocument();
            TextConsoleViewer viewer = ((page instanceof TextConsolePage) ? ((TextConsolePage) page).getViewer()
                    : null);
            if (doc != null) {
                try {
                    int textLen = (event.getText() != null ? event.getText().length() : 0);
                    int doclen = doc.getLength() - 1;
                    // move cursor to end of repl-prompt when necessary
                    if (textLen > 0 && viewer.getTextWidget().getCaretOffset() < doclen) {
                        IRegion reg = doc.getLineInformationOfOffset(doclen);
                        // use end offset excluding any eol characters
                        viewer.getTextWidget().setCaretOffset(reg.getOffset() + reg.getLength());
                        /*
                         * IRegion reg = doc.getLineInformationOfOffset(doclen);
                         * String selectionConsole =
                         * ConsolePageParticipant.getInterpreterSelection();
                         * if (selectionConsole.equals("R")) { String line =
                         * doc.get(reg.getOffset(), reg.getLength());
                         * if(line.contains("R#")){ int lineNumber
                         * =line.indexOf("#");
                         * int
                         * number=Integer.parseInt(line.replaceAll("\\D+",""));
                         * System.out.println("Line Number: "+number);
                         * }
                         * }
                         */
                    }
                    // System.out.println(textLen);
                } catch (BadLocationException e) {
                }
            }
        }
    };
    private String selected;

    public IOConsole getIoc() {

        return ioc;
    }

    public static String getInterpreterSelection() {

        return interpreterSelection;
    }

    public static void setNativeInterpreterSelection(String interpreterSelection) {

        if (interpreterSelection.equals("R")) {
            ConsoleCommands.getInstance().startR();
        }
    }

    private void fastSaveAndReloadRWorkspace() {

        IPreferenceStore store = null;
        String path = null;
        try {
            store = Activator.getDefault().getPreferenceStore();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
        }
        if (store != null) {
            path = store.getString(PreferenceConstants.P_TEMP_R);
        }
        selected = path + "\\tempCurrent";
        File dir = new File(path);
        if (dir.canWrite()) {
            if (Util.getOS().equals("Windows")) {
                selected = path + "\\tempCurrent";
                selected = selected.replace("\\", "\\\\");
            }
            /* For Linux and MacOSX! */
            else {
                selected = path + "/tempCurrent";
            }
            if (selected != null) {
                final String save = "try(save.image(file =\"" + selected + ".RData"
                        + "\", version = NULL, ascii = FALSE))";
                if (RState.isBusy() == false) {
                    RState.setBusy(true);
                    Job job = new Job("Transfer data to native started R!") {

                        @Override
                        protected IStatus run(IProgressMonitor monitor) {

                            monitor.beginTask("Transfer data to native started R ...", IProgressMonitor.UNKNOWN);
                            RConnection con = RServe.getConnection();
                            try {
                                con.voidEval(save);
                            } catch (RserveException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            String fileR = selected + ".RData";
                            if (interpreterSelection.equals("R")) {
                                ConsolePageParticipant.pipeInputToConsole("load(file =\"" + fileR + "\");", true,
                                        true);
                            } else {
                                MsgDialog.message("Please start the \"Native R\" shell in the Plugin console!");
                            }
                            monitor.done();
                            return Status.OK_STATUS;
                        }
                    };
                    job.addJobChangeListener(new JobChangeAdapter() {

                        public void done(IJobChangeEvent event) {

                            if (event.getResult().isOK()) {
                                RState.setBusy(false);
                            } else {
                                RState.setBusy(false);
                            }
                        }
                    });
                    job.schedule();
                    // LineFeed.
                } else {
                    MsgDialog.message("Rserve is busy!");
                }
            }
        } else {
            MsgDialog.message("Directory is not writable!");
        }
    }

    public static ConsolePageParticipant getConsolePageParticipantInstance() {

        return ConsolePageParticipantInstance;
    }

    /* Add toolbar actions to the console view! */
    public void setRDebugToolbarActions() {

        Starter utils = Starter.getConsoleInstance();
        /* Add the debug actions dynamically! */
        IToolBarManager tm = toolBarManager;
        /* Remove the distance label! */
        tm.remove("PlaceholderLabel");
        IContributionItem[] its = toolBarManager.getItems();
        boolean exist = false;
        for (int i = 0; i < its.length; i++) {
            if (its[i].getId() != null) {
                /* Control if the items exists already! */
                if (its[i].getId().equals("Stop")) {
                    tm.add(item);
                    exist = true;
                }
            }
        }
        /*
         * if(exist == false) {
         * tm.add(new DebugRScript());
         * tm.add(new DebugStopAction());
         * tm.add(new DebugNextAction());
         * tm.add(new DebugContinueAction());
         * tm.add(new DebugStepIntoAction());
         * tm.add(new DebugStepFinishAction());
         * tm.add(new DebugInfoAction());
         * Add the distance label again!
         * tm.add(item);
         * actionBars.updateActionBars();
         * }
         */
        /* Remove all toolbar actions from the console view! */
        /*
         * if (utils != null) { utils.cons.clear(); }
         */
    }

    public void deleteDebugToolbarActions() {

        toolBarManager.remove("Debug");
        toolBarManager.remove("Stop");
        toolBarManager.remove("Next");
        toolBarManager.remove("Continue");
        toolBarManager.remove("StepInto");
        toolBarManager.remove("Finish");
        toolBarManager.remove("DebugInfo");
        actionBars.updateActionBars();
    }

    public Process getShellProcess() {

        return shellProcess;
    }

    public Process getNativeShellProcess() {

        return nativeShellProcess;
    }

    public void setNativeShellProcess(Process nativeShellProcess) {

        this.nativeShellProcess = nativeShellProcess;
    }

    public Process getRProcess() {

        return RProcess;
    }

    public void setRProcess(Process rProcess) {

        RProcess = rProcess;
    }

    public Pid getrPid() {

        return rPid;
    }

    public void pipeToRConsole(String command) {

        String selectionConsole = ConsolePageParticipant.getInterpreterSelection();
        if (selectionConsole.equals("R")) {
            ConsolePageParticipant con = ConsolePageParticipant.getConsolePageParticipantInstance();
            Process rProcess = con.getRProcess();
            if (rProcess != null) {
                final OutputStream os = rProcess.getOutputStream();
                final OutputStreamWriter osw = new OutputStreamWriter(os);
                final BufferedWriter bw = new BufferedWriter(osw, 100);
                try {
                    bw.write(command);
                    bw.newLine();
                    // If necessary: bw.write("\r\n");
                    os.flush();
                    bw.flush();
                    // bw.close();
                    System.out.flush();
                } catch (IOException e) {
                    System.err.println("");
                }
            }
        }
    }

    /**
     * Sends sequence of two chars(codes 5 and 3) to a process output stream
     * Source from:
     * https://github.com/joewalnes/idea-community/blob/master/platform
     * /platform-impl/src/com/intellij/execution/process/RunnerMediator.java
     * 
     * Copyright 2000-2010 JetBrains s.r.o.
     *
     * Licensed under the Apache License, Version 2.0 (the "License"); you may
     * not use this file except in compliance with the License. You may obtain a
     * copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     * License for the specific language governing permissions and limitations
     * under the License.
     */
    private static void sendCtrlBreakThroughStream(Process process) {

        if (process != null) {
            OutputStream os = process.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            try {
                pw.print(IAC);
                pw.print(BRK);
                pw.flush();
            } finally {
                pw.close();
            }
        }
    }
}