All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bibliothek.gui.dock.facile.mode.MaximizedModeSetting Maven / Gradle / Ivy

Go to download

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

There is a newer version: 1.1.2p20b.fix-1
Show 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.facile.mode;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import bibliothek.gui.Dockable;
import bibliothek.gui.dock.support.mode.ModeSetting;
import bibliothek.gui.dock.support.mode.ModeSettingFactory;
import bibliothek.gui.dock.support.mode.ModeSettingsConverter;
import bibliothek.util.Path;
import bibliothek.util.Version;
import bibliothek.util.xml.XElement;

/**
 * Settings associated with a {@link MaximizedMode}.
 * @author Benjamin Sigg
 *
 */
public class MaximizedModeSetting implements ModeSetting{
	/** factory creating new {@link MaximizedModeSetting}s */
	public static ModeSettingFactory FACTORY = new ModeSettingFactory(){
		public ModeSetting create(){
			return new MaximizedModeSetting();
		}
		public Path getModeId(){
			return MaximizedMode.IDENTIFIER;
		}
	};
	
	/** the mode in which some dockable with id=key was before maximizing */
	private HashMap lastMaximizedMode = new HashMap();
	
	/** the location some dockable had before maximizing */
	private HashMap lastMaximizedLocation = new HashMap();
	
	public Path getModeId(){
		return MaximizedMode.IDENTIFIER;
	}

	/**
	 * Sets the location of {@link Dockable}s that are maximized. This method makes a copy
	 * of the map.
	 * @param lastMaximizedLocation the map that is going to be copied 
	 */
	public void setLastMaximizedLocation( Map lastMaximizedLocation ){
		this.lastMaximizedLocation = new HashMap( lastMaximizedLocation );
	}
	
	/**
	 * Sets the mode of {@link Dockable}s that are maximized. This method makes a copy
	 * of the map.
	 * @param lastMaximizedMode the map that is going to be copied
	 */
	public void setLastMaximizedMode( Map lastMaximizedMode ){
		this.lastMaximizedMode = new HashMap( lastMaximizedMode );
	}
	
	/**
	 * Gets the location of {@link Dockable}s that are currently maximized.
	 * @return an unmodifiable map
	 */
	public Map getLastMaximizedLocation(){
		return Collections.unmodifiableMap( lastMaximizedLocation );
	}
	
	/**
	 * Gets the modes of {@link Dockable}s that are currently maximized.
	 * @return an unmodifiable map
	 */
	public Map getLastMaximizedMode(){
		return Collections.unmodifiableMap( lastMaximizedMode );
	}
	
	public  void write( DataOutputStream out, ModeSettingsConverter converter ) throws IOException{
        Version.write( out, Version.VERSION_1_0_8 );
        if( lastMaximizedMode == null ){
            out.writeInt( 0 );
        }
        else{
            int count = 0;
            for( Path check : lastMaximizedMode.values() ){
                if( check != null ){
                    count++;
                }
            }

            out.writeInt( count );
            for( Map.Entry entry : lastMaximizedMode.entrySet() ){
                if( entry.getValue() != null ){
                    out.writeUTF( entry.getKey() );
                    out.writeUTF( entry.getValue().toString() );
                }
            }
        }

        if( lastMaximizedLocation == null ){
            out.writeInt( 0 );
        }
        else{
            int count = 0;
            for( Location location : lastMaximizedLocation.values() ){
                if( location != null ){
                    count++;
                }
            }

            out.writeInt( count );
            for( Map.Entry entry : lastMaximizedLocation.entrySet() ){
                if( entry.getValue() != null ){
                    out.writeUTF( entry.getKey() );
                    converter.writeProperty( converter.convertToSetting( entry.getValue() ), out );
                }
            }
        }
	}

    public  void read( DataInputStream in, ModeSettingsConverter converter ) throws IOException {
        Version version = Version.read( in );
        version.checkCurrent();

        lastMaximizedLocation = new HashMap();
        lastMaximizedMode = new HashMap();

        int count = in.readInt();
        for( int i = 0; i < count; i++ ){
        	String key = in.readUTF();
        	String value = in.readUTF();
        	lastMaximizedMode.put( key, new Path( value ));
        }

        count = in.readInt();
        for( int i = 0; i < count; i++ ){
        	String key = in.readUTF();
        	Location location = converter.convertToWorld( converter.readProperty( in ) );
        	lastMaximizedLocation.put( key, location );
        }
    }

    public  void write( XElement element, ModeSettingsConverter converter ) {
        Set keys = new HashSet();
        if( lastMaximizedLocation != null ){
            keys.addAll( lastMaximizedLocation.keySet() );
        }
        if( lastMaximizedMode != null ){
            keys.addAll( lastMaximizedMode.keySet() );
        }

        if( !keys.isEmpty() ){
            XElement xmaximized = element.addElement( "maximized" );

            for( String key : keys ){
                Path mode = lastMaximizedMode.get( key );
                Location location = lastMaximizedLocation.get( key );

                if( mode != null || location != null ){
                    XElement xitem = xmaximized.addElement( "item" );
                    xitem.addString( "id", key );
                    if( mode != null ){
                        xitem.addElement( "mode" ).setString( mode.toString() );
                    }
                    if( location != null ){
                    	converter.writePropertyXML( converter.convertToSetting( location ), xitem.addElement( "location" ) );
                    }
                }
            }
        }
    }

    public  void read( XElement element, ModeSettingsConverter converter ) {
    	lastMaximizedLocation = new HashMap();
    	lastMaximizedMode = new HashMap();

    	XElement xmaximized = element.getElement( "maximized" );

    	if( xmaximized != null ){
    		for( XElement xitem : xmaximized.getElements( "item" )){
    			String key = xitem.getString( "id" );

    			XElement xmode = xitem.getElement( "mode" );
    			if( xmode != null ){
    				lastMaximizedMode.put( key, new Path( xmode.getString() ));
    			}

    			XElement xlocation = xitem.getElement( "location" );
    			if( xlocation != null ){
    				lastMaximizedLocation.put( key, converter.convertToWorld( converter.readPropertyXML( xlocation ) ) );
    			}
    		}
    	}
    }
}