Mercurial > projects > dwt-win
annotate dwt/custom/TableCursor.d @ 213:36f5cb12e1a2
Update to SWT 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 17 May 2008 17:34:28 +0200 |
parents | ab60f3309436 |
children | fd9c62a2998e |
rev | line source |
---|---|
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 | |
213 | 163 Color background = null; |
164 Color foreground = null; | |
165 | |
155 | 166 // By default, invert the list selection colors |
167 static final int BACKGROUND = DWT.COLOR_LIST_SELECTION_TEXT; | |
168 static final int FOREGROUND = DWT.COLOR_LIST_SELECTION; | |
169 | |
170 /** | |
171 * Constructs a new instance of this class given its parent | |
172 * table and a style value describing its behavior and appearance. | |
173 * <p> | |
174 * The style value is either one of the style constants defined in | |
175 * class <code>DWT</code> which is applicable to instances of this | |
176 * class, or must be built by <em>bitwise OR</em>'ing together | |
177 * (that is, using the <code>int</code> "|" operator) two or more | |
178 * of those <code>DWT</code> style constants. The class description | |
179 * lists the style constants that are applicable to the class. | |
180 * Style bits are also inherited from superclasses. | |
181 * </p> | |
182 * | |
183 * @param parent a Table control which will be the parent of the new instance (cannot be null) | |
184 * @param style the style of control to construct | |
185 * | |
186 * @exception IllegalArgumentException <ul> | |
187 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
188 * </ul> | |
189 * @exception DWTException <ul> | |
190 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
191 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
192 * </ul> | |
193 * | |
194 * @see DWT#BORDER | |
195 * @see Widget#checkSubclass() | |
196 * @see Widget#getStyle() | |
197 */ | |
198 public this(Table parent, int style) { | |
199 super(parent, style); | |
200 table = parent; | |
201 setBackground(null); | |
202 setForeground(null); | |
203 | |
204 Listener listener = new class() Listener { | |
205 public void handleEvent(Event event) { | |
206 switch (event.type) { | |
207 case DWT.Dispose : | |
208 dispose(event); | |
209 break; | |
210 case DWT.FocusIn : | |
211 case DWT.FocusOut : | |
212 redraw(); | |
213 break; | |
214 case DWT.KeyDown : | |
215 keyDown(event); | |
216 break; | |
217 case DWT.Paint : | |
218 paint(event); | |
219 break; | |
220 case DWT.Traverse : { | |
221 event.doit = true; | |
222 switch (event.detail) { | |
223 case DWT.TRAVERSE_ARROW_NEXT : | |
224 case DWT.TRAVERSE_ARROW_PREVIOUS : | |
225 case DWT.TRAVERSE_RETURN : | |
226 event.doit = false; | |
227 break; | |
228 default: | |
229 } | |
230 break; | |
231 } | |
232 default: | |
233 } | |
234 } | |
235 }; | |
236 int[] events = [DWT.Dispose, DWT.FocusIn, DWT.FocusOut, DWT.KeyDown, DWT.Paint, DWT.Traverse]; | |
237 for (int i = 0; i < events.length; i++) { | |
238 addListener(events[i], listener); | |
239 } | |
240 | |
241 tableListener = new class() Listener { | |
242 public void handleEvent(Event event) { | |
243 switch (event.type) { | |
244 case DWT.MouseDown : | |
245 tableMouseDown(event); | |
246 break; | |
247 case DWT.FocusIn : | |
248 tableFocusIn(event); | |
249 break; | |
250 default: | |
251 } | |
252 } | |
253 }; | |
254 table.addListener(DWT.FocusIn, tableListener); | |
255 table.addListener(DWT.MouseDown, tableListener); | |
256 | |
257 disposeItemListener = new class() Listener { | |
258 public void handleEvent(Event event) { | |
213 | 259 unhookRowColumnListeners(); |
155 | 260 row = null; |
261 column = null; | |
262 _resize(); | |
263 } | |
264 }; | |
265 disposeColumnListener = new class() Listener { | |
266 public void handleEvent(Event event) { | |
213 | 267 unhookRowColumnListeners(); |
155 | 268 row = null; |
269 column = null; | |
270 _resize(); | |
271 } | |
272 }; | |
273 resizeListener = new class() Listener { | |
274 public void handleEvent(Event event) { | |
275 _resize(); | |
276 } | |
277 }; | |
278 ScrollBar hBar = table.getHorizontalBar(); | |
279 if (hBar !is null) { | |
280 hBar.addListener(DWT.Selection, resizeListener); | |
281 } | |
282 ScrollBar vBar = table.getVerticalBar(); | |
283 if (vBar !is null) { | |
284 vBar.addListener(DWT.Selection, resizeListener); | |
285 } | |
286 } | |
287 | |
288 /** | |
289 * Adds the listener to the collection of listeners who will | |
290 * be notified when the user changes the receiver's selection, by sending | |
291 * it one of the messages defined in the <code>SelectionListener</code> | |
292 * interface. | |
293 * <p> | |
294 * When <code>widgetSelected</code> is called, the item field of the event object is valid. | |
295 * If the receiver has <code>DWT.CHECK</code> style set and the check selection changes, | |
296 * the event object detail field contains the value <code>DWT.CHECK</code>. | |
297 * <code>widgetDefaultSelected</code> is typically called when an item is double-clicked. | |
298 * </p> | |
299 * | |
300 * @param listener the listener which should be notified when the user changes the receiver's selection | |
301 * | |
302 * @exception IllegalArgumentException <ul> | |
303 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
304 * </ul> | |
305 * @exception DWTException <ul> | |
306 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
307 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
308 * </ul> | |
309 * | |
310 * @see SelectionListener | |
311 * @see SelectionEvent | |
312 * @see #removeSelectionListener(SelectionListener) | |
313 * | |
314 */ | |
315 public void addSelectionListener(SelectionListener listener) { | |
316 checkWidget(); | |
317 if (listener is null) | |
318 DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
319 TypedListener typedListener = new TypedListener(listener); | |
320 addListener(DWT.Selection, typedListener); | |
321 addListener(DWT.DefaultSelection, typedListener); | |
322 } | |
323 | |
324 void dispose(Event event) { | |
325 table.removeListener(DWT.FocusIn, tableListener); | |
326 table.removeListener(DWT.MouseDown, tableListener); | |
213 | 327 unhookRowColumnListeners(); |
155 | 328 ScrollBar hBar = table.getHorizontalBar(); |
329 if (hBar !is null) { | |
330 hBar.removeListener(DWT.Selection, resizeListener); | |
331 } | |
332 ScrollBar vBar = table.getVerticalBar(); | |
333 if (vBar !is null) { | |
334 vBar.removeListener(DWT.Selection, resizeListener); | |
335 } | |
336 } | |
337 | |
338 void keyDown(Event event) { | |
339 if (row is null) return; | |
340 switch (event.character) { | |
341 case DWT.CR : | |
342 notifyListeners(DWT.DefaultSelection, new Event()); | |
343 return; | |
344 default: | |
345 } | |
346 int rowIndex = table.indexOf(row); | |
347 int columnIndex = column is null ? 0 : table.indexOf(column); | |
348 switch (event.keyCode) { | |
349 case DWT.ARROW_UP : | |
350 setRowColumn(Math.max(0, rowIndex - 1), columnIndex, true); | |
351 break; | |
352 case DWT.ARROW_DOWN : | |
353 setRowColumn(Math.min(rowIndex + 1, table.getItemCount() - 1), columnIndex, true); | |
354 break; | |
355 case DWT.ARROW_LEFT : | |
356 case DWT.ARROW_RIGHT : | |
357 { | |
358 int columnCount = table.getColumnCount(); | |
359 if (columnCount is 0) break; | |
360 int[] order = table.getColumnOrder(); | |
361 int index = 0; | |
362 while (index < order.length) { | |
363 if (order[index] is columnIndex) break; | |
364 index++; | |
365 } | |
366 if (index is order.length) index = 0; | |
367 int leadKey = (getStyle() & DWT.RIGHT_TO_LEFT) !is 0 ? DWT.ARROW_RIGHT : DWT.ARROW_LEFT; | |
368 if (event.keyCode is leadKey) { | |
369 setRowColumn(rowIndex, order[Math.max(0, index - 1)], true); | |
370 } else { | |
371 setRowColumn(rowIndex, order[Math.min(columnCount - 1, index + 1)], true); | |
372 } | |
373 break; | |
374 } | |
375 case DWT.HOME : | |
376 setRowColumn(0, columnIndex, true); | |
377 break; | |
378 case DWT.END : | |
379 { | |
380 int i = table.getItemCount() - 1; | |
381 setRowColumn(i, columnIndex, true); | |
382 break; | |
383 } | |
384 case DWT.PAGE_UP : | |
385 { | |
386 int index = table.getTopIndex(); | |
387 if (index is rowIndex) { | |
388 Rectangle rect = table.getClientArea(); | |
389 TableItem item = table.getItem(index); | |
390 Rectangle itemRect = item.getBounds(0); | |
391 rect.height -= itemRect.y; | |
392 int height = table.getItemHeight(); | |
393 int page = Math.max(1, rect.height / height); | |
394 index = Math.max(0, index - page + 1); | |
395 } | |
396 setRowColumn(index, columnIndex, true); | |
397 break; | |
398 } | |
399 case DWT.PAGE_DOWN : | |
400 { | |
401 int index = table.getTopIndex(); | |
402 Rectangle rect = table.getClientArea(); | |
403 TableItem item = table.getItem(index); | |
404 Rectangle itemRect = item.getBounds(0); | |
405 rect.height -= itemRect.y; | |
406 int height = table.getItemHeight(); | |
407 int page = Math.max(1, rect.height / height); | |
408 int end = table.getItemCount() - 1; | |
409 index = Math.min(end, index + page - 1); | |
410 if (index is rowIndex) { | |
411 index = Math.min(end, index + page - 1); | |
412 } | |
413 setRowColumn(index, columnIndex, true); | |
414 break; | |
415 } | |
416 default: | |
417 } | |
418 } | |
419 | |
420 void paint(Event event) { | |
421 if (row is null) return; | |
422 int columnIndex = column is null ? 0 : table.indexOf(column); | |
423 GC gc = event.gc; | |
424 Display display = getDisplay(); | |
425 gc.setBackground(getBackground()); | |
426 gc.setForeground(getForeground()); | |
427 gc.fillRectangle(event.x, event.y, event.width, event.height); | |
428 int x = 0; | |
429 Point size = getSize(); | |
430 Image image = row.getImage(columnIndex); | |
431 if (image !is null) { | |
432 Rectangle imageSize = image.getBounds(); | |
433 int imageY = (size.y - imageSize.height) / 2; | |
434 gc.drawImage(image, x, imageY); | |
435 x += imageSize.width; | |
436 } | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
437 String text = row.getText(columnIndex); |
155 | 438 if (text.length > 0) { |
439 Rectangle bounds = row.getBounds(columnIndex); | |
440 Point extent = gc.stringExtent(text); | |
441 // Temporary code - need a better way to determine table trim | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
442 String platform = DWT.getPlatform(); |
155 | 443 if ("win32"==platform) { //$NON-NLS-1$ |
444 if (table.getColumnCount() is 0 || columnIndex is 0) { | |
445 x += 2; | |
446 } else { | |
447 int alignmnent = column.getAlignment(); | |
448 switch (alignmnent) { | |
449 case DWT.LEFT: | |
450 x += 6; | |
451 break; | |
452 case DWT.RIGHT: | |
453 x = bounds.width - extent.x - 6; | |
454 break; | |
455 case DWT.CENTER: | |
456 x += (bounds.width - x - extent.x) / 2; | |
457 break; | |
458 default: | |
459 } | |
460 } | |
461 } else { | |
462 if (table.getColumnCount() is 0) { | |
463 x += 5; | |
464 } else { | |
465 int alignmnent = column.getAlignment(); | |
466 switch (alignmnent) { | |
467 case DWT.LEFT: | |
468 x += 5; | |
469 break; | |
470 case DWT.RIGHT: | |
471 x = bounds.width- extent.x - 2; | |
472 break; | |
473 case DWT.CENTER: | |
474 x += (bounds.width - x - extent.x) / 2 + 2; | |
475 break; | |
476 default: | |
477 } | |
478 } | |
479 } | |
480 int textY = (size.y - extent.y) / 2; | |
481 gc.drawString(text, x, textY); | |
482 } | |
483 if (isFocusControl()) { | |
484 gc.setBackground(display.getSystemColor(DWT.COLOR_BLACK)); | |
485 gc.setForeground(display.getSystemColor(DWT.COLOR_WHITE)); | |
486 gc.drawFocus(0, 0, size.x, size.y); | |
487 } | |
488 } | |
489 | |
490 void tableFocusIn(Event event) { | |
213 | 491 if (isDisposed()) return; |
492 if (isVisible()) { | |
493 if (row is null && column is null) return; | |
155 | 494 setFocus(); |
213 | 495 } |
155 | 496 } |
497 | |
498 void tableMouseDown(Event event) { | |
499 if (isDisposed() || !isVisible()) return; | |
500 Point pt = new Point(event.x, event.y); | |
501 int lineWidth = table.getLinesVisible() ? table.getGridLineWidth() : 0; | |
502 TableItem item = table.getItem(pt); | |
503 if ((table.getStyle() & DWT.FULL_SELECTION) !is 0) { | |
504 if (item is null) return; | |
505 } else { | |
506 int start = item !is null ? table.indexOf(item) : table.getTopIndex(); | |
507 int end = table.getItemCount(); | |
508 Rectangle clientRect = table.getClientArea(); | |
509 for (int i = start; i < end; i++) { | |
510 TableItem nextItem = table.getItem(i); | |
511 Rectangle rect = nextItem.getBounds(0); | |
512 if (pt.y >= rect.y && pt.y < rect.y + rect.height + lineWidth) { | |
513 item = nextItem; | |
514 break; | |
515 } | |
516 if (rect.y > clientRect.y + clientRect.height) return; | |
517 } | |
518 if (item is null) return; | |
519 } | |
520 TableColumn newColumn = null; | |
521 int columnCount = table.getColumnCount(); | |
213 | 522 if (columnCount is 0) { |
523 if ((table.getStyle() & DWT.FULL_SELECTION) is 0) { | |
524 Rectangle rect = item.getBounds(0); | |
525 rect.width += lineWidth; | |
526 rect.height += lineWidth; | |
527 if (!rect.contains(pt)) return; | |
528 } | |
529 } else { | |
155 | 530 for (int i = 0; i < columnCount; i++) { |
531 Rectangle rect = item.getBounds(i); | |
532 rect.width += lineWidth; | |
533 rect.height += lineWidth; | |
534 if (rect.contains(pt)) { | |
535 newColumn = table.getColumn(i); | |
536 break; | |
537 } | |
538 } | |
539 if (newColumn is null) { | |
213 | 540 if ((table.getStyle() & DWT.FULL_SELECTION) is 0) return; |
155 | 541 newColumn = table.getColumn(0); |
542 } | |
543 } | |
544 setRowColumn(item, newColumn, true); | |
545 setFocus(); | |
546 return; | |
547 } | |
548 void setRowColumn(int row, int column, bool notify) { | |
549 TableItem item = row is -1 ? null : table.getItem(row); | |
550 TableColumn col = column is -1 || table.getColumnCount() is 0 ? null : table.getColumn(column); | |
551 setRowColumn(item, col, notify); | |
552 } | |
553 void setRowColumn(TableItem row, TableColumn column, bool notify) { | |
554 if (this.row is row && this.column is column) { | |
555 return; | |
556 } | |
557 if (this.row !is null && this.row !is row) { | |
558 this.row.removeListener(DWT.Dispose, disposeItemListener); | |
559 this.row = null; | |
560 } | |
561 if (this.column !is null && this.column !is column) { | |
562 this.column.removeListener(DWT.Dispose, disposeColumnListener); | |
563 this.column.removeListener(DWT.Move, resizeListener); | |
564 this.column.removeListener(DWT.Resize, resizeListener); | |
565 this.column = null; | |
566 } | |
567 if (row !is null) { | |
568 if (this.row !is row) { | |
569 this.row = row; | |
570 row.addListener(DWT.Dispose, disposeItemListener); | |
571 table.showItem(row); | |
572 } | |
573 if (this.column !is column && column !is null) { | |
574 this.column = column; | |
575 column.addListener(DWT.Dispose, disposeColumnListener); | |
576 column.addListener(DWT.Move, resizeListener); | |
577 column.addListener(DWT.Resize, resizeListener); | |
578 table.showColumn(column); | |
579 } | |
580 int columnIndex = column is null ? 0 : table.indexOf(column); | |
581 setBounds(row.getBounds(columnIndex)); | |
582 redraw(); | |
583 if (notify) { | |
584 notifyListeners(DWT.Selection, new Event()); | |
585 } | |
586 } | |
587 } | |
588 | |
589 public override void setVisible(bool visible) { | |
590 checkWidget(); | |
591 if (visible) _resize(); | |
592 super.setVisible(visible); | |
593 } | |
594 | |
595 /** | |
596 * Removes the listener from the collection of listeners who will | |
597 * be notified when the user changes the receiver's selection. | |
598 * | |
599 * @param listener the listener which should no longer be notified | |
600 * | |
601 * @exception IllegalArgumentException <ul> | |
602 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
603 * </ul> | |
604 * @exception DWTException <ul> | |
605 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
606 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
607 * </ul> | |
608 * | |
609 * @see SelectionListener | |
610 * @see #addSelectionListener(SelectionListener) | |
611 * | |
612 * @since 3.0 | |
613 */ | |
614 public void removeSelectionListener(SelectionListener listener) { | |
615 checkWidget(); | |
616 if (listener is null) { | |
617 DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
618 } | |
619 removeListener(DWT.Selection, listener); | |
620 removeListener(DWT.DefaultSelection, listener); | |
621 } | |
622 | |
623 void _resize() { | |
624 if (row is null) { | |
625 setBounds(-200, -200, 0, 0); | |
626 } else { | |
627 int columnIndex = column is null ? 0 : table.indexOf(column); | |
628 setBounds(row.getBounds(columnIndex)); | |
629 } | |
630 } | |
631 /** | |
632 * Returns the column over which the TableCursor is positioned. | |
633 * | |
634 * @return the column for the current position | |
635 * | |
636 * @exception DWTException <ul> | |
637 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
638 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
639 * </ul> | |
640 */ | |
641 public int getColumn() { | |
642 checkWidget(); | |
643 return column is null ? 0 : table.indexOf(column); | |
644 } | |
645 /** | |
213 | 646 * Returns the background color that the receiver will use to draw. |
647 * | |
648 * @return the receiver's background color | |
649 */ | |
650 public Color getBackground() { | |
651 checkWidget(); | |
652 if (background is null) { | |
653 return getDisplay().getSystemColor(BACKGROUND); | |
654 } | |
655 return background; | |
656 } | |
657 /** | |
658 * Returns the foreground color that the receiver will use to draw. | |
659 * | |
660 * @return the receiver's foreground color | |
661 */ | |
662 public Color getForeground() { | |
663 checkWidget(); | |
664 if (foreground is null) { | |
665 return getDisplay().getSystemColor(FOREGROUND); | |
666 } | |
667 return foreground; | |
668 } | |
669 /** | |
155 | 670 * Returns the row over which the TableCursor is positioned. |
671 * | |
672 * @return the item for the current position | |
673 * | |
674 * @exception DWTException <ul> | |
675 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
676 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
677 * </ul> | |
678 */ | |
679 public TableItem getRow() { | |
680 checkWidget(); | |
681 return row; | |
682 } | |
213 | 683 /** |
684 * Sets the receiver's background color to the color specified | |
685 * by the argument, or to the default system color for the control | |
686 * if the argument is null. | |
687 * <p> | |
688 * Note: This operation is a hint and may be overridden by the platform. | |
689 * For example, on Windows the background of a Button cannot be changed. | |
690 * </p> | |
691 * @param color the new color (or null) | |
692 * | |
693 * @exception IllegalArgumentException <ul> | |
694 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> | |
695 * </ul> | |
696 * @exception DWTException <ul> | |
697 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
698 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
699 * </ul> | |
700 */ | |
155 | 701 public override void setBackground (Color color) { |
213 | 702 background = color; |
703 super.setBackground(getBackground()); | |
155 | 704 redraw(); |
705 } | |
213 | 706 /** |
707 * Sets the receiver's foreground color to the color specified | |
708 * by the argument, or to the default system color for the control | |
709 * if the argument is null. | |
710 * <p> | |
711 * Note: This operation is a hint and may be overridden by the platform. | |
712 * </p> | |
713 * @param color the new color (or null) | |
714 * | |
715 * @exception IllegalArgumentException <ul> | |
716 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> | |
717 * </ul> | |
718 * @exception DWTException <ul> | |
719 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
720 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
721 * </ul> | |
722 */ | |
155 | 723 public override void setForeground (Color color) { |
213 | 724 foreground = color; |
725 super.setForeground(getForeground()); | |
155 | 726 redraw(); |
727 } | |
728 /** | |
729 * Positions the TableCursor over the cell at the given row and column in the parent table. | |
730 * | |
731 * @param row the index of the row for the cell to select | |
732 * @param column the index of column for the cell to select | |
733 * | |
734 * @exception DWTException <ul> | |
735 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
736 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
737 * </ul> | |
738 * | |
739 */ | |
740 public void setSelection(int row, int column) { | |
741 checkWidget(); | |
742 int columnCount = table.getColumnCount(); | |
743 int maxColumnIndex = columnCount is 0 ? 0 : columnCount - 1; | |
744 if (row < 0 | |
745 || row >= table.getItemCount() | |
746 || column < 0 | |
747 || column > maxColumnIndex) | |
748 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
749 setRowColumn(row, column, false); | |
750 } | |
751 /** | |
752 * Positions the TableCursor over the cell at the given row and column in the parent table. | |
753 * | |
754 * @param row the TableItem of the row for the cell to select | |
755 * @param column the index of column for the cell to select | |
756 * | |
757 * @exception DWTException <ul> | |
758 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
759 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
760 * </ul> | |
761 * | |
762 */ | |
763 public void setSelection(TableItem row, int column) { | |
764 checkWidget(); | |
765 int columnCount = table.getColumnCount(); | |
766 int maxColumnIndex = columnCount is 0 ? 0 : columnCount - 1; | |
767 if (row is null | |
768 || row.isDisposed() | |
769 || column < 0 | |
770 || column > maxColumnIndex) | |
771 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
772 setRowColumn(table.indexOf(row), column, false); | |
773 } | |
213 | 774 void unhookRowColumnListeners() { |
775 if (column !is null) { | |
776 column.removeListener(DWT.Dispose, disposeColumnListener); | |
777 column.removeListener(DWT.Move, resizeListener); | |
778 column.removeListener(DWT.Resize, resizeListener); | |
779 column = null; | |
780 } | |
781 if (row !is null) { | |
782 row.removeListener(DWT.Dispose, disposeItemListener); | |
783 row = null; | |
784 } | |
155 | 785 } |
213 | 786 } |