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

bibliothek.gui.dock.extension.css.path.MultiCssPath Maven / Gradle / Ivy

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) 2007 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.extension.css.path;

import java.util.ArrayList;

import java.util.List;

import bibliothek.gui.dock.extension.css.CssNode;
import bibliothek.gui.dock.extension.css.CssPath;
import bibliothek.util.Todo;
import bibliothek.util.Todo.Compatibility;
import bibliothek.util.Todo.Priority;
import bibliothek.util.Todo.Version;

/**
 * A {@link MultiCssPath} is a list of {@link CssPath}s.
 * @author Benjamin Sigg
 */
public class MultiCssPath extends AbstractCssPath{
	/** the different sub-paths */
	private List parts = new ArrayList();
	
	private int[] offsets = null;
	private int size = -1;
	
	private CssPathListener partListener = new CssPathListener(){
		@Override
		public void pathChanged( CssPath path ){
			size = calculateSize();
			offsets = calculateOffsets( offsets );
			firePathChanged();
		}
	};
	
	/**
	 * Creates a new multi path
	 * @param parts all the parts of the path
	 */
	public MultiCssPath( CssPath... parts ){
		for( CssPath path : parts ){
			addPart( path );
		}
	}
	
	@Override
	protected void bind(){
		for( CssPath part : parts ){	
			part.addPathListener( partListener );
		}
		size = calculateSize();
		offsets = calculateOffsets( offsets );
	}
	
	@Override
	protected void unbind(){
		for( CssPath part : parts ){
			part.removePathListener( partListener );
		}
		offsets = null;
	}
	
	@Override
	public int getSize(){
		if( isBound() ){
			return size;
		}
		else{
			return calculateSize();
		}
	}
	
	private int calculateSize(){
		int count = 0;
		for( CssPath part : parts ){
			count += part.getSize();
		}
		return count;
	}
	
	@Override
	@Todo( compatibility=Compatibility.COMPATIBLE, priority=Priority.MINOR,
		target=Version.VERSION_1_1_2, description="use a binary search to find the offset-index")
	public CssNode getNode( int index ){
		int[] offsets;
		if( isBound() ){
			offsets = this.offsets;
		}
		else{
			offsets = calculateOffsets( null );
		}
		
		for( int i = 0; i < offsets.length; i++ ){
			if( index < offsets[i]){
				if( i == 0 ){
					return parts.get( 0 ).getNode( index );
				}
				else{
					return parts.get( i ).getNode( index - offsets[i-1] );
				}
			}
		}
		
		throw new IllegalArgumentException( "index out of bonuds: " + index );
	}
	
	private int[] calculateOffsets( int[] offsets ){
		if( offsets == null || offsets.length != parts.size() ){
			offsets = new int[ parts.size() ];
		}
		int index = 0;
		int offset = 0;
		
		for( CssPath part : parts ){
			offset += part.getSize();
			offsets[ index++ ] = offset;
		}
		
		return offsets;
	}
	
	/**
	 * Gets the number of parts of this path.
	 * @return the number of parts
	 */
	public int getPartsCount(){
		return parts.size();
	}
	
	/**
	 * Gest the index'th part of this path.
	 * @param index the index of some part
	 * @return the part
	 */
	public CssPath getPart( int index ){
		return parts.get( index );
	}
	
	/**
	 * Adds part at the end of this list of paths.
	 * @param part the part to add, not null
	 */
	public void addPart( CssPath part ){
		addPart( getPartsCount(), part );
	}
	
	/**
	 * Inserts part at the index'th location of
	 * this list of paths.
	 * @param index the index of the new part
	 * @param part the new part, not null
	 */
	public void addPart( int index, CssPath part ){
		if( part == null ){
			throw new IllegalArgumentException( "part must not be null" );
		}
		parts.add( index, part );
		if( isBound() ){
			part.addPathListener( partListener );
			firePathChanged();
		}
	}
	
	/**
	 * Gets the location of part in this list of paths.
	 * @param part the part to search
	 * @return the location or -1 if not found
	 */
	public int indexOf( CssPath part ){
		return parts.indexOf( part );
	}
	

	/**
	 * Removes the index'th part of this path.
	 * @param index the index of the part to remove
	 * @return the part that was removed
	 */
	public CssPath removePart( int index ){
		CssPath part = parts.remove( index );
		if( isBound() ){
			part.removePathListener( partListener );
			firePathChanged();
		}
		return part;
	}
	
	/**
	 * Removes part from this list of paths.
	 * @param part the part to remove
	 */
	public void remove( CssPath part ){
		int index = indexOf( part );
		if( index >= 0 ){
			removePart( index );
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy