mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27:13 -06:00
DesktopPane: automatically layout icons in dock (without invoking from DesktopManager), which eliminates the need for FlatDesktopManager
This commit is contained in:
@@ -16,20 +16,16 @@
|
|||||||
|
|
||||||
package com.formdev.flatlaf.ui;
|
package com.formdev.flatlaf.ui;
|
||||||
|
|
||||||
|
import java.awt.Component;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
|
import java.awt.EventQueue;
|
||||||
import java.awt.event.ComponentAdapter;
|
import java.awt.event.ComponentAdapter;
|
||||||
import java.awt.event.ComponentEvent;
|
import java.awt.event.ComponentEvent;
|
||||||
import java.awt.event.ComponentListener;
|
import java.awt.event.ContainerEvent;
|
||||||
import java.util.ArrayList;
|
import java.awt.event.ContainerListener;
|
||||||
import java.util.List;
|
|
||||||
import javax.swing.DefaultDesktopManager;
|
|
||||||
import javax.swing.DesktopManager;
|
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
import javax.swing.JInternalFrame;
|
|
||||||
import javax.swing.JInternalFrame.JDesktopIcon;
|
import javax.swing.JInternalFrame.JDesktopIcon;
|
||||||
import javax.swing.plaf.ComponentUI;
|
import javax.swing.plaf.ComponentUI;
|
||||||
import javax.swing.plaf.UIResource;
|
|
||||||
import javax.swing.plaf.basic.BasicDesktopIconUI;
|
|
||||||
import javax.swing.plaf.basic.BasicDesktopPaneUI;
|
import javax.swing.plaf.basic.BasicDesktopPaneUI;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,79 +41,61 @@ import javax.swing.plaf.basic.BasicDesktopPaneUI;
|
|||||||
public class FlatDesktopPaneUI
|
public class FlatDesktopPaneUI
|
||||||
extends BasicDesktopPaneUI
|
extends BasicDesktopPaneUI
|
||||||
{
|
{
|
||||||
private ComponentListener componentListener;
|
private LayoutDockListener layoutDockListener;
|
||||||
|
private boolean layoutDockPending;
|
||||||
// list of iconified internal frames, which define the order of icons in dock
|
|
||||||
protected List<JInternalFrame> iconifiedFrames;
|
|
||||||
|
|
||||||
public static ComponentUI createUI( JComponent c ) {
|
public static ComponentUI createUI( JComponent c ) {
|
||||||
return new FlatDesktopPaneUI();
|
return new FlatDesktopPaneUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void uninstallUI( JComponent c ) {
|
public void installUI( JComponent c ) {
|
||||||
super.uninstallUI( c );
|
super.installUI( c );
|
||||||
|
|
||||||
iconifiedFrames = null;
|
layoutDockLaterOnce();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void installListeners() {
|
protected void installListeners() {
|
||||||
super.installListeners();
|
super.installListeners();
|
||||||
|
|
||||||
componentListener = new ComponentAdapter() {
|
layoutDockListener = new LayoutDockListener();
|
||||||
@Override
|
desktop.addContainerListener( layoutDockListener );
|
||||||
public void componentResized( ComponentEvent e ) {
|
desktop.addComponentListener( layoutDockListener );
|
||||||
layoutDock();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
desktop.addComponentListener( componentListener );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void uninstallListeners() {
|
protected void uninstallListeners() {
|
||||||
super.uninstallListeners();
|
super.uninstallListeners();
|
||||||
|
|
||||||
desktop.removeComponentListener( componentListener );
|
desktop.removeContainerListener( layoutDockListener );
|
||||||
componentListener = null;
|
desktop.removeComponentListener( layoutDockListener );
|
||||||
|
layoutDockListener = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void layoutDockLaterOnce() {
|
||||||
protected void installDesktopManager() {
|
if( layoutDockPending )
|
||||||
// Check current installed desktop manager to avoid recursive call
|
|
||||||
// with property change event (will fire a stack overflow).
|
|
||||||
// Do not handle install if already installed.
|
|
||||||
DesktopManager dm = desktop.getDesktopManager();
|
|
||||||
if( dm instanceof FlatDesktopManager || dm instanceof FlatWrapperDesktopManager )
|
|
||||||
return;
|
return;
|
||||||
|
layoutDockPending = true;
|
||||||
|
|
||||||
desktopManager = (dm != null)
|
EventQueue.invokeLater( () -> {
|
||||||
? new FlatWrapperDesktopManager( dm )
|
layoutDockPending = false;
|
||||||
: new FlatDesktopManager();
|
if( desktop != null )
|
||||||
desktop.setDesktopManager( desktopManager );
|
layoutDock();
|
||||||
}
|
} );
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void uninstallDesktopManager() {
|
|
||||||
// uninstall wrapper
|
|
||||||
DesktopManager dm = desktop.getDesktopManager();
|
|
||||||
if( dm instanceof FlatWrapperDesktopManager )
|
|
||||||
desktop.setDesktopManager( ((FlatWrapperDesktopManager)dm).parent );
|
|
||||||
|
|
||||||
super.uninstallDesktopManager();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void layoutDock() {
|
protected void layoutDock() {
|
||||||
if( iconifiedFrames == null || iconifiedFrames.isEmpty() )
|
|
||||||
return;
|
|
||||||
|
|
||||||
Dimension desktopSize = desktop.getSize();
|
Dimension desktopSize = desktop.getSize();
|
||||||
int x = 0;
|
int x = 0;
|
||||||
int y = desktopSize.height;
|
int y = desktopSize.height;
|
||||||
int rowHeight = 0;
|
int rowHeight = 0;
|
||||||
|
|
||||||
for( JInternalFrame f : iconifiedFrames ) {
|
for( Component c : desktop.getComponents() ) {
|
||||||
JDesktopIcon icon = f.getDesktopIcon();
|
if( !(c instanceof JDesktopIcon) )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
JDesktopIcon icon = (JDesktopIcon) c;
|
||||||
Dimension iconSize = icon.getPreferredSize();
|
Dimension iconSize = icon.getPreferredSize();
|
||||||
|
|
||||||
if( x + iconSize.width > desktopSize.width ) {
|
if( x + iconSize.width > desktopSize.width ) {
|
||||||
@@ -134,150 +112,25 @@ public class FlatDesktopPaneUI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addToDock( JInternalFrame f ) {
|
//---- class LayoutDockListener -------------------------------------------
|
||||||
if( iconifiedFrames == null )
|
|
||||||
iconifiedFrames = new ArrayList<>();
|
|
||||||
|
|
||||||
if( !iconifiedFrames.contains( f ) )
|
private class LayoutDockListener
|
||||||
iconifiedFrames.add( f );
|
extends ComponentAdapter
|
||||||
layoutDock();
|
implements ContainerListener
|
||||||
}
|
|
||||||
|
|
||||||
protected void removeFromDock( JInternalFrame f ) {
|
|
||||||
if( iconifiedFrames == null )
|
|
||||||
return;
|
|
||||||
|
|
||||||
iconifiedFrames.remove( f );
|
|
||||||
layoutDock();
|
|
||||||
}
|
|
||||||
|
|
||||||
//---- class FlatDesktopManager -------------------------------------------
|
|
||||||
|
|
||||||
private class FlatDesktopManager
|
|
||||||
extends DefaultDesktopManager
|
|
||||||
implements UIResource
|
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void iconifyFrame( JInternalFrame f ) {
|
public void componentAdded( ContainerEvent e ) {
|
||||||
super.iconifyFrame( f );
|
layoutDockLaterOnce();
|
||||||
addToDock( f );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deiconifyFrame( JInternalFrame f ) {
|
public void componentRemoved( ContainerEvent e ) {
|
||||||
super.deiconifyFrame( f );
|
layoutDockLaterOnce();
|
||||||
removeFromDock( f );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void closeFrame( JInternalFrame f ) {
|
public void componentResized( ComponentEvent e ) {
|
||||||
super.closeFrame( f );
|
layoutDockLaterOnce();
|
||||||
removeFromDock( f );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//---- class FlatWrapperDesktopManager ------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For already installed desktop manager to use the flat desktop manager features.
|
|
||||||
* <p>
|
|
||||||
* Although this class extends {@link DefaultDesktopManager}, it does not invoke
|
|
||||||
* any method of the superclass.
|
|
||||||
* All methods are delegated to parent desktop manager.
|
|
||||||
* <p>
|
|
||||||
* This class extends {@link DefaultDesktopManager}
|
|
||||||
* (instead of implementing {@link DesktopManager}),
|
|
||||||
* because otherwise {@link DesktopManager#iconifyFrame(JInternalFrame)}
|
|
||||||
* and {@link DesktopManager#deiconifyFrame(JInternalFrame)} are not invoked
|
|
||||||
* from {@link BasicDesktopIconUI#installUI(JComponent)}
|
|
||||||
* and {@link BasicDesktopIconUI#uninstallUI(JComponent)}
|
|
||||||
* when switching Laf.
|
|
||||||
*/
|
|
||||||
private class FlatWrapperDesktopManager
|
|
||||||
extends DefaultDesktopManager
|
|
||||||
{
|
|
||||||
private final DesktopManager parent;
|
|
||||||
|
|
||||||
private FlatWrapperDesktopManager( DesktopManager parent ) {
|
|
||||||
this.parent = parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void openFrame( JInternalFrame f ) {
|
|
||||||
parent.openFrame( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void closeFrame( JInternalFrame f ) {
|
|
||||||
parent.closeFrame( f );
|
|
||||||
removeFromDock( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void maximizeFrame( JInternalFrame f ) {
|
|
||||||
parent.maximizeFrame( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void minimizeFrame( JInternalFrame f ) {
|
|
||||||
parent.minimizeFrame( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void activateFrame( JInternalFrame f ) {
|
|
||||||
parent.activateFrame( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void deactivateFrame( JInternalFrame f ) {
|
|
||||||
parent.deactivateFrame( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void iconifyFrame( JInternalFrame f ) {
|
|
||||||
parent.iconifyFrame( f );
|
|
||||||
addToDock( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void deiconifyFrame( JInternalFrame f ) {
|
|
||||||
parent.deiconifyFrame( f );
|
|
||||||
removeFromDock( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void beginDraggingFrame( JComponent f ) {
|
|
||||||
parent.beginDraggingFrame( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void dragFrame( JComponent f, int newX, int newY ) {
|
|
||||||
parent.dragFrame( f, newX, newY );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void endDraggingFrame( JComponent f ) {
|
|
||||||
parent.endDraggingFrame( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void beginResizingFrame( JComponent f, int direction ) {
|
|
||||||
parent.beginResizingFrame( f, direction );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void resizeFrame( JComponent f, int newX, int newY, int newWidth, int newHeight ) {
|
|
||||||
parent.resizeFrame( f, newX, newY, newWidth, newHeight );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void endResizingFrame( JComponent f ) {
|
|
||||||
parent.endResizingFrame( f );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setBoundsForFrame( JComponent f, int newX, int newY, int newWidth, int newHeight ) {
|
|
||||||
parent.setBoundsForFrame( f, newX, newY, newWidth, newHeight );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user