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

📄 rtextareaui.java

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

      // install keymap
      installListeners();
      installKeyboardActions();

      LayoutManager oldLayout = textArea.getLayout();
      if ( (oldLayout == null) || (oldLayout instanceof UIResource)) {
        // by default, use default LayoutManger implementation that
        // will position the components associated with a View object.
        textArea.setLayout(updateHandler);
      } // End of if (c instanceof RTextArea).

    }

    else {
      throw new Error("RTextAreaUI needs an instance of RTextArea!");
    }

  }

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

  /**
   * Flags model changes.
   * This is called whenever the model has changed.
   * It is implemented to rebuild the view hierarchy
   * to represent the default root element of the
   * associated model.
   */
  protected void modelChanged() {
    // create a view hierarchy
    ViewFactory f = rootView.getViewFactory();
    Document doc = textArea.getDocument();
    Element elem = doc.getDefaultRootElement();
    setView(f.create(elem));
  }

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

  /**
   * Converts the given location in the model to a place in
   * the view coordinate system.
   * The component must have a non-zero positive size for
   * this translation to be computed.
   *
   * @param tc the text component for which this UI is installed
   * @param pos the local location in the model to translate >= 0
   * @return the coordinates as a rectangle, null if the model is not painted
   * @exception BadLocationException  if the given position does not
   *   represent a valid location in the associated document
   * @see TextUI#modelToView
   */
  public Rectangle modelToView(JTextComponent tc, int pos) throws
      BadLocationException {
    return modelToView(tc, pos, Position.Bias.Forward);
  }

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

  /**
   * Converts the given location in the model to a place in the view
   * coordinate system.  The view must have a non-zero positive size for
   * this translation to be computed.
   *
   * @param tc the text component for which this UI is installed
   * @param pos the local location in the model to translate >= 0
   * @return the coordinates as a rectangle, null if the model is not painted
   * @exception BadLocationException  if the given position does not
   *   represent a valid location in the associated document
   * @see TextUI#modelToView
   */
  public Rectangle modelToView(JTextComponent tc, int pos, Position.Bias bias) throws
      BadLocationException {

    RTextAreaDocument doc = (RTextAreaDocument) textArea.getDocument();
    doc.readLock();
    try {
      Rectangle alloc = getVisibleEditorRect();
      if (alloc != null) {
        rootView.setSize(alloc.width, alloc.height);
        Shape s = rootView.modelToView(pos, alloc, bias);
        if (s != null) {
          return s.getBounds();
        }
      }
    }
    finally {
      doc.readUnlock();
    }

    return null;

  }

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

  /**
   * Paints the text area.  This is routed to the <code>paintSafely</code>
   * method under the guarantee that the model won't change from the view of
   * this thread while it's rendering.  This enables the model to
   * potentially be updated asynchronously.
   *
   * @param g The graphics context in which to paint.
   * @param c The <code>RTextArea</code> to paint.
   */
  public final void paint(Graphics g, JComponent c) {
    if ( (rootView.getViewCount() > 0) && (rootView.getView(0) != null)) {
      RTextAreaDocument doc = (RTextAreaDocument) textArea.getDocument();
      doc.readLock();
      try {
        paintSafely(g);
      }
      finally {
        doc.readUnlock();
      }
    }
  }

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

  /**
   * Paints the highlighted current line, if it is enabled.
   *
   * @param g The graphics context with which to paint.
   * @param visibleRect The visible rectangle of the text area.
   */
  protected void paintCurrentLineHighlight(Graphics g, Rectangle visibleRect) {

    if (textArea.isCurrentLineHighlightEnabled()) {

      Caret caret = textArea.getCaret();
      if (caret.getDot() == caret.getMark()) {

        Color highlight = textArea.getCurrentLineHighlightColor();
        // NOTE:  We use the getLineHeight() method below instead
        // of currentCaretRect.height because of a bug where
        // currentCaretRect.height is incorrect when an
        // RSyntaxTextArea is first displayed (it is initialized
        // with the text area's font.getHeight() (via RTextArea),
        // but isn't changed to account for the syntax styles
        // before it is displayed).
        //int height = textArea.currentCaretRect.height);
        int height = textArea.getLineHeight();

        if (textArea.getFadeCurrentLineHighlight()) {
          Graphics2D g2d = (Graphics2D) g;
          Color bg = textArea.getBackground();
          GradientPaint paint = new GradientPaint(
              visibleRect.x, 0, highlight,
              visibleRect.x + visibleRect.width, 0,
              bg == null ? Color.WHITE : bg);
          g2d.setPaint(paint);
          g2d.fillRect(visibleRect.x, textArea.currentCaretY,
                       visibleRect.width, height);
        }
        else {
          g.setColor(highlight);
          g.fillRect(visibleRect.x, textArea.currentCaretY,
                     visibleRect.width, height);
        }

      } // End of if (caret.getDot()==caret.getMark()).

    } // End of if (textArea.isCurrentLineHighlightEnabled()...

  }

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

  /**
   * Draws the "margin line" if enabled.
   *
   * @param g The graphics context to paint with.
   * @param visibleRect The visible rectangle of this text area.
   */
  protected void paintMarginLine(Graphics g, Rectangle visibleRect) {
    if (textArea.isMarginLineEnabled()) {
      g.setColor(textArea.getMarginLineColor());
      Insets insets = textArea.getInsets();
      int marginLineX = textArea.getMarginLinePixelLocation() +
          (insets == null ? 0 : insets.left);
      g.drawLine(marginLineX, visibleRect.y,
                 marginLineX, visibleRect.y + visibleRect.height);
    }
  }

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

  /**
   * Paints the root view (i.e., all of the text), if necessary.
   *
   * @param g The graphics context.
   */
  protected void paintRootView(Graphics g) {
    Rectangle alloc = getVisibleEditorRect();
    if (alloc != null) {
      rootView.paint(g, alloc);
    }
  }

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

  /**
   * Paints the interface safely with a guarantee that the model won't change
   * from the view of this thread.  This does the following things, rendering
   * from back to front.
   * <ol>
   * <li>If the component is marked as opaque, the background is painted in
   *     the current background color of the component.</li>
   * <li>The highlights (if any) are painted.</li>
   * <li>The view hierarchy is painted.</li>
   * <li>The caret is painted.</li>
   * </ol>
   *
   * @param g The graphics context.
   */
  protected void paintSafely(Graphics g) {

    painted = true;

    // Paint the "background" stuff (though main background
    // is already done).
    Rectangle visibleRect = textArea.getVisibleRect();
    paintCurrentLineHighlight(g, visibleRect);
    paintMarginLine(g, visibleRect);

    // Paint the actual text.
    paintRootView(g);

    // Paint the caret.
    Caret caret = textArea.getCaret();
    if (caret != null) {
      caret.paint(g);

    }
  }

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

  /**
   * This method gets called when a bound property is changed
   * on the associated RTextArea.  This is a hook
   * which UI implementations may change to reflect how the
   * UI displays bound properties of JTextComponent subclasses.<p>
   *
   * These events are forwarded to us from
   * {@link org.fife.ui.rtextarea.RTAUpdateHandler}.
   *
   * @param e The property change event.
   */
  protected void propertyChange(PropertyChangeEvent e) {

    String propertyName = e.getPropertyName();

    if (propertyName.equals("lineWrap") ||
        propertyName.equals("wrapStyleWord") ||
        propertyName.equals("tabSize")) {
      // rebuild the view
      modelChanged();
    }
    else if ("editable".equals(propertyName)) {
      updateFocusTraversalKeys();
    }

  }

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

  /**
   * Sets the current root of the view hierarchy and calls invalidate().
   * If there were any child components, they will be removed (i.e.
   * there are assumed to have come from components embedded in views).
   *
   * @param v the root view
   */
  protected final void setView(View v) {
    rootView.setView(v);
    painted = false;
    textArea.revalidate();
    textArea.repaint();
  }

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

  /**
   * Sets the component properties that haven't been explicitly overridden to
   * null.  A property is considered overridden if its current value
   * is not a UIResource.
   *
   * @see #installDefaults
   * @see #uninstallUI
   */
  protected void uninstallDefaults() {

    if (textArea.getCaretColor()instanceof UIResource) {
      textArea.setCaretColor(null);
    }

    if (textArea.getSelectionColor()instanceof UIResource) {
      textArea.setSelectionColor(null);
    }

    if (textArea.getDisabledTextColor()instanceof UIResource) {
      textArea.setDisabledTextColor(null);
    }

    if (textArea.getSelectedTextColor()instanceof UIResource) {
      textArea.setSelectedTextColor(null);
    }

    if (textArea.getBorder()instanceof UIResource) {
      textArea.setBorder(null);
    }

    if (textArea.getMargin()instanceof UIResource) {
      textArea.setMargin(null);
    }

    if (textArea.getCaret()instanceof UIResource) {
      textArea.setCaret(null);
    }

  }

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

  /**
   * Uninstalls the registered keyboard actions.
   */
  protected void uninstallKeyboardActions() {
    textArea.setKeymap(null);
    SwingUtilities.replaceUIInputMap(textArea, JComponent.
                                     WHEN_IN_FOCUSED_WINDOW, null);
    SwingUtilities.replaceUIActionMap(textArea, null);
  }

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

  /**
   * Uninstalls listeners for the UI.
   */
  protected void uninstallListeners() {
  }

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

  /**
   * Uninstalls the private defaults of the UI.
   */
  protected void uninstallPrivateDefaults() {

    textArea.removeMouseListener(defaultDragRecognizer);

⌨️ 快捷键说明

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