Mercurial Hosting > luan
changeset 1954:b785eff96faf default tip
fix double-click
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 26 May 2025 19:11:10 -0600 |
parents | 1caa7ddfc461 |
children | |
files | src/luan/modules/swing/SwingLuan.java src/luan/modules/swing/TextAreaLuan.java src/luan/modules/swing/TextFieldLuan.java |
diffstat | 3 files changed, 58 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/swing/SwingLuan.java Mon May 26 10:07:44 2025 -0600 +++ b/src/luan/modules/swing/SwingLuan.java Mon May 26 19:11:10 2025 -0600 @@ -242,4 +242,47 @@ viewport.setViewPosition(p); } } + + + + + public static boolean isWordChar(char c) { + return Character.isJavaIdentifierPart(c); + } + + public static abstract class SelectAction extends AbstractAction { + @Override public void actionPerformed(ActionEvent e) { + JTextComponent comp = (JTextComponent) e.getSource(); + int offset = comp.getCaretPosition(); + String text = comp.getText(); + if (offset < 0 || offset >= text.length()) { + return; + } + if (!ok(text.charAt(offset))) { + //select(offset, offset); + return; + } + int start = offset; + while (start > 0 && ok(text.charAt(start - 1))) + start--; + int end = offset; + while (end < text.length() && ok(text.charAt(end))) + end++; + comp.select(start, end); + } + + abstract boolean ok(char c); + } + + public static final Action selectWordAction = new SelectAction() { + @Override boolean ok(char c) { + return isWordChar(c); + } + }; + + public static final Action selectWordAndDotsAction = new SelectAction() { + @Override boolean ok(char c) { + return isWordChar(c) || c=='.'; + } + }; }
--- a/src/luan/modules/swing/TextAreaLuan.java Mon May 26 10:07:44 2025 -0600 +++ b/src/luan/modules/swing/TextAreaLuan.java Mon May 26 19:11:10 2025 -0600 @@ -18,6 +18,7 @@ import javax.swing.text.Element; import javax.swing.text.WrappedPlainView; import javax.swing.text.Segment; +import javax.swing.text.DefaultEditorKit; import javax.swing.event.DocumentEvent; import goodjava.logging.Logger; import goodjava.logging.LoggerFactory; @@ -66,30 +67,26 @@ if (candidate == p1) return p1; // Everything fits — don't wrap early Segment segment = new Segment(); - getDocument().getText(p0, candidate - p0, segment); + getDocument().getText(p0, candidate - p0 + 1, segment); char[] a = segment.array; int start = segment.offset; - int i = start + segment.count; - if( !isWordChar(a[i]) ) + int i = start + segment.count - 1; + if( !SwingLuan.isWordChar(a[i]) ) return candidate; do { if( --i < start ) return candidate; - } while( isWordChar(a[i]) ); + } while( SwingLuan.isWordChar(a[i]) ); int breakPos = i + 1 - start + p0; do { if( --i < start ) return candidate; - } while( !isWordChar(a[i]) ); + } while( !SwingLuan.isWordChar(a[i]) ); return breakPos; } catch (BadLocationException e) { throw new RuntimeException(e); } } - - private static boolean isWordChar(char c) { - return Character.isJavaIdentifierPart(c); - } } @@ -105,6 +102,8 @@ setCaret(flatLafCaret); } getDocument().addDocumentListener(new WeakDocumentListener(ldl)); + getActionMap().put( DefaultEditorKit.selectWordAction, SwingLuan.selectWordAction ); + getActionMap().put( DefaultEditorKit.selectLineAction, SwingLuan.selectWordAndDotsAction ); } @Override public void updateUI() {
--- a/src/luan/modules/swing/TextFieldLuan.java Mon May 26 10:07:44 2025 -0600 +++ b/src/luan/modules/swing/TextFieldLuan.java Mon May 26 19:11:10 2025 -0600 @@ -5,6 +5,7 @@ import java.awt.Color; import javax.swing.JTextField; import javax.swing.text.BadLocationException; +import javax.swing.text.DefaultEditorKit; import goodjava.logging.Logger; import goodjava.logging.LoggerFactory; @@ -14,6 +15,12 @@ private boolean showWhitespace = false; + public TextFieldLuan() { + super(); + getActionMap().put( DefaultEditorKit.selectWordAction, SwingLuan.selectWordAction ); + getActionMap().put( DefaultEditorKit.selectLineAction, SwingLuan.selectWordAndDotsAction ); + } + public boolean isWhitespaceVisible() { return showWhitespace; }