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