KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > TableLayoutComposite


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.update.internal.ui.wizards;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ControlAdapter;
18 import org.eclipse.swt.events.ControlEvent;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.TableColumn;
24 import org.eclipse.core.runtime.Assert;
25 import org.eclipse.jface.viewers.ColumnLayoutData;
26 import org.eclipse.jface.viewers.ColumnPixelData;
27 import org.eclipse.jface.viewers.ColumnWeightData;
28
29 /**
30  * A special composite to layout columns inside a table. The composite is needed since we have
31  * to layout the columns "before" the actual table gets layouted. Hence we can't use a normal
32  * layout manager.
33  */

34 public class TableLayoutComposite extends Composite {
35
36     private List JavaDoc columns= new ArrayList JavaDoc();
37     
38     /**
39      * The number of extra pixels taken as horizontal trim by the table column.
40      * To ensure there are N pixels available for the content of the column,
41      * assign N+COLUMN_TRIM for the column width.
42      *
43      * @since 3.1
44      */

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

47     /**
48      * Creates a new <code>TableLayoutComposite</code>.
49      */

50     public TableLayoutComposite(Composite parent, int style) {
51         super(parent, style);
52         addControlListener(new ControlAdapter() {
53             public void controlResized(ControlEvent e) {
54                 Rectangle area= getClientArea();
55                 Table table= (Table)getChildren()[0];
56                 Point preferredSize= computeTableSize(table);
57                 int width= area.width - 2 * table.getBorderWidth();
58                 if (preferredSize.y > area.height) {
59                     // Subtract the scrollbar width from the total column width
60
// if a vertical scrollbar will be required
61
Point vBarSize = table.getVerticalBar().getSize();
62                     width -= vBarSize.x;
63                 }
64                 layoutTable(table, width, area, table.getSize().x < area.width);
65             }
66         });
67     }
68     
69     /**
70      * Adds a new column of data to this table layout.
71      *
72      * @param data the column layout data
73      */

74     public void addColumnData(ColumnLayoutData data) {
75         columns.add(data);
76     }
77     
78     //---- Helpers -------------------------------------------------------------------------------------
79

80     private Point computeTableSize(Table table) {
81         Point result= table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
82         
83         int width= 0;
84         int size= columns.size();
85         for (int i= 0; i < size; ++i) {
86             ColumnLayoutData layoutData= (ColumnLayoutData) columns.get(i);
87             if (layoutData instanceof ColumnPixelData) {
88                 ColumnPixelData col= (ColumnPixelData) layoutData;
89                 width += col.width;
90                 if (col.addTrim) {
91                     width += COLUMN_TRIM;
92                 }
93             } else if (layoutData instanceof ColumnWeightData) {
94                 ColumnWeightData col= (ColumnWeightData) layoutData;
95                 width += col.minimumWidth;
96             } else {
97                 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
98
}
99         }
100         if (width > result.x)
101             result.x= width;
102         return result;
103     }
104     
105     private void layoutTable(Table table, int width, Rectangle area, boolean increase) {
106         // XXX: Layout is being called with an invalid value the first time
107
// it is being called on Linux. This method resets the
108
// Layout to null so we make sure we run it only when
109
// the value is OK.
110
if (width <= 1)
111             return;
112
113         TableColumn[] tableColumns= table.getColumns();
114         int size= Math.min(columns.size(), tableColumns.length);
115         int[] widths= new int[size];
116         int fixedWidth= 0;
117         int numberOfWeightColumns= 0;
118         int totalWeight= 0;
119
120         // First calc space occupied by fixed columns
121
for (int i= 0; i < size; i++) {
122             ColumnLayoutData col= (ColumnLayoutData) columns.get(i);
123             if (col instanceof ColumnPixelData) {
124                 ColumnPixelData cpd = (ColumnPixelData) col;
125                 int pixels = cpd.width;
126                 if (cpd.addTrim) {
127                     pixels += COLUMN_TRIM;
128                 }
129                 widths[i]= pixels;
130                 fixedWidth += pixels;
131             } else if (col instanceof ColumnWeightData) {
132                 ColumnWeightData cw= (ColumnWeightData) col;
133                 numberOfWeightColumns++;
134                 // first time, use the weight specified by the column data, otherwise use the actual width as the weight
135
// int weight = firstTime ? cw.weight : tableColumns[i].getWidth();
136
int weight= cw.weight;
137                 fixedWidth+=cw.minimumWidth;
138                 totalWeight += weight;
139             } else {
140                 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
141
}
142         }
143
144         // Do we have columns that have a weight
145
if (numberOfWeightColumns > 0) {
146             // Now distribute the rest to the columns with weight.
147
int rest= width - fixedWidth;
148             int totalDistributed= 0;
149             for (int i= 0; i < size; ++i) {
150                 ColumnLayoutData col= (ColumnLayoutData) columns.get(i);
151                 if (col instanceof ColumnWeightData) {
152                     ColumnWeightData cw= (ColumnWeightData) col;
153                     // calculate weight as above
154
// int weight = firstTime ? cw.weight : tableColumns[i].getWidth();
155
int weight= cw.weight;
156                     int sparePixels= totalWeight == 0 ? 0 : weight * rest / totalWeight;
157                     int newWidth = cw.minimumWidth;
158                     if (sparePixels >0 )
159                         newWidth += sparePixels;
160                     totalDistributed += newWidth;
161                     widths[i]= newWidth;
162                 }
163             }
164
165             // Distribute any remaining pixels to columns with weight.
166
int diff= rest - totalDistributed;
167             for (int i= 0; diff > 0; ++i) {
168                 if (i == size)
169                     i= 0;
170                 ColumnLayoutData col= (ColumnLayoutData) columns.get(i);
171                 if (col instanceof ColumnWeightData) {
172                     ++widths[i];
173                     --diff;
174                 }
175             }
176         }
177         
178         if (increase) {
179             table.setSize(area.width, area.height);
180         }
181         for (int i= 0; i < size; i++) {
182             tableColumns[i].setWidth(widths[i]);
183         }
184         if (!increase) {
185             table.setSize(area.width, area.height);
186         }
187     }
188 }
189
Popular Tags