KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > RowLayouter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.util;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.widgets.Control;
17
18
19 /**
20  * Helper class to layout a number of children if the composite uses a <code>GridLayout</code>.
21  * If the numbers of widgets to be layouted into one row is smaller than the number of columns
22  * defined for the grid layout the helper class assigns a corresponing value to the <code>
23  * GridData.horizontalSpan</code> field.
24  *
25  * Additionally a row layouter manages a default <code>GridData</code> object for each column.
26  * If set this grid data is used for the widget if it doesn't manage its own grid data object.
27  *
28  * Call one of the <code>perform</code> methods to assign the correct grid data objects to
29  * a set of widgets according to the number of columns passed to the layouter's constructor.
30  */

31 public class RowLayouter {
32
33     public int spanHorizontalAlignment= -1;
34     public int spanGrabExcessHorizontalSpace= -1;
35     public int spanHorizontalSpan= -1;
36     public int spanHorizontalIndent= -1;
37     public int spanWidthHint= -1;
38         
39     public int spanVerticalAlignment= -1;
40     public int spanGrabExcessVerticalSpace= -1;
41     public int spanVerticalSpan= -1;
42     public int spanHeightHint= -1;
43     
44     private int fNumColumns;
45     private boolean fOrder;
46     private Control fLastControl;
47     private GridData[] fDefaultGridDatas= new GridData[4];
48         
49     public RowLayouter(int numColumns) {
50         this(numColumns, false);
51     }
52
53     public RowLayouter(int numColumns, boolean order) {
54         fNumColumns= numColumns;
55         fOrder= order;
56     }
57     
58     public void setDefaultSpan() {
59         spanHorizontalAlignment= GridData.FILL;
60         spanGrabExcessHorizontalSpace= 1;
61     }
62     
63     public void perform(Control c1) {
64         perform(new Control[] {c1}, 0);
65     }
66     
67     public void perform(Control c1, Control c2, int span) {
68         perform(new Control[] {c1, c2}, span);
69     }
70     
71     public void perform(Control c1, Control c2, Control c3, int span) {
72         perform(new Control[] {c1, c2, c3}, span);
73     }
74     
75     public void perform(Control[] controls, int spanColumn) {
76         int numColumns= numColumns();
77         Assert.isTrue(controls.length <= numColumns);
78         order(controls);
79         int gridIndex= 0;
80         for (int i= 0; i < controls.length; i++) {
81             Control control= controls[i];
82             GridData gd= (GridData)control.getLayoutData();
83             if (gd == null)
84                 gd= getGridData(gridIndex);
85                 
86             if (i == spanColumn) {
87                 int span= numColumns - (controls.length - 1);
88                 gridIndex+= span;
89                 if (gd == null)
90                     gd= new GridData();
91                 applyDelta(gd);
92                 gd.horizontalSpan= span;
93             } else {
94                 gridIndex++;
95             }
96             control.setLayoutData(gd);
97         }
98     }
99     
100     private void applyDelta(GridData gd) {
101         if (spanHorizontalAlignment != -1)
102             gd.horizontalAlignment= spanHorizontalAlignment;
103             
104         if (spanGrabExcessHorizontalSpace != -1) {
105             if (spanGrabExcessHorizontalSpace == 0)
106                 gd.grabExcessHorizontalSpace= false;
107             else
108                 gd.grabExcessHorizontalSpace= true;
109         }
110                 
111             
112         if (spanHorizontalSpan != -1)
113             gd.horizontalSpan= spanHorizontalSpan;
114             
115         if (spanHorizontalIndent != -1)
116             gd.horizontalIndent= spanHorizontalIndent;
117         
118         if (spanWidthHint != -1)
119             gd.widthHint= spanWidthHint;
120             
121         if (spanVerticalAlignment != -1)
122             gd.verticalAlignment= spanVerticalAlignment;
123             
124         if (spanGrabExcessVerticalSpace != -1) {
125             if (spanGrabExcessVerticalSpace == 0)
126                 gd.grabExcessVerticalSpace= false;
127             else
128                 gd.grabExcessVerticalSpace= true;
129         }
130             
131         if (spanVerticalSpan != -1)
132             gd.verticalSpan= spanVerticalSpan;
133             
134         if (spanHeightHint != -1)
135             gd.heightHint= spanHeightHint;
136     }
137     public void setDefaultGridData(GridData gd, int index) {
138         if (index >= fDefaultGridDatas.length) {
139             GridData[] newDatas= new GridData[index + 4];
140             System.arraycopy(fDefaultGridDatas, 0, newDatas, 0, fDefaultGridDatas.length);
141             fDefaultGridDatas= newDatas;
142         }
143         fDefaultGridDatas[index]= gd;
144     }
145     
146     public GridData getGridData(int index) {
147         if (index > fDefaultGridDatas.length)
148             return null;
149             
150         return cloneGridData(fDefaultGridDatas[index]);
151     }
152     
153     public int numColumns() {
154         return fNumColumns;
155     }
156     
157     protected void order(Control[] controls) {
158         if (!fOrder)
159             return;
160             
161         for (int i= 0; i < controls.length; i++) {
162             Control control= controls[i];
163             control.moveBelow(fLastControl);
164             fLastControl= control;
165         }
166     }
167     
168     protected GridData cloneGridData(GridData gd) {
169         if (gd == null)
170             return null;
171             
172         GridData result= new GridData();
173         result.horizontalAlignment= gd.horizontalAlignment;
174         result.grabExcessHorizontalSpace= gd.grabExcessHorizontalSpace;
175         result.horizontalSpan= gd.horizontalSpan;
176         result.horizontalIndent= gd.horizontalIndent;
177         result.widthHint= gd.widthHint;
178         
179         result.verticalAlignment= gd.verticalAlignment;
180         result.grabExcessVerticalSpace= gd.grabExcessVerticalSpace;
181         result.verticalSpan= gd.verticalSpan;
182         result.heightHint= gd.heightHint;
183         return result;
184     }
185 }
186
Popular Tags