view java/src/uk/ac/qmul/eecs/ccmi/utils/GridBagUtilities.java @ 3:9e67171477bc

PHANTOM Omni Heptic device release
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Wed, 25 Apr 2012 17:09:09 +0100
parents 9418ab7b7f3f
children d66dd5880081
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/  

package uk.ac.qmul.eecs.ccmi.utils;

import java.awt.GridBagConstraints;

/**
 * 
 * A Utility class providing static method to quickly arrange components, laid out by 
 * a GridBagLayout, in the following way: one component per row and 
 * either taking the whole column or just the right part of it, if preceded by a label.  
 *
 *
 */
public class GridBagUtilities {
	public GridBagUtilities(){
		 labelPad = DEFAULT_LABEL_PAD;
		 row = 0;
	}
	
	/**
	 * Provides the {@code GridBagConstrains} for a label. The label is placed
	 * on the left
	 * @param pad the pad between the label and the left margin of the component containing
	 * it 
	 * @return a {@code GridBagConstrains} object to pass to the {@code add} method of {@code JComponent}
	 */
	public GridBagConstraints label(int pad){
		GridBagConstraints c ;
		
		c = new GridBagConstraints();
		c.anchor = GridBagConstraints.WEST;
		c.gridx = 0;
		c.gridy = row;
		c.insets = new java.awt.Insets(PAD,PAD,PAD,pad);
		
		return c;
	}
	
	/**
	 * Equivalent to {@link #label(int)} passing as argument the value previously 
	 * set by {@link #setLabelPad(int)} or {@link #DEFAULT_LABEL_PAD} otherwise.
	 * @return
	 */
	public GridBagConstraints label(){
		return label(labelPad);
	}
	
	/**
	 * Sets the value used by {@link #label()} as the pad between the label 
	 * and the left margin of the component containing it 
	 * @param labelPad the label pad 
	 */
	public void setLabelPad(int labelPad){
		this.labelPad = labelPad;
	}
	
	public GridBagConstraints field(){
		GridBagConstraints c;
		
		c = new GridBagConstraints();
		c.anchor = GridBagConstraints.CENTER;
		c.gridx = 1;
		c.gridy = row++;
		c.insets = new java.awt.Insets(PAD,PAD,PAD,PAD);
		c.fill = GridBagConstraints.HORIZONTAL;
		
		return c;
	}
	
	public GridBagConstraints all(){
		GridBagConstraints c;
		
		c = new GridBagConstraints();
		c.gridy = row++;
		c.anchor = GridBagConstraints.CENTER;
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new java.awt.Insets(PAD,PAD,PAD,PAD);
		return c;
	}
	
	private int labelPad;
	private int row;
	public static final int DEFAULT_LABEL_PAD = 50;
	public static final int PAD = 2;
}