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

bibliothek.gui.dock.station.toolbar.menu.CustomizationMenuContentGrid Maven / Gradle / Ivy

There is a newer version: 1.1.2p6a
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) 2012 Herve Guillaume, 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
 * 
 * Herve Guillaume
 * [email protected]
 * FR - France
 *
 * Benjamin Sigg
 * [email protected]
 * CH - Switzerland
 */
package bibliothek.gui.dock.station.toolbar.menu;

import java.awt.Component;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import bibliothek.gui.DockController;

/**
 * A {@link CustomizationMenuContent} using a {@link GridLayout} to show a set of other 
 * {@link CustomizationMenuContent}s.
 * @author Benjamin Sigg
 */
public class CustomizationMenuContentGrid implements CustomizationMenuContent{
	/** all the children of this grid */
	private List content = new ArrayList();
	
	/** the currently used view */
	private JPanel view;
	
	/** the width of the grid in the number of components to show */
	private int columns;
	
	/** the height of the grid in the number of components to show */
	private int rows;
	
	/** the controller in whose realm this grid is used */
	private DockController controller;
	
	/**
	 * Creates a new grid.
	 * @param columns the width of the grid in the number of components
	 * @param rows the height of the grid in the number of components
	 */
	public CustomizationMenuContentGrid( int columns, int rows ){
		this.columns = columns;
		this.rows = rows;
	}
	
	@Override
	public Component getView(){
		return view;
	}
	
	@Override
	public void setController( DockController controller ){
		this.controller = controller;
		for( CustomizationMenuContent item : content ){
			item.setController( controller );
		}
	}
	
	@Override
	public void bind( CustomizationMenuCallback callback ){
		view = new JPanel( new GridLayout( columns, rows ));
		for( CustomizationMenuContent item : content ){
			item.bind( callback );
			view.add( item.getView() );
		}
	}
	
	@Override
	public void unbind(){
		view.removeAll();
		view = null;
		
		for( CustomizationMenuContent item : content ){
			item.unbind();
		}
	}
	
	/**
	 * Adds item to this grid. It is the clients responsibility to ensure that item
	 * is not already used by another object. If the menu is currently visible, then calling this method
	 * has no immediate effect.
	 * @param item the item to add, not null
	 */
	public void add( CustomizationMenuContent item ){
		content.add( item );
		item.setController( controller );
	}

	/**
	 * Adds item to this grid. It is the clients responsibility to ensure that item
	 * is not already used by another object. If the menu is currently visible, then calling this method
	 * has no immediate effect.
	 * @param index the location where to insert item
	 * @param item the item to add, not null
	 */
	public void add( int index, CustomizationMenuContent item ){
		content.add( index, item );
		item.setController( controller );
	}
	
	/**
	 * Removes the index'th item from this grid. If the menu is currently visible, then
	 * calling this method has no immediate effect.
	 * @param index the index of the item to remove
	 */
	public void remove( int index ){
		CustomizationMenuContent item = content.remove( index );
		item.setController( null );
	}
	
	/**
	 * Removes item from this grid. If the menu is currently visible, then calling
	 * this method has no immediate effect.
	 * @param item the item to remove
	 */
	public void remove( CustomizationMenuContent item ){
		if( content.remove( item ) ){
			item.setController( null );
		}
	}
	
	/**
	 * Gets the number of items on this grid.
	 * @return the number of items
	 */
	public int getItemCount(){
		return content.size();
	}
	
	/**
	 * Gets the index'th item of this grid.
	 * @param index the index of the item
	 * @return the item, not null
	 */
	public CustomizationMenuContent getItem( int index ){
		return content.get( index );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy