diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f3b62bc..5a114d36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ FlatLaf Change Log - TextField, FormattedTextField and PasswordField: Support leading and trailing icons (set client property `JTextField.leadingIcon` or `JTextField.trailingIcon` to an `Icon`). (PR #378; issue #368) +- TextComponents: Double-click-and-drag now extends selection by whole words. - Theming improvements: Reworks core themes to make it easier to create new themes (e.g. reduced explicit colors by using color functions). **Note**: There are minor incompatible changes in FlatLaf properties files. (PR #390) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatCaret.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatCaret.java index b6d4b7b0..578a0a54 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatCaret.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatCaret.java @@ -22,14 +22,19 @@ import java.awt.Rectangle; import java.awt.event.FocusEvent; import java.awt.event.MouseEvent; import javax.swing.JFormattedTextField; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; import javax.swing.plaf.UIResource; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultCaret; import javax.swing.text.Document; import javax.swing.text.JTextComponent; +import javax.swing.text.Utilities; /** * Caret that can select all text on focus gained. + * Also fixes Swing's double-click-and-drag behavior so that dragging after + * a double-click extends selection by whole words. * * @author Karl Tauber */ @@ -43,6 +48,9 @@ public class FlatCaret private boolean wasFocused; private boolean wasTemporaryLost; private boolean isMousePressed; + private boolean isWordSelection; + private int beginInitialWord; + private int endInitialWord; public FlatCaret( String selectAllOnFocusPolicy, boolean selectAllOnMouseClick ) { this.selectAllOnFocusPolicy = selectAllOnFocusPolicy; @@ -97,14 +105,56 @@ public class FlatCaret public void mousePressed( MouseEvent e ) { isMousePressed = true; super.mousePressed( e ); + + // left double-click starts word selection + isWordSelection = e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton( e ) && !e.isConsumed(); + if( isWordSelection ) { + beginInitialWord = getMark(); + endInitialWord = getDot(); + } } @Override public void mouseReleased( MouseEvent e ) { isMousePressed = false; + isWordSelection = false; super.mouseReleased( e ); } + @Override + public void mouseDragged( MouseEvent e ) { + if( isWordSelection && !e.isConsumed() && SwingUtilities.isLeftMouseButton( e ) ) { + // fix Swing's double-click-and-drag behavior so that dragging after + // a double-click extends selection by whole words + JTextComponent c = getComponent(); + int pos = c.viewToModel( e.getPoint() ); + if( pos < 0 ) + return; + + try { + int mark; + int dot; + if( pos > endInitialWord ) { + mark = beginInitialWord; + dot = Utilities.getWordEnd( c, pos ); + } else if( pos < beginInitialWord ) { + mark = endInitialWord; + dot = Utilities.getWordStart( c, pos ); + } else { + mark = beginInitialWord; + dot = endInitialWord; + } + if( mark != getMark() ) + setDot( mark ); + if( dot != getDot() ) + moveDot( dot ); + } catch( BadLocationException ex ) { + UIManager.getLookAndFeel().provideErrorFeedback( c ); + } + } else + super.mouseDragged( e ); + } + protected void selectAllOnFocusGained() { JTextComponent c = getComponent(); Document doc = c.getDocument(); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatEditorPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatEditorPaneUI.java index b5386c49..45f02e74 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatEditorPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatEditorPaneUI.java @@ -30,6 +30,7 @@ import javax.swing.JEditorPane; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicEditorPaneUI; +import javax.swing.text.Caret; import javax.swing.text.JTextComponent; import com.formdev.flatlaf.FlatClientProperties; import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable; @@ -147,6 +148,11 @@ public class FlatEditorPaneUI focusListener = null; } + @Override + protected Caret createCaret() { + return new FlatCaret( null, false ); + } + @Override protected void propertyChange( PropertyChangeEvent e ) { // invoke updateBackground() before super.propertyChange() diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextAreaUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextAreaUI.java index 569ab1ab..4b8241c7 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextAreaUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextAreaUI.java @@ -29,6 +29,7 @@ import javax.swing.JTextArea; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicTextAreaUI; +import javax.swing.text.Caret; import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable; import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI; import com.formdev.flatlaf.util.HiDPIUtils; @@ -136,6 +137,11 @@ public class FlatTextAreaUI focusListener = null; } + @Override + protected Caret createCaret() { + return new FlatCaret( null, false ); + } + @Override protected void propertyChange( PropertyChangeEvent e ) { // invoke updateBackground() before super.propertyChange() diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextPaneUI.java index 7d43760b..bd55c311 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextPaneUI.java @@ -29,6 +29,7 @@ import javax.swing.JEditorPane; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicTextPaneUI; +import javax.swing.text.Caret; import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable; import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI; import com.formdev.flatlaf.util.HiDPIUtils; @@ -144,6 +145,11 @@ public class FlatTextPaneUI focusListener = null; } + @Override + protected Caret createCaret() { + return new FlatCaret( null, false ); + } + @Override protected void propertyChange( PropertyChangeEvent e ) { // invoke updateBackground() before super.propertyChange() diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.java index 73e3af9a..a63fb11f 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.java @@ -137,6 +137,15 @@ public class FlatTextComponentsTest JLabel label4 = new JLabel(); JComboBox comboBox6 = new JComboBox<>(); JSpinner spinner5 = new JSpinner(); + JLabel label5 = new JLabel(); + JTextField textField4 = new JTextField(); + JLabel label6 = new JLabel(); + JScrollPane scrollPane2 = new JScrollPane(); + JTextArea textArea2 = new JTextArea(); + JScrollPane scrollPane4 = new JScrollPane(); + JTextPane textPane4 = new JTextPane(); + JScrollPane scrollPane6 = new JScrollPane(); + JEditorPane editorPane5 = new JEditorPane(); JPopupMenu popupMenu1 = new JPopupMenu(); JMenuItem cutMenuItem = new JMenuItem(); JMenuItem copyMenuItem = new JMenuItem(); @@ -168,7 +177,9 @@ public class FlatTextComponentsTest "[::14]" + "[::14]" + "[]" + - "[]")); + "[]para" + + "[]" + + "[90,fill]")); //---- textFieldLabel ---- textFieldLabel.setText("JTextField:"); @@ -519,6 +530,54 @@ public class FlatTextComponentsTest spinner5.setName("spinner5"); add(spinner5, "cell 1 15,growx,hmax 14"); + //---- label5 ---- + label5.setText("Double-click-and-drag:"); + label5.setName("label5"); + add(label5, "cell 0 16"); + + //---- textField4 ---- + textField4.setText("123 456 789 abc def"); + textField4.setName("textField4"); + add(textField4, "cell 1 16 2 1,growx"); + + //---- label6 ---- + label6.setText("JTextArea
JTextPane
JEditorPane"); + label6.setName("label6"); + add(label6, "cell 0 17,align right top,grow 0 0"); + + //======== scrollPane2 ======== + { + scrollPane2.setName("scrollPane2"); + + //---- textArea2 ---- + textArea2.setText("1 123 456 789 abc def\n2 123 456 789 abc def\n3 123 456 789 abc def\n4 123 456 789 abc def\n5 123 456 789 abc def\n6 123 456 789 abc def\n7 123 456 789 abc def\n8 123 456 789 abc def"); + textArea2.setName("textArea2"); + scrollPane2.setViewportView(textArea2); + } + add(scrollPane2, "cell 1 17 4 1,growx"); + + //======== scrollPane4 ======== + { + scrollPane4.setName("scrollPane4"); + + //---- textPane4 ---- + textPane4.setText("1 123 456 789 abc def\n2 123 456 789 abc def\n3 123 456 789 abc def\n4 123 456 789 abc def\n5 123 456 789 abc def\n6 123 456 789 abc def\n7 123 456 789 abc def\n8 123 456 789 abc def"); + textPane4.setName("textPane4"); + scrollPane4.setViewportView(textPane4); + } + add(scrollPane4, "cell 1 17 4 1,growx"); + + //======== scrollPane6 ======== + { + scrollPane6.setName("scrollPane6"); + + //---- editorPane5 ---- + editorPane5.setText("1 123 456 789 abc def\n2 123 456 789 abc def\n3 123 456 789 abc def\n4 123 456 789 abc def\n5 123 456 789 abc def\n6 123 456 789 abc def\n7 123 456 789 abc def\n8 123 456 789 abc def"); + editorPane5.setName("editorPane5"); + scrollPane6.setViewportView(editorPane5); + } + add(scrollPane6, "cell 1 17 4 1,growx"); + //======== popupMenu1 ======== { popupMenu1.setName("popupMenu1"); diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.jfd index c26f61e4..6504d628 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.jfd @@ -1,4 +1,4 @@ -JFDML JFormDesigner: "7.0.4.0.360" Java: "16" encoding: "UTF-8" +JFDML JFormDesigner: "7.0.4.0.360" Java: "17" encoding: "UTF-8" new FormModel { contentType: "form/swing" @@ -10,7 +10,7 @@ new FormModel { add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { "$layoutConstraints": "ltr,insets dialog,hidemode 3" "$columnConstraints": "[][][::100][100,fill][fill]" - "$rowConstraints": "[][][][50,fill][50,fill][50,fill][][]para[40][40][][][::14][::14][][]" + "$rowConstraints": "[][][][50,fill][50,fill][50,fill][][]para[40][40][][][::14][::14][][]para[][90,fill]" } ) { name: "this" add( new FormComponent( "javax.swing.JLabel" ) { @@ -416,9 +416,54 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 15,growx,hmax 14" } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label5" + "text": "Double-click-and-drag:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 16" + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "textField4" + "text": "123 456 789 abc def" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 16 2 1,growx" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label6" + "text": "JTextArea
JTextPane
JEditorPane" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 17,align right top,grow 0 0" + } ) + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "scrollPane2" + add( new FormComponent( "javax.swing.JTextArea" ) { + name: "textArea2" + "text": "1 123 456 789 abc def\n2 123 456 789 abc def\n3 123 456 789 abc def\n4 123 456 789 abc def\n5 123 456 789 abc def\n6 123 456 789 abc def\n7 123 456 789 abc def\n8 123 456 789 abc def" + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 17 4 1,growx" + } ) + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "scrollPane4" + add( new FormComponent( "javax.swing.JTextPane" ) { + name: "textPane4" + "text": "1 123 456 789 abc def\n2 123 456 789 abc def\n3 123 456 789 abc def\n4 123 456 789 abc def\n5 123 456 789 abc def\n6 123 456 789 abc def\n7 123 456 789 abc def\n8 123 456 789 abc def" + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 17 4 1,growx" + } ) + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "scrollPane6" + add( new FormComponent( "javax.swing.JEditorPane" ) { + name: "editorPane5" + "text": "1 123 456 789 abc def\n2 123 456 789 abc def\n3 123 456 789 abc def\n4 123 456 789 abc def\n5 123 456 789 abc def\n6 123 456 789 abc def\n7 123 456 789 abc def\n8 123 456 789 abc def" + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 17 4 1,growx" + } ) }, new FormLayoutConstraints( null ) { "location": new java.awt.Point( 0, 0 ) - "size": new java.awt.Dimension( 530, 660 ) + "size": new java.awt.Dimension( 640, 725 ) } ) add( new FormContainer( "javax.swing.JPopupMenu", new FormLayoutManager( class javax.swing.JPopupMenu ) ) { name: "popupMenu1" @@ -435,7 +480,7 @@ new FormModel { "text": "Paste" } ) }, new FormLayoutConstraints( null ) { - "location": new java.awt.Point( 0, 705 ) + "location": new java.awt.Point( 0, 745 ) "size": new java.awt.Dimension( 91, 87 ) } ) } diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatRSyntaxTextAreaUI.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatRSyntaxTextAreaUI.java index b59f90e6..873678d8 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatRSyntaxTextAreaUI.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatRSyntaxTextAreaUI.java @@ -18,9 +18,16 @@ package com.formdev.flatlaf.themeeditor; import java.awt.Graphics; import java.awt.Rectangle; +import java.awt.event.MouseEvent; import javax.swing.JComponent; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; import javax.swing.text.BadLocationException; +import javax.swing.text.Caret; +import javax.swing.text.JTextComponent; +import javax.swing.text.Utilities; import org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaUI; +import org.fife.ui.rtextarea.ConfigurableCaret; /** * @author Karl Tauber @@ -32,6 +39,13 @@ class FlatRSyntaxTextAreaUI super( rSyntaxTextArea ); } + @Override + protected Caret createCaret() { + Caret caret = new FlatConfigurableCaret(); + caret.setBlinkRate( 500 ); + return caret; + } + @Override protected void paintCurrentLineHighlight( Graphics g, Rectangle visibleRect ) { if( !textArea.getHighlightCurrentLine() ) @@ -49,4 +63,66 @@ class FlatRSyntaxTextAreaUI super.paintCurrentLineHighlight( g, visibleRect ); } } + + //---- class FlatConfigurableCaret ---------------------------------------- + + private static class FlatConfigurableCaret + extends ConfigurableCaret + { + private boolean isWordSelection; + private int beginInitialWord; + private int endInitialWord; + + @Override + public void mousePressed( MouseEvent e ) { + super.mousePressed( e ); + + // left double-click starts word selection + isWordSelection = e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton( e ) && !e.isConsumed(); + if( isWordSelection ) { + beginInitialWord = getMark(); + endInitialWord = getDot(); + } + } + + @Override + public void mouseReleased( MouseEvent e ) { + isWordSelection = false; + super.mouseReleased( e ); + } + + @Override + public void mouseDragged( MouseEvent e ) { + if( isWordSelection && !e.isConsumed() && SwingUtilities.isLeftMouseButton( e ) ) { + // fix Swing's double-click-and-drag behavior so that dragging after + // a double-click extends selection by whole words + JTextComponent c = getComponent(); + int pos = c.viewToModel( e.getPoint() ); + if( pos < 0 ) + return; + + try { + int mark; + int dot; + if( pos > endInitialWord ) { + mark = beginInitialWord; + dot = Utilities.getWordEnd( c, pos ); + } else if( pos < beginInitialWord ) { + mark = endInitialWord; + dot = Utilities.getWordStart( c, pos ); + } else { + mark = beginInitialWord; + dot = endInitialWord; + } + if( mark != getMark() ) + setDot( mark ); + if( dot != getDot() ) + moveDot( dot ); + } catch( BadLocationException ex ) { + UIManager.getLookAndFeel().provideErrorFeedback( c ); + } + } else + super.mouseDragged( e ); + } + } }