Try out this example. It is just another CGroupBehavior
, you can add it to the list of behaviors you already return in getBehaviorForTarget
.
The most important thing in this code, is the for loop in th e apply
method. It moves one Dockable after each other back to the center area.
package test;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import bibliothek.gui.DockStation;
import bibliothek.gui.Dockable;
import bibliothek.gui.dock.StackDockStation;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.group.CGroupBehavior;
import bibliothek.gui.dock.common.group.CGroupBehaviorCallback;
import bibliothek.gui.dock.common.group.CGroupMovement;
import bibliothek.gui.dock.common.group.SingleGroupMovement;
import bibliothek.gui.dock.common.mode.ExtendedMode;
import bibliothek.gui.dock.facile.mode.LocationMode;
import bibliothek.gui.dock.facile.mode.LocationModeManager;
public class SplitGroupBehaviorTest {
public static void main( String[] args ) {
JFrame frame = new JFrame( "Test" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
CControl control = new CControl( frame );
// custom behavior for moving around dockables
control.setGroupBehavior( new SplittingGroupBehavior() );
frame.add( control.getContentArea() );
CGrid grid = new CGrid( control );
grid.add( 0, 0, 1, 1, new DefaultSingleCDockable( "a", "Aaaa" ) );
grid.add( 0, 1, 1, 1, new DefaultSingleCDockable( "b", "Bbbb" ) );
grid.add( 1, 0, 1, 1, new DefaultSingleCDockable( "c", "Cccc" ) );
grid.add( 1, 1, 1, 1, new DefaultSingleCDockable( "d", "Dddd" ) );
control.getContentArea().deploy( grid );
frame.setBounds( 50, 50, 1000, 800 );
frame.setVisible( true );
}
public static class SplittingGroupBehavior implements CGroupBehavior {
@Override
public CGroupMovement prepare( LocationModeManager<? extends LocationMode> manager, Dockable dockable, ExtendedMode target ) {
if( isStackedAndExternalized( manager, dockable ) && target == ExtendedMode.NORMALIZED ) {
// for this test, we are just interested in the action of moving stacked floating dockables back to the
// normalized mode.
return new SplittingGroupMovement( dockable.getDockParent() );
} else {
return new SingleGroupMovement( dockable, target );
}
}
private boolean isStackedAndExternalized( LocationModeManager<? extends LocationMode> manager, Dockable dockable ) {
return dockable.getDockParent() instanceof StackDockStation &&
manager.getCurrentMode( dockable ).getExtendedMode() == ExtendedMode.EXTERNALIZED;
}
@Override
public Dockable getGroupElement( LocationModeManager<? extends LocationMode> manager, Dockable dockable, ExtendedMode mode ) {
return dockable;
}
@Override
public Dockable getReplaceElement( LocationModeManager<? extends LocationMode> manager, Dockable old, Dockable dockable, ExtendedMode mode ) {
if( dockable == old )
return null;
return old;
}
@Override
public boolean shouldForwardActions( LocationModeManager<? extends LocationMode> manager, DockStation station, Dockable dockable, ExtendedMode mode ) {
return false;
}
}
private static class SplittingGroupMovement implements CGroupMovement {
private final DockStation station;
public SplittingGroupMovement( DockStation station ) {
this.station = station;
}
@Override
public void apply( CGroupBehaviorCallback callback ) {
// just iterate through all the dockables. It's like clicking on the "normalize" button mutiple times.
for( Dockable dockable : dockablesToMove() ) {
callback.setMode( dockable, ExtendedMode.NORMALIZED );
}
}
private List<Dockable> dockablesToMove() {
List<Dockable> result = new ArrayList<>();
for( int i = 0, n = station.getDockableCount(); i < n; i++ ) {
result.add( station.getDockable( i ) );
}
return result;
}
@Override
public boolean forceAccept( DockStation parent, Dockable child ) {
return true;
}
}
}