KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > TableLayout


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jface.viewers;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.jface.layout.TableColumnLayout;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Item;
22 import org.eclipse.swt.widgets.Layout;
23 import org.eclipse.swt.widgets.Table;
24 import org.eclipse.swt.widgets.TableColumn;
25 import org.eclipse.swt.widgets.Tree;
26 import org.eclipse.swt.widgets.TreeColumn;
27
28 /**
29  * A layout for a table. Call <code>addColumnData</code> to add columns.
30  * The TableLayout {@link ColumnLayoutData} is only valid until the table
31  * is resized. To keep the proportions constant when the table is resized
32  * see {@link TableColumnLayout}
33  */

34 public class TableLayout extends Layout {
35
36     /**
37      * The number of extra pixels taken as horizontal trim by the table column.
38      * To ensure there are N pixels available for the content of the column,
39      * assign N+COLUMN_TRIM for the column width.
40      *
41      * @since 3.1
42      */

43     private static int COLUMN_TRIM = "carbon".equals(SWT.getPlatform()) ? 24 : 3; //$NON-NLS-1$
44

45     /**
46      * The list of column layout data (element type:
47      * <code>ColumnLayoutData</code>).
48      */

49     private List JavaDoc columns = new ArrayList JavaDoc();
50
51     /**
52      * Indicates whether <code>layout</code> has yet to be called.
53      */

54     private boolean firstTime = true;
55
56     /**
57      * Creates a new table layout.
58      */

59     public TableLayout() {
60     }
61
62     /**
63      * Adds a new column of data to this table layout.
64      *
65      * @param data
66      * the column layout data
67      */

68     public void addColumnData(ColumnLayoutData data) {
69         columns.add(data);
70     }
71
72     /*
73      * (non-Javadoc) Method declared on Layout.
74      */

75     public Point computeSize(Composite c, int wHint, int hHint, boolean flush) {
76         if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
77             return new Point(wHint, hHint);
78         }
79
80         Table table = (Table) c;
81         // To avoid recursions.
82
table.setLayout(null);
83         // Use native layout algorithm
84
Point result = table.computeSize(wHint, hHint, flush);
85         table.setLayout(this);
86
87         int width = 0;
88         int size = columns.size();
89         for (int i = 0; i < size; ++i) {
90             ColumnLayoutData layoutData = (ColumnLayoutData) columns.get(i);
91             if (layoutData instanceof ColumnPixelData) {
92                 ColumnPixelData col = (ColumnPixelData) layoutData;
93                 width += col.width;
94                 if (col.addTrim) {
95                     width += COLUMN_TRIM;
96                 }
97             } else if (layoutData instanceof ColumnWeightData) {
98                 ColumnWeightData col = (ColumnWeightData) layoutData;
99                 width += col.minimumWidth;
100             } else {
101                 Assert.isTrue(false, "Unknown column layout data");//$NON-NLS-1$
102
}
103         }
104         if (width > result.x) {
105             result.x = width;
106         }
107         return result;
108     }
109
110     /*
111      * (non-Javadoc) Method declared on Layout.
112      */

113     public void layout(Composite c, boolean flush) {
114         // Only do initial layout. Trying to maintain proportions when resizing
115
// is too hard,
116
// causes lots of widget flicker, causes scroll bars to appear and
117
// occasionally stick around (on Windows),
118
// requires hooking column resizing as well, and may not be what the
119
// user wants anyway.
120
if (!firstTime) {
121             return;
122         }
123
124         int width = c.getClientArea().width;
125
126         // XXX: Layout is being called with an invalid value the first time
127
// it is being called on Linux. This method resets the
128
// Layout to null so we make sure we run it only when
129
// the value is OK.
130
if (width <= 1) {
131             return;
132         }
133
134         Item[] tableColumns = getColumns(c);
135         int size = Math.min(columns.size(), tableColumns.length);
136         int[] widths = new int[size];
137         int fixedWidth = 0;
138         int numberOfWeightColumns = 0;
139         int totalWeight = 0;
140
141         // First calc space occupied by fixed columns
142
for (int i = 0; i < size; i++) {
143             ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
144             if (col instanceof ColumnPixelData) {
145                 ColumnPixelData cpd = (ColumnPixelData) col;
146                 int pixels = cpd.width;
147                 if (cpd.addTrim) {
148                     pixels += COLUMN_TRIM;
149                 }
150                 widths[i] = pixels;
151                 fixedWidth += pixels;
152             } else if (col instanceof ColumnWeightData) {
153                 ColumnWeightData cw = (ColumnWeightData) col;
154                 numberOfWeightColumns++;
155                 // first time, use the weight specified by the column data,
156
// otherwise use the actual width as the weight
157
// int weight = firstTime ? cw.weight :
158
// tableColumns[i].getWidth();
159
int weight = cw.weight;
160                 totalWeight += weight;
161             } else {
162                 Assert.isTrue(false, "Unknown column layout data");//$NON-NLS-1$
163
}
164         }
165
166         // Do we have columns that have a weight
167
if (numberOfWeightColumns > 0) {
168             // Now distribute the rest to the columns with weight.
169
int rest = width - fixedWidth;
170             int totalDistributed = 0;
171             for (int i = 0; i < size; ++i) {
172                 ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
173                 if (col instanceof ColumnWeightData) {
174                     ColumnWeightData cw = (ColumnWeightData) col;
175                     // calculate weight as above
176
// int weight = firstTime ? cw.weight :
177
// tableColumns[i].getWidth();
178
int weight = cw.weight;
179                     int pixels = totalWeight == 0 ? 0 : weight * rest
180                             / totalWeight;
181                     if (pixels < cw.minimumWidth) {
182                         pixels = cw.minimumWidth;
183                     }
184                     totalDistributed += pixels;
185                     widths[i] = pixels;
186                 }
187             }
188
189             // Distribute any remaining pixels to columns with weight.
190
int diff = rest - totalDistributed;
191             for (int i = 0; diff > 0; ++i) {
192                 if (i == size) {
193                     i = 0;
194                 }
195                 ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
196                 if (col instanceof ColumnWeightData) {
197                     ++widths[i];
198                     --diff;
199                 }
200             }
201         }
202
203         firstTime = false;
204
205         for (int i = 0; i < size; i++) {
206             setWidth(tableColumns[i], widths[i]);
207         }
208     }
209
210     /**
211      * Set the width of the item.
212      *
213      * @param item
214      * @param width
215      */

216     private void setWidth(Item item, int width) {
217         if (item instanceof TreeColumn) {
218             ((TreeColumn) item).setWidth(width);
219         } else {
220             ((TableColumn) item).setWidth(width);
221         }
222
223     }
224
225     /**
226      * Return the columns for the receiver.
227      *
228      * @param composite
229      * @return Item[]
230      */

231     private Item[] getColumns(Composite composite) {
232         if (composite instanceof Tree) {
233             return ((Tree) composite).getColumns();
234         }
235         return ((Table) composite).getColumns();
236     }
237 }
238
Popular Tags