bibliothek.gui.dock.perspective.Perspective Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docking-frames-core Show documentation
Show all versions of docking-frames-core Show documentation
A window docking framework for Swing
The newest version!
/*
* 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.perspective;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import bibliothek.gui.DockStation;
import bibliothek.gui.Dockable;
import bibliothek.gui.dock.DockElement;
import bibliothek.gui.dock.DockFactory;
import bibliothek.gui.dock.layout.AdjacentDockFactory;
import bibliothek.gui.dock.layout.DockLayout;
import bibliothek.gui.dock.layout.DockLayoutComposition;
import bibliothek.gui.dock.layout.DockLayoutInfo;
import bibliothek.gui.dock.layout.DockSituation;
import bibliothek.gui.dock.layout.DockSituationIgnore;
import bibliothek.util.xml.XElement;
/**
* A {@link Perspective} is a helper class that allows clients to create a layout without the need to
* create {@link DockStation}s or {@link Dockable}s.
* Perspectives cannot be created directly, clients must first set up the {@link DockSituation} that
* would load the layout and then call the method {@link DockSituation#createPerspective()} to create
* a {@link Perspective} which in return is able to write a file that matches the need of its
* owner {@link DockSituation}.
* @author Benjamin Sigg
*/
public abstract class Perspective {
/** Provides algorithms used to convert the {@link PerspectiveElement}s */
private DockSituation situation;
/**
* Creates a new {@link Perspective} using situation
to read and write
* items.
* @param situation the set of factories to use
*/
public Perspective( DockSituation situation ){
this.situation = situation;
}
/**
* Reads the contents of root
and returns them in a map.
* @param root the data to read
* @return the content of root
*/
public Map readXML( XElement root ){
Map map = situation.readCompositionsXML( root );
Map result = new HashMap();
for( Map.Entry entry : map.entrySet() ){
PerspectiveElement element = convert( entry.getValue() );
PerspectiveStation station = element == null ? null : element.asStation();
if( station != null ){
result.put( entry.getKey(), station );
}
}
return result;
}
/**
* Converts the content of stations
to XML.
* @param stations the items to write
* @param element the element to write into
*/
public void writeXML( Map stations, XElement element ){
Map map = new HashMap();
for( Map.Entry entry : stations.entrySet() ){
DockLayoutComposition composition = convert( entry.getValue() );
if( composition != null ){
map.put( entry.getKey(), composition );
}
}
situation.writeCompositionsXML( map, element );
}
/**
* Reads the contents of in
and returns them in a map.
* @param in the stream to read from
* @return the content of in
* @throws IOException if an I/O-error ocurrs
*/
public Map read( DataInputStream in ) throws IOException {
Map map = situation.readCompositions( in );
Map result = new HashMap();
for( Map.Entry entry : map.entrySet() ){
PerspectiveElement element = convert( entry.getValue() );
PerspectiveStation station = element == null ? null : element.asStation();
if( station != null ){
result.put( entry.getKey(), station );
}
}
return result;
}
/**
* Writes the layout created by stations
to out
. The data written
* by this method can be read by {@link DockSituation#read(DataInputStream)}
* @param stations the root-stations to store
* @param out the stream to write into
* @throws IOException if an I/O-error occurred
*/
public void write( Map stations, DataOutputStream out ) throws IOException {
Map map = new HashMap();
for( Map.Entry entry : stations.entrySet() ){
DockLayoutComposition composition = convert( entry.getValue() );
if( composition != null ){
map.put( entry.getKey(), composition );
}
}
situation.writeCompositions( map, out );
}
/**
* Converts element
using the {@link DockFactory}s that are registered at this
* perspective.
* @param element the element to convert, not null
* @return the converted element
* @throws IllegalArgumentException if a factory is missing
*/
@SuppressWarnings("unchecked")
public DockLayoutComposition convert( PerspectiveElement element ){
if( element == null ){
throw new IllegalArgumentException( "element must not be null" );
}
DockSituationIgnore ignore = situation.getIgnore();
if( ignore != null && ignore.ignoreElement( element )){
return null;
}
String id = getID( element );
DockFactory factory = (DockFactory)getFactory( id );
if( factory == null ){
throw new IllegalArgumentException( "missing factory: " + element.getFactoryID() );
}
Map ids = new HashMap();
List children = new ArrayList();
boolean ignoreChildren = false;
PerspectiveStation station = element.asStation();
if( station != null ){
if( ignore == null || !ignore.ignoreChildren( station )){
int index = 0;
for( int i = 0, n = station.getDockableCount(); i layout = new DockLayout