중간에 gridwidth 가 1보다 큰 cell이 나타날 때 gridx값이 잘못 설정되는 버그 수정.
/**
* It simplifies the use of GridBagLayout and decreases the amount of coding.
* 2010-05-04
* @ThreadUnsafe
*/
package com.ynseo.downloader.ui;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.NoSuchElementException;
import javax.swing.JPanel;
public class GbUtil {
final public static Fill FILL_HOR = new Horizontal();
final public static Fill FILL_VER = new Vertical();
final public static Fill FILL_BOTH = new Both();
final public static Fill FILL_NONE = new None();
final public static ANCHOR N = new N();
final public static ANCHOR E = new E();
final public static ANCHOR S = new S();
final public static ANCHOR W = new W();
final public static ANCHOR NE = new NE();
final public static ANCHOR NW = new NW();
final public static ANCHOR SE = new SE();
final public static ANCHOR SW = new SW();
final public static ANCHOR CENTER = new CENTER();
private static interface GBC {public int value();}
/**
* It is used for the value of GridBagConstraint.fill
* @author yeori
*
*/
public abstract static class Fill implements GBC {}
private static class Horizontal extends Fill {
public int value(){ return GridBagConstraints.HORIZONTAL;}
}
private static class Vertical extends Fill {
public int value(){ return GridBagConstraints.VERTICAL;}
}
private static class Both extends Fill {
public int value(){ return GridBagConstraints.BOTH;}
}
private static class None extends Fill {
public int value(){ return GridBagConstraints.NONE;}
}
public abstract static class ANCHOR implements GBC {}
private static class N extends ANCHOR {
public int value(){ return GridBagConstraints.NORTH;}
}
private static class E extends ANCHOR {
public int value(){ return GridBagConstraints.EAST;}
}
private static class S extends ANCHOR {
public int value(){ return GridBagConstraints.SOUTH;}
}
private static class W extends ANCHOR {
public int value(){ return GridBagConstraints.WEST;}
}
private static class NE extends ANCHOR {
public int value(){ return GridBagConstraints.NORTHEAST;}
}
private static class NW extends ANCHOR {
public int value(){ return GridBagConstraints.NORTHWEST;}
}
private static class SE extends ANCHOR {
public int value(){ return GridBagConstraints.SOUTHEAST;}
}
private static class SW extends ANCHOR {
public int value(){ return GridBagConstraints.SOUTHWEST;}
}
private static class CENTER extends ANCHOR {
public int value(){ return GridBagConstraints.CENTER;}
}
public static Component NULL_COMP = new JPanel();
private static Insets NULL_INSETS = new Insets(-1, -1, -1, -1);
private GridBagLayout gbl = new GridBagLayout();
private Container parent;
private GridBagConstraints c = new GridBagConstraints();
/* row level */
private Component [] rows ;
private GridBagConstraints [] cs;
/* global level */
private double [] defaultWeightX = new double []{.0, .0, .0, .0};
private double defaultWeightY = 0.0;
private Insets margin = new Insets(0, 0, 0, 0);
private int cellSpace = 0;
private int rowCnt = 0;
private ANCHOR defaultAnchor = CENTER;
public GbUtil(){
;
}
public GbUtil(Container parent){
this.parent = parent;
parent.setLayout(gbl);
}
public void setTarget(Container parent){
this.parent = parent;
parent.setLayout(gbl);
}
public void addRow(Component [] rows){
if( rows ==null || rows.length == 0 ){
throw new IllegalArgumentException("invalid component array. use addDummyRow()");
}
this.rows = rows;
this.cs = new GridBagConstraints[rows.length];
for( int i = 0 ; i < rows.length ; i++){
cs[i] = (GridBagConstraints) c.clone();
cs[i].gridx = i;
cs[i].gridy = rowCnt;
cs[i].gridwidth = 1;
cs[i].gridheight = 1;
cs[i].weightx = -1;
cs[i].weighty = -1;
cs[i].anchor = -1;
cs[i].insets = (Insets) NULL_INSETS.clone();
}
}
public GbUtil defaultAnchor(ANCHOR anchor){
if ( anchor == null )
throw new IllegalArgumentException("null anchor parameter");
this.defaultAnchor = anchor;
return this;
}
public GbUtil defaultCellSpace(int space){
if ( space < 0 )
throw new IllegalArgumentException("space : " + space + " is not allowed.");
this.cellSpace = space;
return this;
}
public GbUtil defaultMargin(int top, int left, int bottom, int right){
if ( top < 0 || left < 0 || bottom < 0 || right < 0)
throw new IllegalArgumentException(" some values are less than zero.");
this.margin.top = top;
this.margin.left = left;
this.margin.bottom = bottom;
this.margin.right = right;
return this;
}
public GbUtil defaultWeightX(double [] weights){
if ( weights == null || weights.length == 0 )
throw new IllegalArgumentException("check weights array");
this.defaultWeightX = weights;
return this;
}
public GbUtil anchor(Component comp, ANCHOR policy){
if ( comp == null || policy == null)
throw new IllegalArgumentException("check parameter");
for( int i = 0 ; i < rows.length ; i++){
if ( rows[i] == comp){
cs[i].anchor = policy.value();
return this;
}
}
throw new NoSuchElementException("cannot find <" + comp.getName() + "> from the current rows.");
}
public GbUtil weightX(Component comp, double weight){
for( int i = 0 ; i < rows.length ; i++){
if ( rows[i] == comp){
cs[i].weightx = weight;
return this;
}
}
throw new NoSuchElementException("cannot find <" + comp.getName() + "> from the current rows.");
}
public GbUtil weightY(Component comp, double weightY){
if ( comp == null || weightY < 0 )
throw new IllegalArgumentException("check parameter");
for( int i = 0 ; i < rows.length ; i++){
if ( rows[i] == comp){
cs[i].weighty = weightY;
return this;
}
}
throw new NoSuchElementException("cannot find <" + comp.getName() + "> from the current rows.");
}
public GbUtil span(Component comp, int gridWidth, int gridHeight){
if ( comp == null || gridWidth < 1 || gridHeight < 1 )
throw new IllegalArgumentException("check parameter");
for( int i = 0 ; i < rows.length ; i++){
if ( rows[i] == comp){
int wGap = gridWidth - cs[i].gridwidth ;
int hGap = gridHeight - cs[i].gridheight;
cs[i].gridwidth = gridWidth;
cs[i].gridheight = gridHeight;
for( int j = i+1 ; j < rows.length ; j++){
cs[j].gridx += wGap;
}
return this;
}
}
throw new NoSuchElementException("cannot find <" + comp.getName() + "> from the current rows.");
}
public GbUtil fill(Component comp, Fill type){
if ( comp == null) throw new NullPointerException("null component");
if ( comp == null) throw new NullPointerException("null anchor");
for( int i = 0 ; i < rows.length ; i++){
if ( rows[i] == comp){
cs[i].fill = type.value();
return this;
}
}
throw new NoSuchElementException("cannot find <" + comp.getName() + "> from the current rows.");
}
public GbUtil marginBottom(Component comp, int space){
if ( comp == null) throw new NullPointerException("null component");
if ( space < 0 ) throw new IllegalArgumentException("margin bottom : " + space + " < 0");
for( int i = 0 ; i < rows.length ; i++){
if ( rows[i] == comp){
cs[i].insets.bottom = space;
return this;
}
}
throw new NoSuchElementException("cannot find <" + comp.getName() + "> from the current rows.");
}
public GbUtil marginRight(Component comp, int space){
if ( comp == null) throw new NullPointerException("null component");
if ( space < 0 ) throw new IllegalArgumentException("margin bottom : " + space + " < 0");
if ( space < 0 )
throw new IllegalArgumentException("margin right : " + space + " < 0");
for( int i = 0 ; i < rows.length ; i++){
if ( rows[i] == comp){
cs[i].insets.right = space;
return this;
}
}
throw new NoSuchElementException("cannot find <" + comp.getName() + "> from the current rows.");
}
public void commit(){
commit(false);
}
public GbUtil addDummyRow(){
addRow(new Component[]{NULL_COMP});
return this;
}
public void commit(boolean lastRow){
for( int i = 0, cols = 0 ; i < defaultWeightX.length ; i++){
if ( i == rows.length) break;
if ( rows[i] == NULL_COMP) continue;
if( cs[i].weightx < 0.0){
cs[i].weightx = 0.0;
for ( int col = cols ; col < cs[i].gridwidth ; col++)
cs[i].weightx += this.defaultWeightX[i + col];
cols += cs[i].gridwidth -1;
}
if( cs[i].weighty < 0.0){
cs[i].weighty = this.defaultWeightY;
}
if ( cs[i].insets.equals(NULL_INSETS)){
cs[i].insets.top = 0;
cs[i].insets.left = 0;
cs[i].insets.bottom = this.cellSpace;
cs[i].insets.right = this.cellSpace;
} else {
if ( cs[i].insets.top < 0 ) cs[i].insets.top = 0;
if ( cs[i].insets.left < 0) cs[i].insets.left = 0;
if ( cs[i].insets.bottom < 0) cs[i].insets.bottom = this.cellSpace;
if ( cs[i].insets.right < 0 ) cs[i].insets.right = this.cellSpace;
}
if ( i == 0 ) cs[i].insets.left = this.margin.left;
if ( i == rows.length -1 ) cs[i].insets.right = this.margin.right;
if ( rowCnt == 0 ) cs[i].insets.top = this.margin.top;
if ( lastRow ) cs[i].insets.bottom = this.margin.bottom;
if ( cs[i].anchor < 0 ) cs[i].anchor = defaultAnchor.value();
parent.add(rows[i], cs[i]);
// print(cs[i]);
}
this.rows = null;
this.cs = null;
rowCnt ++;
}
private void print(GridBagConstraints c){
StringBuffer buf = new StringBuffer();
buf.append("[x : " + c.gridx +
", y : " + c.gridy +
", span( " + c.gridwidth +
", " + c.gridheight +
"), weight ( " + c.weightx +
", " + c.weighty +
")");
System.out.println(buf);
}
}
'Dev > Java' 카테고리의 다른 글
유틸을 사용한 GridBagLayout 예제 (2) | 2010.05.06 |
---|---|
[Swing] GridBagLayout에 대한 정리 2 (3) | 2010.05.06 |
[Swing] GridBagLayout 에 대한 정리 1 (3) | 2010.05.06 |