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

📄 configurablecaret.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    }
  }

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

  /**
   * Called when the component containing the caret loses
   * focus.  This is implemented to set the caret to visibility
   * to false.
   *
   * @param e the focus event
   * @see FocusListener#focusLost
   */
  public void focusLost(FocusEvent e) {
    setVisible(false);
    setSelectionVisible(ownsSelection || e.isTemporary());
  }

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

  /**
   * Gets the caret blink rate.
   *
   * @return the delay in milliseconds.  If this is
   *  zero the caret will not blink.
   * @see Caret#getBlinkRate
   */
  public int getBlinkRate() {
    return (flasher == null) ? 0 : flasher.getDelay();
  }

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

  /**
   * Returns an array of all the change listeners
   * registered on this caret.
   *
   * @return all of this caret's <code>ChangeListener</code>s
   *         or an empty
   *         array if no change listeners are currently registered
   *
   * @see #addChangeListener
   * @see #removeChangeListener
   */
  public ChangeListener[] getChangeListeners() {
    return (ChangeListener[]) listenerList.getListeners(
        ChangeListener.class);
  }

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

  private ClipboardOwner getClipboardOwner() {
    return handler;
  }

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

  /**
   * Gets the text editor component that this caret is
   * is bound to.
   *
   * @return the component
   */
  protected final JTextComponent getComponent() {
    return component;
  }

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

  /**
   * Fetches the current position of the caret.
   *
   * @return the position >= 0
   * @see Caret#getDot
   */
  public int getDot() {
    return dot;
  }

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

  private NavigationFilter.FilterBypass getFilterBypass() {
    if (filterBypass == null) {
      filterBypass = new DefaultFilterBypass();
    }
    return filterBypass;
  }

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

  /**
   * Returns an array of all the objects currently registered
   * as <code><em>Foo</em>Listener</code>s
   * upon this caret.
   * <code><em>Foo</em>Listener</code>s are registered using the
   * <code>add<em>Foo</em>Listener</code> method.
   *
   * @param listenerType the type of listeners requested; this parameter
   *          should specify an interface that descends from
   *          <code>java.util.EventListener</code>
   * @return an array of all objects registered as
   *          <code><em>Foo</em>Listener</code>s on this component,
   *          or an empty array if no such
   *          listeners have been added
   * @see #getChangeListeners
   */
  public EventListener[] getListeners(Class listenerType) {
    return listenerList.getListeners(listenerType);
  }

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

  /**
   * Gets the saved caret position.
   *
   * @return the position
   * see #setMagicCaretPosition
   */
  public Point getMagicCaretPosition() {
    return magicCaretPosition;
  }

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

  /**
   * Fetches the current position of the mark.  If there is a selection,
   * the dot and mark will not be the same.
   *
   * @return the position >= 0
   * @see Caret#getMark
   */
  public int getMark() {
    return mark;
  }

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

  /**
   * Returns whether this caret's selection uses rounded edges.
   *
   * @return Whether this caret's edges are rounded.
   * @see #setRoundedSelectionEdges
   */
  public boolean getRoundedSelectionEdges() {
    return ( (ChangableHighlightPainter) getSelectionPainter()).
        getRoundedEdges();
  }

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

  /**
   * Gets the painter for the Highlighter.
   *
   * @return the painter
   */
  protected Highlighter.HighlightPainter getSelectionPainter() {
    return selectionPainter;
  }

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

  /**
   * Gets the current style of this caret.
   *
   * @return The caret's style.
   * @see #setStyle
   */
  public int getStyle() {
    return style;
  }

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

  private Clipboard getSystemSelection() {
    try {
      return component.getToolkit().getSystemSelection();
    }
    catch (HeadlessException he) {
      // do nothing... there is no system clipboard
    }
    catch (SecurityException se) {
      // do nothing... there is no allowed system clipboard
    }
    return null;
  }

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

  /**
   * Actually does the dirty work of moving the dot.
   */
  protected void handleMoveDot(int dot) {

    changeCaretPosition(dot);

    if (selectionVisible) {
      Highlighter h = component.getHighlighter();
      if (h != null) {
        int p0 = Math.min(dot, mark);
        int p1 = Math.max(dot, mark);

        // if p0 == p1 then there should be no highlight, remove it
        // if necessary.
        if (p0 == p1) {
          if (selectionTag != null) {
            h.removeHighlight(selectionTag);
            selectionTag = null;
          }
          // otherwise, change or add the highlight
        }
        else {
          try {
            if (selectionTag != null) {
              h.changeHighlight(selectionTag, p0, p1);
            }
            else {
              Highlighter.HighlightPainter p = getSelectionPainter();
              selectionTag = h.addHighlight(p0, p1, p);
            }
          }
          catch (BadLocationException e) {
            throw new InternalError("Bad caret position");
          }
        }
      } // End of if (h != null).
    } // End of if (selectionVisible).

  }

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

  /**
   * Handles the dirty-work of setting the dot location.
   */
  void handleSetDot(int dot) {

    // Move the dot, if it changed locations.
    Document doc = component.getDocument();
    if (doc != null) {
      dot = Math.min(dot, doc.getLength());
    }
    dot = Math.max(dot, 0);

    mark = dot;
    if (this.dot != dot || selectionTag != null ||
        forceCaretPositionChange) {
      changeCaretPosition(dot);
    }
    Highlighter h = component.getHighlighter();
    if ( (h != null) && (selectionTag != null)) {
      h.removeHighlight(selectionTag);
      selectionTag = null;
    }

  }

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

  /**
   * Called when the UI is being installed into the
   * interface of a JTextComponent.  This can be used
   * to gain access to the model that is being navigated
   * by the implementation of this interface.  Sets the dot
   * and mark to 0, and establishes document, property change,
   * focus, mouse, and mouse motion listeners.
   *
   * @param c The text component.  If this is not an
   *          <code>RTextArea</code>, an <code>Exception</code>
   *          will be thrown.
   * @see Caret#install
   */
  public void install(JTextComponent c) {

    if (! (c instanceof RTextArea)) {
      throw new IllegalArgumentException(
          "c must be instance of RTextArea");
    }
    component = (RTextArea) c;
    Document doc = c.getDocument();
    dot = mark = 0;
    if (doc != null) {
      doc.addDocumentListener(handler);
    }
    c.addPropertyChangeListener(handler);
    c.addFocusListener(this);
    c.addMouseListener(this);
    c.addMouseMotionListener(this);

    // if the component already has focus, it won't
    // be notified.
    if (component.hasFocus()) {
      focusGained(null);

    }
  }

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

  /**
   * Determines if the caret is currently active.
   * <p>
   * This method returns whether or not the <code>Caret</code>
   * is currently in a blinking state. It does not provide
   * information as to whether it is currently blinked on or off.
   * To determine if the caret is currently painted use the
   * <code>isVisible</code> method.
   *
   * @return <code>true</code> if active else <code>false</code>
   * @see #isVisible
   */
  public boolean isActive() {
    return active;
  }

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

  /**
   * Checks whether the current selection is visible.
   *
   * @return true if the selection is visible
   */
  public boolean isSelectionVisible() {
    return selectionVisible;
  }

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

  /**
   * Indicates whether or not the caret is currently visible. As the
   * caret flashes on and off the return value of this will change
   * between true, when the caret is painted, and false, when the
   * caret is not painted. <code>isActive</code> indicates whether
   * or not the caret is in a blinking state, such that it <b>can</b>
   * be visible, and <code>isVisible</code> indicates whether or not
   * the caret <b>is</b> actually visible.
   * <p>
   * Subclasses that wish to render a different flashing caret
   * should override paint and only paint the caret if this method
   * returns true.
   *
   * @return true if visible else false
   * @see Caret#isVisible
   * @see #isActive
   */
  public boolean isVisible() {
    return visible;
  }

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

  /**
   * Called when the mouse is clicked.  If the click was generated
   * from button1, a double click selects a word,
   * and a triple click the current line.
   *
   * @param e the mouse event
   * @see MouseListener#mouseClicked
   */
  public void mouseClicked(MouseEvent e) {
    if (!e.isConsumed()) {
      int nclicks = e.getClickCount();
      if (SwingUtilities.isLeftMouseButton(e)) {
        if (nclicks > 1) {
          nclicks = 2 + (nclicks % 2); // Alternate selecting word/line.
        }
        switch (nclicks) {
          case 1:
            selectedWordEvent = null;
            break;
          case 2:
            selectWord(e);
            selectedWordEvent = null;
            break;
          case 3:
            Action a = null;
            ActionMap map = getComponent().getActionMap();
            if (map != null) {
              a = map.get(RTextAreaEditorKit.selectLineAction);
            }
            if (a == null) {
              if (selectLine == null) {
                selectLine = new RTextAreaEditorKit.SelectLineAction();
              }
              a = selectLine;
            }
            a.actionPerformed(new ActionEvent(getComponent(),
                                              ActionEvent.ACTION_PERFORMED,
                                              null, e.getWhen(), e.getModifiers()));
        } // End of switch (nclicks).
      }
      else if (SwingUtilities.isMiddleMouseButton(e)) {
        if (nclicks == 1 && component.isEditable() && component.isEnabled()) {
          // Paste the system selection, if it exists (e.g., on UNIX
          // platforms, the user can select text, the middle-mouse click
          // to paste it; this doesn't work on Windows).  If the system
          // doesn't support system selection, just do a normal paste.
          JTextComponent c = (JTextComponent) e.getSource();
          if (c != null) {
            try {
              Toolkit tk = c.getToolkit();
              Clipboard buffer = tk.getSystemSelection();
              // If the system supports system selections, (e.g. UNIX),
              // try to do it.
              if (buffer != null) {
                adjustCaret(e);
                TransferHandler th = c.getTransferHandler();
                if (th != null) {
                  Transferable trans = buffer.getContents(null);
                  if (trans != null) {
                    th.importData(c, trans);
                  }
                }
                adjustFocus(true);
              }
              // If the system doesn't support system selections
              // (e.g. Windows), just do a normal paste.
              else {
                component.paste();
              }
            }
            catch (HeadlessException he) {
              // do nothing... there is no system clipboard
            }
          } // End of if (c!=null).
        } // End of if (nclicks == 1 && component.isEditable() && component.isEnabled()).
      } // End of else if (SwingUtilities.isMiddleMouseButton(e)).
    } // End of if (!c.isConsumed()).
  }

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

  /**
   * Moves the caret position according to the mouse pointer's
   * current location.  This effectively extends the selection.
   * By default, this is only done for mouse button 1.
   *
   * @param e the mouse event
   * @see MouseMotionListener#mouseDragged
   */
  public void mouseDragged(MouseEvent e) {
    if ( (!e.isConsumed()) && SwingUtilities.isLeftMouseButton(e)) {
      moveCaret(e);
    }
  }

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

  /**
   * Called when the mouse enters a region.
   *
   * @param e the mouse event
   * @see MouseListener#mouseEntered
   */

⌨️ 快捷键说明

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