Hi, this patch fixes the missing shift-click behavior of the caret (PR 26416).
And while I was at this class I saw that we are missing word (double-click) and line (triple-click) behavior and implemented that. In case of the double click the RI will select the gap between two if you click on it. We do that too, of course. :) @Mark: I really would like to have this in the release branch. :) 2006-03-06 Robert Schuster <[EMAIL PROTECTED]> * javax/swing/text/DefaultCaret.java: (mouseDragged): Do selection when shift is pressed. (mouseClicked): Implemented. cya Robert
Index: javax/swing/text/DefaultCaret.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultCaret.java,v retrieving revision 1.32 diff -u -r1.32 DefaultCaret.java --- javax/swing/text/DefaultCaret.java 1 Mar 2006 20:39:49 -0000 1.32 +++ javax/swing/text/DefaultCaret.java 6 Mar 2006 02:02:08 -0000 @@ -366,7 +366,8 @@ * <ul> * <li>If we receive a double click, the caret position (dot) is set * to the position associated to the mouse click and the word at - * this location is selected.</li> + * this location is selected. If there is no word at the pointer + * the gap is selected instead.</li> * <li>If we receive a triple click, the caret position (dot) is set * to the position associated to the mouse click and the line at * this location is selected.</li> @@ -376,7 +377,50 @@ */ public void mouseClicked(MouseEvent event) { - // TODO: Implement double- and triple-click behaviour here. + int count = event.getClickCount(); + + if (count >= 2) + { + int newDot = getComponent().viewToModel(event.getPoint()); + JTextComponent t = getComponent(); + + try + { + if (count == 3) + t.select(Utilities.getRowStart(t, newDot), Utilities.getRowEnd(t, newDot)); + else + { + int nextWord = Utilities.getNextWord(t, newDot); + + // When the mouse points at the offset of the first character + // in a word Utilities().getPreviousWord will not return that + // word but we want to select that. We have to use + // Utilities.nextWord() to get it. + if (newDot == nextWord) + t.select(nextWord, Utilities.getNextWord(t, nextWord)); + else + { + int previousWord = Utilities.getPreviousWord(t, newDot); + int previousWordEnd = Utilities.getWordEnd(t, previousWord); + + // If the user clicked in the space between two words, + // then select the space. + if (newDot >= previousWordEnd && newDot <= nextWord) + t.select(previousWordEnd, nextWord); + // Otherwise select the word under the mouse pointer. + else + t.select(previousWord, previousWordEnd); + } + } + } + catch(BadLocationException ble) + { + // TODO: Swallowing ok here? + } + + dot = newDot; + } + } /** @@ -411,7 +455,10 @@ */ public void mousePressed(MouseEvent event) { - positionCaret(event); + if (event.isShiftDown()) + moveCaret(event); + else + positionCaret(event); } /**
signature.asc
Description: OpenPGP digital signature