Skip to content

Commit ea2a985

Browse files
committed
ToolBar: do not change floatable if it was changed from application code (issue #1075)
1 parent 7680c3a commit ea2a985

File tree

4 files changed

+87
-82
lines changed

4 files changed

+87
-82
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ FlatLaf Change Log
77
#1068)
88
- Popup: Fixed scrolling popup painting issue on Windows 10 when a glass pane is
99
visible and frame is maximized. (issue #1071)
10+
- ToolBar: Grip disappeared when switching between Look and Feels. (issue #1075)
1011

1112

1213
## 3.7

flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java

Lines changed: 31 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.awt.event.FocusEvent;
3535
import java.awt.event.FocusListener;
3636
import java.awt.geom.Rectangle2D;
37-
import java.beans.PropertyChangeEvent;
3837
import java.beans.PropertyChangeListener;
3938
import java.util.Map;
4039
import javax.swing.Action;
@@ -66,6 +65,7 @@
6665
import com.formdev.flatlaf.icons.FlatCheckBoxIcon;
6766
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
6867
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI;
68+
import com.formdev.flatlaf.ui.FlatUIUtils.FlatPropertyWatcher;
6969
import com.formdev.flatlaf.util.Graphics2DProxy;
7070
import com.formdev.flatlaf.util.HiDPIUtils;
7171
import 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

flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolBarUI.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.formdev.flatlaf.FlatClientProperties;
4848
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
4949
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI;
50+
import com.formdev.flatlaf.ui.FlatUIUtils.FlatPropertyWatcher;
5051
import com.formdev.flatlaf.util.HiDPIUtils;
5152
import com.formdev.flatlaf.util.LoggingFacade;
5253
import com.formdev.flatlaf.util.UIScale;
@@ -144,11 +145,13 @@ protected void installDefaults() {
144145
hoverButtonGroupBackground = UIManager.getColor( "ToolBar.hoverButtonGroupBackground" );
145146

146147
// floatable
148+
oldFloatable = null;
147149
if( !UIManager.getBoolean( "ToolBar.floatable" ) ) {
148-
oldFloatable = toolBar.isFloatable();
149-
toolBar.setFloatable( false );
150-
} else
151-
oldFloatable = null;
150+
FlatPropertyWatcher.runIfNotChanged( toolBar, "floatable", () -> {
151+
oldFloatable = toolBar.isFloatable();
152+
toolBar.setFloatable( false );
153+
} );
154+
}
152155
}
153156

154157
@Override
@@ -158,7 +161,9 @@ protected void uninstallDefaults() {
158161
hoverButtonGroupBackground = null;
159162

160163
if( oldFloatable != null ) {
161-
toolBar.setFloatable( oldFloatable );
164+
FlatPropertyWatcher.runIfNotChanged( toolBar, "floatable", () -> {
165+
toolBar.setFloatable( oldFloatable );
166+
} );
162167
oldFloatable = null;
163168
}
164169
}

flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import java.awt.geom.Point2D;
4545
import java.awt.geom.Rectangle2D;
4646
import java.awt.geom.RoundRectangle2D;
47+
import java.beans.PropertyChangeEvent;
48+
import java.beans.PropertyChangeListener;
4749
import java.util.IdentityHashMap;
4850
import java.util.WeakHashMap;
4951
import java.util.function.Predicate;
@@ -1413,4 +1415,47 @@ public boolean isBorderOpaque() {
14131415
return delegate.isBorderOpaque();
14141416
}
14151417
}
1418+
1419+
//---- class FlatPropertyWatcher ------------------------------------------
1420+
1421+
/**
1422+
* Listener that watches for change of a property from application code.
1423+
* This information can be used to decide whether FlatLaf modifies the property.
1424+
* If it is modified in application code, FlatLaf no longer changes it.
1425+
* <p>
1426+
* The listener is added once for a property, but never removed.
1427+
* So switching Laf/theme reuses existing listener.
1428+
*/
1429+
static class FlatPropertyWatcher
1430+
implements PropertyChangeListener
1431+
{
1432+
private boolean changed;
1433+
1434+
static void runIfNotChanged( JComponent c, String propName, Runnable runnable ) {
1435+
FlatPropertyWatcher watcher = getOrInstall( c, propName );
1436+
if( watcher.changed )
1437+
return;
1438+
1439+
runnable.run();
1440+
watcher.changed = false;
1441+
}
1442+
1443+
private static FlatPropertyWatcher getOrInstall( JComponent c, String propName ) {
1444+
for( PropertyChangeListener l : c.getPropertyChangeListeners( propName ) ) {
1445+
if( l instanceof FlatPropertyWatcher )
1446+
return (FlatPropertyWatcher) l;
1447+
}
1448+
1449+
FlatPropertyWatcher watcher = new FlatPropertyWatcher();
1450+
c.addPropertyChangeListener( propName, watcher );
1451+
return watcher;
1452+
}
1453+
1454+
//---- interface PropertyChangeListener ----
1455+
1456+
@Override
1457+
public void propertyChange( PropertyChangeEvent e ) {
1458+
changed = true;
1459+
}
1460+
}
14161461
}

0 commit comments

Comments
 (0)