⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtextareaui.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
      doc.readUnlock();
    }
    return d;
  }

      /*****************************************************************************/

  /**
   * Provides a way to determine the next visually represented model
   * location that one might place a caret.  Some views may not be visible,
   * they might not be in the same order found in the model, or they just
   * might not allow access to some of the locations in the model.
   *
   * @param t The RTextArea for which to get the next visual position.
   * @param pos The position to convert >= 0
   * @param b The bias.
   * @param direction The direction from the current position that can
   *  be thought of as the arrow keys typically found on a keyboard.
   *  This may be SwingConstants.WEST, SwingConstants.EAST,
   *  SwingConstants.NORTH, or SwingConstants.SOUTH.
   * @return the location within the model that best represents the next
   *  location visual position.
   * @exception BadLocationException
   * @exception IllegalArgumentException for an invalid direction
   */
  public int getNextVisualPositionFrom(JTextComponent t, int pos,
                                       Position.Bias b, int direction,
                                       Position.Bias[] biasRet) throws
      BadLocationException {

    RTextAreaDocument doc = (RTextAreaDocument) textArea.getDocument();
    doc.readLock();
    try {
      if (painted) {
        Rectangle alloc = getVisibleEditorRect();
        rootView.setSize(alloc.width, alloc.height);
        return rootView.getNextVisualPositionFrom(pos, b, alloc,
                                                  direction, biasRet);
      }
    }
    finally {
      doc.readUnlock();
    }

    return -1;

  }

      /*****************************************************************************/

  /**
   * Gets the preferred size for the editor component.  If the component
   * has been given a size prior to receiving this request, it will
   * set the size of the view hierarchy to reflect the size of the component
   * before requesting the preferred size of the view hierarchy.  This
   * allows formatted views to format to the current component size before
   * answering the request.  Other views don't care about currently formatted
   * size and give the same answer either way.
   *
   * @param c the editor component
   * @return the size
   */
  public Dimension getPreferredSize(JComponent c) {

    RTextAreaDocument doc = (RTextAreaDocument) textArea.getDocument();
    Insets i = c.getInsets();
    Dimension d = c.getSize();

    doc.readLock();

    try {
      if ( (d.width > (i.left + i.right)) && (d.height > (i.top + i.bottom))) {
        rootView.setSize(d.width - i.left - i.right,
                         d.height - i.top - i.bottom);
      }
      else if (d.width == 0 && d.height == 0) {
        // Probably haven't been layed out yet, force some sort of
        // initial sizing.
        rootView.setSize(Integer.MAX_VALUE, Integer.MAX_VALUE);
      }
      d.width = (int) Math.min( (long) rootView.getPreferredSpan(View.X_AXIS) +
                               (long) i.left + (long) i.right,
                               Integer.MAX_VALUE);
      d.height = (int) Math.min( (long) rootView.getPreferredSpan(View.Y_AXIS) +
                                (long) i.top + (long) i.bottom,
                                Integer.MAX_VALUE);
    }
    finally {
      doc.readUnlock();
    }

    return d;

  }

      /*****************************************************************************/

  /**
   * Fetches the name used as a key to look up properties through the
   * UIManager.  This is used as a prefix to all the standard
   * text properties.
   *
   * @return The name ("TextArea")
   */
  protected String getPropertyPrefix() {
    return "TextArea";
  }

      /*****************************************************************************/

  /**
   * Returns the root view for this UI.  The child view of the root view is
   * the view responsible for painting the text of the text area.
   *
   * @param textComponent This parameter is ignored.
   */
  public View getRootView(JTextComponent textComponent) {
    return rootView;
  }

      /*****************************************************************************/

  /**
   * Returns the text area for which we are the UI.
   *
   * @return The text area.
   */
  public RTextArea getRTextArea() {
    return textArea;
  }

      /*****************************************************************************/

  /**
   * Returns the <code>TransferHandler</code> that will be installed if
   * their isn't one installed on the <code>JTextComponent</code>.
   */
  TransferHandler getTransferHandler() {
    return transferHandler;
  }

      /*****************************************************************************/

  /**
   * Gets the allocation to give the root View.  Due
   * to an unfortunate set of historical events this
   * method is inappropriately named.  The Rectangle
   * returned has nothing to do with visibility.
   * The component must have a non-zero positive size for
   * this translation to be computed.
   *
   * @return the bounding box for the root view
   */
  protected Rectangle getVisibleEditorRect() {
    Rectangle alloc = textArea.getBounds();
    if ( (alloc.width > 0) && (alloc.height > 0)) {
      alloc.x = alloc.y = 0;
      Insets insets = textArea.getInsets();
      alloc.x += insets.left;
      alloc.y += insets.top;
      alloc.width -= insets.left + insets.right;
      alloc.height -= insets.top + insets.bottom;
      return alloc;
    }
    return null;
  }

      /*****************************************************************************/

  /**
   * Initializes component properties, e.g. font, foreground, background,
   * caret color, selection color, selected text color, disabled text color,
   * and border color.  The properties are only set if the current value is
   * <code>null</code> (as opposed to BasicTextUI, which checks for instances
   * of <code>UIResource</code>).
   *
   * @see #uninstallDefaults
   * @see #installUI
   */
  protected void installDefaults() {

    String prefix = getPropertyPrefix();

    Font f = textArea.getFont();
    if ( (f == null) || (f instanceof UIResource)) {
      textArea.setFont(UIManager.getFont(prefix + ".font"));

    }
    Object bg = textArea.getBackgroundObject();
    if (bg == null) {
      textArea.setBackground(UIManager.getColor(prefix +
                                                ".background"));

    }
    Color fg = textArea.getForeground();
    if (fg == null) {
      textArea.setForeground(UIManager.getColor(prefix +
                                                ".foreground"));

    }
    Color color = textArea.getCaretColor();
    if (color == null) {
      textArea.setCaretColor(RTextArea.getDefaultCaretColor());

    }
    Color s = textArea.getSelectionColor();
    if (s == null) {
      textArea.setSelectionColor(UIManager.getColor(prefix +
          ".selectionBackground"));

    }
    Color sfg = textArea.getSelectedTextColor();
    if (sfg == null) {
      textArea.setSelectedTextColor(UIManager.getColor(prefix +
          ".selectionForeground"));

    }
    Color dfg = textArea.getDisabledTextColor();
    if (dfg == null) {
      textArea.setDisabledTextColor(UIManager.getColor(prefix +
          ".inactiveForeground"));

    }
    Border b = textArea.getBorder();
    if (b == null) {
      textArea.setBorder(UIManager.getBorder(prefix + ".border"));

    }
    Insets margin = textArea.getMargin();
    if (margin == null) {
      textArea.setMargin(UIManager.getInsets(prefix + ".margin"));

    }
  }

      /*****************************************************************************/

  protected void installKeyboardActions() {

    // backward compatibility support... keymaps for the UI
    // are now installed in the more friendly input map.
    textArea.setKeymap(createKeymap());

    InputMap km = getInputMap();
    if (km != null) {
      SwingUtilities.replaceUIInputMap(textArea,
                                       JComponent.WHEN_FOCUSED, km);
    }

    ActionMap map = getActionMap();
    if (map != null) {
      SwingUtilities.replaceUIActionMap(textArea, map);
    }

    updateFocusAcceleratorBinding(false);

  }

      /*****************************************************************************/

  /**
   * Installs listeners for the UI.
   */
  protected void installListeners() {
  }

      /*****************************************************************************/

  /**
   * Installs defaults that the user shouldn't be able to muck with.
   */
  private void installPrivateDefaults() {

    textArea.addMouseListener(defaultDragRecognizer);
    textArea.addMouseMotionListener(defaultDragRecognizer);

    // NOTE:  At a minimum, we must at least reinstall the current caret;
    // otherwise, our added mouse listeners won't take effect (as
    // ConfigurableCaret is not an instance of UIResource, and
    // unfortunately, due to the design of the Java Swing text package,
    // the drag listeners must be registered before the caret for DnD to
    // work).
    Caret caret = textArea.getCaret();
    if (caret == null) {
      textArea.setCaret(createCaret());
    }
    else {
      textArea.setCaret(caret);

    }
    Highlighter highlighter = textArea.getHighlighter();
    if (highlighter == null || highlighter instanceof UIResource) {
      textArea.setHighlighter(createHighlighter());
    }

    TransferHandler th = textArea.getTransferHandler();
    if (th == null || th instanceof UIResource) {
      textArea.setTransferHandler(getTransferHandler());
    }
    DropTarget dropTarget = textArea.getDropTarget();
    if (dropTarget instanceof UIResource) {
      if (dropTargetListener == null) {
        dropTargetListener = new RTADropTargetListener();
      }
      try {
        dropTarget.addDropTargetListener(dropTargetListener);
      }
      catch (TooManyListenersException tmle) {
        // should not happen... swing drop target is multicast
      }
    }

  }

      /*****************************************************************************/

  /**
   * Installs this UI to the given text component.
   */
  public void installUI(JComponent c) {

    if (c instanceof RTextArea) {

      textArea = (RTextArea) c;

      // Create our view.
      rootView = new RTARootView(this);

      // Install defaults.
      installDefaults();
      installPrivateDefaults();

      // NOTE:  the opaque property should be set to true when the
      // background is a color.  When an image is used for the
      // background, opaque should be set to false.  This is because
      // we perform faster when setOpaque is true, but if we use an
      // image for the background when opaque is true, we get on-screen
      // garbage when the user scrolls via the arrow keys.  Thus we
      // need setOpaque to be false in that case.
      textArea.setOpaque(true);

      textArea.setAutoscrolls(true);
      textArea.setDragEnabled(true);

      // Attach to the model and editor
      textArea.addPropertyChangeListener(updateHandler);
      Document doc = textArea.getDocument();
      if (doc == null) {
        // If there is no model, create a default one.  This will
        // fire a notification to the updateHandler which takes
        // care of the rest.
        textArea.setDocument(getEditorKit(textArea).
                             createDefaultDocument());
      }
      else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -