3434import java .awt .event .FocusEvent ;
3535import java .awt .event .FocusListener ;
3636import java .awt .geom .Rectangle2D ;
37- import java .beans .PropertyChangeEvent ;
3837import java .beans .PropertyChangeListener ;
3938import java .util .Map ;
4039import javax .swing .Action ;
6665import com .formdev .flatlaf .icons .FlatCheckBoxIcon ;
6766import com .formdev .flatlaf .ui .FlatStylingSupport .Styleable ;
6867import com .formdev .flatlaf .ui .FlatStylingSupport .StyleableUI ;
68+ import com .formdev .flatlaf .ui .FlatUIUtils .FlatPropertyWatcher ;
6969import com .formdev .flatlaf .util .Graphics2DProxy ;
7070import com .formdev .flatlaf .util .HiDPIUtils ;
7171import com .formdev .flatlaf .util .LoggingFacade ;
@@ -189,29 +189,26 @@ protected void installDefaults() {
189189 if ( rowHeight > 0 )
190190 LookAndFeel .installProperty ( table , "rowHeight" , UIScale .scale ( rowHeight ) );
191191
192- FlatTablePropertyWatcher watcher = FlatTablePropertyWatcher .get ( table );
193- if ( watcher != null )
194- watcher .enabled = false ;
195-
196- if ( !showHorizontalLines && (watcher == null || !watcher .showHorizontalLinesChanged ) ) {
197- oldShowHorizontalLines = table .getShowHorizontalLines ();
198- table .setShowHorizontalLines ( false );
192+ if ( !showHorizontalLines ) {
193+ FlatPropertyWatcher .runIfNotChanged ( table , "showHorizontalLines" , () -> {
194+ oldShowHorizontalLines = table .getShowHorizontalLines ();
195+ table .setShowHorizontalLines ( false );
196+ } );
199197 }
200- if ( !showVerticalLines && (watcher == null || !watcher .showVerticalLinesChanged ) ) {
201- oldShowVerticalLines = table .getShowVerticalLines ();
202- table .setShowVerticalLines ( false );
198+ if ( !showVerticalLines ) {
199+ FlatPropertyWatcher .runIfNotChanged ( table , "showVerticalLines" , () -> {
200+ oldShowVerticalLines = table .getShowVerticalLines ();
201+ table .setShowVerticalLines ( false );
202+ } );
203203 }
204204
205- if ( intercellSpacing != null && (watcher == null || !watcher .intercellSpacingChanged ) ) {
206- oldIntercellSpacing = table .getIntercellSpacing ();
207- table .setIntercellSpacing ( intercellSpacing );
205+ if ( intercellSpacing != null ) {
206+ FlatPropertyWatcher .runIfNotChanged ( table , "rowMargin" , () -> {
207+ oldIntercellSpacing = table .getIntercellSpacing ();
208+ table .setIntercellSpacing ( intercellSpacing );
209+ } );
208210 }
209211
210- if ( watcher != null )
211- watcher .enabled = true ;
212- else
213- table .addPropertyChangeListener ( new FlatTablePropertyWatcher () );
214-
215212 // install boolean renderer
216213 oldBooleanRenderer = table .getDefaultRenderer ( Boolean .class );
217214 if ( oldBooleanRenderer instanceof UIResource )
@@ -231,25 +228,24 @@ protected void uninstallDefaults() {
231228
232229 oldStyleValues = null ;
233230
234- FlatTablePropertyWatcher watcher = FlatTablePropertyWatcher .get ( table );
235- if ( watcher != null )
236- watcher .enabled = false ;
237-
238231 // restore old show horizontal/vertical lines (if not modified)
239- if ( !showHorizontalLines && oldShowHorizontalLines && !table .getShowHorizontalLines () &&
240- (watcher == null || !watcher .showHorizontalLinesChanged ) )
241- table .setShowHorizontalLines ( true );
242- if ( !showVerticalLines && oldShowVerticalLines && !table .getShowVerticalLines () &&
243- (watcher == null || !watcher .showVerticalLinesChanged ) )
244- table .setShowVerticalLines ( true );
232+ if ( !showHorizontalLines && oldShowHorizontalLines && !table .getShowHorizontalLines () ) {
233+ FlatPropertyWatcher .runIfNotChanged ( table , "showHorizontalLines" , () -> {
234+ table .setShowHorizontalLines ( true );
235+ } );
236+ }
237+ if ( !showVerticalLines && oldShowVerticalLines && !table .getShowVerticalLines () ) {
238+ FlatPropertyWatcher .runIfNotChanged ( table , "showVerticalLines" , () -> {
239+ table .setShowVerticalLines ( true );
240+ } );
241+ }
245242
246243 // restore old intercell spacing (if not modified)
247- if ( intercellSpacing != null && table .getIntercellSpacing ().equals ( intercellSpacing ) &&
248- (watcher == null || !watcher .intercellSpacingChanged ) )
249- table .setIntercellSpacing ( oldIntercellSpacing );
250-
251- if ( watcher != null )
252- watcher .enabled = true ;
244+ if ( intercellSpacing != null && table .getIntercellSpacing ().equals ( intercellSpacing ) ) {
245+ FlatPropertyWatcher .runIfNotChanged ( table , "rowMargin" , () -> {
246+ table .setIntercellSpacing ( oldIntercellSpacing );
247+ } );
248+ }
253249
254250 // uninstall boolean renderer
255251 if ( table .getDefaultRenderer ( Boolean .class ) instanceof FlatBooleanRenderer ) {
@@ -938,48 +934,6 @@ private void repaintAreaBelowTable() {
938934 }
939935 }
940936
941- //---- class FlatTablePropertyWatcher -------------------------------------
942-
943- /**
944- * Listener that watches for change of some table properties from application code.
945- * This information is used in {@link FlatTableUI#installDefaults()} and
946- * {@link FlatTableUI#uninstallDefaults()} to decide whether FlatLaf modifies those properties.
947- * If they are modified in application code, FlatLaf no longer changes them.
948- *
949- * The listener is added once for each table, but never removed.
950- * So switching Laf/theme reuses existing listener.
951- */
952- private static class FlatTablePropertyWatcher
953- implements PropertyChangeListener
954- {
955- boolean enabled = true ;
956- boolean showHorizontalLinesChanged ;
957- boolean showVerticalLinesChanged ;
958- boolean intercellSpacingChanged ;
959-
960- static FlatTablePropertyWatcher get ( JTable table ) {
961- for ( PropertyChangeListener l : table .getPropertyChangeListeners () ) {
962- if ( l instanceof FlatTablePropertyWatcher )
963- return (FlatTablePropertyWatcher ) l ;
964- }
965- return null ;
966- }
967-
968- //---- interface PropertyChangeListener ----
969-
970- @ Override
971- public void propertyChange ( PropertyChangeEvent e ) {
972- if ( !enabled )
973- return ;
974-
975- switch ( e .getPropertyName () ) {
976- case "showHorizontalLines" : showHorizontalLinesChanged = true ; break ;
977- case "showVerticalLines" : showVerticalLinesChanged = true ; break ;
978- case "rowMargin" : intercellSpacingChanged = true ; break ;
979- }
980- }
981- }
982-
983937 //---- class FlatBooleanRenderer ------------------------------------------
984938
985939 private static class FlatBooleanRenderer
0 commit comments