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;
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;
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.
36 public class TableLayoutComposite extends Composite {
38 private List columns = new ArrayList();
41 * Creates a new <code>TableLayoutComposite</code>.
43 public TableLayoutComposite(Composite parent, int 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();
57 layoutTable(table, width, area, table.getSize().x < area.width);
63 * Adds a new column of data to this table layout.
66 * the column layout data
68 public void addColumnData(ColumnLayoutData data) {
73 // -------------------------------------------------------------------------------------
75 private Point computeTableSize(Table table) {
76 Point result = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
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;
85 } else if (layoutData instanceof ColumnWeightData) {
86 ColumnWeightData col = (ColumnWeightData) layoutData;
87 width += col.minimumWidth;
89 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
97 private void layoutTable(Table table, int width, Rectangle area,
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
106 TableColumn[] tableColumns = table.getColumns();
107 int size = Math.min(columns.size(), tableColumns.length);
108 int[] widths = new int[size];
110 int numberOfWeightColumns = 0;
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;
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;
130 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
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
149 if (pixels < cw.minimumWidth)
150 pixels = cw.minimumWidth;
151 totalDistributed += pixels;
156 // Distribute any remaining pixels to columns with weight.
157 int diff = rest - totalDistributed;
158 for (int i = 0; diff > 0; ++i) {
161 ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
162 if (col instanceof ColumnWeightData) {
170 table.setSize(area.width, area.height);
172 for (int i = 0; i < size; i++) {
173 tableColumns[i].setWidth(widths[i]);
176 table.setSize(area.width, area.height);