| 1 | package de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.utils; |
| 2 | |
| 3 | import java.awt.FileDialog; |
| 4 | import java.awt.Frame; |
| 5 | |
| 6 | import org.rosuda.JRI.RMainLoopCallbacks; |
| 7 | import org.rosuda.JRI.Rengine; |
| 8 | |
| 9 | /**Text console adapter for R engines. |
| 10 | * This text console adapter is used to log inputs, outputs and the history |
| 11 | * for a corresponding R engine. |
| 12 | * @author groenda |
| 13 | */ |
| 14 | public class RTextConsole implements RMainLoopCallbacks { |
| 15 | |
| 16 | /** The current output on the console. */ |
| 17 | private String consoleOutput; |
| 18 | /** The history of the output on the text console. */ |
| 19 | private String consoleOutputHistory; |
| 20 | /** Content of the last message issued to the R engine. */ |
| 21 | private String lastMessage; |
| 22 | |
| 23 | /**Construct a new console. |
| 24 | */ |
| 25 | public RTextConsole() { |
| 26 | consoleOutput = ""; |
| 27 | consoleOutputHistory = ""; |
| 28 | lastMessage = ""; |
| 29 | } |
| 30 | /**Used if text is written to the console. |
| 31 | * @param re Connected R engine. |
| 32 | * @param text Console output. |
| 33 | */ |
| 34 | public void rWriteConsole(final Rengine re, final String text) { |
| 35 | consoleOutput += text + "\n"; |
| 36 | } |
| 37 | |
| 38 | /** {@inheritDoc} |
| 39 | */ |
| 40 | public void rBusy(final Rengine re, final int which) { |
| 41 | // Nothing to do |
| 42 | } |
| 43 | |
| 44 | /** {@inheritDoc} |
| 45 | */ |
| 46 | public String rReadConsole(final Rengine re, final String prompt, |
| 47 | final int addToHistory) { |
| 48 | // Nothing to do |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | /** {@inheritDoc} |
| 53 | */ |
| 54 | public void rShowMessage(final Rengine re, final String message) { |
| 55 | // Nothing to do |
| 56 | consoleOutput += message + "\n"; |
| 57 | } |
| 58 | |
| 59 | /** {@inheritDoc} |
| 60 | */ |
| 61 | @SuppressWarnings("deprecation") |
| 62 | public String rChooseFile(final Rengine re, final int newFile) { |
| 63 | FileDialog fd = new FileDialog(new Frame(), |
| 64 | (newFile == 0) ? "Select a file" : "Select a new file", |
| 65 | (newFile == 0) ? FileDialog.LOAD : FileDialog.SAVE); |
| 66 | fd.show(); |
| 67 | String res = null; |
| 68 | if (fd.getDirectory() != null) { |
| 69 | res = fd.getDirectory(); |
| 70 | } |
| 71 | if (fd.getFile() != null) { |
| 72 | res = (res == null) ? fd.getFile() : (res + fd.getFile()); |
| 73 | } |
| 74 | return res; |
| 75 | } |
| 76 | |
| 77 | /** {@inheritDoc} |
| 78 | */ |
| 79 | public void rFlushConsole(final Rengine re) { |
| 80 | // Nothing to do |
| 81 | } |
| 82 | |
| 83 | /** {@inheritDoc} |
| 84 | */ |
| 85 | public void rLoadHistory(final Rengine re, final String filename) { |
| 86 | // Nothing to do |
| 87 | } |
| 88 | |
| 89 | /** {@inheritDoc} |
| 90 | */ |
| 91 | public void rSaveHistory(final Rengine re, final String filename) { |
| 92 | // Nothing to do |
| 93 | } |
| 94 | |
| 95 | /** {@inheritDoc} |
| 96 | */ |
| 97 | public void rWriteConsole(final Rengine arg0, final String message, |
| 98 | final int arg2) { |
| 99 | lastMessage = message; |
| 100 | consoleOutputHistory += lastMessage; |
| 101 | } |
| 102 | |
| 103 | /**Returns the last message displayed on the R console. |
| 104 | * @return the last message on the R console. |
| 105 | */ |
| 106 | public String getLastMessage() { |
| 107 | return lastMessage; |
| 108 | } |
| 109 | } |