DesktopPane: avoid using two instances of DefaultDesktopManager if a custom desktop manager is used/wrapped (see PR #294)

This commit is contained in:
Karl Tauber
2021-04-09 18:17:15 +02:00
parent 6db39d1860
commit ab1ce7fab1
4 changed files with 132 additions and 40 deletions

View File

@@ -7,6 +7,11 @@ FlatLaf Change Log
- Fixed crash when running in Webswing. (issue #290) - Fixed crash when running in Webswing. (issue #290)
#### Fixed bugs
- DesktopPane: Fixed empty minimized icon when using a custom desktop manager.
(PR #294)
## 1.1.1 ## 1.1.1

View File

@@ -16,7 +16,10 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import javax.swing.*; import javax.swing.DefaultDesktopManager;
import javax.swing.DesktopManager;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource; import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicDesktopPaneUI; import javax.swing.plaf.basic.BasicDesktopPaneUI;
@@ -40,26 +43,35 @@ public class FlatDesktopPaneUI
@Override @Override
protected void installDesktopManager() { protected void installDesktopManager() {
// Check current installed desktop manager // Check current installed desktop manager to avoid recursive call
// to avoid recursive call with property change event (will fire a stack overflow) // with property change event (will fire a stack overflow).
// Do not handle install if already installed // Do not handle install if already installed.
DesktopManager old = desktop.getDesktopManager(); DesktopManager dm = desktop.getDesktopManager();
if (old instanceof FlatDesktopManager) return; if( dm instanceof FlatDesktopManager || dm instanceof FlatWrapperDesktopManager )
return;
if( old == null ) { desktopManager = (dm != null)
// default version ? new FlatWrapperDesktopManager( dm )
desktopManager = new FlatDesktopManager(); : new FlatDesktopManager();
desktop.setDesktopManager( desktopManager );
}
} else { @Override
// create the wrapper version of the desktop manager protected void uninstallDesktopManager() {
desktopManager = new FlatWrapperDesktopManager(old); // uninstall wrapper
DesktopManager dm = desktop.getDesktopManager();
if( dm instanceof FlatWrapperDesktopManager )
desktop.setDesktopManager( ((FlatWrapperDesktopManager)dm).parent );
} super.uninstallDesktopManager();
desktop.setDesktopManager(desktopManager); }
private static void updateDockIcon( JInternalFrame f ) {
((FlatDesktopIconUI)f.getDesktopIcon().getUI()).updateDockIcon();
} }
//---- class FlatDesktopManager ------------------------------------------- //---- class FlatDesktopManager -------------------------------------------
private class FlatDesktopManager private class FlatDesktopManager
extends DefaultDesktopManager extends DefaultDesktopManager
implements UIResource implements UIResource
@@ -68,96 +80,98 @@ public class FlatDesktopPaneUI
public void iconifyFrame( JInternalFrame f ) { public void iconifyFrame( JInternalFrame f ) {
super.iconifyFrame( f ); super.iconifyFrame( f );
((FlatDesktopIconUI)f.getDesktopIcon().getUI()).updateDockIcon(); updateDockIcon( f );
} }
} }
//---- class for already installed desktop manager to use the flat desktop manager features //---- class FlatWrapperDesktopManager ------------------------------------
/**
* For already installed desktop manager to use the flat desktop manager features.
*/
private class FlatWrapperDesktopManager private class FlatWrapperDesktopManager
extends FlatDesktopManager implements DesktopManager
{ {
private DesktopManager parent = null; private final DesktopManager parent;
private FlatWrapperDesktopManager(DesktopManager parent) { private FlatWrapperDesktopManager( DesktopManager parent ) {
super();
this.parent = parent; this.parent = parent;
} }
@Override @Override
public void openFrame(JInternalFrame f) { public void openFrame( JInternalFrame f ) {
parent.openFrame( f ); parent.openFrame( f );
} }
@Override @Override
public void closeFrame(JInternalFrame f) { public void closeFrame( JInternalFrame f ) {
parent.closeFrame( f ); parent.closeFrame( f );
} }
@Override @Override
public void maximizeFrame(JInternalFrame f) { public void maximizeFrame( JInternalFrame f ) {
parent.maximizeFrame( f ); parent.maximizeFrame( f );
} }
@Override @Override
public void minimizeFrame(JInternalFrame f) { public void minimizeFrame( JInternalFrame f ) {
parent.minimizeFrame( f ); parent.minimizeFrame( f );
} }
@Override @Override
public void activateFrame(JInternalFrame f) { public void activateFrame( JInternalFrame f ) {
parent.activateFrame( f ); parent.activateFrame( f );
} }
@Override @Override
public void deactivateFrame(JInternalFrame f) { public void deactivateFrame( JInternalFrame f ) {
parent.deactivateFrame( f ); parent.deactivateFrame( f );
} }
@Override @Override
public void iconifyFrame( JInternalFrame f ) { public void iconifyFrame( JInternalFrame f ) {
super.iconifyFrame( f ); parent.iconifyFrame( f );
updateDockIcon( f );
} }
@Override @Override
public void deiconifyFrame( JInternalFrame f ) { public void deiconifyFrame( JInternalFrame f ) {
super.deiconifyFrame( f ); parent.deiconifyFrame( f );
} }
@Override @Override
public void beginDraggingFrame(JComponent f) { public void beginDraggingFrame( JComponent f ) {
parent.beginDraggingFrame( f ); parent.beginDraggingFrame( f );
} }
@Override @Override
public void dragFrame(JComponent f, int newX, int newY) { public void dragFrame( JComponent f, int newX, int newY ) {
parent.dragFrame( f, newX, newY ); parent.dragFrame( f, newX, newY );
} }
@Override @Override
public void endDraggingFrame(JComponent f) { public void endDraggingFrame( JComponent f ) {
parent.endDraggingFrame( f ); parent.endDraggingFrame( f );
} }
@Override @Override
public void beginResizingFrame(JComponent f, int direction) { public void beginResizingFrame( JComponent f, int direction ) {
parent.beginResizingFrame( f, direction ); parent.beginResizingFrame( f, direction );
} }
@Override @Override
public void resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) { public void resizeFrame( JComponent f, int newX, int newY, int newWidth, int newHeight ) {
parent.resizeFrame( f, newX, newY, newWidth, newHeight ); parent.resizeFrame( f, newX, newY, newWidth, newHeight );
} }
@Override @Override
public void endResizingFrame(JComponent f) { public void endResizingFrame( JComponent f ) {
parent.endResizingFrame( f ); parent.endResizingFrame( f );
} }
@Override @Override
public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) { public void setBoundsForFrame( JComponent f, int newX, int newY, int newWidth, int newHeight ) {
parent.setBoundsForFrame( f, newX, newY, newWidth, newHeight ); parent.setBoundsForFrame( f, newX, newY, newWidth, newHeight );
} }
} }

View File

@@ -107,6 +107,12 @@ public class FlatInternalFrameTest
frameCount++; frameCount++;
} }
private void customDesktopManagerChanged() {
desktopPane.setDesktopManager( customDesktopManagerCheckBox.isSelected()
? new CustomDesktopManager()
: null );
}
private void initComponents() { private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
desktopPane = new JDesktopPane(); desktopPane = new JDesktopPane();
@@ -120,6 +126,8 @@ public class FlatInternalFrameTest
titleLabel = new JLabel(); titleLabel = new JLabel();
titleField = new JTextField(); titleField = new JTextField();
createFrameButton = new JButton(); createFrameButton = new JButton();
panel1 = new JPanel();
customDesktopManagerCheckBox = new JCheckBox();
//======== this ======== //======== this ========
setLayout(new MigLayout( setLayout(new MigLayout(
@@ -127,7 +135,8 @@ public class FlatInternalFrameTest
// columns // columns
"[grow,fill]", "[grow,fill]",
// rows // rows
"[grow,fill]")); "[grow,fill]" +
"[]"));
//======== desktopPane ======== //======== desktopPane ========
{ {
@@ -194,6 +203,22 @@ public class FlatInternalFrameTest
palette.setBounds(15, 25, 275, 185); palette.setBounds(15, 25, 275, 185);
} }
add(desktopPane, "cell 0 0,width 600,height 600"); add(desktopPane, "cell 0 0,width 600,height 600");
//======== panel1 ========
{
panel1.setLayout(new MigLayout(
"insets 0,hidemode 3",
// columns
"[fill]",
// rows
"[]"));
//---- customDesktopManagerCheckBox ----
customDesktopManagerCheckBox.setText("custom desktop manager");
customDesktopManagerCheckBox.addActionListener(e -> customDesktopManagerChanged());
panel1.add(customDesktopManagerCheckBox, "cell 0 0");
}
add(panel1, "cell 0 1");
// JFormDesigner - End of component initialization //GEN-END:initComponents // JFormDesigner - End of component initialization //GEN-END:initComponents
if( UIScale.getUserScaleFactor() > 1 ) if( UIScale.getUserScaleFactor() > 1 )
@@ -212,5 +237,37 @@ public class FlatInternalFrameTest
private JLabel titleLabel; private JLabel titleLabel;
private JTextField titleField; private JTextField titleField;
private JButton createFrameButton; private JButton createFrameButton;
private JPanel panel1;
private JCheckBox customDesktopManagerCheckBox;
// JFormDesigner - End of variables declaration //GEN-END:variables // JFormDesigner - End of variables declaration //GEN-END:variables
//---- class CustomDesktopManager -----------------------------------------
private static class CustomDesktopManager
extends DefaultDesktopManager
{
@Override
public void activateFrame( JInternalFrame f ) {
System.out.println( "activateFrame: " + f.getTitle() );
super.activateFrame( f );
}
@Override
public void deactivateFrame( JInternalFrame f ) {
System.out.println( "deactivateFrame: " + f.getTitle() );
super.deactivateFrame( f );
}
@Override
public void iconifyFrame( JInternalFrame f ) {
System.out.println( "iconifyFrame: " + f.getTitle() );
super.iconifyFrame( f );
}
@Override
public void deiconifyFrame( JInternalFrame f ) {
System.out.println( "deiconifyFrame: " + f.getTitle() );
super.deiconifyFrame( f );
}
}
} }

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.2.0.298" Java: "14" encoding: "UTF-8" JFDML JFormDesigner: "7.0.3.1.342" Java: "16" encoding: "UTF-8"
new FormModel { new FormModel {
contentType: "form/swing" contentType: "form/swing"
@@ -6,7 +6,7 @@ new FormModel {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "insets dialog,hidemode 3" "$layoutConstraints": "insets dialog,hidemode 3"
"$columnConstraints": "[grow,fill]" "$columnConstraints": "[grow,fill]"
"$rowConstraints": "[grow,fill]" "$rowConstraints": "[grow,fill][]"
} ) { } ) {
name: "this" name: "this"
add( new FormContainer( "javax.swing.JDesktopPane", new FormLayoutManager( class javax.swing.JDesktopPane ) ) { add( new FormContainer( "javax.swing.JDesktopPane", new FormLayoutManager( class javax.swing.JDesktopPane ) ) {
@@ -90,6 +90,22 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0,width 600,height 600" "value": "cell 0 0,width 600,height 600"
} ) } )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "insets 0,hidemode 3"
"$columnConstraints": "[fill]"
"$rowConstraints": "[]"
} ) {
name: "panel1"
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "customDesktopManagerCheckBox"
"text": "custom desktop manager"
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "customDesktopManagerChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1"
} )
}, new FormLayoutConstraints( null ) { }, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 ) "location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 500, 515 ) "size": new java.awt.Dimension( 500, 515 )