bibliothek.gui.dock.common.perspective.CWorkingPerspective Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docking-frames-common Show documentation
Show all versions of docking-frames-common Show documentation
DockingFrames is an open source Java Swing docking framework, licenced under LGPL 2.1.
This is the same distribution as the original distribution (http://www.docking-frames.org/), only reinstalled in maven
/*
* Bibliothek - DockingFrames
* Library built on Java/Swing, allows the user to "drag and drop"
* panels containing any Swing-Component the developer likes to add.
*
* Copyright (C) 2010 Benjamin Sigg
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Benjamin Sigg
* [email protected]
* CH - Switzerland
*/
package bibliothek.gui.dock.common.perspective;
import bibliothek.gui.dock.common.CWorkingArea;
import bibliothek.gui.dock.common.MultipleCDockable;
import bibliothek.gui.dock.common.SingleCDockable;
import bibliothek.gui.dock.perspective.PerspectiveDockable;
import bibliothek.gui.dock.perspective.PerspectiveStation;
import bibliothek.gui.dock.station.split.SplitDockPerspective.Entry;
import bibliothek.gui.dock.station.split.SplitDockPerspective.EntryListener;
import bibliothek.gui.dock.station.split.SplitDockPerspective.Leaf;
import bibliothek.util.Path;
/**
* A representation of a {@link CWorkingArea}. When using this perspective to handle the children of a {@link CWorkingArea}
* the following rules must be followed:
*
* - Adding or removing a {@link CDockablePerspective} to this station may trigger a call to {@link CDockablePerspective#setWorkingArea(CStationPerspective)}, clients
* can however first add/remove a dockable and then alter the working-area of the dockable to their likings.
* - Adding: the working-area of a child is only set to
this
if the working-area had a value of null
* - Removing: the working-area of a child is only set to
null
if the working-area had a value of this
and if
* {@link #isAutoUnset()} returns true
.
* - This perspective recursively visits all dockables and stations when adding or removing a child.
* - {@link MultipleCDockablePerspective}: if the working-area is set, then it is automatically set in the {@link MultipleCDockable} as well.
* - {@link SingleCDockablePerspective}: if the working-area is set, then it is automatically set in the {@link SingleCDockable} as well.
* - This perspective does not track changes on children. If some {@link PerspectiveStation} was added and then some children are added
* to that station, then this
CworkingPerspective
will not set the working-area property of the new children.
*
* @author Benjamin Sigg
*/
public class CWorkingPerspective extends CGridPerspective{
private boolean autoUnset = false;
/**
* Creates a new working area.
* @param id the unique identifier of this area
*/
public CWorkingPerspective( String id ){
this( id, null );
}
/**
* Creates a new working area.
* @param id the unique identifier of this area
* @param typeId the type of this station, can be null
*/
public CWorkingPerspective( String id, Path typeId ){
super( id, typeId == null ? CWorkingArea.TYPE_ID : typeId, true );
}
/**
* Tells this station to set the {@link CDockablePerspective#setWorkingArea(CStationPerspective) working-area}
* to null
when a child of this station is removed. The default value of this property is
* false
.
* @param autoUnset whether the working-area should be automatically set to null
*/
public void setAutoUnset( boolean autoUnset ){
this.autoUnset = autoUnset;
}
/**
* Tells whether the working-area of children is automatically set to null
when the children
* are removed from this station.
* @return whether the working-area is set to null
*/
public boolean isAutoUnset(){
return autoUnset;
}
@Override
protected CommonSplitDockPerspective create(){
CommonSplitDockPerspective result = super.create();
result.addListener( new EntryListener(){
public void removed( Entry parent, Entry child ){
if( isAutoUnset() ){
remove( child );
}
}
public void added( Entry parent, Entry child ){
add( child );
}
public void modified( Leaf leaf, PerspectiveDockable oldDockable, PerspectiveDockable newDockable ){
if( oldDockable != null ){
if( isAutoUnset() ){
remove( oldDockable );
}
}
if( newDockable != null ){
add( newDockable );
}
}
});
return result;
}
private void remove( Entry child ){
if( child != null ){
if( child.asNode() != null ){
remove( child.asNode().getChildA() );
remove( child.asNode().getChildB() );
}
else{
PerspectiveDockable dockable = child.asLeaf().getDockable();
if( dockable != null ){
remove( dockable );
}
}
}
}
private void remove( PerspectiveDockable dockable ){
if( dockable instanceof CommonElementPerspective ){
CDockablePerspective cdockable = ((CommonElementPerspective)dockable).getElement().asDockable();
if( cdockable.getWorkingArea() == this ){
cdockable.setWorkingArea( null );
}
}
PerspectiveStation station = dockable.asStation();
if( station != null ){
for( int i = 0, n = station.getDockableCount(); i