📄 configurablecaret.java~1~
字号:
*/ 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 */ public void mouseEntered(MouseEvent e) { }/*****************************************************************************/ /** * Called when the mouse exits a region. * * @param e the mouse event * @see MouseListener#mouseExited */ public void mouseExited(MouseEvent e) { }/*****************************************************************************/ /** * Called when the mouse is moved. * * @param e the mouse event * @see MouseMotionListener#mouseMoved */ public void mouseMoved(MouseEvent e) { }/*****************************************************************************/ /** * If button 1 is pressed, this is implemented to * request focus on the associated text component, * and to set the caret position. If the shift key is held down, * the caret will be moved, potentially resulting in a selection, * otherwise the * caret position will be set to the new location. If the component * is not enabled, there will be no request for focus. * * @param e the mouse event * @see MouseListener#mousePressed */ public void mousePressed(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)) { if (e.isConsumed()) { shouldHandleRelease = true; } else { shouldHandleRelease = false; adjustCaretAndFocus(e); if (e.getClickCount() == 2) selectWord(e); } } }/*****************************************************************************/ /** * Called when the mouse is released. * * @param e the mouse event * @see MouseListener#mouseReleased */ public void mouseReleased(MouseEvent e) { if (shouldHandleRelease && SwingUtilities.isLeftMouseButton(e)) adjustCaretAndFocus(e); }/*****************************************************************************/ /** * Tries to move the position of the caret from * the coordinates of a mouse event, using viewToModel(). * This will cause a selection if the dot and mark * are different. * * @param e the mouse event */ protected void moveCaret(MouseEvent e) { Point pt = new Point(e.getX(), e.getY()); int pos = component.getUI().viewToModel(component, pt); moveDot(pos); }/*****************************************************************************/ /** * Moves the caret position to some other position. * * @param dot the position >= 0 * @see Caret#moveDot */ public void moveDot(int dot) { // don't allow selection on disabled components. if (!component.isEnabled()) { setDot(dot); return; } if (dot!=this.dot) { NavigationFilter filter = component.getNavigationFilter(); if (filter != null) filter.moveDot(getFilterBypass(), dot, Position.Bias.Forward); else handleMoveDot(dot); } }/*****************************************************************************/ /** * Paints the cursor. * * @param g The graphics context in which to paint. */ public void paint(Graphics g) { // If the cursor is currently visible... if (isVisible()) { try { g.setColor(component.getCaretColor()); TextUI mapper = component.getUI(); Rectangle r = mapper.modelToView(component, dot); // "Correct" the value of rect.width (takes into // account caret being at EOL (and thus rect.width==1), // etc. // We do this even for LINE_STYLE because // if they change from that caret to block/underline, // the first time they do so width==1, so it will take // one caret flash to paint correctly (wider). If we // do this every time, then it's painted correctly the // first blink. validateWidth(r); // Need to subtract 2 from height, otherwise // the caret will expand too far vertically. r.height -= 2; switch (style) { // Draw a big rectangle, and xor the foreground color. case BLOCK_STYLE: g.setXORMode(Color.WHITE); // fills x==r.x to x==(r.x+(r.width)-1), inclusive. g.fillRect(r.x,r.y, r.width,r.height); break; // Draw a rectangular border. case BLOCK_BORDER_STYLE: // fills x==r.x to x==(r.x+(r.width-1)), inclusive. g.drawRect(r.x,r.y, r.width-1,r.height); break; // Draw an "underline" below the current position. case UNDERLINE_STYLE: g.setXORMode(Color.WHITE); int y = r.y + r.height; g.drawLine(r.x,y, r.x+r.width-1,y); break; // Draw a vertical line. default: g.drawLine(r.x,r.y, r.x,r.y+r.height); } // End of switch (style). } catch (BadLocationException ble) { ble.printStackTrace(); } } // End of if (isVisible()). }/*****************************************************************************/ /** * Tries to set the position of the caret from * the coordinates of a mouse event, using viewToModel(). * * @param e the mouse event */ protected void positionCaret(MouseEvent e) { Point pt = new Point(e.getX(), e.getY()); int pos = component.getUI().viewToModel(component, pt); setDot(pos); }/*****************************************************************************/ private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); setStyle(s.readInt()); handler = new Handler(); }/*****************************************************************************/ /** * Removes a listener that was tracking caret position changes. * * @param l the listener * @see Caret#removeChangeListener */ public void removeChangeListener(ChangeListener l) { listenerList.remove(ChangeListener.class, l); }/*****************************************************************************/ /** * Cause the caret to be painted. The repaint * area is the bounding box of the caret (i.e. * the caret rectangle or <em>this</em>). * <p> * This method is thread safe, although most Swing methods * are not. Please see * <A HREF="http://java.sun.com/products/jfc/swingdoc-archive/threads.html"> * Threads and Swing</A> for more information. */ protected final synchronized void repaint() { if (component != null) component.repaint(x, y, width, height); }/*****************************************************************************/ /** * Repaints the new caret position, with the * assumption that this is happening on the * event thread so that calling <code>modelToView</code> * is safe. */ void repaintNewCaret() { if (component != null) { TextUI mapper = component.getUI(); Document doc = component.getDocument(); if ((mapper != null) && (doc != null)) { // determine the new location and scroll if // not visible. Rectangle newLoc; try { newLoc = mapper.modelToView(component, this.dot); validateWidth(newLoc); } catch (BadLocationException e) { newLoc = null; } if (newLoc != null) { adjustVisibility(newLoc); // If there is no magic caret position, make one if (getMagicCaretPosition() == null) setMagicCaretPosition(new Point(newLoc.x, newLoc.y)); } // repaint the new position damage(newLoc); } } // End of if (component != null). }/*****************************************************************************/ /** * Selects word based on the MouseEvent */ private void selectWord(MouseEvent e) { if (selectedWordEvent != null && selectedWordEvent.getX() == e.getX() && selectedWordEvent.getY() == e.getY()) { // We've already the done selection for this. return; } Action a = null; ActionMap map = getComponent().getActionMap(); if (map != null) a = map.get(RTextAreaEditorKit.selectWordAction); if (a == null) { if (selectWord == null) selectWord = new RTextAreaEditorKit.SelectWordAction(); a = selectWord; } a.actionPerformed(new ActionEvent(getComponent(), ActionEvent.ACTION_PERFORMED, null, e.getWhen(), e.getModifiers())); selectedWordEvent = e; }/*****************************************************************************/ /** * Sets the caret blink rate. * * @param rate the rate in milliseconds, 0 to stop blinking * @see Caret#setBlinkRate */ public void setBlinkRate(int rate) { if (rate != 0) { if (flasher == null) flasher = new Timer(rate, handler); flasher.setDelay(rate); } else { if (flasher != null) { flasher.stop(); flasher.removeActionListener(handler); flasher = null; } } }/*****************************************************************************/ /** * Sets the caret position and mark to some position. This * implicitly sets the selection range to zero. * * @param dot the position >= 0 * @see Caret#setDot */ public void setDot(int dot) { NavigationFilter filter = component.getNavigationFilter(); if (filter != null) filter.setDot(getFilterBypass(), dot, Position.Bias.Forward); else handleSetDot(dot); }/*****************************************************************************/ /** * Saves the current caret position. This is used when * caret up/down actions occur, moving between lines * that have uneven end positions. * * @param p the position * @see #getMagicCaretPosition */ public void setMagicCaretPosition(Point p) { magicCaretPosition = p; }/*****************************************************************************/ /** * Sets whether this caret's selection should have rounded edges. * * @param rounded Whether it should have rounded edges. * @see #getRoundedSelectionEdges */ public void setRoundedSelectionEdges(boolean rounded) { ((ChangableHighlightPainter)getSelectionPainter()). setRoundedEdges(rounded); }/*****************************************************************************/ /** * Changes the selection visibility. * * @param vis the new visibility */ public void setSelectionVisible(boolean vis) { if (vis != selectionVisible) { selectionVisible = vis; if (selectionVisible) { Highlighter h = component.getHighlighter(); if ((dot != mark) && (h != null) && (selectionTag == null)) { int p0 = Math.min(dot, mark); int p1 = Math.max(dot, mark); Highlighter.HighlightPainter p = getSelectionPainter(); try { selectionTag = h.addHighlight(p0, p1, p); } catch (BadLocationException bl) { selectionTag = null; } } } else { if (selectionTag != null) { Highlighter h = component.getHighlighter(); h.removeHighlight(selectionTag); selectionTag = null; } } } // End of if (vis != selectionVisible). }/*****************************************************************************/ /** * Sets the style used when painting the caret. * * @param style The style to use. If this isn't one of * <code>VERTICAL_LINE_STYLE</code>, * <code>UNDERLINE_STYLE</code>, or <code>BLOCK_STYLE</code>, * then <code>VERTICAL_LINE_STYLE</code> is used. * @see #getStyle */ public void setStyle(int style) { if (style<MIN_STYLE || style>MAX_STYLE) style = VERTICAL_LINE_STYLE; this.style = style;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -