/** * Realizes the GUI */ package gui; import java.awt.*; import java_cup.runtime.ComplexSymbolFactory; import java_cup.runtime.SymbolFactory; import javax.swing.*; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.BadLocationException; import commands.shell; import java.awt.event.*; import java.io.*; import java.util.Observable; import java.util.Observer; import kernel.AbstractProcess; import kernel.CommandList; import kernel.Constants; @SuppressWarnings("serial") public class TextConsoleWindow extends JFrame implements Observer { private shell shell; private StringBuffer in = null; private int MinDotPos = 0; private String prompt; private JScrollPane jScrollPane1 = new JScrollPane(); private JTextArea fConsole = new JTextArea(); private boolean stdInMode = false; private ErrOutCustomer errorCust; private StdOutCustomer stdCust; private boolean printPrompt = true; public TextConsoleWindow(shell sh) { /* * try { jbInit(); setSize(500, 300); show(); } catch(Exception e) { * e.printStackTrace(); } */ setPrompt(System.getProperty("user.dir") + System.getProperty("file.separator") + ": "); // this.fConsole.setText(getPrompt()); // this.MinDotPos = getPrompt().length(); writePrompt(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.in = new StringBuffer(); this.shell = sh; this.shell.setCurrentPath(System.getProperty("user.dir") + System.getProperty("file.separator")); try { jbInit(); setSize(800, 600); // setSize(640, 480); // show(); setVisible(true); } catch (Exception e) { e.printStackTrace(); } this.startStdOutDeamon(); } private void jbInit() throws Exception { this.fConsole.setToolTipText(""); this.fConsole.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(KeyEvent e) { fConsole_keyTyped(e); } }); this.fConsole.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(KeyEvent e) { fConsole_keyPressed(e); } }); this.fConsole.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(KeyEvent e) { fConsole_keyReleased(e); } }); this.fConsole.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent arg0) { fConsole_caterMooved(arg0); } }); this.fConsole.setFont(new java.awt.Font("Monospaced", 0, 12)); this.fConsole.setAutoscrolls(true); this.getContentPane().add(jScrollPane1, BorderLayout.CENTER); this.jScrollPane1.getViewport().add(this.fConsole, null); this.fConsole.getCaret().setDot(MinDotPos); } public synchronized void writeData(Object[] o) throws Exception { /* We will output all objects as new lines into the fConsole. */ try { this.fConsole.getCaret().setDot(this.fConsole.getText().length()); for (int i = 0; i < o.length; i++) { if (o[i] != null) { this.fConsole.append(o[i].toString()); } } this.fConsole.getCaret().setDot(this.fConsole.getText().length()); } catch (Throwable a) { throw (new Exception("writeData[" + a.getClass().getName() + ": " + a.getMessage() + "]")); } } public synchronized void writeData(String s) { /* We will output all objects as new lines into the fConsole. */ if (s != null) { this.fConsole.getCaret().setDot(this.fConsole.getText().length()); this.fConsole.append(s); this.fConsole.getCaret().setDot(this.fConsole.getText().length()); } } void fConsole_caterMooved(CaretEvent arg0) { try { if (arg0.getDot() < MinDotPos) this.fConsole.getCaret().setDot(MinDotPos); } catch (Exception e) { } } void fConsole_keyTyped(KeyEvent e) { // if user typed the Enter key, then we will take this as input of current // line printPrompt = true; if ((e.getKeyChar() == KeyEvent.VK_ENTER) && (!isStdInMode())) { try { int ln = this.fConsole.getLineOfOffset(fConsole.getCaretPosition()) - 1; int strt = this.fConsole.getLineStartOffset(ln) + getPrompt().length(); // TODO: upravit... jen testovaci kvuli pousteni prikazu this.in.append(this.fConsole.getText(strt, this.fConsole.getLineEndOffset(ln) - strt - 1).toCharArray()); } catch (Exception ex) { ex.printStackTrace(); } if (this.in.toString().length() == 0) { writePrompt(); } else { try { System.out.println("ExecCommand: " + this.in.toString()); this.shell.execCommand(this.in.toString()); } catch (Exception a) { this.writeData(a.getMessage() + "\n"); this.writePrompt(); } } this.in.setLength(0); } else if ((e.getKeyChar() == Constants.CTRL_D) && (e.isControlDown()) && (isStdInMode())) { setStdInMode(false); System.out.println("StdInMode(false)"); try { this.getStdIn().close(); } catch (IOException e1) { e1.printStackTrace(); } } else if (isStdInMode()) { try { this.getStdIn().write(e.getKeyChar()); this.getStdIn().flush(); } catch (IOException e1) { e1.printStackTrace(); } } } void fConsole_keyPressed(KeyEvent e) { if ((e.getKeyChar() == KeyEvent.VK_BACK_SPACE) && (fConsole.getCaret().getDot() <= MinDotPos)) { fConsole.setEditable(false); // System.out.println("ba"); } if ((e.getKeyChar() == KeyEvent.VK_DELETE) && (fConsole.getSelectionStart() < MinDotPos)) { fConsole.setSelectionStart(MinDotPos); // System.out.println(fConsole.getSelectionStart()+"/"+getMinDotPos()); // System.out.println("ba"); } } void fConsole_keyReleased(KeyEvent e) { if ((e.getKeyChar() == KeyEvent.VK_BACK_SPACE) && (fConsole.getCaret().getDot() <= MinDotPos)) { fConsole.setEditable(true); // System.out.println("unba"); } } public int getMinDotPos() { return MinDotPos; } public void setMinDotPos(int minDotPos) { MinDotPos = minDotPos; // System.out.println("MinDotPos:"+minDotPos); } public void setMinDotPos() { setMinDotPos(fConsole.getText().length()); // System.out.println("MinDotPos:"+minDotPos); } public void update(Observable ob, Object arg) { if (arg == Constants.DISABLE_STDIN) { System.out.println("Switching to command line."); setStdInMode(false); return; } if (arg == Constants.ENABLE_STDIN) { System.out.println("Switching to standard input."); // {HOTOVO spis pipedoutpudstream?} TODO: vytvorit pipedinputstream a mit // moznost do nej zapisovat z okna, pri CTRL_D se stream flushne(), abych // z nej mohl cist setStdInMode(true); return; } if (arg == Constants.EXIT_SHELL) { System.out.println("Exiting Shell...."); this.setVisible(false); this.dispose(); System.exit(0); return; } if (arg == Constants.DIR_CHANGE) { setPrompt(shell.getCurrentPath() + ": "); return; } } public String getPrompt() { return prompt; } public void writePrompt() { if (printPrompt) { printPrompt = false; Object[] o = new Object[1]; this.fConsole.getCaret().setDot(this.fConsole.getText().length()); // if (this.fConsole.getLineCount()==1) // o[0] = getPrompt(); // else o[0] = getPrompt(); try { writeData(o); } catch (Exception e) { } setMinDotPos(); this.fConsole.getCaret().setDot(this.fConsole.getText().length()); } } public void setPrompt(String prompt) { this.prompt = prompt; } public boolean isStdInMode() { return stdInMode; } public void setStdInMode(boolean stdInMode) { this.stdInMode = stdInMode; } public PipedInputStream getStdOut() { return (PipedInputStream) this.shell.getStdOutPipe().getPipeIn(); } public PipedOutputStream getStdIn() { return (PipedOutputStream) this.shell.getStdInPipe().getPipeOut(); } public void startStdOutDeamon() { this.stdCust = new StdOutCustomer(this); } }