Made all bundles require a Java 1.4 EE, and backported some classescode to Java 1.4
[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
31  * since we have to layout the columns "before" the actual table gets layouted.
32  * Hence we can't use a normal 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
64          *            the column layout data
65          */
66         public void addColumnData(ColumnLayoutData data) {
67                 columns.add(data);
68         }
69
70         // ---- Helpers
71         // -------------------------------------------------------------------------------------
72
73         private Point computeTableSize(Table table) {
74                 Point result = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
75
76                 int width = 0;
77                 int size = columns.size();
78                 for (int i = 0; i < size; ++i) {
79                         ColumnLayoutData layoutData = (ColumnLayoutData) columns.get(i);
80                         if (layoutData instanceof ColumnPixelData) {
81                                 ColumnPixelData col = (ColumnPixelData) layoutData;
82                                 width += col.width;
83                         } else if (layoutData instanceof ColumnWeightData) {
84                                 ColumnWeightData col = (ColumnWeightData) layoutData;
85                                 width += col.minimumWidth;
86                         } else {
87                                 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
88                         }
89                 }
90                 if (width > result.x)
91                         result.x = width;
92                 return result;
93         }
94
95         private void layoutTable(Table table, int width, Rectangle area,
96                         boolean increase) {
97                 // XXX: Layout is being called with an invalid value the first time
98                 // it is being called on Linux. This method resets the
99                 // Layout to null so we make sure we run it only when
100                 // the value is OK.
101                 if (width <= 1)
102                         return;
103
104                 TableColumn[] tableColumns = table.getColumns();
105                 int size = Math.min(columns.size(), tableColumns.length);
106                 int[] widths = new int[size];
107                 int fixedWidth = 0;
108                 int numberOfWeightColumns = 0;
109                 int totalWeight = 0;
110
111                 // First calc space occupied by fixed columns
112                 for (int i = 0; i < size; i++) {
113                         ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
114                         if (col instanceof ColumnPixelData) {
115                                 int pixels = ((ColumnPixelData) col).width;
116                                 widths[i] = pixels;
117                                 fixedWidth += pixels;
118                         } else if (col instanceof ColumnWeightData) {
119                                 ColumnWeightData cw = (ColumnWeightData) col;
120                                 numberOfWeightColumns++;
121                                 // first time, use the weight specified by the column data,
122                                 // otherwise use the actual width as the weight
123                                 // int weight = firstTime ? cw.weight :
124                                 // tableColumns[i].getWidth();
125                                 int weight = cw.weight;
126                                 totalWeight += weight;
127                         } else {
128                                 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
129                         }
130                 }
131
132                 // Do we have columns that have a weight
133                 if (numberOfWeightColumns > 0) {
134                         // Now distribute the rest to the columns with weight.
135                         int rest = width - fixedWidth;
136                         int totalDistributed = 0;
137                         for (int i = 0; i < size; ++i) {
138                                 ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
139                                 if (col instanceof ColumnWeightData) {
140                                         ColumnWeightData cw = (ColumnWeightData) col;
141                                         // calculate weight as above
142                                         // int weight = firstTime ? cw.weight :
143                                         // tableColumns[i].getWidth();
144                                         int weight = cw.weight;
145                                         int pixels = totalWeight == 0 ? 0 : weight * rest
146                                                         / totalWeight;
147                                         if (pixels < cw.minimumWidth)
148                                                 pixels = cw.minimumWidth;
149                                         totalDistributed += pixels;
150                                         widths[i] = pixels;
151                                 }
152                         }
153
154                         // Distribute any remaining pixels to columns with weight.
155                         int diff = rest - totalDistributed;
156                         for (int i = 0; diff > 0; ++i) {
157                                 if (i == size)
158                                         i = 0;
159                                 ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
160                                 if (col instanceof ColumnWeightData) {
161                                         ++widths[i];
162                                         --diff;
163                                 }
164                         }
165                 }
166
167                 if (increase) {
168                         table.setSize(area.width, area.height);
169                 }
170                 for (int i = 0; i < size; i++) {
171                         tableColumns[i].setWidth(widths[i]);
172                 }
173                 if (!increase) {
174                         table.setSize(area.width, area.height);
175                 }
176         }
177 }