// Show the command from the command history, pointed to by the index. // Note that the index runs in reverse. private void showHistory() { String lShowLine; if (commandHistoryIndex == 0) lShowLine = currentCommand; else lShowLine = commandHistory.get(commandHistory.size() - commandHistoryIndex); replaceConsoleText(lShowLine, commandPos, textLength()); text.setCaretPosition(textLength()); text.repaint(); }
private String getCmd() { try { return text.getText(commandPos, textLength() - commandPos); } catch (BadLocationException e) { return ""; } }
private void processCommand() { String lCommandRepr = getCmd(); if (lCommandRepr.length() != 0) commandHistory.add(lCommandRepr); lCommandRepr = lCommandRepr + "\n"; appendConsoleText("\n"); commandHistoryIndex = 0; acceptLine(lCommandRepr); text.repaint(); }
/** * The user of the component can write a command in the console as if the user typed the command * himself. The application using the console can simnulate user actions in this way. * * @param aCommand */ public void setCommand(String aCommand) { String lCommandRepr = aCommand; if (lCommandRepr.length() != 0) commandHistory.add(lCommandRepr); lCommandRepr = lCommandRepr + "\n"; appendConsoleText(lCommandRepr); commandHistoryIndex = 0; acceptLine(lCommandRepr); text.repaint(); }
public JConsole() { super(); setBackground(new Color(70, 70, 70)); setForeground(Color.WHITE); text = new MyJTextPane(); text.setAutoscrolls(true); final Font lFont = new Font("Monospaced", Font.PLAIN, 15); text.setText(""); text.setFont(lFont); text.setMargin(new Insets(5, 3, 5, 3)); text.addKeyListener(new MyKeyListener()); setViewportView(text); contextMenu = new JPopupMenu(); final ActionListener lActionListener = new MyActionListener(); contextMenu.add(new JMenuItem(CMD_CUT)).addActionListener(lActionListener); contextMenu.add(new JMenuItem(CMD_COPY)).addActionListener(lActionListener); contextMenu.add(new JMenuItem(CMD_PASTE)).addActionListener(lActionListener); text.addMouseListener(new MyMouseListener()); MutableAttributeSet attr = new SimpleAttributeSet(); StyleConstants.setForeground(attr, Color.BLACK); attr = new SimpleAttributeSet(); StyleConstants.setForeground(attr, Color.WHITE); attrOut = attr; attr = new SimpleAttributeSet(); StyleConstants.setForeground(attr, Color.RED); StyleConstants.setItalic(attr, true); StyleConstants.setBold(attr, true); attrError = attr; try { fromConsoleStream = new PipedOutputStream(); in = new PipedInputStream((PipedOutputStream) fromConsoleStream); final PipedOutputStream lOutPipe = new PipedOutputStream(); out = new PrintStream(lOutPipe); final PipedOutputStream lErrPipe = new PipedOutputStream(); err = new PrintStream(lErrPipe); } catch (IOException e) { e.printStackTrace(); } requestFocus(); }
private int textLength() { return text.getDocument().getLength(); }
public void setFont(Font aFont) { super.setFont(aFont); if (text != null) text.setFont(aFont); }
/** Clear the console. */ public void clear() { text.setText(""); text.repaint(); }
private void moveCaret() { if (text.getCaretPosition() < commandPos) { text.setCaretPosition(textLength()); } text.repaint(); }
// Replace part of the text in the text component. private String replaceConsoleText(Object aContent, int aFrom, int aTo) { final String aContentRepr = aContent.toString(); text.select(aFrom, aTo); text.replaceSelection(aContentRepr); return aContentRepr; }
// Append text to the end of the text already present in the // text component. private void appendConsoleText(String aContent) { final int lTxtLen = textLength(); text.select(lTxtLen, lTxtLen); text.replaceSelection(aContent); }
// Focus handling. public void requestFocus() { super.requestFocus(); text.requestFocus(); }