155
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwt.custom.TableCursor;
|
|
14
|
|
15
|
|
16 import dwt.DWT;
|
|
17 import dwt.DWTException;
|
|
18 import dwt.events.SelectionEvent;
|
|
19 import dwt.events.SelectionListener;
|
|
20 import dwt.graphics.Color;
|
|
21 import dwt.graphics.GC;
|
|
22 import dwt.graphics.Image;
|
|
23 import dwt.graphics.Point;
|
|
24 import dwt.graphics.Rectangle;
|
|
25 import dwt.widgets.Canvas;
|
|
26 import dwt.widgets.Display;
|
|
27 import dwt.widgets.Event;
|
|
28 import dwt.widgets.Listener;
|
|
29 import dwt.widgets.ScrollBar;
|
|
30 import dwt.widgets.Table;
|
|
31 import dwt.widgets.TableColumn;
|
|
32 import dwt.widgets.TableItem;
|
|
33 import dwt.widgets.TypedListener;
|
|
34 import dwt.widgets.Widget;
|
|
35 import dwt.dwthelper.utils;
|
|
36
|
|
37 /**
|
|
38 * A TableCursor provides a way for the user to navigate around a Table
|
|
39 * using the keyboard. It also provides a mechanism for selecting an
|
|
40 * individual cell in a table.
|
|
41 *
|
|
42 * <p> Here is an example of using a TableCursor to navigate to a cell and then edit it.
|
|
43 *
|
|
44 * <code><pre>
|
|
45 * public static void main(String[] args) {
|
|
46 * Display display = new Display();
|
|
47 * Shell shell = new Shell(display);
|
|
48 * shell.setLayout(new GridLayout());
|
|
49 *
|
|
50 * // create a a table with 3 columns and fill with data
|
|
51 * final Table table = new Table(shell, DWT.BORDER | DWT.MULTI | DWT.FULL_SELECTION);
|
|
52 * table.setLayoutData(new GridData(GridData.FILL_BOTH));
|
|
53 * TableColumn column1 = new TableColumn(table, DWT.NONE);
|
|
54 * TableColumn column2 = new TableColumn(table, DWT.NONE);
|
|
55 * TableColumn column3 = new TableColumn(table, DWT.NONE);
|
|
56 * for (int i = 0; i < 100; i++) {
|
|
57 * TableItem item = new TableItem(table, DWT.NONE);
|
|
58 * item.setText(new String[] { "cell "+i+" 0", "cell "+i+" 1", "cell "+i+" 2"});
|
|
59 * }
|
|
60 * column1.pack();
|
|
61 * column2.pack();
|
|
62 * column3.pack();
|
|
63 *
|
|
64 * // create a TableCursor to navigate around the table
|
|
65 * final TableCursor cursor = new TableCursor(table, DWT.NONE);
|
|
66 * // create an editor to edit the cell when the user hits "ENTER"
|
|
67 * // while over a cell in the table
|
|
68 * final ControlEditor editor = new ControlEditor(cursor);
|
|
69 * editor.grabHorizontal = true;
|
|
70 * editor.grabVertical = true;
|
|
71 *
|
|
72 * cursor.addSelectionListener(new SelectionAdapter() {
|
|
73 * // when the TableEditor is over a cell, select the corresponding row in
|
|
74 * // the table
|
|
75 * public void widgetSelected(SelectionEvent e) {
|
|
76 * table.setSelection(new TableItem[] {cursor.getRow()});
|
|
77 * }
|
|
78 * // when the user hits "ENTER" in the TableCursor, pop up a text editor so that
|
|
79 * // they can change the text of the cell
|
|
80 * public void widgetDefaultSelected(SelectionEvent e){
|
|
81 * final Text text = new Text(cursor, DWT.NONE);
|
|
82 * TableItem row = cursor.getRow();
|
|
83 * int column = cursor.getColumn();
|
|
84 * text.setText(row.getText(column));
|
|
85 * text.addKeyListener(new KeyAdapter() {
|
|
86 * public void keyPressed(KeyEvent e) {
|
|
87 * // close the text editor and copy the data over
|
|
88 * // when the user hits "ENTER"
|
|
89 * if (e.character is DWT.CR) {
|
|
90 * TableItem row = cursor.getRow();
|
|
91 * int column = cursor.getColumn();
|
|
92 * row.setText(column, text.getText());
|
|
93 * text.dispose();
|
|
94 * }
|
|
95 * // close the text editor when the user hits "ESC"
|
|
96 * if (e.character is DWT.ESC) {
|
|
97 * text.dispose();
|
|
98 * }
|
|
99 * }
|
|
100 * });
|
|
101 * editor.setEditor(text);
|
|
102 * text.setFocus();
|
|
103 * }
|
|
104 * });
|
|
105 * // Hide the TableCursor when the user hits the "MOD1" or "MOD2" key.
|
|
106 * // This allows the user to select multiple items in the table.
|
|
107 * cursor.addKeyListener(new KeyAdapter() {
|
|
108 * public void keyPressed(KeyEvent e) {
|
|
109 * if (e.keyCode is DWT.MOD1 ||
|
|
110 * e.keyCode is DWT.MOD2 ||
|
|
111 * (e.stateMask & DWT.MOD1) !is 0 ||
|
|
112 * (e.stateMask & DWT.MOD2) !is 0) {
|
|
113 * cursor.setVisible(false);
|
|
114 * }
|
|
115 * }
|
|
116 * });
|
|
117 * // Show the TableCursor when the user releases the "MOD2" or "MOD1" key.
|
|
118 * // This signals the end of the multiple selection task.
|
|
119 * table.addKeyListener(new KeyAdapter() {
|
|
120 * public void keyReleased(KeyEvent e) {
|
|
121 * if (e.keyCode is DWT.MOD1 && (e.stateMask & DWT.MOD2) !is 0) return;
|
|
122 * if (e.keyCode is DWT.MOD2 && (e.stateMask & DWT.MOD1) !is 0) return;
|
|
123 * if (e.keyCode !is DWT.MOD1 && (e.stateMask & DWT.MOD1) !is 0) return;
|
|
124 * if (e.keyCode !is DWT.MOD2 && (e.stateMask & DWT.MOD2) !is 0) return;
|
|
125 *
|
|
126 * TableItem[] selection = table.getSelection();
|
|
127 * TableItem row = (selection.length is 0) ? table.getItem(table.getTopIndex()) : selection[0];
|
|
128 * table.showItem(row);
|
|
129 * cursor.setSelection(row, 0);
|
|
130 * cursor.setVisible(true);
|
|
131 * cursor.setFocus();
|
|
132 * }
|
|
133 * });
|
|
134 *
|
|
135 * shell.open();
|
|
136 * while (!shell.isDisposed()) {
|
|
137 * if (!display.readAndDispatch())
|
|
138 * display.sleep();
|
|
139 * }
|
|
140 * display.dispose();
|
|
141 * }
|
|
142 * </pre></code>
|
|
143 *
|
|
144 * <dl>
|
|
145 * <dt><b>Styles:</b></dt>
|
|
146 * <dd>BORDER</dd>
|
|
147 * <dt><b>Events:</b></dt>
|
|
148 * <dd>Selection, DefaultSelection</dd>
|
|
149 * </dl>
|
|
150 *
|
|
151 * @since 2.0
|
|
152 *
|
|
153 */
|
|
154 public class TableCursor : Canvas {
|
|
155
|
|
156 alias Canvas.dispose dispose;
|
|
157
|
|
158 Table table;
|
|
159 TableItem row = null;
|
|
160 TableColumn column = null;
|
|
161 Listener tableListener, resizeListener, disposeItemListener, disposeColumnListener;
|
|
162
|
|
163 // By default, invert the list selection colors
|
|
164 static final int BACKGROUND = DWT.COLOR_LIST_SELECTION_TEXT;
|
|
165 static final int FOREGROUND = DWT.COLOR_LIST_SELECTION;
|
|
166
|
|
167 /**
|
|
168 * Constructs a new instance of this class given its parent
|
|
169 * table and a style value describing its behavior and appearance.
|
|
170 * <p>
|
|
171 * The style value is either one of the style constants defined in
|
|
172 * class <code>DWT</code> which is applicable to instances of this
|
|
173 * class, or must be built by <em>bitwise OR</em>'ing together
|
|
174 * (that is, using the <code>int</code> "|" operator) two or more
|
|
175 * of those <code>DWT</code> style constants. The class description
|
|
176 * lists the style constants that are applicable to the class.
|
|
177 * Style bits are also inherited from superclasses.
|
|
178 * </p>
|
|
179 *
|
|
180 * @param parent a Table control which will be the parent of the new instance (cannot be null)
|
|
181 * @param style the style of control to construct
|
|
182 *
|
|
183 * @exception IllegalArgumentException <ul>
|
|
184 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
|
|
185 * </ul>
|
|
186 * @exception DWTException <ul>
|
|
187 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
|
|
188 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
|
|
189 * </ul>
|
|
190 *
|
|
191 * @see DWT#BORDER
|
|
192 * @see Widget#checkSubclass()
|
|
193 * @see Widget#getStyle()
|
|
194 */
|
|
195 public this(Table parent, int style) {
|
|
196 super(parent, style);
|
|
197 table = parent;
|
|
198 setBackground(null);
|
|
199 setForeground(null);
|
|
200
|
|
201 Listener listener = new class() Listener {
|
|
202 public void handleEvent(Event event) {
|
|
203 switch (event.type) {
|
|
204 case DWT.Dispose :
|
|
205 dispose(event);
|
|
206 break;
|
|
207 case DWT.FocusIn :
|
|
208 case DWT.FocusOut :
|
|
209 redraw();
|
|
210 break;
|
|
211 case DWT.KeyDown :
|
|
212 keyDown(event);
|
|
213 break;
|
|
214 case DWT.Paint :
|
|
215 paint(event);
|
|
216 break;
|
|
217 case DWT.Traverse : {
|
|
218 event.doit = true;
|
|
219 switch (event.detail) {
|
|
220 case DWT.TRAVERSE_ARROW_NEXT :
|
|
221 case DWT.TRAVERSE_ARROW_PREVIOUS :
|
|
222 case DWT.TRAVERSE_RETURN :
|
|
223 event.doit = false;
|
|
224 break;
|
|
225 default:
|
|
226 }
|
|
227 break;
|
|
228 }
|
|
229 default:
|
|
230 }
|
|
231 }
|
|
232 };
|
|
233 int[] events = [DWT.Dispose, DWT.FocusIn, DWT.FocusOut, DWT.KeyDown, DWT.Paint, DWT.Traverse];
|
|
234 for (int i = 0; i < events.length; i++) {
|
|
235 addListener(events[i], listener);
|
|
236 }
|
|
237
|
|
238 tableListener = new class() Listener {
|
|
239 public void handleEvent(Event event) {
|
|
240 switch (event.type) {
|
|
241 case DWT.MouseDown :
|
|
242 tableMouseDown(event);
|
|
243 break;
|
|
244 case DWT.FocusIn :
|
|
245 tableFocusIn(event);
|
|
246 break;
|
|
247 default:
|
|
248 }
|
|
249 }
|
|
250 };
|
|
251 table.addListener(DWT.FocusIn, tableListener);
|
|
252 table.addListener(DWT.MouseDown, tableListener);
|
|
253
|
|
254 disposeItemListener = new class() Listener {
|
|
255 public void handleEvent(Event event) {
|
|
256 row = null;
|
|
257 column = null;
|
|
258 _resize();
|
|
259 }
|
|
260 };
|
|
261 disposeColumnListener = new class() Listener {
|
|
262 public void handleEvent(Event event) {
|
|
263 row = null;
|
|
264 column = null;
|
|
265 _resize();
|
|
266 }
|
|
267 };
|
|
268 resizeListener = new class() Listener {
|
|
269 public void handleEvent(Event event) {
|
|
270 _resize();
|
|
271 }
|
|
272 };
|
|
273 ScrollBar hBar = table.getHorizontalBar();
|
|
274 if (hBar !is null) {
|
|
275 hBar.addListener(DWT.Selection, resizeListener);
|
|
276 }
|
|
277 ScrollBar vBar = table.getVerticalBar();
|
|
278 if (vBar !is null) {
|
|
279 vBar.addListener(DWT.Selection, resizeListener);
|
|
280 }
|
|
281 }
|
|
282
|
|
283 /**
|
|
284 * Adds the listener to the collection of listeners who will
|
|
285 * be notified when the user changes the receiver's selection, by sending
|
|
286 * it one of the messages defined in the <code>SelectionListener</code>
|
|
287 * interface.
|
|
288 * <p>
|
|
289 * When <code>widgetSelected</code> is called, the item field of the event object is valid.
|
|
290 * If the receiver has <code>DWT.CHECK</code> style set and the check selection changes,
|
|
291 * the event object detail field contains the value <code>DWT.CHECK</code>.
|
|
292 * <code>widgetDefaultSelected</code> is typically called when an item is double-clicked.
|
|
293 * </p>
|
|
294 *
|
|
295 * @param listener the listener which should be notified when the user changes the receiver's selection
|
|
296 *
|
|
297 * @exception IllegalArgumentException <ul>
|
|
298 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
299 * </ul>
|
|
300 * @exception DWTException <ul>
|
|
301 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
302 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
303 * </ul>
|
|
304 *
|
|
305 * @see SelectionListener
|
|
306 * @see SelectionEvent
|
|
307 * @see #removeSelectionListener(SelectionListener)
|
|
308 *
|
|
309 */
|
|
310 public void addSelectionListener(SelectionListener listener) {
|
|
311 checkWidget();
|
|
312 if (listener is null)
|
|
313 DWT.error(DWT.ERROR_NULL_ARGUMENT);
|
|
314 TypedListener typedListener = new TypedListener(listener);
|
|
315 addListener(DWT.Selection, typedListener);
|
|
316 addListener(DWT.DefaultSelection, typedListener);
|
|
317 }
|
|
318
|
|
319 void dispose(Event event) {
|
|
320 table.removeListener(DWT.FocusIn, tableListener);
|
|
321 table.removeListener(DWT.MouseDown, tableListener);
|
|
322 if (column !is null) {
|
|
323 column.removeListener(DWT.Dispose, disposeColumnListener);
|
|
324 column.removeListener(DWT.Move, resizeListener);
|
|
325 column.removeListener(DWT.Resize, resizeListener);
|
|
326 column = null;
|
|
327 }
|
|
328 if (row !is null) {
|
|
329 row.removeListener(DWT.Dispose, disposeItemListener);
|
|
330 row = null;
|
|
331 }
|
|
332 ScrollBar hBar = table.getHorizontalBar();
|
|
333 if (hBar !is null) {
|
|
334 hBar.removeListener(DWT.Selection, resizeListener);
|
|
335 }
|
|
336 ScrollBar vBar = table.getVerticalBar();
|
|
337 if (vBar !is null) {
|
|
338 vBar.removeListener(DWT.Selection, resizeListener);
|
|
339 }
|
|
340 }
|
|
341
|
|
342 void keyDown(Event event) {
|
|
343 if (row is null) return;
|
|
344 switch (event.character) {
|
|
345 case DWT.CR :
|
|
346 notifyListeners(DWT.DefaultSelection, new Event());
|
|
347 return;
|
|
348 default:
|
|
349 }
|
|
350 int rowIndex = table.indexOf(row);
|
|
351 int columnIndex = column is null ? 0 : table.indexOf(column);
|
|
352 switch (event.keyCode) {
|
|
353 case DWT.ARROW_UP :
|
|
354 setRowColumn(Math.max(0, rowIndex - 1), columnIndex, true);
|
|
355 break;
|
|
356 case DWT.ARROW_DOWN :
|
|
357 setRowColumn(Math.min(rowIndex + 1, table.getItemCount() - 1), columnIndex, true);
|
|
358 break;
|
|
359 case DWT.ARROW_LEFT :
|
|
360 case DWT.ARROW_RIGHT :
|
|
361 {
|
|
362 int columnCount = table.getColumnCount();
|
|
363 if (columnCount is 0) break;
|
|
364 int[] order = table.getColumnOrder();
|
|
365 int index = 0;
|
|
366 while (index < order.length) {
|
|
367 if (order[index] is columnIndex) break;
|
|
368 index++;
|
|
369 }
|
|
370 if (index is order.length) index = 0;
|
|
371 int leadKey = (getStyle() & DWT.RIGHT_TO_LEFT) !is 0 ? DWT.ARROW_RIGHT : DWT.ARROW_LEFT;
|
|
372 if (event.keyCode is leadKey) {
|
|
373 setRowColumn(rowIndex, order[Math.max(0, index - 1)], true);
|
|
374 } else {
|
|
375 setRowColumn(rowIndex, order[Math.min(columnCount - 1, index + 1)], true);
|
|
376 }
|
|
377 break;
|
|
378 }
|
|
379 case DWT.HOME :
|
|
380 setRowColumn(0, columnIndex, true);
|
|
381 break;
|
|
382 case DWT.END :
|
|
383 {
|
|
384 int i = table.getItemCount() - 1;
|
|
385 setRowColumn(i, columnIndex, true);
|
|
386 break;
|
|
387 }
|
|
388 case DWT.PAGE_UP :
|
|
389 {
|
|
390 int index = table.getTopIndex();
|
|
391 if (index is rowIndex) {
|
|
392 Rectangle rect = table.getClientArea();
|
|
393 TableItem item = table.getItem(index);
|
|
394 Rectangle itemRect = item.getBounds(0);
|
|
395 rect.height -= itemRect.y;
|
|
396 int height = table.getItemHeight();
|
|
397 int page = Math.max(1, rect.height / height);
|
|
398 index = Math.max(0, index - page + 1);
|
|
399 }
|
|
400 setRowColumn(index, columnIndex, true);
|
|
401 break;
|
|
402 }
|
|
403 case DWT.PAGE_DOWN :
|
|
404 {
|
|
405 int index = table.getTopIndex();
|
|
406 Rectangle rect = table.getClientArea();
|
|
407 TableItem item = table.getItem(index);
|
|
408 Rectangle itemRect = item.getBounds(0);
|
|
409 rect.height -= itemRect.y;
|
|
410 int height = table.getItemHeight();
|
|
411 int page = Math.max(1, rect.height / height);
|
|
412 int end = table.getItemCount() - 1;
|
|
413 index = Math.min(end, index + page - 1);
|
|
414 if (index is rowIndex) {
|
|
415 index = Math.min(end, index + page - 1);
|
|
416 }
|
|
417 setRowColumn(index, columnIndex, true);
|
|
418 break;
|
|
419 }
|
|
420 default:
|
|
421 }
|
|
422 }
|
|
423
|
|
424 void paint(Event event) {
|
|
425 if (row is null) return;
|
|
426 int columnIndex = column is null ? 0 : table.indexOf(column);
|
|
427 GC gc = event.gc;
|
|
428 Display display = getDisplay();
|
|
429 gc.setBackground(getBackground());
|
|
430 gc.setForeground(getForeground());
|
|
431 gc.fillRectangle(event.x, event.y, event.width, event.height);
|
|
432 int x = 0;
|
|
433 Point size = getSize();
|
|
434 Image image = row.getImage(columnIndex);
|
|
435 if (image !is null) {
|
|
436 Rectangle imageSize = image.getBounds();
|
|
437 int imageY = (size.y - imageSize.height) / 2;
|
|
438 gc.drawImage(image, x, imageY);
|
|
439 x += imageSize.width;
|
|
440 }
|
|
441 char[] text = row.getText(columnIndex);
|
|
442 if (text.length > 0) {
|
|
443 Rectangle bounds = row.getBounds(columnIndex);
|
|
444 Point extent = gc.stringExtent(text);
|
|
445 // Temporary code - need a better way to determine table trim
|
|
446 char[] platform = DWT.getPlatform();
|
|
447 if ("win32"==platform) { //$NON-NLS-1$
|
|
448 if (table.getColumnCount() is 0 || columnIndex is 0) {
|
|
449 x += 2;
|
|
450 } else {
|
|
451 int alignmnent = column.getAlignment();
|
|
452 switch (alignmnent) {
|
|
453 case DWT.LEFT:
|
|
454 x += 6;
|
|
455 break;
|
|
456 case DWT.RIGHT:
|
|
457 x = bounds.width - extent.x - 6;
|
|
458 break;
|
|
459 case DWT.CENTER:
|
|
460 x += (bounds.width - x - extent.x) / 2;
|
|
461 break;
|
|
462 default:
|
|
463 }
|
|
464 }
|
|
465 } else {
|
|
466 if (table.getColumnCount() is 0) {
|
|
467 x += 5;
|
|
468 } else {
|
|
469 int alignmnent = column.getAlignment();
|
|
470 switch (alignmnent) {
|
|
471 case DWT.LEFT:
|
|
472 x += 5;
|
|
473 break;
|
|
474 case DWT.RIGHT:
|
|
475 x = bounds.width- extent.x - 2;
|
|
476 break;
|
|
477 case DWT.CENTER:
|
|
478 x += (bounds.width - x - extent.x) / 2 + 2;
|
|
479 break;
|
|
480 default:
|
|
481 }
|
|
482 }
|
|
483 }
|
|
484 int textY = (size.y - extent.y) / 2;
|
|
485 gc.drawString(text, x, textY);
|
|
486 }
|
|
487 if (isFocusControl()) {
|
|
488 gc.setBackground(display.getSystemColor(DWT.COLOR_BLACK));
|
|
489 gc.setForeground(display.getSystemColor(DWT.COLOR_WHITE));
|
|
490 gc.drawFocus(0, 0, size.x, size.y);
|
|
491 }
|
|
492 }
|
|
493
|
|
494 void tableFocusIn(Event event) {
|
|
495 if (isDisposed())
|
|
496 return;
|
|
497 if (isVisible())
|
|
498 setFocus();
|
|
499 }
|
|
500
|
|
501 void tableMouseDown(Event event) {
|
|
502 if (isDisposed() || !isVisible()) return;
|
|
503 Point pt = new Point(event.x, event.y);
|
|
504 int lineWidth = table.getLinesVisible() ? table.getGridLineWidth() : 0;
|
|
505 TableItem item = table.getItem(pt);
|
|
506 if ((table.getStyle() & DWT.FULL_SELECTION) !is 0) {
|
|
507 if (item is null) return;
|
|
508 } else {
|
|
509 int start = item !is null ? table.indexOf(item) : table.getTopIndex();
|
|
510 int end = table.getItemCount();
|
|
511 Rectangle clientRect = table.getClientArea();
|
|
512 for (int i = start; i < end; i++) {
|
|
513 TableItem nextItem = table.getItem(i);
|
|
514 Rectangle rect = nextItem.getBounds(0);
|
|
515 if (pt.y >= rect.y && pt.y < rect.y + rect.height + lineWidth) {
|
|
516 item = nextItem;
|
|
517 break;
|
|
518 }
|
|
519 if (rect.y > clientRect.y + clientRect.height) return;
|
|
520 }
|
|
521 if (item is null) return;
|
|
522 }
|
|
523 TableColumn newColumn = null;
|
|
524 int columnCount = table.getColumnCount();
|
|
525 if (columnCount > 0) {
|
|
526 for (int i = 0; i < columnCount; i++) {
|
|
527 Rectangle rect = item.getBounds(i);
|
|
528 rect.width += lineWidth;
|
|
529 rect.height += lineWidth;
|
|
530 if (rect.contains(pt)) {
|
|
531 newColumn = table.getColumn(i);
|
|
532 break;
|
|
533 }
|
|
534 }
|
|
535 if (newColumn is null) {
|
|
536 newColumn = table.getColumn(0);
|
|
537 }
|
|
538 }
|
|
539 setRowColumn(item, newColumn, true);
|
|
540 setFocus();
|
|
541 return;
|
|
542 }
|
|
543 void setRowColumn(int row, int column, bool notify) {
|
|
544 TableItem item = row is -1 ? null : table.getItem(row);
|
|
545 TableColumn col = column is -1 || table.getColumnCount() is 0 ? null : table.getColumn(column);
|
|
546 setRowColumn(item, col, notify);
|
|
547 }
|
|
548 void setRowColumn(TableItem row, TableColumn column, bool notify) {
|
|
549 if (this.row is row && this.column is column) {
|
|
550 return;
|
|
551 }
|
|
552 if (this.row !is null && this.row !is row) {
|
|
553 this.row.removeListener(DWT.Dispose, disposeItemListener);
|
|
554 this.row = null;
|
|
555 }
|
|
556 if (this.column !is null && this.column !is column) {
|
|
557 this.column.removeListener(DWT.Dispose, disposeColumnListener);
|
|
558 this.column.removeListener(DWT.Move, resizeListener);
|
|
559 this.column.removeListener(DWT.Resize, resizeListener);
|
|
560 this.column = null;
|
|
561 }
|
|
562 if (row !is null) {
|
|
563 if (this.row !is row) {
|
|
564 this.row = row;
|
|
565 row.addListener(DWT.Dispose, disposeItemListener);
|
|
566 table.showItem(row);
|
|
567 }
|
|
568 if (this.column !is column && column !is null) {
|
|
569 this.column = column;
|
|
570 column.addListener(DWT.Dispose, disposeColumnListener);
|
|
571 column.addListener(DWT.Move, resizeListener);
|
|
572 column.addListener(DWT.Resize, resizeListener);
|
|
573 table.showColumn(column);
|
|
574 }
|
|
575 int columnIndex = column is null ? 0 : table.indexOf(column);
|
|
576 setBounds(row.getBounds(columnIndex));
|
|
577 redraw();
|
|
578 if (notify) {
|
|
579 notifyListeners(DWT.Selection, new Event());
|
|
580 }
|
|
581 }
|
|
582 }
|
|
583
|
|
584 public override void setVisible(bool visible) {
|
|
585 checkWidget();
|
|
586 if (visible) _resize();
|
|
587 super.setVisible(visible);
|
|
588 }
|
|
589
|
|
590 /**
|
|
591 * Removes the listener from the collection of listeners who will
|
|
592 * be notified when the user changes the receiver's selection.
|
|
593 *
|
|
594 * @param listener the listener which should no longer be notified
|
|
595 *
|
|
596 * @exception IllegalArgumentException <ul>
|
|
597 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
598 * </ul>
|
|
599 * @exception DWTException <ul>
|
|
600 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
601 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
602 * </ul>
|
|
603 *
|
|
604 * @see SelectionListener
|
|
605 * @see #addSelectionListener(SelectionListener)
|
|
606 *
|
|
607 * @since 3.0
|
|
608 */
|
|
609 public void removeSelectionListener(SelectionListener listener) {
|
|
610 checkWidget();
|
|
611 if (listener is null) {
|
|
612 DWT.error(DWT.ERROR_NULL_ARGUMENT);
|
|
613 }
|
|
614 removeListener(DWT.Selection, listener);
|
|
615 removeListener(DWT.DefaultSelection, listener);
|
|
616 }
|
|
617
|
|
618 void _resize() {
|
|
619 if (row is null) {
|
|
620 setBounds(-200, -200, 0, 0);
|
|
621 } else {
|
|
622 int columnIndex = column is null ? 0 : table.indexOf(column);
|
|
623 setBounds(row.getBounds(columnIndex));
|
|
624 }
|
|
625 }
|
|
626 /**
|
|
627 * Returns the column over which the TableCursor is positioned.
|
|
628 *
|
|
629 * @return the column for the current position
|
|
630 *
|
|
631 * @exception DWTException <ul>
|
|
632 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
633 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
634 * </ul>
|
|
635 */
|
|
636 public int getColumn() {
|
|
637 checkWidget();
|
|
638 return column is null ? 0 : table.indexOf(column);
|
|
639 }
|
|
640 /**
|
|
641 * Returns the row over which the TableCursor is positioned.
|
|
642 *
|
|
643 * @return the item for the current position
|
|
644 *
|
|
645 * @exception DWTException <ul>
|
|
646 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
647 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
648 * </ul>
|
|
649 */
|
|
650 public TableItem getRow() {
|
|
651 checkWidget();
|
|
652 return row;
|
|
653 }
|
|
654 public override void setBackground (Color color) {
|
|
655 if (color is null) color = getDisplay().getSystemColor(BACKGROUND);
|
|
656 super.setBackground(color);
|
|
657 redraw();
|
|
658 }
|
|
659 public override void setForeground (Color color) {
|
|
660 if (color is null) color = getDisplay().getSystemColor(FOREGROUND);
|
|
661 super.setForeground(color);
|
|
662 redraw();
|
|
663 }
|
|
664 /**
|
|
665 * Positions the TableCursor over the cell at the given row and column in the parent table.
|
|
666 *
|
|
667 * @param row the index of the row for the cell to select
|
|
668 * @param column the index of column for the cell to select
|
|
669 *
|
|
670 * @exception DWTException <ul>
|
|
671 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
672 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
673 * </ul>
|
|
674 *
|
|
675 */
|
|
676 public void setSelection(int row, int column) {
|
|
677 checkWidget();
|
|
678 int columnCount = table.getColumnCount();
|
|
679 int maxColumnIndex = columnCount is 0 ? 0 : columnCount - 1;
|
|
680 if (row < 0
|
|
681 || row >= table.getItemCount()
|
|
682 || column < 0
|
|
683 || column > maxColumnIndex)
|
|
684 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
685 setRowColumn(row, column, false);
|
|
686 }
|
|
687 /**
|
|
688 * Positions the TableCursor over the cell at the given row and column in the parent table.
|
|
689 *
|
|
690 * @param row the TableItem of the row for the cell to select
|
|
691 * @param column the index of column for the cell to select
|
|
692 *
|
|
693 * @exception DWTException <ul>
|
|
694 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
695 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
696 * </ul>
|
|
697 *
|
|
698 */
|
|
699 public void setSelection(TableItem row, int column) {
|
|
700 checkWidget();
|
|
701 int columnCount = table.getColumnCount();
|
|
702 int maxColumnIndex = columnCount is 0 ? 0 : columnCount - 1;
|
|
703 if (row is null
|
|
704 || row.isDisposed()
|
|
705 || column < 0
|
|
706 || column > maxColumnIndex)
|
|
707 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
708 setRowColumn(table.indexOf(row), column, false);
|
|
709 }
|
|
710 }
|