comparison org.eclipse.jface/src/org/eclipse/jface/internal/ConfigureColumnsDialog.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) 2008 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 ******************************************************************************/
13
14 module org.eclipse.jface.internal.ConfigureColumnsDialog;
15
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Event;
23 import org.eclipse.swt.widgets.Item;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Listener;
26 import org.eclipse.swt.widgets.Table;
27 import org.eclipse.swt.widgets.TableColumn;
28 import org.eclipse.swt.widgets.TableItem;
29 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.swt.widgets.Tree;
31 import org.eclipse.swt.widgets.TreeColumn;
32 import org.eclipse.jface.dialogs.Dialog;
33 import org.eclipse.jface.layout.GridDataFactory;
34 import org.eclipse.jface.layout.GridLayoutFactory;
35 import org.eclipse.jface.resource.JFaceResources;
36 import org.eclipse.jface.window.IShellProvider;
37
38 import java.lang.all;
39 import java.util.Set;
40
41 /**
42 * NON-API - This class is internal and will be moved to another package in 3.5.
43 *
44 */
45 public class ConfigureColumnsDialog : Dialog {
46
47 private Control targetControl;
48 private ColumnObject[] columnObjects;
49 private Table table;
50 private Button upButton;
51 private Button downButton;
52 private Text text;
53 private bool moveableColumnsFound;
54
55 class ColumnObject {
56 Item column;
57 int index;
58 String name;
59 Image image;
60 bool visible;
61 int width;
62 bool moveable;
63 bool resizable;
64
65 this(Item column, int index, String text, Image image,
66 int width, bool moveable, bool resizable, bool visible) {
67 this.column = column;
68 this.index = index;
69 this.name = text;
70 this.image = image;
71 this.width = width;
72 this.moveable = moveable;
73 this.resizable = resizable;
74 this.visible = visible;
75 }
76 }
77
78 /**
79 * NON-API - This class is internal and will be moved to another package in
80 * 3.5. Creates a new dialog for configuring columns of the given column
81 * viewer. The column viewer must have an underlying {@link Tree} or {@link
82 * Table}, other controls are not supported.
83 *
84 * @param shellProvider
85 * @param table
86 */
87 public this(IShellProvider shellProvider, Table table) {
88 this(shellProvider, cast(Control) table);
89 }
90
91 /**
92 * NON-API - This class is internal and will be moved to another package in
93 * 3.5. Creates a new dialog for configuring columns of the given column
94 * viewer. The column viewer must have an underlying {@link Tree} or {@link
95 * Table}, other controls are not supported.
96 *
97 * @param shellProvider
98 * @param tree
99 */
100 public this(IShellProvider shellProvider, Tree tree) {
101 this(shellProvider, cast(Control) tree);
102 }
103
104 /**
105 * @param shellProvider
106 * @param control
107 */
108 private this(IShellProvider shellProvider, Control control) {
109 super(shellProvider);
110 this.targetControl = control;
111 this.moveableColumnsFound = createColumnObjects();
112 }
113
114 protected bool isResizable() {
115 return true;
116 }
117
118 public void create() {
119 super.create();
120 getShell().setText(
121 JFaceResources.getString("ConfigureColumnsDialog_Title")); //$NON-NLS-1$
122 }
123
124 protected void initializeBounds() {
125 super.initializeBounds();
126 table.setSelection(0);
127 handleSelectionChanged(0);
128 }
129
130 /**
131 * Returns true if any of the columns is moveable (can be reordered).
132 */
133 private bool createColumnObjects() {
134 bool result = true;
135 Item[] columns = getViewerColumns();
136 ColumnObject[] cObjects = new ColumnObject[columns.length];
137 for (int i = 0; i < columns.length; i++) {
138 Item c = columns[i];
139 bool moveable = getMoveable(c);
140 result = result && moveable;
141 cObjects[i] = new ColumnObject(c, i, getColumnName(c),
142 getColumnImage(c), getColumnWidth(c), moveable,
143 getResizable(c), true);
144 }
145 int[] columnOrder = getColumnOrder();
146 columnObjects = new ColumnObject[columns.length];
147 for (int i = 0; i < columnOrder.length; i++) {
148 columnObjects[i] = cObjects[columnOrder[i]];
149 }
150 return result;
151 }
152
153 /**
154 * @param c
155 * @return
156 */
157 private Image getColumnImage(Item item) {
158 if (null !is cast(TableColumn)item ) {
159 return (cast(TableColumn) item).getImage();
160 } else if (null !is cast(TreeColumn)item ) {
161 return (cast(TreeColumn) item).getImage();
162 }
163 return null;
164 }
165
166 /**
167 * @return
168 */
169 private int[] getColumnOrder() {
170 if (null !is cast(Table)targetControl ) {
171 return (cast(Table) targetControl).getColumnOrder();
172 } else if (null !is cast(Tree)targetControl ) {
173 return (cast(Tree) targetControl).getColumnOrder();
174 }
175 return new int[0];
176 }
177
178 /**
179 * @param c
180 * @return
181 */
182 private bool getMoveable(Item item) {
183 if (null !is cast(TableColumn)item ) {
184 return (cast(TableColumn) item).getMoveable();
185 } else if (null !is cast(TreeColumn)item ) {
186 return (cast(TreeColumn) item).getMoveable();
187 }
188 return false;
189 }
190
191 /**
192 * @param c
193 * @return
194 */
195 private bool getResizable(Item item) {
196 if (null !is cast(TableColumn)item ) {
197 return (cast(TableColumn) item).getResizable();
198 } else if (null !is cast(TreeColumn)item ) {
199 return (cast(TreeColumn) item).getResizable();
200 }
201 return false;
202 }
203
204 protected Control createDialogArea(Composite parent) {
205 Composite composite = cast(Composite) super.createDialogArea(parent);
206
207 table = new Table(composite, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL
208 | SWT.H_SCROLL /*
209 * | SWT.CHECK
210 */);
211 for (int i = 0; i < columnObjects.length; i++) {
212 TableItem tableItem = new TableItem(table, SWT.NONE);
213 tableItem.setText(columnObjects[i].name);
214 tableItem.setImage(columnObjects[i].image);
215 tableItem.setData(columnObjects[i]);
216 }
217
218 GridDataFactory.defaultsFor(table)
219 .span(1, moveableColumnsFound ? 3 : 1).applyTo(table);
220
221 if (moveableColumnsFound) {
222 upButton = new Button(composite, SWT.PUSH);
223 upButton.setText(JFaceResources
224 .getString("ConfigureColumnsDialog_up")); //$NON-NLS-1$
225 upButton.addListener(SWT.Selection, new class Listener {
226 public void handleEvent(Event event) {
227 handleMove(table, true);
228 }
229 });
230 setButtonLayoutData(upButton);
231 downButton = new Button(composite, SWT.PUSH);
232 downButton.setText(JFaceResources
233 .getString("ConfigureColumnsDialog_down")); //$NON-NLS-1$
234 downButton.addListener(SWT.Selection, new class Listener {
235 public void handleEvent(Event event) {
236 handleMove(table, false);
237 }
238 });
239 setButtonLayoutData(downButton);
240
241 // filler label
242 createLabel(composite, ""); //$NON-NLS-1$
243 }
244
245 Composite widthComposite = new Composite(composite, SWT.NONE);
246 createLabel(widthComposite, JFaceResources
247 .getString("ConfigureColumnsDialog_WidthOfSelectedColumn")); //$NON-NLS-1$
248
249 text = new Text(widthComposite, SWT.SINGLE | SWT.BORDER);
250 // see #initializeBounds
251 text.setText(Integer.toString(1000));
252
253 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(widthComposite);
254
255 int numColumns = moveableColumnsFound ? 2 : 1;
256
257 GridDataFactory.defaultsFor(widthComposite).grab(false, false).span(
258 numColumns, 1).applyTo(widthComposite);
259
260 GridLayoutFactory.swtDefaults().numColumns(numColumns).applyTo(
261 composite);
262
263 table.addListener(SWT.Selection, new class Listener {
264 public void handleEvent(Event event) {
265 handleSelectionChanged(table.indexOf(cast(TableItem) event.item));
266 }
267 });
268 text.addListener(SWT.Modify, new class Listener {
269 public void handleEvent(Event event) {
270 ColumnObject columnObject = columnObjects[table
271 .getSelectionIndex()];
272 if (!columnObject.resizable) {
273 return;
274 }
275 try {
276 int width = Integer.parseInt(text.getText());
277 columnObject.width = width;
278 } catch (NumberFormatException ex) {
279 // ignore for now
280 }
281 }
282 });
283
284 Dialog.applyDialogFont(composite);
285
286 return composite;
287 }
288
289 /**
290 * @param table
291 * @param up
292 */
293 protected void handleMove(Table table, bool up) {
294 int index = table.getSelectionIndex();
295 int newIndex = index + (up ? -1 : 1);
296 if (index < 0 || index >= table.getItemCount()) {
297 return;
298 }
299 ColumnObject columnObject = columnObjects[index];
300 columnObjects[index] = columnObjects[newIndex];
301 columnObjects[newIndex] = columnObject;
302 table.getItem(index).dispose();
303 TableItem newItem = new TableItem(table, SWT.NONE, newIndex);
304 newItem.setText(columnObject.name);
305 newItem.setImage(columnObject.image);
306 newItem.setData(columnObject);
307 table.setSelection(newIndex);
308 handleSelectionChanged(newIndex);
309 }
310
311 private void createLabel(Composite composite, String string) {
312 Label label = new Label(composite, SWT.NONE);
313 label.setText(string);
314 }
315
316 /**
317 * @param item
318 * @return
319 */
320 private String getColumnName(Item item) {
321 String result = ""; //$NON-NLS-1$
322 if (null !is cast(TableColumn)item ) {
323 result = (cast(TableColumn) item).getText();
324 if (result.trim().equals("")) { //$NON-NLS-1$
325 result = (cast(TableColumn) item).getToolTipText();
326 }
327 } else if (null !is cast(TreeColumn)item ) {
328 result = (cast(TreeColumn) item).getText();
329 if (result.trim().equals("")) { //$NON-NLS-1$
330 result = (cast(TreeColumn) item).getToolTipText();
331 }
332 }
333 return result;
334 }
335
336 /**
337 * @param item
338 * @return
339 */
340 private int getColumnWidth(Item item) {
341 if (null !is cast(TableColumn)item ) {
342 return (cast(TableColumn) item).getWidth();
343 } else if (null !is cast(TreeColumn)item ) {
344 return (cast(TreeColumn) item).getWidth();
345 }
346 return 0;
347 }
348
349 /**
350 * @return
351 */
352 private Item[] getViewerColumns() {
353 if (null !is cast(Table)targetControl ) {
354 return (cast(Table) targetControl).getColumns();
355 } else if (null !is cast(Tree)targetControl ) {
356 return (cast(Tree) targetControl).getColumns();
357 }
358 return new Item[0];
359 }
360
361 private void handleSelectionChanged(int index) {
362 ColumnObject c = columnObjects[index];
363 text.setText(Integer.toString(c.width));
364 text.setEnabled(c.resizable);
365 if (moveableColumnsFound) {
366 upButton.setEnabled(c.moveable && index > 0);
367 downButton.setEnabled(c.moveable
368 && index + 1 < table.getItemCount());
369 }
370 }
371
372 protected void okPressed() {
373 int[] columnOrder = new int[columnObjects.length];
374 for (int i = 0; i < columnObjects.length; i++) {
375 ColumnObject columnObject = columnObjects[i];
376 columnOrder[i] = columnObject.index;
377 setColumnWidth(columnObject.column, columnObject.width);
378 }
379 setColumnOrder(columnOrder);
380 super.okPressed();
381 }
382
383 /**
384 * @param column
385 * @param width
386 */
387 private void setColumnWidth(Item item, int width) {
388 if (null !is cast(TableColumn)item ) {
389 (cast(TableColumn) item).setWidth(width);
390 } else if (null !is cast(TreeColumn)item ) {
391 (cast(TreeColumn) item).setWidth(width);
392 }
393 }
394
395 /**
396 * @param columnOrder
397 */
398 private void setColumnOrder(int[] order) {
399 if (null !is cast(Table)targetControl ) {
400 (cast(Table) targetControl).setColumnOrder(order);
401 } else if (null !is cast(Tree)targetControl ) {
402 (cast(Tree) targetControl).setColumnOrder(order);
403 }
404 }
405 }