bibliothek.gui.dock.common.intern.station.CommonDockStationFactory 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) 2011 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.intern.station;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.Map;
import bibliothek.gui.Dockable;
import bibliothek.gui.dock.DockElement;
import bibliothek.gui.dock.DockFactory;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CStation;
import bibliothek.gui.dock.common.SingleCDockable;
import bibliothek.gui.dock.common.SingleCDockableFactory;
import bibliothek.gui.dock.common.intern.CDockable;
import bibliothek.gui.dock.common.intern.CommonSingleDockableFactory;
import bibliothek.gui.dock.common.perspective.CElementPerspective;
import bibliothek.gui.dock.common.perspective.CStationPerspective;
import bibliothek.gui.dock.common.perspective.CommonDockStationPerspective;
import bibliothek.gui.dock.frontend.FrontendPerspectiveCache;
import bibliothek.gui.dock.layout.DockLayout;
import bibliothek.gui.dock.layout.LocationEstimationMap;
import bibliothek.gui.dock.perspective.PerspectiveDockable;
import bibliothek.gui.dock.perspective.PerspectiveElement;
import bibliothek.gui.dock.station.support.PlaceholderStrategy;
import bibliothek.util.Version;
import bibliothek.util.xml.XElement;
import bibliothek.util.xml.XException;
/**
* A factory that is responsible for storing and loading the layout of the
* {@link CommonDockStation}s. This factory actually forwards the work to
* a real {@link DockFactory} and only stores meta-data (like "was the
* {@link CommonDockStation} as {@link SingleCDockable}?").
* @author Benjamin Sigg
*/
public class CommonDockStationFactory implements DockFactory, CommonDockStationPerspective, CommonDockStationLayout>{
/** The unique identifier of this factory */
public static final String FACTORY_ID = "CommonDockStationFactory";
/** The control in whose realm this factory is used */
private CControl control;
/** The factory used to create new {@link CommonDockStation}s that are also {@link SingleCDockable}s */
private CommonSingleDockableFactory singleDockableFactory;
/** Factory used to access missing {@link PerspectiveElement}s */
private FrontendPerspectiveCache cache;
/**
* Creates a new factory
* @param control the {@link CControl} in whose realm this factory works, not null
* @param cache used to create missing {@link PerspectiveElement}s, can be null
* @param singleDockableFactory the factory used to create new {@link CommonDockStation}s that are also {@link SingleCDockable}s, not null
*/
public CommonDockStationFactory( CControl control, FrontendPerspectiveCache cache, CommonSingleDockableFactory singleDockableFactory ){
if( control == null ){
throw new IllegalArgumentException( "control must not be null" );
}
if( singleDockableFactory == null ){
throw new IllegalArgumentException( "singleDockableFactory must not be null" );
}
this.control = control;
this.cache = cache;
this.singleDockableFactory = singleDockableFactory;
}
/**
* Creates a new {@link CommonDockStation} whose {@link CStation} is also a {@link SingleCDockable} with
* unique identifier id
.
* @param id the unique identifier of a {@link SingleCDockable}
* @return the new station or null
if id
is unknown
*/
protected CommonDockStation, ?> createStation( String id ){
SingleCDockableFactory factory = singleDockableFactory.getFactory( id );
if( factory == null ){
return null;
}
SingleCDockable dockable = factory.createBackup( id );
if( dockable == null ){
return null;
}
String factoryId = dockable.intern().getFactoryID();
if( !factoryId.equals( getID() )){
throw new IllegalArgumentException( "Wrong type of dockable for unique id '" + id + "': The backup factory created a dockable which expects a factory of type '" + factoryId +
"', but the call was done from a factory of type '" + getID() + "'" );
}
CStation> station = dockable.asStation();
if( station == null ){
System.err.println( "unique identifier '" + id + "' was supposed to be a CStation, but factory created a dockable" );
return null;
}
return (CommonDockStation, ?>)station.getStation();
}
/**
* Register station
at the {@link CControl} in whose realm this factory works.
* @param station the station to register
* @param root whether to set the root flag or not
*/
protected void registerStation( CStation> station, boolean root ){
if( control.getStation( station.getUniqueId() ) != station ){
control.addStation( station, root );
}
CDockable dockable = station.asDockable();
if( dockable != null ){
if( dockable instanceof SingleCDockable ){
SingleCDockable single = (SingleCDockable)dockable;
if( control.getSingleDockable( single.getUniqueId() ) != single ){
control.addDockable( single );
}
}
}
}
public String getID(){
return FACTORY_ID;
}
@SuppressWarnings("unchecked")
public CommonDockStationLayout getLayout( CommonDockStation, ?> element, Map children ){
String factoryId = element.getConverterID();
DockFactory factory = (DockFactory)control.intern().getDockFactory( factoryId );
if( factory == null ){
return null;
}
Object layout = factory.getLayout( element, children );
if( layout == null ){
return null;
}
CDockable dockable = element.getStation().asDockable();
String id = null;
if( dockable instanceof SingleCDockable ){
id = ((SingleCDockable)dockable).getUniqueId();
}
boolean root = control.isRootStation( element.getStation() );
return new CommonDockStationLayout( id, root, factoryId, new DockLayout