Example #1
0
 /** Clear the terminal. */
 public void clear() {
   initialise();
   text.setText("");
   if (errorText != null) {
     errorText.setText("");
   }
   hideErrorPane();
 }
Example #2
0
 /** Reset the font according to preferences. */
 public void resetFont() {
   initialise();
   Font terminalFont = getTerminalFont();
   text.setFont(terminalFont);
   if (errorText != null) {
     errorText.setFont(terminalFont);
   }
 }
Example #3
0
  @Override
  public void keyTyped(KeyEvent event) {
    // We handle most things we are interested in here. The InputMap filters out
    // most other unwanted actions (but allows copy/paste).

    char ch = event.getKeyChar();

    if ((!isMacOs) && handleFontsizeKeys(event, ch)) {
      // Note: On Mac OS with Java 7+, we don't see command+= / command+- as a
      // key-typed event and so we handle it in keyReleased instead.
      return;
    }

    if ((event.getModifiers() & Event.META_MASK) != 0) {
      return; // return without consuming the event
    }
    if (isActive) {
      switch (ch) {
        case 4: // CTRL-D (unix/Mac EOF)
        case 26: // CTRL-Z (DOS/Windows EOF)
          buffer.signalEOF();
          writeToTerminal("\n");
          event.consume();
          break;

        case '\b': // backspace
          if (buffer.backSpace()) {
            try {
              int length = text.getDocument().getLength();
              text.replaceRange("", length - 1, length);
            } catch (Exception exc) {
              Debug.reportError("bad location " + exc);
            }
          }
          event.consume();
          break;

        case '\r': // carriage return
        case '\n': // newline
          if (buffer.putChar('\n')) {
            writeToTerminal(String.valueOf(ch));
            buffer.notifyReaders();
          }
          event.consume();
          break;

        default:
          if (ch >= 32) {
            if (buffer.putChar(ch)) {
              writeToTerminal(String.valueOf(ch));
            }
            event.consume();
          }
          break;
      }
    }
  }
Example #4
0
  /** Create a second scrolled text area to the window, for error output. */
  private void createErrorPane() {
    errorText = new TermTextArea(Config.isGreenfoot() ? 20 : 5, 80, null, project, this, true);
    errorScrollPane = new JScrollPane(errorText);
    errorText.setFont(getTerminalFont());
    errorText.setEditable(false);
    errorText.setMargin(new Insets(6, 6, 6, 6));
    errorText.setUnlimitedBuffering(true);

    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPane, errorScrollPane);
  }
Example #5
0
 /** Make the window active. */
 public void activate(boolean active) {
   if (active != isActive) {
     initialise();
     text.setEditable(active);
     if (!active) {
       text.getCaret().setVisible(false);
     }
     isActive = active;
   }
 }
Example #6
0
  private void methodResult(ExecutionEvent event) {
    if (recordMethodCalls) {
      String result = null;
      String resultType = event.getResult();

      if (resultType == ExecutionEvent.NORMAL_EXIT) {
        DebuggerObject object = event.getResultObject();
        if (object != null) {
          if (event.getClassName() != null && event.getMethodName() == null) {
            // Constructor call - the result object is the created object.
            // Don't display the result separately:
            return;
          } else {
            // if the method returns a void, we must handle it differently
            if (object.isNullObject()) {
              return; // Don't show result of void calls
            } else {
              // other - the result object is a wrapper with a single result field
              DebuggerField resultField = object.getField(0);
              result = "    returned " + resultField.getType().toString(true) + " ";
              result += resultField.getValueString();
            }
          }
        }
      } else if (resultType == ExecutionEvent.EXCEPTION_EXIT) {
        result = "    Exception occurred.";
      } else if (resultType == ExecutionEvent.TERMINATED_EXIT) {
        result = "    VM terminated.";
      }

      if (result != null) {
        text.appendMethodCall(result + "\n");
      }
    }
  }
Example #7
0
  /** Write some text to the terminal. */
  private void writeToPane(boolean stdout, String s) {
    prepare();
    if (!stdout) showErrorPane();

    // The form-feed character should clear the screen.
    int n = s.lastIndexOf('\f');
    if (n != -1) {
      clear();
      s = s.substring(n + 1);
    }

    TermTextArea tta = stdout ? text : errorText;

    tta.append(s);
    tta.setCaretPosition(tta.getDocument().getLength());
  }
Example #8
0
  /** Show or hide the Terminal window. */
  public void showHide(boolean show) {
    DataCollector.showHideTerminal(project, show);

    initialise();
    setVisible(show);
    if (show) {
      text.requestFocus();
    }
  }
Example #9
0
  private Action getCopyAction() {
    Action[] textActions = text.getActions();
    for (int i = 0; i < textActions.length; i++) {
      if (textActions[i].getValue(Action.NAME).equals("copy-to-clipboard")) {
        return textActions[i];
      }
    }

    return null;
  }
Example #10
0
 /** An interactive method call has been made by a user. */
 private void methodCall(String callString) {
   newMethodCall = false;
   if (clearOnMethodCall) {
     clear();
   }
   if (recordMethodCalls) {
     text.appendMethodCall(callString + "\n");
   }
   newMethodCall = true;
 }
Example #11
0
 /** Initialise the terminal; create the UI. */
 private synchronized void initialise() {
   if (!initialised) {
     buffer = new InputBuffer(256);
     int width = Config.isGreenfoot() ? 80 : Config.getPropInteger("bluej.terminal.width", 80);
     int height = Config.isGreenfoot() ? 10 : Config.getPropInteger("bluej.terminal.height", 22);
     makeWindow(width, height);
     initialised = true;
     text.setUnlimitedBuffering(unlimitedBufferingCall);
   }
 }
Example #12
0
 private void constructorCall(InvokerRecord ir) {
   newMethodCall = false;
   if (clearOnMethodCall) {
     clear();
   }
   if (recordMethodCalls) {
     String callString =
         ir.getResultTypeString() + " " + ir.getResultName() + " = " + ir.toExpression() + ";";
     text.appendMethodCall(callString + "\n");
   }
   newMethodCall = true;
 }
Example #13
0
 /** Save the terminal text to file. */
 public void save() {
   initialise();
   String fileName =
       FileUtility.getFileName(
           this,
           Config.getString("terminal.save.title"),
           Config.getString("terminal.save.buttonText"),
           null,
           false);
   if (fileName != null) {
     File f = new File(fileName);
     if (f.exists()) {
       if (DialogManager.askQuestion(this, "error-file-exists") != 0) return;
     }
     try {
       FileWriter writer = new FileWriter(fileName);
       text.write(writer);
       writer.close();
     } catch (IOException ex) {
       DialogManager.showError(this, "error-save-file");
     }
   }
 }
Example #14
0
  /** Create the Swing window. */
  private void makeWindow(int columns, int rows) {
    Image icon = BlueJTheme.getIconImage();
    if (icon != null) {
      setIconImage(icon);
    }
    text = new TermTextArea(rows, columns, buffer, project, this, false);
    final InputMap origInputMap = text.getInputMap();
    text.setInputMap(
        JComponent.WHEN_FOCUSED,
        new InputMap() {
          {
            setParent(origInputMap);
          }

          @Override
          public Object get(KeyStroke keyStroke) {
            Object actionName = super.get(keyStroke);

            if (actionName == null) {
              return null;
            }

            char keyChar = keyStroke.getKeyChar();
            if (keyChar == KeyEvent.CHAR_UNDEFINED || keyChar < 32) {
              // We might want to filter the action
              if ("copy-to-clipboard".equals(actionName)) {
                return actionName;
              }
              if ("paste-from-clipboard".equals(actionName)) {
                // Handled via paste() in TermTextArea
                return actionName;
              }
              return PrefMgr.getFlag(PrefMgr.ACCESSIBILITY_SUPPORT) ? actionName : null;
            }

            return actionName;
          }
        });

    scrollPane = new JScrollPane(text);
    text.setFont(getTerminalFont());
    text.setEditable(false);
    text.setMargin(new Insets(6, 6, 6, 6));
    text.addKeyListener(this);

    getContentPane().add(scrollPane, BorderLayout.CENTER);

    setJMenuBar(makeMenuBar());

    // Close Action when close button is pressed
    addWindowListener(
        new WindowAdapter() {
          @Override
          public void windowClosing(WindowEvent event) {
            Window win = (Window) event.getSource();

            // don't allow them to close the window if the debug machine
            // is running.. tries to stop them from closing down the
            // input window before finishing off input in the terminal
            if (project != null) {
              if (project.getDebugger().getStatus() == Debugger.RUNNING) return;
            }
            DataCollector.showHideTerminal(project, false);
            win.setVisible(false);
          }
        });

    // save position when window is moved
    addComponentListener(
        new ComponentAdapter() {
          @Override
          public void componentMoved(ComponentEvent event) {
            Config.putLocation("bluej.terminal", getLocation());
          }
        });

    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

    setLocation(Config.getLocation("bluej.terminal"));

    pack();
  }