Can the maximize button be disabled when it is not usefull


#1

When a DockStation already occupies all the available space, the maximize button can be used while there is no possible enlargement. In this case, can the maximize button be disabled automatically ?

Thanks in advance


#2

Sorry for the long delay, took me a while to figure out a “simple” solution.

Just copy the code below…

All the buttons (actions) for a Dockable are provided by a DockActionSource. Here we replace the standard source by a “FilteredDockActionSource” that just hides the maximize action in cases where do not want to see it.

[code]
package test;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import bibliothek.gui.DockStation;
import bibliothek.gui.Dockable;
import bibliothek.gui.dock.SplitDockStation;
import bibliothek.gui.dock.action.DefaultActionOffer;
import bibliothek.gui.dock.action.DockAction;
import bibliothek.gui.dock.action.DockActionSource;
import bibliothek.gui.dock.action.FilteredDockActionSource;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.action.core.CommonSimpleButtonAction;
import bibliothek.gui.dock.common.action.predefined.CMaximizeAction;
import bibliothek.gui.dock.event.DockActionSourceListener;
import bibliothek.gui.dock.event.DockStationListener;
import bibliothek.gui.dock.util.DockUtilities;

public class NoMaximizeButtonTest {
public static void main( String[] args ) {
JFrame frame = new JFrame( “Test” );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    CControl control = new CControl( frame );
    frame.add( control.getContentArea() );
    SplitDockStation center = control.getContentArea().getCenter();

    // The "filteredOffer" is responsible for hiding actions
    MaximizeButtonFilteredActionOffer filteredOffer = new MaximizeButtonFilteredActionOffer( center );
    control.getController().addActionOffer( filteredOffer );

    CGrid grid = new CGrid( control );
    grid.add( 0, 0, 1, 1, dockable( "Aaaa" ) );
    grid.add( 0, 1, 1, 1, dockable( "Bbbb" ) );
    grid.add( 1, 0, 1, 1, dockable( "Cccc" ) );
    control.getContentArea().deploy( grid );

    // update visibility of maximize button whenever a dockable is added or removed from the center SplitDockStation
    center.addDockStationListener( new DockStationListener() {
        @Override
        public void dockablesRepositioned( DockStation station, Dockable[] dockables ) {
            // ignore
        }

        @Override
        public void dockableShowingChanged( DockStation station, Dockable dockable, boolean showing ) {
            // ignore
        }

        @Override
        public void dockableSelected( DockStation station, Dockable oldSelection, Dockable newSelection ) {
            // ignore
        }

        @Override
        public void dockableRemoving( DockStation station, Dockable dockable ) {
            // ignore
        }

        @Override
        public void dockableRemoved( DockStation station, Dockable dockable ) {
            checkActions( filteredOffer );
        }

        @Override
        public void dockableAdding( DockStation station, Dockable dockable ) {
            // ignore
        }

        @Override
        public void dockableAdded( DockStation station, Dockable dockable ) {
            checkActions( filteredOffer );
        }
    } );

    frame.setBounds( 50, 50, 1000, 800 );
    frame.setVisible( true );
}

private static void checkActions( MaximizeButtonFilteredActionOffer filteredOffer ) {
    // delay update, make sure we do not modify too much of the UI during one event
    EventQueue.invokeLater( () -> {
        filteredOffer.refresh();
    } );
}

private static DefaultSingleCDockable dockable( String title ) {
    return new DefaultSingleCDockable( title, title );
}

public static class MaximizeButtonFilteredActionOffer extends DefaultActionOffer {
    private List<MaximizeButtonFilteredSource> activeSources = new ArrayList<>();
    private SplitDockStation center;

    public MaximizeButtonFilteredActionOffer( SplitDockStation center ) {
        this.center = center;
    }

    @Override
    public boolean interested( Dockable dockable ) {
        // only hide maximize buttons on children of the center station. "Externalized" dockables should keep the button.
        return DockUtilities.isAncestor( center, dockable );
    }

    @Override
    public DockActionSource getSource( Dockable dockable, DockActionSource source, DockActionSource[] guards, DockActionSource parent, DockActionSource[] parents ) {
        DockActionSource original = super.getSource( dockable, source, guards, parent, parents );
        return new MaximizeButtonFilteredSource( this, original );
    }

    public boolean isFiltered() {
        return center.getDockableCount() <= 1;
    }

    public void refresh() {
        for( MaximizeButtonFilteredSource source : activeSources ) {
            source.refresh();
        }
    }

    public void add( MaximizeButtonFilteredSource source ) {
        activeSources.add( source );
    }

    public void remove( MaximizeButtonFilteredSource source ) {
        activeSources.remove( source );
    }
}

public static class MaximizeButtonFilteredSource extends FilteredDockActionSource {
    private MaximizeButtonFilteredActionOffer owner;

    public MaximizeButtonFilteredSource( MaximizeButtonFilteredActionOffer owner, DockActionSource source ) {
        super( source );
        this.owner = owner;
    }

    @Override
    public void addDockActionSourceListener( DockActionSourceListener listener ) {
        if( !hasListeners() ) {
            owner.add( this );
        }
        super.addDockActionSourceListener( listener );
    }

    @Override
    public void removeDockActionSourceListener( DockActionSourceListener listener ) {
        super.removeDockActionSourceListener( listener );
        if( !hasListeners() ) {
            owner.remove( this );
        }
    }

    @Override
    protected boolean include( DockAction action ) {
        if( owner.isFiltered() ) {
            if( action instanceof CommonSimpleButtonAction ) {
                if( ((CommonSimpleButtonAction) action).getAction() instanceof CMaximizeAction ) {
                    return false;
                }
            }
        }
        return true;
    }
}

}[/code]


#3

Cool. It works fine.

Thanks a lot Beni !!! :clap: