So basically you want to have two completely independent layouts? Then you can tell the CControl to save the layout of the working-areas as well using “setIgnoreWorkingForEntry”. And if you want to add dockables two both layouts then you could use the perspective API.
Perhaps this example app is closer to what you want to achieve? It has two layouts, with different editors - but you can add a “shared” editor if you want. You will need to upgrade the framwork to 1.1.2p10a for this properly work - sorry, I found a little bug…
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CWorkingArea;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.SingleCDockable;
import bibliothek.gui.dock.common.SingleCDockableFactory;
import bibliothek.gui.dock.common.perspective.CControlPerspective;
import bibliothek.gui.dock.common.perspective.CGridPerspective;
import bibliothek.gui.dock.common.perspective.CPerspective;
import bibliothek.gui.dock.common.perspective.CWorkingPerspective;
import bibliothek.gui.dock.common.perspective.SingleCDockablePerspective;
import bibliothek.util.filter.RegexFilter;
public class PerspectiveTest3 {
private static int next = 0;
public static void main( String[] args ) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
CControl control = new CControl( frame );
frame.setBounds( 20, 20, 800, 600 );
frame.add( control.getContentArea() );
// with this menu we can load old layouts
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar( menuBar );
JMenu menu = new JMenu( "Layouts" );
menu.add( load( "a", control ) );
menu.add( load( "b", control ) );
menu.add( share( control ) );
menuBar.add( menu );
// do not change (or even save) the content of working-areas
// control.setIgnoreWorkingForEntry( true );
control.setIgnoreWorkingForEntry( false );
// register some factories that will create missing dockables or stations
control.addSingleDockableFactory( "work", workingAreaFactory( control ) );
control.addSingleDockableFactory( new RegexFilter( ".+" ), dockableFactory( control ) );
frame.add(control.getContentArea());
// at this point we do set up two completely independent perspectives
CControlPerspective perspectives = control.getPerspectives();
CPerspective perspectiveA = perspectives.createEmptyPerspective();
CPerspective perspectiveB = perspectives.createEmptyPerspective();
fillPerspectiveA( perspectiveA );
fillPerspectiveB( perspectiveB );
perspectives.setPerspective( "a", perspectiveA );
perspectives.setPerspective( "b", perspectiveB );
// we apply one perspective to setup the initial layout. The parameter "true" indicates, that we
// want to setup the content of the working-area as well.
perspectives.setPerspective( perspectiveA, true );
frame.setVisible( true );
}
private static void addShared( CControl control ){
String randomId = "shared " + (++next);
// save current perspective
CControlPerspective perspectives = control.getPerspectives();
for( String name : perspectives.getNames() ){
CPerspective perspective = perspectives.getPerspective( name, true );
addSharedToPerspectve( perspective, randomId );
perspectives.setPerspective( name, perspective );
}
CPerspective current = perspectives.getPerspective( true );
addSharedToPerspectve( current, randomId );
perspectives.setPerspective( current, true );
}
private static void addSharedToPerspectve( CPerspective perspective, String randomId ){
CWorkingPerspective working = (CWorkingPerspective)perspective.getStation( "work" );
working.gridAdd( 0, 0, 100, 300, createDockablePerspective( randomId, working ) );
working.gridDeploy();
}
private static void fillPerspectiveA( CPerspective perspective ){
CWorkingPerspective working = new CWorkingPerspective( "work" );
perspective.addStation( working );
working.gridAdd( 0, 0, 1, 1, createDockablePerspective( "text editor 1", working ) );
working.gridAdd( 1, 0, 1, 1, createDockablePerspective( "text editor 2", working ) );
working.gridDeploy();
CGridPerspective center = perspective.getContentArea().getCenter();
center.gridAdd( 1, 0, 1, 2, working );
center.gridAdd( 0, 0, 1, 1, createDockablePerspective( "text 1", null ));
center.gridAdd( 0, 1, 1, 1, createDockablePerspective( "text 2", null ));
center.gridDeploy();
}
private static void fillPerspectiveB( CPerspective perspective ){
CWorkingPerspective working = new CWorkingPerspective( "work" );
perspective.addStation( working );
working.gridAdd( 0, 0, 1, 1, createDockablePerspective( "shape editor 1", working ) );
working.gridAdd( 0, 0, 1, 1, createDockablePerspective( "shape editor 2", working ) );
working.gridDeploy();
CGridPerspective center = perspective.getContentArea().getCenter();
center.gridAdd( 1, 0, 1, 1, working );
center.gridAdd( 0, 0, 1, 1, createDockablePerspective( "shape 1", null ));
center.gridAdd( 0, 0, 1, 1, createDockablePerspective( "shape 2", null ));
center.gridDeploy();
}
private static SingleCDockablePerspective createDockablePerspective( String id, CWorkingPerspective working ){
SingleCDockablePerspective result = new SingleCDockablePerspective( id );
result.setWorkingArea( working );
return result;
}
private static SingleCDockableFactory workingAreaFactory( final CControl control ){
return new SingleCDockableFactory() {
@Override
public SingleCDockable createBackup( String id ) {
CWorkingArea working = new CWorkingArea( control, id );
working.setTitleShown( true );
working.setTitleText( "I am the working area" );
return working;
}
};
}
private static SingleCDockableFactory dockableFactory( CControl control ){
return new SingleCDockableFactory() {
@Override
public SingleCDockable createBackup( String id ) {
DefaultSingleCDockable dockable = new DefaultSingleCDockable( id );
dockable.setTitleText( "Identifier: " + id );
return dockable;
}
};
}
private static JMenuItem load( final String id, final CControl control ){
JMenuItem item = new JMenuItem( "Load " + id );
item.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
// if we want, we can save the current layout
control.save();
control.load( id );
}
} );
return item;
}
private static JMenuItem share( final CControl control ){
JMenuItem item = new JMenuItem( "Add shared dockable" );
item.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
addShared( control );
}
} );
return item;
}
}