import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.geometry.Dimension; import org.eclipse.swt.SWT; /** * @author Klaus Wenger */ public final class GridData { /** * Constructs a new instance of GridData using * default values. */ public GridData() { super(); } /** * 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) { this.verticalAlignment = BEGINNING; } if ((style & VERTICAL_ALIGN_CENTER) != 0) { this.verticalAlignment = CENTER; } if ((style & VERTICAL_ALIGN_FILL) != 0) { this.verticalAlignment = FILL; } if ((style & VERTICAL_ALIGN_END) != 0) { this.verticalAlignment = END; } if ((style & HORIZONTAL_ALIGN_BEGINNING) != 0) { this.horizontalAlignment = BEGINNING; } if ((style & HORIZONTAL_ALIGN_CENTER) != 0) { this.horizontalAlignment = CENTER; } if ((style & HORIZONTAL_ALIGN_FILL) != 0) { this.horizontalAlignment = FILL; } if ((style & HORIZONTAL_ALIGN_END) != 0) { this.horizontalAlignment = END; } this.grabExcessHorizontalSpace =(style & GRAB_HORIZONTAL) != 0; this.grabExcessVerticalSpace =(style & GRAB_VERTICAL) != 0; } /** * 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); } /** * 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; } /** * Constructs a new instance of GridData according to the parameters. * A value of SWT.DEFAULT indicates that no minimum width or * no minumum height is specified. * * @param width a minimum width for the column * @param height a minimum height for the row * * @since 3.0 */ public GridData(int width, int height) { super(); this.widthHint = width; this.heightHint = height; } /** * @param control * @param wHint * @param hHint * @param flushCache */ /* package */ void getPreferredSize(IFigure control, int wHint, int hHint, boolean flushCache) { if (this.cacheWidth != -1 && this.cacheHeight != -1) { return; } if (wHint == this.widthHint && hHint == this.heightHint) { if (this.defaultWidth == -1 || this.defaultHeight == -1 || wHint != this.defaultWhint || hHint != this.defaultHhint) { Dimension size = control.getPreferredSize(wHint, hHint); this.defaultWhint = wHint; this.defaultHhint = hHint; this.defaultWidth = size.width; this.defaultHeight = size.height; } this.cacheWidth = this.defaultWidth; this.cacheHeight = this.defaultHeight; return; } if (this.currentWidth == -1 || this.currentHeight == -1 || wHint != this.currentWhint || hHint != this.currentHhint) { Dimension size = control.getPreferredSize(wHint, hHint); this.currentWhint = wHint; this.currentHhint = hHint; this.currentWidth = size.width; this.currentHeight = size.height; } this.cacheWidth = this.currentWidth; this.cacheHeight = this.currentHeight; } /** * */ /* package */ void flushCache() { this.cacheWidth = this.cacheHeight = -1; this.defaultWidth = this.defaultHeight = -1; this.currentWidth = this.currentHeight = -1; } /** * @return The basename of the class. */ /* package */ String getName() { String string = getClass().getName(); int index = string.lastIndexOf('.'); if (index == -1) return string; return string.substring(index + 1, string.length()); } /** * @see java.lang.Object#toString() */ public String toString() { String hAlign = ""; //$NON-NLS-1$ switch (this.horizontalAlignment) { case SWT.FILL: hAlign = "SWT.FILL"; break; //$NON-NLS-1$ case SWT.BEGINNING: hAlign = "SWT.BEGINNING"; break; //$NON-NLS-1$ case SWT.LEFT: hAlign = "SWT.LEFT"; break; //$NON-NLS-1$ case SWT.END: hAlign = "SWT.END"; break; //$NON-NLS-1$ case END: hAlign = "GridData.END"; break; //$NON-NLS-1$ case SWT.RIGHT: hAlign = "SWT.RIGHT"; break; //$NON-NLS-1$ case SWT.CENTER: hAlign = "SWT.CENTER"; break; //$NON-NLS-1$ case CENTER: hAlign = "GridData.CENTER"; break; //$NON-NLS-1$ default: hAlign = "Undefined " + //$NON-NLS-1$ this.horizontalAlignment; break; } String vAlign = ""; //$NON-NLS-1$ switch (this.verticalAlignment) { case SWT.FILL: vAlign = "SWT.FILL"; break; //$NON-NLS-1$ case SWT.BEGINNING: vAlign = "SWT.BEGINNING"; break; //$NON-NLS-1$ case SWT.TOP: vAlign = "SWT.TOP"; break; //$NON-NLS-1$ case SWT.END: vAlign = "SWT.END"; break; //$NON-NLS-1$ case END: vAlign = "GridData.END"; break; //$NON-NLS-1$ case SWT.BOTTOM: vAlign = "SWT.BOTTOM"; break; //$NON-NLS-1$ case SWT.CENTER: vAlign = "SWT.CENTER"; break; //$NON-NLS-1$ case CENTER: vAlign = "GridData.CENTER"; break; //$NON-NLS-1$ default: vAlign = "Undefined " + //$NON-NLS-1$ this.verticalAlignment; break; } String string = getName()+" {"; //$NON-NLS-1$ string += "horizontalAlignment=" + //$NON-NLS-1$ hAlign + " "; //$NON-NLS-1$ if (this.horizontalIndent != 0) { string += "horizontalIndent=" + //$NON-NLS-1$ this.horizontalIndent + " "; //$NON-NLS-1$ } if (this.horizontalSpan != 1) { string += "horizontalSpan=" + //$NON-NLS-1$ this.horizontalSpan + " "; //$NON-NLS-1$ } if (this.grabExcessHorizontalSpace) { string += "grabExcessHorizontalSpace=" + //$NON-NLS-1$ this.grabExcessHorizontalSpace + " "; //$NON-NLS-1$ } if (this.widthHint != SWT.DEFAULT) { string += "widthHint=" + //$NON-NLS-1$ this.widthHint + " "; //$NON-NLS-1$ } if (this.minimumWidth != 0) { string += "minimumWidth=" + //$NON-NLS-1$ this.minimumWidth + " "; //$NON-NLS-1$ } string += "verticalAlignment=" + //$NON-NLS-1$ vAlign + " "; //$NON-NLS-1$ if (this.verticalIndent != 0) { string += "verticalIndent=" + //$NON-NLS-1$ this.verticalIndent + " "; //$NON-NLS-1$ } if (this.verticalSpan != 1) { string += "verticalSpan=" + //$NON-NLS-1$ this.verticalSpan + " "; //$NON-NLS-1$ } if (this.grabExcessVerticalSpace) { string += "grabExcessVerticalSpace=" + //$NON-NLS-1$ this.grabExcessVerticalSpace + " "; //$NON-NLS-1$ } if (this.heightHint != SWT.DEFAULT) { string += "heightHint=" + //$NON-NLS-1$ this.heightHint + " "; //$NON-NLS-1$ } if (this.minimumHeight != 0) { string += "minimumHeight=" + //$NON-NLS-1$ this.minimumHeight + " "; //$NON-NLS-1$ } string = string.trim(); string += "}"; //$NON-NLS-1$ return string; } /** * verticalAlignment specifies how controls will be positioned * vertically within a cell. * * The default value is CENTER. * * Possible values are: * * SWT.BEGINNING(or SWT.TOP): Position the control at the top of the cell * SWT.CENTER: Position the control in the vertical center of the cell * SWT.END(or SWT.BOTTOM): Position the control at the bottom of the cell * SWT.FILL: Resize the control to fill the cell vertically */ public int verticalAlignment = CENTER; /** * horizontalAlignment specifies how controls will be positioned * horizontally within a cell. * * The default value is BEGINNING. * * Possible values are: * * SWT.BEGINNING(or SWT.LEFT): Position the control at the left of the cell * SWT.CENTER: Position the control in the horizontal center of the cell * SWT.END (or SWT.RIGHT): Position the control at the right of the cell * SWT.FILL: Resize the control to fill the cell horizontally */ public int horizontalAlignment = BEGINNING; /** * widthHint specifies the preferred width in pixels. This value * is the wHint passed into IFigure#getPreferredSize(int, int, boolean) * to determine the preferred size of the control. * * The default value is SWT.DEFAULT. */ public int widthHint = SWT.DEFAULT; /** * heightHint specifies the preferred height in pixels. This value * is the hHint passed into IFigure#getPreferredSize(int, int, boolean) * to determine the preferred size of the control. * * The default value is SWT.DEFAULT. */ public int heightHint = SWT.DEFAULT; /** * horizontalIndent specifies the number of pixels of indentation * that will be placed along the left side of the cell. * * The default value is 0. */ public int horizontalIndent = 0; /** * verticalIndent specifies the number of pixels of indentation * that will be placed along the top side of the cell. * * The default value is 0. * * @since 3.1 */ public int verticalIndent = 0; /** * horizontalSpan specifies the number of column cells that the control * will take up. * * The default value is 1. */ public int horizontalSpan = 1; /** * verticalSpan specifies the number of row cells that the control * will take up. * * The default value is 1. */ public int verticalSpan = 1; /** *

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.

*/ 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.

*/ public boolean grabExcessVerticalSpace = false; /** * 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 IFigure#getPreferredSize(int, int, boolean) where wHint is * determined by GridData#widthHint. * * The default value is 0. * * @since 3.1 */ public int minimumWidth = 0; /** * 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 IFigure#getPreferredSize(int, int, boolean) where hHint is * determined by GridData#heightHint. * * The default value is 0. * * @since 3.1 */ public int minimumHeight = 0; /** * 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, cacheHeight = -1; int defaultWhint, defaultHhint, defaultWidth = -1, defaultHeight = -1; int currentWhint, currentHhint, currentWidth = -1, currentHeight = -1; }