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

📄 defaultcaret.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public void deinstall(JTextComponent c)  {    textComponent.removeFocusListener(this);    textComponent.removeMouseListener(this);    textComponent.removeMouseMotionListener(this);    textComponent.getDocument().removeDocumentListener(documentListener);    documentListener = null;    textComponent.removePropertyChangeListener(propertyChangeListener);    propertyChangeListener = null;    textComponent = null;    // Deinstall blink timer if present.    if (blinkTimer != null)      blinkTimer.stop();    blinkTimer = null;  }  /**   * Installs this <code>Caret</code> on the specified   * <code>JTextComponent</code>. This registers a couple of listeners   * on the text component.   *   * @param c the text component on which to install this caret   */  public void install(JTextComponent c)  {    textComponent = c;    textComponent.addFocusListener(this);    textComponent.addMouseListener(this);    textComponent.addMouseMotionListener(this);    propertyChangeListener = new PropertyChangeHandler();    textComponent.addPropertyChangeListener(propertyChangeListener);    documentListener = new DocumentHandler();    textComponent.getDocument().addDocumentListener(documentListener);    repaint();  }  /**   * Sets the current visual position of this <code>Caret</code>.   *   * @param p the Point to use for the saved location. May be <code>null</code>   *        to indicate that there is no visual location   */  public void setMagicCaretPosition(Point p)  {    magicCaretPosition = p;  }  /**   * Returns the current visual position of this <code>Caret</code>.   *   * @return the current visual position of this <code>Caret</code>   *   * @see #setMagicCaretPosition   */  public Point getMagicCaretPosition()  {    return magicCaretPosition;  }  /**   * Returns the current position of the <code>mark</code>. The   * <code>mark</code> marks the location in the <code>Document</code> that   * is the end of a selection. If there is no selection, the <code>mark</code>   * is the same as the <code>dot</code>.   *   * @return the current position of the mark   */  public int getMark()  {    return mark;  }  private void handleHighlight()  {    Highlighter highlighter = textComponent.getHighlighter();        if (highlighter == null)      return;        int p0 = Math.min(dot, mark);    int p1 = Math.max(dot, mark);        if (selectionVisible && p0 != p1)      {	try	  {	    if (highlightEntry == null)	      highlightEntry = highlighter.addHighlight(p0, p1, getSelectionPainter());	    else	      highlighter.changeHighlight(highlightEntry, p0, p1);	  }	catch (BadLocationException e)	  {	    // This should never happen.	    throw new InternalError();	  }      }    else      {	if (highlightEntry != null)	  {	    highlighter.removeHighlight(highlightEntry);	    highlightEntry = null;	  }      }  }  /**   * Sets the visiblity state of the selection.   *   * @param v <code>true</code> if the selection should be visible,   *        <code>false</code> otherwise   */  public void setSelectionVisible(boolean v)  {    if (selectionVisible == v)      return;        selectionVisible = v;    handleHighlight();    repaint();  }  /**   * Returns <code>true</code> if the selection is currently visible,   * <code>false</code> otherwise.   *   * @return <code>true</code> if the selection is currently visible,   *         <code>false</code> otherwise   */  public boolean isSelectionVisible()  {    return selectionVisible;  }  /**   * Causes the <code>Caret</code> to repaint itself.   */  protected final void repaint()  {    getComponent().repaint(x, y, width, height);  }  /**   * Paints this <code>Caret</code> using the specified <code>Graphics</code>   * context.   *   * @param g the graphics context to use   */  public void paint(Graphics g)  {    JTextComponent comp = getComponent();    if (comp == null)      return;    int dot = getDot();    Rectangle rect = null;    try      {        rect = textComponent.modelToView(dot);      }    catch (BadLocationException e)      {        assert false : "Unexpected bad caret location: " + dot;        return;      }    if (rect == null)      return;    // Check if paint has possibly been called directly, without a previous    // call to damage(). In this case we need to do some cleanup first.    if ((x != rect.x) || (y != rect.y))      {        repaint(); // Erase previous location of caret.        x = rect.x;        y = rect.y;        width = 1;        height = rect.height;      }    // Now draw the caret on the new position if visible.    if (visible)      {        g.setColor(textComponent.getCaretColor());        g.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height);      }  }  /**   * Returns all registered event listeners of the specified type.   *   * @param listenerType the type of listener to return   *   * @return all registered event listeners of the specified type   */  public EventListener[] getListeners(Class listenerType)  {    return listenerList.getListeners(listenerType);  }  /**   * Registers a {@link ChangeListener} that is notified whenever that state   * of this <code>Caret</code> changes.   *   * @param listener the listener to register to this caret   */  public void addChangeListener(ChangeListener listener)  {    listenerList.add(ChangeListener.class, listener);  }  /**   * Removes a {@link ChangeListener} from the list of registered listeners.   *   * @param listener the listener to remove   */  public void removeChangeListener(ChangeListener listener)  {    listenerList.remove(ChangeListener.class, listener);  }  /**   * Returns all registered {@link ChangeListener}s of this <code>Caret</code>.   *   * @return all registered {@link ChangeListener}s of this <code>Caret</code>   */  public ChangeListener[] getChangeListeners()  {    return (ChangeListener[]) getListeners(ChangeListener.class);  }  /**   * Notifies all registered {@link ChangeListener}s that the state   * of this <code>Caret</code> has changed.   */  protected void fireStateChanged()  {    ChangeListener[] listeners = getChangeListeners();    for (int index = 0; index < listeners.length; ++index)      listeners[index].stateChanged(changeEvent);  }  /**   * Returns the <code>JTextComponent</code> on which this <code>Caret</code>   * is installed.   *   * @return the <code>JTextComponent</code> on which this <code>Caret</code>   *         is installed   */  protected final JTextComponent getComponent()  {    return textComponent;  }  /**   * Returns the blink rate of this <code>Caret</code> in milliseconds.   * A value of <code>0</code> means that the caret does not blink.   *   * @return the blink rate of this <code>Caret</code> or <code>0</code> if   *         this caret does not blink   */  public int getBlinkRate()  {    return blinkRate;  }  /**   * Sets the blink rate of this <code>Caret</code> in milliseconds.   * A value of <code>0</code> means that the caret does not blink.   *   * @param rate the new blink rate to set   */  public void setBlinkRate(int rate)  {    if (blinkTimer != null)      blinkTimer.setDelay(rate);    blinkRate = rate;  }  /**   * Returns the current position of this <code>Caret</code> within the   * <code>Document</code>.   *   * @return the current position of this <code>Caret</code> within the   *         <code>Document</code>   */  public int getDot()  {    return dot;  }  /**   * Moves the <code>dot</code> location without touching the   * <code>mark</code>. This is used when making a selection.   *   * @param dot the location where to move the dot   *   * @see #setDot(int)   */  public void moveDot(int dot)  {    if (dot >= 0)      {        this.dot = dot;        handleHighlight();        adjustVisibility(this);        appear();      }  }  /**   * Sets the current position of this <code>Caret</code> within the   * <code>Document</code>. This also sets the <code>mark</code> to the new   * location.   *    * @param dot   *          the new position to be set   * @see #moveDot(int)   */  public void setDot(int dot)  {    if (dot >= 0)      {        this.mark = dot;        this.dot = dot;        handleHighlight();        adjustVisibility(this);        appear();      }  }    /**   * Show the caret (may be hidden due blinking) and adjust the timer not to   * hide it (possibly immediately).   *    * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)   */  void appear()  {    // All machinery is only required if the carret is blinking.    if (blinkListener != null)      {        blinkListener.ignoreNextEvent = true;        // If the caret is visible, erase the current position by repainting        // over.        if (visible)          repaint();        // Draw the caret in the new position.        visible = true;        Rectangle area = null;        try          {            area = getComponent().modelToView(getDot());          }        catch (BadLocationException ex)          {            assert false : "Unexpected bad caret location: " + getDot();          }        if (area != null)          damage(area);      }    repaint();  }    /**   * Returns <code>true</code> if this <code>Caret</code> is currently visible,   * and <code>false</code> if it is not.   *   * @return <code>true</code> if this <code>Caret</code> is currently visible,   *         and <code>false</code> if it is not   */  public boolean isVisible()  {    return visible;  }  /**   * Sets the visibility state of the caret. <code>true</code> shows the   * <code>Caret</code>, <code>false</code> hides it.   *   * @param v the visibility to set   */    public void setVisible(boolean v)  {    if (v != visible)      {        visible = v;        if (visible)          if (textComponent.isEnabled() && textComponent.isEditable())            {              if (blinkTimer == null)                initBlinkTimer();              blinkTimer.start();            }        else          {            if (blinkTimer != null)              blinkTimer.stop();          }        Rectangle area = null;        try          {                        area = getComponent().modelToView(getDot());          }        catch (BadLocationException ex)          {            assert false: "Unexpected bad caret location: " + getDot();          }        if (area != null)          damage(area);      }  }  /**   * Returns the {@link Highlighter.HighlightPainter} that should be used   * to paint the selection.   *   * @return the {@link Highlighter.HighlightPainter} that should be used   *         to paint the selection   */  protected Highlighter.HighlightPainter getSelectionPainter()  {    return DefaultHighlighter.DefaultPainter;  }  /**   * Updates the carets rectangle properties to the specified rectangle and   * repaints the caret.   *   * @param r the rectangle to set as the caret rectangle   */  protected void damage(Rectangle r)  {    if (r == null)      return;    x = r.x;    y = r.y;    width = 1;    // height is normally set in paint and we leave it untouched. However, we    // must set a valid value here, since otherwise the painting mechanism    // sets a zero clip and never calls paint.    if (height <= 0)      height = getComponent().getHeight();    repaint();  }  /**   * Adjusts the text component so that the caret is visible. This default   * implementation simply calls   * {@link JComponent#scrollRectToVisible(Rectangle)} on the text component.   * Subclasses may wish to change this.   */  protected void adjustVisibility(Rectangle rect)  {    getComponent().scrollRectToVisible(rect);  }  /**   * Initializes the blink timer.   */  private void initBlinkTimer()  {    // Setup the blink timer.    blinkListener = new BlinkTimerListener();    blinkTimer = new Timer(getBlinkRate(), blinkListener);    blinkTimer.setRepeats(true);  }}

⌨️ 快捷键说明

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