comparison org.eclipse.jface/src/org/eclipse/jface/viewers/TableLayout.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Florian Priester - bug 106059
11 * Port to the D programming language:
12 * Frank Benoit <benoit@tionex.de>
13 *******************************************************************************/
14 module org.eclipse.jface.viewers.TableLayout;
15
16 import org.eclipse.jface.viewers.ColumnLayoutData;
17 import org.eclipse.jface.viewers.ColumnPixelData;
18 import org.eclipse.jface.viewers.ColumnWeightData;
19
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Item;
25 import org.eclipse.swt.widgets.Layout;
26 import org.eclipse.swt.widgets.Table;
27 import org.eclipse.swt.widgets.TableColumn;
28 import org.eclipse.swt.widgets.Tree;
29 import org.eclipse.swt.widgets.TreeColumn;
30 import org.eclipse.core.runtime.Assert;
31 import org.eclipse.jface.layout.TableColumnLayout;
32
33 import java.lang.all;
34 import java.util.List;
35 import java.util.ArrayList;
36 import java.util.Set;
37
38 /**
39 * A layout for a table. Call <code>addColumnData</code> to add columns.
40 * The TableLayout {@link ColumnLayoutData} is only valid until the table
41 * is resized. To keep the proportions constant when the table is resized
42 * see {@link TableColumnLayout}
43 */
44 public class TableLayout : Layout {
45
46 /**
47 * The number of extra pixels taken as horizontal trim by the table column.
48 * To ensure there are N pixels available for the content of the column,
49 * assign N+COLUMN_TRIM for the column width.
50 *
51 * @since 3.1
52 */
53 private static int COLUMN_TRIM_ = -1;
54 private static int COLUMN_TRIM(){
55 if( COLUMN_TRIM_ is -1 ){
56 synchronized( TableLayout.classinfo ){
57 if( COLUMN_TRIM_ is -1 ){
58 if ("win32".equals(SWT.getPlatform())) { //$NON-NLS-1$
59 COLUMN_TRIM_ = 4;
60 } else if ("carbon".equals(SWT.getPlatform())) { //$NON-NLS-1$
61 COLUMN_TRIM_ = 24;
62 } else {
63 COLUMN_TRIM_ = 3;
64 }
65 }
66 }
67 }
68 return COLUMN_TRIM_;
69 }
70
71 /**
72 * The list of column layout data (element type:
73 * <code>ColumnLayoutData</code>).
74 */
75 private List columns;
76
77 /**
78 * Indicates whether <code>layout</code> has yet to be called.
79 */
80 private bool firstTime = true;
81
82 /**
83 * Creates a new table layout.
84 */
85 public this() {
86 columns = new ArrayList();
87 }
88
89 /**
90 * Adds a new column of data to this table layout.
91 *
92 * @param data
93 * the column layout data
94 */
95 public void addColumnData(ColumnLayoutData data) {
96 columns.add(data);
97 }
98
99 /*
100 * (non-Javadoc) Method declared on Layout.
101 */
102 public override Point computeSize(Composite c, int wHint, int hHint, bool flush) {
103 if (wHint !is SWT.DEFAULT && hHint !is SWT.DEFAULT) {
104 return new Point(wHint, hHint);
105 }
106
107 Table table = cast(Table) c;
108 // To avoid recursions.
109 table.setLayout(null);
110 // Use native layout algorithm
111 Point result = table.computeSize(wHint, hHint, flush);
112 table.setLayout(this);
113
114 int width = 0;
115 int size = columns.size();
116 for (int i = 0; i < size; ++i) {
117 ColumnLayoutData layoutData = cast(ColumnLayoutData) columns.get(i);
118 if ( auto col = cast(ColumnPixelData) layoutData ) {
119 width += col.width;
120 if (col.addTrim) {
121 width += COLUMN_TRIM;
122 }
123 } else if ( auto col = cast(ColumnWeightData) layoutData ) {
124 width += col.minimumWidth;
125 } else {
126 Assert.isTrue(false, "Unknown column layout data");//$NON-NLS-1$
127 }
128 }
129 if (width > result.x) {
130 result.x = width;
131 }
132 return result;
133 }
134
135 /*
136 * (non-Javadoc) Method declared on Layout.
137 */
138 public override void layout(Composite c, bool flush) {
139 // Only do initial layout. Trying to maintain proportions when resizing
140 // is too hard,
141 // causes lots of widget flicker, causes scroll bars to appear and
142 // occasionally stick around (on Windows),
143 // requires hooking column resizing as well, and may not be what the
144 // user wants anyway.
145 if (!firstTime) {
146 return;
147 }
148
149 int width = c.getClientArea().width;
150
151 // XXX: Layout is being called with an invalid value the first time
152 // it is being called on Linux. This method resets the
153 // Layout to null so we make sure we run it only when
154 // the value is OK.
155 if (width <= 1) {
156 return;
157 }
158
159 Item[] tableColumns = getColumns(c);
160 int size = Math.min(columns.size(), tableColumns.length);
161 int[] widths = new int[size];
162 int fixedWidth = 0;
163 int numberOfWeightColumns = 0;
164 int totalWeight = 0;
165
166 // First calc space occupied by fixed columns
167 for (int i = 0; i < size; i++) {
168 ColumnLayoutData col = cast(ColumnLayoutData) columns.get(i);
169 if ( auto cpd = cast(ColumnPixelData) col ) {
170 int pixels = cpd.width;
171 if (cpd.addTrim) {
172 pixels += COLUMN_TRIM;
173 }
174 widths[i] = pixels;
175 fixedWidth += pixels;
176 } else if ( auto cw = cast(ColumnWeightData) col ) {
177 numberOfWeightColumns++;
178 // first time, use the weight specified by the column data,
179 // otherwise use the actual width as the weight
180 // int weight = firstTime ? cw.weight :
181 // tableColumns[i].getWidth();
182 int weight = cw.weight;
183 totalWeight += weight;
184 } else {
185 Assert.isTrue(false, "Unknown column layout data");//$NON-NLS-1$
186 }
187 }
188
189 // Do we have columns that have a weight
190 if (numberOfWeightColumns > 0) {
191 // Now distribute the rest to the columns with weight.
192 int rest = width - fixedWidth;
193 int totalDistributed = 0;
194 for (int i = 0; i < size; ++i) {
195 ColumnLayoutData col = cast(ColumnLayoutData) columns.get(i);
196 if (auto cw = cast(ColumnWeightData) col ) {
197 // calculate weight as above
198 // int weight = firstTime ? cw.weight :
199 // tableColumns[i].getWidth();
200 int weight = cw.weight;
201 int pixels = totalWeight is 0 ? 0 : weight * rest
202 / totalWeight;
203 if (pixels < cw.minimumWidth) {
204 pixels = cw.minimumWidth;
205 }
206 totalDistributed += pixels;
207 widths[i] = pixels;
208 }
209 }
210
211 // Distribute any remaining pixels to columns with weight.
212 int diff = rest - totalDistributed;
213 for (int i = 0; diff > 0; ++i) {
214 if (i is size) {
215 i = 0;
216 }
217 ColumnLayoutData col = cast(ColumnLayoutData) columns.get(i);
218 if (cast(ColumnWeightData)col ) {
219 ++widths[i];
220 --diff;
221 }
222 }
223 }
224
225 firstTime = false;
226
227 for (int i = 0; i < size; i++) {
228 setWidth(tableColumns[i], widths[i]);
229 }
230 }
231
232 /**
233 * Set the width of the item.
234 *
235 * @param item
236 * @param width
237 */
238 private void setWidth(Item item, int width) {
239 if ( cast(TreeColumn)item ) {
240 (cast(TreeColumn) item).setWidth(width);
241 } else {
242 (cast(TableColumn) item).setWidth(width);
243 }
244
245 }
246
247 /**
248 * Return the columns for the receiver.
249 *
250 * @param composite
251 * @return Item[]
252 */
253 private Item[] getColumns(Composite composite) {
254 if (cast(Tree)composite ) {
255 return (cast(Tree) composite).getColumns();
256 }
257 return (cast(Table) composite).getColumns();
258 }
259 }