KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 public class TableLayoutComposite extends Composite {
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     private List JavaDoc columns= new ArrayList JavaDoc();
48
49     /**
50      * Creates a new <code>TableLayoutComposite</code>.
51      */

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

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

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