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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.util;
13 import java.util.ArrayList;
14 import java.util.List;
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;
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.
34 public class TableLayoutComposite extends Composite {
36 private List columns = new ArrayList();
39 * Creates a new <code>TableLayoutComposite</code>.
41 public TableLayoutComposite(Composite parent, int 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();
55 layoutTable(table, width, area, table.getSize().x < area.width);
61 * Adds a new column of data to this table layout.
64 * the column layout data
66 public void addColumnData(ColumnLayoutData data) {
71 // -------------------------------------------------------------------------------------
73 private Point computeTableSize(Table table) {
74 Point result = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
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;
83 } else if (layoutData instanceof ColumnWeightData) {
84 ColumnWeightData col = (ColumnWeightData) layoutData;
85 width += col.minimumWidth;
87 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
95 private void layoutTable(Table table, int width, Rectangle area,
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
104 TableColumn[] tableColumns = table.getColumns();
105 int size = Math.min(columns.size(), tableColumns.length);
106 int[] widths = new int[size];
108 int numberOfWeightColumns = 0;
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;
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;
128 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
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
147 if (pixels < cw.minimumWidth)
148 pixels = cw.minimumWidth;
149 totalDistributed += pixels;
154 // Distribute any remaining pixels to columns with weight.
155 int diff = rest - totalDistributed;
156 for (int i = 0; diff > 0; ++i) {
159 ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
160 if (col instanceof ColumnWeightData) {
168 table.setSize(area.width, area.height);
170 for (int i = 0; i < size; i++) {
171 tableColumns[i].setWidth(widths[i]);
174 table.setSize(area.width, area.height);