Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / util / TableLayoutComposite.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 net.sourceforge.phpdt.internal.ui.util;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.eclipse.jface.util.Assert;
17 import org.eclipse.jface.viewers.ColumnLayoutData;
18 import org.eclipse.jface.viewers.ColumnPixelData;
19 import org.eclipse.jface.viewers.ColumnWeightData;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.ControlAdapter;
22 import org.eclipse.swt.events.ControlEvent;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Table;
27 import org.eclipse.swt.widgets.TableColumn;
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 columns= new ArrayList();
37
38         /**
39          * Creates a new <code>TableLayoutComposite</code>.
40          */
41         public TableLayoutComposite(Composite parent, int style) {
42                 super(parent, style);
43         addControlListener(new ControlAdapter() {
44             public void controlResized(ControlEvent e) {
45                 Rectangle area= getClientArea();
46                 Table table= (Table)getChildren()[0];
47                 Point preferredSize= computeTableSize(table);
48                 int width= area.width - 2 * table.getBorderWidth();
49                 if (preferredSize.y > area.height) {
50                     // Subtract the scrollbar width from the total column width
51                     // if a vertical scrollbar will be required
52                     Point vBarSize = table.getVerticalBar().getSize();
53                     width -= vBarSize.x;
54                 }
55                 layoutTable(table, width, area, table.getSize().x < area.width);
56             }
57         });
58         }
59         
60         /**
61          * Adds a new column of data to this table layout.
62          *
63          * @param data the column layout data
64          */
65         public void addColumnData(ColumnLayoutData data) {
66                 columns.add(data);
67         }
68         
69         //---- Helpers -------------------------------------------------------------------------------------
70         
71         private Point computeTableSize(Table table) {
72                 Point result= table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
73                 
74                 int width= 0;
75                 int size= columns.size();
76                 for (int i= 0; i < size; ++i) {
77                         ColumnLayoutData layoutData= (ColumnLayoutData) columns.get(i);
78                         if (layoutData instanceof ColumnPixelData) {
79                                 ColumnPixelData col= (ColumnPixelData) layoutData;
80                                 width += col.width;
81                         } else if (layoutData instanceof ColumnWeightData) {
82                                 ColumnWeightData col= (ColumnWeightData) layoutData;
83                                 width += col.minimumWidth;
84                         } else {
85                                 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
86                         }
87                 }
88                 if (width > result.x)
89                         result.x= width;
90                 return result;
91         }
92         
93         private void layoutTable(Table table, int width, Rectangle area, boolean increase) {
94                 // XXX: Layout is being called with an invalid value the first time
95                 // it is being called on Linux. This method resets the
96                 // Layout to null so we make sure we run it only when
97                 // the value is OK.
98                 if (width <= 1)
99                         return;
100
101                 TableColumn[] tableColumns= table.getColumns();
102                 int size= Math.min(columns.size(), tableColumns.length);
103                 int[] widths= new int[size];
104                 int fixedWidth= 0;
105                 int numberOfWeightColumns= 0;
106                 int totalWeight= 0;
107
108                 // First calc space occupied by fixed columns
109                 for (int i= 0; i < size; i++) {
110                         ColumnLayoutData col= (ColumnLayoutData) columns.get(i);
111                         if (col instanceof ColumnPixelData) {
112                                 int pixels= ((ColumnPixelData) col).width;
113                                 widths[i]= pixels;
114                                 fixedWidth += pixels;
115                         } else if (col instanceof ColumnWeightData) {
116                                 ColumnWeightData cw= (ColumnWeightData) col;
117                                 numberOfWeightColumns++;
118                                 // first time, use the weight specified by the column data, otherwise use the actual width as the weight
119                                 // int weight = firstTime ? cw.weight : tableColumns[i].getWidth();
120                                 int weight= cw.weight;
121                                 totalWeight += weight;
122                         } else {
123                                 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
124                         }
125                 }
126
127                 // Do we have columns that have a weight
128                 if (numberOfWeightColumns > 0) {
129                         // Now distribute the rest to the columns with weight.
130                         int rest= width - fixedWidth;
131                         int totalDistributed= 0;
132                         for (int i= 0; i < size; ++i) {
133                                 ColumnLayoutData col= (ColumnLayoutData) columns.get(i);
134                                 if (col instanceof ColumnWeightData) {
135                                         ColumnWeightData cw= (ColumnWeightData) col;
136                                         // calculate weight as above
137                                         // int weight = firstTime ? cw.weight : tableColumns[i].getWidth();
138                                         int weight= cw.weight;
139                                         int pixels= totalWeight == 0 ? 0 : weight * rest / totalWeight;
140                                         if (pixels < cw.minimumWidth)
141                                                 pixels= cw.minimumWidth;
142                                         totalDistributed += pixels;
143                                         widths[i]= pixels;
144                                 }
145                         }
146
147                         // Distribute any remaining pixels to columns with weight.
148                         int diff= rest - totalDistributed;
149                         for (int i= 0; diff > 0; ++i) {
150                                 if (i == size)
151                                         i= 0;
152                                 ColumnLayoutData col= (ColumnLayoutData) columns.get(i);
153                                 if (col instanceof ColumnWeightData) {
154                                         ++widths[i];
155                                         --diff;
156                                 }
157                         }
158                 }
159                 
160                 if (increase) {
161                         table.setSize(area.width, area.height);
162                 }
163                 for (int i= 0; i < size; i++) {
164                         tableColumns[i].setWidth(widths[i]);
165                 }
166                 if (!increase) {
167                         table.setSize(area.width, area.height);
168                 }
169         }
170 }