/*
* Copyright (c) 2000, 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* Declarative Engineering - Major additions (overlays & minimum sizing) + maintenance, formatting, & commenting.
*/
package com.foundation.view.swt.layout;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
/**
* GridData is the layout data object associated with
* GridLayout. To set a GridData object into a
* control, you use the Control.setLayoutData(Object) method.
*
* There are two ways to create a GridData object with certain
* fields set. The first is to set the fields directly, like this:
*
* GridData gridData = new GridData(); * gridData.horizontalAlignment = GridData.FILL; * gridData.grabExcessHorizontalSpace = true; * button1.setLayoutData(gridData); ** The second is to take advantage of convenience style bits defined * by
GridData:
* * button1.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); ** *
* NOTE: Do not reuse GridData objects. Every control in a
* Composite that is managed by a GridLayout
* must have a unique GridData object. If the layout data
* for a control in a GridLayout is null at layout time,
* a unique GridData object is created for it.
*
grabExcessHorizontalSpace specifies whether the width of the cell
* changes depending on the size of the parent Composite. If
* grabExcessHorizontalSpace is true, the following rules
* apply to the width of the cell:
The default value is false.
* * @see GridData#minimumWidth * @see GridData#widthHint */ public boolean grabExcessHorizontalSpace = false; /** *grabExcessVerticalSpace specifies whether the height of the cell
* changes depending on the size of the parent Composite. If
* grabExcessVerticalSpace is true, the following rules
* apply to the height of the cell:
The default value is false.
* * @see GridData#minimumHeight * @see GridData#heightHint */ public boolean grabExcessVerticalSpace = false; /** *TODO: This should really default to -1, and a value of -1 should be using the control's minimum. Currently 0 or -1 uses the control's minimum, but not for the layout - only for reporting a total minimum for the layout.
* minimumWidth specifies the minimum width in pixels. This value * applies only if grabExcessHorizontalSpace is true. A value of * SWT.DEFAULT means that the minimum width will be the result * of Control.computeSize(int, int, boolean) where wHint is * determined by GridData.widthHint. * * The default value is 0. * * @since 3.1 * @see Control#computeSize(int, int, boolean) * @see GridData#widthHint */ public int minimumWidth = 0; /** *TODO: This should really default to -1, and a value of -1 should be using the control's minimum. Currently 0 or -1 uses the control's minimum, but not for the layout - only for reporting a total minimum for the layout.
* minimumHeight specifies the minimum height in pixels. This value * applies only if grabExcessVerticalSpace is true. A value of * SWT.DEFAULT means that the minimum height will be the result * of Control.computeSize(int, int, boolean) where hHint is * determined by GridData.heightHint. * * The default value is 0. * * @since 3.1 * @see Control#computeSize(int, int, boolean) * @see GridData#heightHint */ public int minimumHeight = 0; /** * exclude informs the layout to ignore this control when sizing * and positioning controls. If this value istrue,
* the size and position of the control will not be managed by the
* layout. If this value is false, the size and
* position of the control will be computed and assigned.
*
* The default value is false.
*
* @since 3.1
*/
public boolean exclude = false;
/**
* Value for horizontalAlignment or verticalAlignment.
* Position the control at the top or left of the cell.
* Not recommended. Use SWT.BEGINNING, SWT.TOP or SWT.LEFT instead.
*/
public static final int BEGINNING = SWT.BEGINNING;
/**
* Value for horizontalAlignment or verticalAlignment.
* Position the control in the vertical or horizontal center of the cell
* Not recommended. Use SWT.CENTER instead.
*/
public static final int CENTER = 2;
/**
* Value for horizontalAlignment or verticalAlignment.
* Position the control at the bottom or right of the cell
* Not recommended. Use SWT.END, SWT.BOTTOM or SWT.RIGHT instead.
*/
public static final int END = 3;
/**
* Value for horizontalAlignment or verticalAlignment.
* Resize the control to fill the cell horizontally or vertically.
* Not recommended. Use SWT.FILL instead.
*/
public static final int FILL = SWT.FILL;
/**
* Style bit for new GridData(int).
* Position the control at the top of the cell.
* Not recommended. Use
* new GridData(int, SWT.BEGINNING, boolean, boolean)
* instead.
*/
public static final int VERTICAL_ALIGN_BEGINNING = 1 << 1;
/**
* Style bit for new GridData(int) to position the
* control in the vertical center of the cell.
* Not recommended. Use
* new GridData(int, SWT.CENTER, boolean, boolean)
* instead.
*/
public static final int VERTICAL_ALIGN_CENTER = 1 << 2;
/**
* Style bit for new GridData(int) to position the
* control at the bottom of the cell.
* Not recommended. Use
* new GridData(int, SWT.END, boolean, boolean)
* instead.
*/
public static final int VERTICAL_ALIGN_END = 1 << 3;
/**
* Style bit for new GridData(int) to resize the
* control to fill the cell vertically.
* Not recommended. Use
* new GridData(int, SWT.FILL, boolean, boolean)
* instead
*/
public static final int VERTICAL_ALIGN_FILL = 1 << 4;
/**
* Style bit for new GridData(int) to position the
* control at the left of the cell.
* Not recommended. Use
* new GridData(SWT.BEGINNING, int, boolean, boolean)
* instead.
*/
public static final int HORIZONTAL_ALIGN_BEGINNING = 1 << 5;
/**
* Style bit for new GridData(int) to position the
* control in the horizontal center of the cell.
* Not recommended. Use
* new GridData(SWT.CENTER, int, boolean, boolean)
* instead.
*/
public static final int HORIZONTAL_ALIGN_CENTER = 1 << 6;
/**
* Style bit for new GridData(int) to position the
* control at the right of the cell.
* Not recommended. Use
* new GridData(SWT.END, int, boolean, boolean)
* instead.
*/
public static final int HORIZONTAL_ALIGN_END = 1 << 7;
/**
* Style bit for new GridData(int) to resize the
* control to fill the cell horizontally.
* Not recommended. Use
* new GridData(SWT.FILL, int, boolean, boolean)
* instead.
*/
public static final int HORIZONTAL_ALIGN_FILL = 1 << 8;
/**
* Style bit for new GridData(int) to resize the
* control to fit the remaining horizontal space.
* Not recommended. Use
* new GridData(int, int, true, boolean)
* instead.
*/
public static final int GRAB_HORIZONTAL = 1 << 9;
/**
* Style bit for new GridData(int) to resize the
* control to fit the remaining vertical space.
* Not recommended. Use
* new GridData(int, int, boolean, true)
* instead.
*/
public static final int GRAB_VERTICAL = 1 << 10;
/**
* Style bit for new GridData(int) to resize the
* control to fill the cell vertically and to fit the remaining
* vertical space.
* FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL
* Not recommended. Use
* new GridData(int, SWT.FILL, boolean, true)
* instead.
*/
public static final int FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL;
/**
* Style bit for new GridData(int) to resize the
* control to fill the cell horizontally and to fit the remaining
* horizontal space.
* FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL
* Not recommended. Use
* new GridData(SWT.FILL, int, true, boolean)
* instead.
*/
public static final int FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL;
/**
* Style bit for new GridData(int) to resize the
* control to fill the cell horizontally and vertically and
* to fit the remaining horizontal and vertical space.
* FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL
* Not recommended. Use
* new GridData(SWT.FILL, SWT.FILL, true, true)
* instead.
*/
public static final int FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL;
int cacheWidth = -1;
int cacheHeight = -1;
int defaultWhint;
int defaultHhint;
int defaultWidth = -1;
int defaultHeight = -1;
int currentWhint;
int currentHhint;
int currentWidth = -1;
int currentHeight = -1;
//Declarative Engineering//
int cacheHSpan = -1;
int cacheVSpan = -1;
/**
* Constructs a new instance of GridData using default values.
*/
public GridData() {
super();
}//GridData()//
/**
* Constructs a new instance based on the GridData style.
* This constructor is not recommended.
*
* @param style the GridData style
*/
public GridData(int style) {
super();
if((style & VERTICAL_ALIGN_BEGINNING) != 0)
verticalAlignment = BEGINNING;
if((style & VERTICAL_ALIGN_CENTER) != 0)
verticalAlignment = CENTER;
if((style & VERTICAL_ALIGN_FILL) != 0)
verticalAlignment = FILL;
if((style & VERTICAL_ALIGN_END) != 0)
verticalAlignment = END;
if((style & HORIZONTAL_ALIGN_BEGINNING) != 0)
horizontalAlignment = BEGINNING;
if((style & HORIZONTAL_ALIGN_CENTER) != 0)
horizontalAlignment = CENTER;
if((style & HORIZONTAL_ALIGN_FILL) != 0)
horizontalAlignment = FILL;
if((style & HORIZONTAL_ALIGN_END) != 0)
horizontalAlignment = END;
grabExcessHorizontalSpace = (style & GRAB_HORIZONTAL) != 0;
grabExcessVerticalSpace = (style & GRAB_VERTICAL) != 0;
}//GridData()//
/**
* Constructs a new instance of GridData according to the parameters.
*
* @param horizontalAlignment how control will be positioned horizontally within a cell
* @param verticalAlignment how control will be positioned vertically within a cell
* @param grabExcessHorizontalSpace whether cell will be made wide enough to fit the remaining horizontal space
* @param grabExcessVerticalSpace whether cell will be made high enough to fit the remaining vertical space
*
* @since 3.0
*/
public GridData(int horizontalAlignment, int verticalAlignment, boolean grabExcessHorizontalSpace, boolean grabExcessVerticalSpace) {
this(horizontalAlignment, verticalAlignment, grabExcessHorizontalSpace, grabExcessVerticalSpace, 1, 1);
}//GridData()//
/**
* Constructs a new instance of GridData according to the parameters.
*
* @param horizontalAlignment how control will be positioned horizontally within a cell
* @param verticalAlignment how control will be positioned vertically within a cell
* @param grabExcessHorizontalSpace whether cell will be made wide enough to fit the remaining horizontal space
* @param grabExcessVerticalSpace whether cell will be made high enough to fit the remaining vertical space
* @param horizontalSpan the number of column cells that the control will take up
* @param verticalSpan the number of row cells that the control will take up
*
* @since 3.0
*/
public GridData(int horizontalAlignment, int verticalAlignment, boolean grabExcessHorizontalSpace, boolean grabExcessVerticalSpace, int horizontalSpan, int verticalSpan) {
super();
this.horizontalAlignment = horizontalAlignment;
this.verticalAlignment = verticalAlignment;
this.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
this.grabExcessVerticalSpace = grabExcessVerticalSpace;
this.horizontalSpan = horizontalSpan;
this.verticalSpan = verticalSpan;
}//GridData()//
/**
* Computes the size of given control, described by this grid data.
* @param control The control referencing this grid data.
* @param wHint The width hint.
* @param hHint The height hint.
* @param flushCache Whether to flush the cache.
*/
void computeSize(Control control, int wHint, int hHint, boolean flushCache) {
if(cacheWidth == -1 || cacheHeight == -1) {
if(wHint == this.widthHint && hHint == this.heightHint) {
if(defaultWidth == -1 || defaultHeight == -1 || wHint != defaultWhint || hHint != defaultHhint) {
Point size = control.computeSize(wHint, hHint, flushCache);
defaultWhint = wHint;
defaultHhint = hHint;
defaultWidth = size.x;
defaultHeight = size.y;
}//if//
cacheWidth = defaultWidth;
cacheHeight = defaultHeight;
return;
}//if//
if(currentWidth == -1 || currentHeight == -1 || wHint != currentWhint || hHint != currentHhint) {
Point size = control.computeSize(wHint, hHint, flushCache);
currentWhint = wHint;
currentHhint = hHint;
currentWidth = size.x;
currentHeight = size.y;
}//if//
cacheWidth = currentWidth;
cacheHeight = currentHeight;
}//if//
}//computeSize()//
/**
* Clears any cached values.
*/
void flushCache() {
cacheWidth = cacheHeight = -1;
defaultWidth = defaultHeight = -1;
currentWidth = currentHeight = -1;
}//flushCache()//
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
String hAlign = "";
String vAlign = "";
String string;
switch(horizontalAlignment) {
case SWT.FILL:
hAlign = "SWT.FILL";
break;
case SWT.BEGINNING:
hAlign = "SWT.BEGINNING";
break;
case SWT.LEFT:
hAlign = "SWT.LEFT";
break;
case SWT.END:
hAlign = "SWT.END";
break;
case END:
hAlign = "GridData.END";
break;
case SWT.RIGHT:
hAlign = "SWT.RIGHT";
break;
case SWT.CENTER:
hAlign = "SWT.CENTER";
break;
case CENTER:
hAlign = "GridData.CENTER";
break;
default:
hAlign = "Undefined " + horizontalAlignment;
break;
}//switch//
switch(verticalAlignment) {
case SWT.FILL:
vAlign = "SWT.FILL";
break;
case SWT.BEGINNING:
vAlign = "SWT.BEGINNING";
break;
case SWT.TOP:
vAlign = "SWT.TOP";
break;
case SWT.END:
vAlign = "SWT.END";
break;
case END:
vAlign = "GridData.END";
break;
case SWT.BOTTOM:
vAlign = "SWT.BOTTOM";
break;
case SWT.CENTER:
vAlign = "SWT.CENTER";
break;
case CENTER:
vAlign = "GridData.CENTER";
break;
default:
vAlign = "Undefined " + verticalAlignment;
break;
}//switch//
string = "GridData {horizontalAlignment: " + hAlign + " ";
if(horizontalIndent != 0)
string += "horizontalIndent=" + horizontalIndent + " ";
if(horizontalSpan != 1)
string += "horizontalSpan=" + horizontalSpan + " ";
if(grabExcessHorizontalSpace)
string += "grabExcessHorizontalSpace=" + grabExcessHorizontalSpace + " ";
if(widthHint != SWT.DEFAULT)
string += "widthHint=" + widthHint + " ";
if(minimumWidth != 0)
string += "minimumWidth=" + minimumWidth + " ";
string += "verticalAlignment=" + vAlign + " ";
if(verticalIndent != 0)
string += "verticalIndent=" + verticalIndent + " ";
if(verticalSpan != 1)
string += "verticalSpan=" + verticalSpan + " ";
if(grabExcessVerticalSpace)
string += "grabExcessVerticalSpace=" + grabExcessVerticalSpace + " ";
if(heightHint != SWT.DEFAULT)
string += "heightHint=" + heightHint + " ";
if(minimumHeight != 0)
string += "minimumHeight=" + minimumHeight + " ";
if(exclude)
string += "exclude=" + exclude + " ";
string = string.trim();
string += "}";
return string;
}//toString()//
}//GridData//