Пример #1
0
  // 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();
  }
Пример #2
0
 private String getCmd() {
   try {
     return text.getText(commandPos, textLength() - commandPos);
   } catch (BadLocationException e) {
     return "";
   }
 }
Пример #3
0
  private void processCommand() {
    String lCommandRepr = getCmd();
    if (lCommandRepr.length() != 0) commandHistory.add(lCommandRepr);
    lCommandRepr = lCommandRepr + "\n";

    appendConsoleText("\n");
    commandHistoryIndex = 0;
    acceptLine(lCommandRepr);
    text.repaint();
  }
Пример #4
0
  /**
   * 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();
  }
Пример #5
0
  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();
  }
Пример #6
0
 private int textLength() {
   return text.getDocument().getLength();
 }
Пример #7
0
 public void setFont(Font aFont) {
   super.setFont(aFont);
   if (text != null) text.setFont(aFont);
 }
Пример #8
0
 /** Clear the console. */
 public void clear() {
   text.setText("");
   text.repaint();
 }
Пример #9
0
 private void moveCaret() {
   if (text.getCaretPosition() < commandPos) {
     text.setCaretPosition(textLength());
   }
   text.repaint();
 }
Пример #10
0
 // 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;
 }
Пример #11
0
 // 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);
 }
Пример #12
0
 // Focus handling.
 public void requestFocus() {
   super.requestFocus();
   text.requestFocus();
 }