001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.util;
011:
012: import org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.swt.layout.GridData;
015: import org.eclipse.swt.widgets.Control;
016:
017: /**
018: * Helper class to layout a number of children if the composite uses a <code>GridLayout</code>.
019: * If the numbers of widgets to be layouted into one row is smaller than the number of columns
020: * defined for the grid layout the helper class assigns a corresponing value to the <code>
021: * GridData.horizontalSpan</code> field.
022: *
023: * Additionally a row layouter manages a default <code>GridData</code> object for each column.
024: * If set this grid data is used for the widget if it doesn't manage its own grid data object.
025: *
026: * Call one of the <code>perform</code> methods to assign the correct grid data objects to
027: * a set of widgets according to the number of columns passed to the layouter's constructor.
028: */
029: public class RowLayouter {
030:
031: public int spanHorizontalAlignment = -1;
032: public int spanGrabExcessHorizontalSpace = -1;
033: public int spanHorizontalSpan = -1;
034: public int spanHorizontalIndent = -1;
035: public int spanWidthHint = -1;
036:
037: public int spanVerticalAlignment = -1;
038: public int spanGrabExcessVerticalSpace = -1;
039: public int spanVerticalSpan = -1;
040: public int spanHeightHint = -1;
041:
042: private int fNumColumns;
043: private boolean fOrder;
044: private Control fLastControl;
045: private GridData[] fDefaultGridDatas = new GridData[4];
046:
047: public RowLayouter(int numColumns) {
048: this (numColumns, false);
049: }
050:
051: public RowLayouter(int numColumns, boolean order) {
052: fNumColumns = numColumns;
053: fOrder = order;
054: }
055:
056: public void setDefaultSpan() {
057: spanHorizontalAlignment = GridData.FILL;
058: spanGrabExcessHorizontalSpace = 1;
059: }
060:
061: public void perform(Control c1) {
062: perform(new Control[] { c1 }, 0);
063: }
064:
065: public void perform(Control c1, Control c2, int span) {
066: perform(new Control[] { c1, c2 }, span);
067: }
068:
069: public void perform(Control c1, Control c2, Control c3, int span) {
070: perform(new Control[] { c1, c2, c3 }, span);
071: }
072:
073: public void perform(Control[] controls, int spanColumn) {
074: int numColumns = numColumns();
075: Assert.isTrue(controls.length <= numColumns);
076: order(controls);
077: int gridIndex = 0;
078: for (int i = 0; i < controls.length; i++) {
079: Control control = controls[i];
080: GridData gd = (GridData) control.getLayoutData();
081: if (gd == null)
082: gd = getGridData(gridIndex);
083:
084: if (i == spanColumn) {
085: int span = numColumns - (controls.length - 1);
086: gridIndex += span;
087: if (gd == null)
088: gd = new GridData();
089: applyDelta(gd);
090: gd.horizontalSpan = span;
091: } else {
092: gridIndex++;
093: }
094: control.setLayoutData(gd);
095: }
096: }
097:
098: private void applyDelta(GridData gd) {
099: if (spanHorizontalAlignment != -1)
100: gd.horizontalAlignment = spanHorizontalAlignment;
101:
102: if (spanGrabExcessHorizontalSpace != -1) {
103: if (spanGrabExcessHorizontalSpace == 0)
104: gd.grabExcessHorizontalSpace = false;
105: else
106: gd.grabExcessHorizontalSpace = true;
107: }
108:
109: if (spanHorizontalSpan != -1)
110: gd.horizontalSpan = spanHorizontalSpan;
111:
112: if (spanHorizontalIndent != -1)
113: gd.horizontalIndent = spanHorizontalIndent;
114:
115: if (spanWidthHint != -1)
116: gd.widthHint = spanWidthHint;
117:
118: if (spanVerticalAlignment != -1)
119: gd.verticalAlignment = spanVerticalAlignment;
120:
121: if (spanGrabExcessVerticalSpace != -1) {
122: if (spanGrabExcessVerticalSpace == 0)
123: gd.grabExcessVerticalSpace = false;
124: else
125: gd.grabExcessVerticalSpace = true;
126: }
127:
128: if (spanVerticalSpan != -1)
129: gd.verticalSpan = spanVerticalSpan;
130:
131: if (spanHeightHint != -1)
132: gd.heightHint = spanHeightHint;
133: }
134:
135: public void setDefaultGridData(GridData gd, int index) {
136: if (index >= fDefaultGridDatas.length) {
137: GridData[] newDatas = new GridData[index + 4];
138: System.arraycopy(fDefaultGridDatas, 0, newDatas, 0,
139: fDefaultGridDatas.length);
140: fDefaultGridDatas = newDatas;
141: }
142: fDefaultGridDatas[index] = gd;
143: }
144:
145: public GridData getGridData(int index) {
146: if (index > fDefaultGridDatas.length)
147: return null;
148:
149: return cloneGridData(fDefaultGridDatas[index]);
150: }
151:
152: public int numColumns() {
153: return fNumColumns;
154: }
155:
156: protected void order(Control[] controls) {
157: if (!fOrder)
158: return;
159:
160: for (int i = 0; i < controls.length; i++) {
161: Control control = controls[i];
162: control.moveBelow(fLastControl);
163: fLastControl = control;
164: }
165: }
166:
167: protected GridData cloneGridData(GridData gd) {
168: if (gd == null)
169: return null;
170:
171: GridData result = new GridData();
172: result.horizontalAlignment = gd.horizontalAlignment;
173: result.grabExcessHorizontalSpace = gd.grabExcessHorizontalSpace;
174: result.horizontalSpan = gd.horizontalSpan;
175: result.horizontalIndent = gd.horizontalIndent;
176: result.widthHint = gd.widthHint;
177:
178: result.verticalAlignment = gd.verticalAlignment;
179: result.grabExcessVerticalSpace = gd.grabExcessVerticalSpace;
180: result.verticalSpan = gd.verticalSpan;
181: result.heightHint = gd.heightHint;
182: return result;
183: }
184: }
|