If you put something on a CWorkingPerspective, then you need to call “setPerspective( …, true )”, because the boolean parameter tells whether to change working areas or not.
Usually the working area contains the “editors” of your application, the open documents. The user does not want them to disappear or be replaced when changing the layout - hence the default behavior of the framework is to never modify the contents of the working-areas.
Anyway… your example has some more issues. You are calling “setPerspective” multiple times, and each time you override the last layout. It would be better to use the other “setPerspective” method, or only call it once.
Have a look at this little application (be sure to include the tutorial.jar file in your classpath). It shows how you should/could use the two different “setPerspective” methods. I hope this helps a little bit, but feel free to ask more questions.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import tutorial.support.ColorSingleCDockable;
import tutorial.support.JTutorialFrame;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.SingleCDockable;
import bibliothek.gui.dock.common.SingleCDockableFactory;
import bibliothek.gui.dock.common.perspective.CContentPerspective;
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 SelectPerspectives {
public static void main( String[] args ){
/* This application showcases how the perspective API can be used to load perspectives. In fact
* this example shows two cases:
* - using perspectives and the "load" and "save" methods to handle them. In this case the contents of the
* working-area is never affected by the changes. If the user changed the layout, then these changes are
* not lost.
* - using "setPerspective" to override the current layout, including the working-area
*
*/
JTutorialFrame frame = new JTutorialFrame( SelectPerspectives.class );
final CControl control = new CControl( frame );
/* we are going to automatically produce Dockables, with different colors */
control.addSingleDockableFactory( new RegexFilter( "blue.*" ), new ColoredFactory( Color.BLUE ) );
control.addSingleDockableFactory( new RegexFilter( "red.*" ), new ColoredFactory( Color.RED ) );
frame.destroyOnClose( control );
/* initializing the required stations before setting up the perspectives */
frame.add( control.getContentArea() );
control.createWorkingArea( "work" );
/* creating some menus... */
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar( menubar );
JMenu saved = new JMenu( "Saved" );
JMenu apply = new JMenu( "Apply" );
menubar.add( saved );
menubar.add( apply );
/* ... for loading and storing layouts */
saved.add( saveItem( control, new BlueSquareBuilder() ) );
saved.add( saveItem( control, new RedStackBuilder() ) );
/* ... or for overriding layouts */
apply.add( applyItem( control, new BlueSquareBuilder() ) );
apply.add( applyItem( control, new RedStackBuilder() ) );
frame.setVisible( true );
}
private static JMenuItem saveItem( final CControl control, final PerspectiveBuilder builder ){
savePerspective( control.getPerspectives(), builder );
JMenuItem item = new JMenuItem( builder.getName() );
item.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
// TODO this will be replaced with "control.save()" in a future version
if( control.intern().getCurrentSetting() != null ){
control.intern().save();
}
control.load( builder.getName() );
}
} );
return item;
}
private static JMenuItem applyItem( final CControl control, final PerspectiveBuilder builder ){
JMenuItem item = new JMenuItem( builder.getName() );
item.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
applyPerspective( control.getPerspectives(), builder );
}
} );
return item;
}
private static void savePerspective( CControlPerspective perspectives, PerspectiveBuilder builder ){
CPerspective perspective = buildPerspective( perspectives, builder );
/* this method just saves the layout, it can be applied with CControl.load */
perspectives.setPerspective( builder.getName(), perspective );
}
private static void applyPerspective( CControlPerspective perspectives, PerspectiveBuilder builder ){
CPerspective perspective = buildPerspective( perspectives, builder );
/* this method applies the layout right now */
perspectives.setPerspective( perspective, true );
}
/* Generic methods to build up a layout */
private static CPerspective buildPerspective( CControlPerspective perspectives, PerspectiveBuilder builder ){
CPerspective perspective = perspectives.createEmptyPerspective();
CContentPerspective content = perspective.getContentArea();
CWorkingPerspective working = new CWorkingPerspective( "work" );
perspective.addStation( working );
builder.build( perspective, content, working );
return perspective;
}
private static class ColoredFactory implements SingleCDockableFactory{
private Color color;
public ColoredFactory( Color color ){
this.color = color;
}
public SingleCDockable createBackup( String id ){
int white = Integer.valueOf( id.split( " " )[1] );
Color selected = new Color( add( color.getRed(), white ), add( color.getGreen(), white ), add( color.getBlue(), white ));
return new ColorSingleCDockable( id, selected );
}
private int add( int color, int white ){
return Math.min( 255, color + 10 * white );
}
}
private interface PerspectiveBuilder{
public String getName();
public void build(CPerspective perspective, CContentPerspective content, CWorkingPerspective working);
}
private static class BlueSquareBuilder implements PerspectiveBuilder{
public String getName(){
return "Blue square";
}
public void build( CPerspective perspective, CContentPerspective content, CWorkingPerspective working ){
CGridPerspective center = content.getCenter();
center.gridAdd( 0, 0, 1, 1, working );
center.gridAdd( 0, 1, 1, 1, new SingleCDockablePerspective( "blue 0" ) );
center.gridAdd( 1, 0, 1, 1, new SingleCDockablePerspective( "blue 1" ) );
center.gridAdd( 1, 1, 1, 1, new SingleCDockablePerspective( "blue 2" ) );
center.gridDeploy();
working.gridAdd( 0, 0, 1, 1, new SingleCDockablePerspective( "blue 3") );
working.gridAdd( 0, 1, 1, 1, new SingleCDockablePerspective( "blue 4") );
working.gridAdd( 1, 0, 1, 1, new SingleCDockablePerspective( "blue 5") );
working.gridAdd( 1, 1, 1, 1, new SingleCDockablePerspective( "blue 6") );
working.gridDeploy();
}
}
private static class RedStackBuilder implements PerspectiveBuilder{
public String getName(){
return "Red stack";
}
public void build( CPerspective perspective, CContentPerspective content, CWorkingPerspective working ){
CGridPerspective center = content.getCenter();
center.gridAdd( 1, 0, 1, 2, working );
center.gridAdd( 0, 0, 1, 1, new SingleCDockablePerspective( "red 0" ) );
center.gridAdd( 0, 0, 1, 1, new SingleCDockablePerspective( "red 1" ) );
center.gridAdd( 0, 1, 1, 1, new SingleCDockablePerspective( "red 2" ) );
center.gridDeploy();
working.gridAdd( 0, 0, 1, 1, new SingleCDockablePerspective( "red 3") );
working.gridAdd( 0, 0, 1, 1, new SingleCDockablePerspective( "red 4") );
working.gridAdd( 0, 1, 1, 1, new SingleCDockablePerspective( "red 5") );
working.gridAdd( 0, 1, 1, 1, new SingleCDockablePerspective( "red 6") );
working.gridDeploy();
}
}
}