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