KickJava   Java API By Example, From Geeks To Geeks.

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


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

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