1 /*******************************************************************************
2 * Copyright (c) 2000, 2016 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.accessibility.*;
18 import org.eclipse.swt.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.widgets.*;
23 * A TableCursor provides a way for the user to navigate around a Table
24 * using the keyboard. It also provides a mechanism for selecting an
25 * individual cell in a table.
27 * For a detailed example of using a TableCursor to navigate to a cell and then edit it see
28 * http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java .
31 * <dt><b>Styles:</b></dt>
33 * <dt><b>Events:</b></dt>
34 * <dd>Selection, DefaultSelection</dd>
39 * @see <a href="http://www.eclipse.org/swt/snippets/#tablecursor">TableCursor snippets</a>
40 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
42 public class TableCursor extends Canvas {
45 TableColumn column = null;
46 Listener listener, tableListener, resizeListener, disposeItemListener, disposeColumnListener;
48 Color background = null;
49 Color foreground = null;
51 /* By default, invert the list selection colors */
52 static final int BACKGROUND = SWT.COLOR_LIST_SELECTION_TEXT;
53 static final int FOREGROUND = SWT.COLOR_LIST_SELECTION;
56 * Constructs a new instance of this class given its parent
57 * table and a style value describing its behavior and appearance.
59 * The style value is either one of the style constants defined in
60 * class <code>SWT</code> which is applicable to instances of this
61 * class, or must be built by <em>bitwise OR</em>'ing together
62 * (that is, using the <code>int</code> "|" operator) two or more
63 * of those <code>SWT</code> style constants. The class description
64 * lists the style constants that are applicable to the class.
65 * Style bits are also inherited from superclasses.
68 * @param parent a Table control which will be the parent of the new instance (cannot be null)
69 * @param style the style of control to construct
71 * @exception IllegalArgumentException <ul>
72 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
74 * @exception SWTException <ul>
75 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
76 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
80 * @see Widget#checkSubclass()
81 * @see Widget#getStyle()
83 public TableCursor(Table parent, int style) {
104 case SWT.Traverse : {
106 switch (event.detail) {
107 case SWT.TRAVERSE_ARROW_NEXT :
108 case SWT.TRAVERSE_ARROW_PREVIOUS :
109 case SWT.TRAVERSE_RETURN :
117 int[] events = new int[] {SWT.Dispose, SWT.FocusIn, SWT.FocusOut, SWT.KeyDown, SWT.Paint, SWT.Traverse};
118 for (int i = 0; i < events.length; i++) {
119 addListener(events[i], listener);
122 tableListener = event -> {
123 switch (event.type) {
125 tableMouseDown(event);
132 table.addListener(SWT.FocusIn, tableListener);
133 table.addListener(SWT.MouseDown, tableListener);
135 disposeItemListener = event -> {
136 unhookRowColumnListeners();
141 disposeColumnListener = event -> {
142 unhookRowColumnListeners();
147 resizeListener = event -> _resize();
148 ScrollBar hBar = table.getHorizontalBar();
150 hBar.addListener(SWT.Selection, resizeListener);
152 ScrollBar vBar = table.getVerticalBar();
154 vBar.addListener(SWT.Selection, resizeListener);
157 getAccessible().addAccessibleControlListener(new AccessibleControlAdapter() {
159 public void getRole(AccessibleControlEvent e) {
160 e.detail = ACC.ROLE_TABLECELL;
163 getAccessible().addAccessibleListener(new AccessibleAdapter() {
165 public void getName(AccessibleEvent e) {
166 if (row == null) return;
167 int columnIndex = column == null ? 0 : table.indexOf(column);
168 e.result = row.getText(columnIndex);
174 * Adds the listener to the collection of listeners who will
175 * be notified when the user changes the receiver's selection, by sending
176 * it one of the messages defined in the <code>SelectionListener</code>
179 * When <code>widgetSelected</code> is called, the item field of the event object is valid.
180 * If the receiver has <code>SWT.CHECK</code> style set and the check selection changes,
181 * the event object detail field contains the value <code>SWT.CHECK</code>.
182 * <code>widgetDefaultSelected</code> is typically called when an item is double-clicked.
185 * @param listener the listener which should be notified when the user changes the receiver's selection
187 * @exception IllegalArgumentException <ul>
188 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
190 * @exception SWTException <ul>
191 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
192 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
195 * @see SelectionListener
196 * @see SelectionEvent
197 * @see #removeSelectionListener(SelectionListener)
200 public void addSelectionListener(SelectionListener listener) {
202 if (listener == null)
203 SWT.error(SWT.ERROR_NULL_ARGUMENT);
204 TypedListener typedListener = new TypedListener(listener);
205 addListener(SWT.Selection, typedListener);
206 addListener(SWT.DefaultSelection, typedListener);
209 void onDispose(Event event) {
210 removeListener(SWT.Dispose, listener);
211 notifyListeners(SWT.Dispose, event);
212 event.type = SWT.None;
214 table.removeListener(SWT.FocusIn, tableListener);
215 table.removeListener(SWT.MouseDown, tableListener);
216 unhookRowColumnListeners();
217 ScrollBar hBar = table.getHorizontalBar();
219 hBar.removeListener(SWT.Selection, resizeListener);
221 ScrollBar vBar = table.getVerticalBar();
223 vBar.removeListener(SWT.Selection, resizeListener);
227 void keyDown(Event event) {
228 if (row == null) return;
229 switch (event.character) {
231 notifyListeners(SWT.DefaultSelection, new Event());
234 int rowIndex = table.indexOf(row);
235 int columnIndex = column == null ? 0 : table.indexOf(column);
236 switch (event.keyCode) {
238 setRowColumn(Math.max(0, rowIndex - 1), columnIndex, true);
240 case SWT.ARROW_DOWN :
241 setRowColumn(Math.min(rowIndex + 1, table.getItemCount() - 1), columnIndex, true);
243 case SWT.ARROW_LEFT :
244 case SWT.ARROW_RIGHT :
246 int columnCount = table.getColumnCount();
247 if (columnCount == 0) break;
248 int[] order = table.getColumnOrder();
250 while (index < order.length) {
251 if (order[index] == columnIndex) break;
254 if (index == order.length) index = 0;
255 int leadKey = (getStyle() & SWT.RIGHT_TO_LEFT) != 0 ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT;
256 if (event.keyCode == leadKey) {
257 setRowColumn(rowIndex, order[Math.max(0, index - 1)], true);
259 setRowColumn(rowIndex, order[Math.min(columnCount - 1, index + 1)], true);
264 setRowColumn(0, columnIndex, true);
268 int i = table.getItemCount() - 1;
269 setRowColumn(i, columnIndex, true);
274 int index = table.getTopIndex();
275 if (index == rowIndex) {
276 Rectangle rect = table.getClientArea();
277 TableItem item = table.getItem(index);
278 Rectangle itemRect = item.getBounds(0);
279 rect.height -= itemRect.y;
280 int height = table.getItemHeight();
281 int page = Math.max(1, rect.height / height);
282 index = Math.max(0, index - page + 1);
284 setRowColumn(index, columnIndex, true);
289 int index = table.getTopIndex();
290 Rectangle rect = table.getClientArea();
291 TableItem item = table.getItem(index);
292 Rectangle itemRect = item.getBounds(0);
293 rect.height -= itemRect.y;
294 int height = table.getItemHeight();
295 int page = Math.max(1, rect.height / height);
296 int end = table.getItemCount() - 1;
297 index = Math.min(end, index + page - 1);
298 if (index == rowIndex) {
299 index = Math.min(end, index + page - 1);
301 setRowColumn(index, columnIndex, true);
307 void paint(Event event) {
308 if (row == null) return;
309 int columnIndex = column == null ? 0 : table.indexOf(column);
311 gc.setBackground(getBackground());
312 gc.setForeground(getForeground());
313 gc.fillRectangle(event.x, event.y, event.width, event.height);
315 Point size = getSize();
316 Image image = row.getImage(columnIndex);
318 Rectangle imageSize = image.getBounds();
319 int imageY = (size.y - imageSize.height) / 2;
320 gc.drawImage(image, x, imageY);
321 x += imageSize.width;
323 String text = row.getText(columnIndex);
324 if (text.length() > 0) {
325 Rectangle bounds = row.getBounds(columnIndex);
326 Point extent = gc.stringExtent(text);
327 // Temporary code - need a better way to determine table trim
328 String platform = SWT.getPlatform();
329 if ("win32".equals(platform)) { //$NON-NLS-1$
330 if (table.getColumnCount() == 0 || columnIndex == 0) {
333 int alignmnent = column.getAlignment();
334 switch (alignmnent) {
339 x = bounds.width - extent.x - 6;
342 x += (bounds.width - x - extent.x) / 2;
347 if (table.getColumnCount() == 0) {
350 int alignmnent = column.getAlignment();
351 switch (alignmnent) {
356 x = bounds.width- extent.x - 2;
359 x += (bounds.width - x - extent.x) / 2 + 2;
364 int textY = (size.y - extent.y) / 2;
365 gc.drawString(text, x, textY);
367 if (isFocusControl()) {
368 Display display = getDisplay();
369 gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
370 gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
371 gc.drawFocus(0, 0, size.x, size.y);
375 void tableFocusIn(Event event) {
376 if (isDisposed()) return;
378 if (row == null && column == null) return;
383 void tableMouseDown(Event event) {
384 if (isDisposed() || !isVisible()) return;
385 Point pt = new Point(event.x, event.y);
386 int lineWidth = table.getLinesVisible() ? table.getGridLineWidth() : 0;
387 TableItem item = table.getItem(pt);
388 if ((table.getStyle() & SWT.FULL_SELECTION) != 0) {
389 if (item == null) return;
391 int start = item != null ? table.indexOf(item) : table.getTopIndex();
392 int end = table.getItemCount();
393 Rectangle clientRect = table.getClientArea();
394 for (int i = start; i < end; i++) {
395 TableItem nextItem = table.getItem(i);
396 Rectangle rect = nextItem.getBounds(0);
397 if (pt.y >= rect.y && pt.y < rect.y + rect.height + lineWidth) {
401 if (rect.y > clientRect.y + clientRect.height) return;
403 if (item == null) return;
405 TableColumn newColumn = null;
406 int columnCount = table.getColumnCount();
407 if (columnCount == 0) {
408 if ((table.getStyle() & SWT.FULL_SELECTION) == 0) {
409 Rectangle rect = item.getBounds(0);
410 rect.width += lineWidth;
411 rect.height += lineWidth;
412 if (!rect.contains(pt)) return;
415 for (int i = 0; i < columnCount; i++) {
416 Rectangle rect = item.getBounds(i);
417 rect.width += lineWidth;
418 rect.height += lineWidth;
419 if (rect.contains(pt)) {
420 newColumn = table.getColumn(i);
424 if (newColumn == null) {
425 if ((table.getStyle() & SWT.FULL_SELECTION) == 0) return;
426 newColumn = table.getColumn(0);
429 setRowColumn(item, newColumn, true);
433 void setRowColumn(int row, int column, boolean notify) {
434 TableItem item = row == -1 ? null : table.getItem(row);
435 TableColumn col = column == -1 || table.getColumnCount() == 0 ? null : table.getColumn(column);
436 setRowColumn(item, col, notify);
438 void setRowColumn(TableItem row, TableColumn column, boolean notify) {
439 if (this.row == row && this.column == column) {
442 if (this.row != null && this.row != row) {
443 this.row.removeListener(SWT.Dispose, disposeItemListener);
446 if (this.column != null && this.column != column) {
447 this.column.removeListener(SWT.Dispose, disposeColumnListener);
448 this.column.removeListener(SWT.Move, resizeListener);
449 this.column.removeListener(SWT.Resize, resizeListener);
453 if (this.row != row) {
455 row.addListener(SWT.Dispose, disposeItemListener);
458 if (this.column != column && column != null) {
459 this.column = column;
460 column.addListener(SWT.Dispose, disposeColumnListener);
461 column.addListener(SWT.Move, resizeListener);
462 column.addListener(SWT.Resize, resizeListener);
463 table.showColumn(column);
465 int columnIndex = column == null ? 0 : table.indexOf(column);
466 setBounds(row.getBounds(columnIndex));
469 notifyListeners(SWT.Selection, new Event());
472 getAccessible().setFocus(ACC.CHILDID_SELF);
476 public void setVisible(boolean visible) {
478 if (visible) _resize();
479 super.setVisible(visible);
483 * Removes the listener from the collection of listeners who will
484 * be notified when the user changes the receiver's selection.
486 * @param listener the listener which should no longer be notified
488 * @exception IllegalArgumentException <ul>
489 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
491 * @exception SWTException <ul>
492 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
493 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
496 * @see SelectionListener
497 * @see #addSelectionListener(SelectionListener)
501 public void removeSelectionListener(SelectionListener listener) {
503 if (listener == null) {
504 SWT.error(SWT.ERROR_NULL_ARGUMENT);
506 removeListener(SWT.Selection, listener);
507 removeListener(SWT.DefaultSelection, listener);
512 setBounds(-200, -200, 0, 0);
514 int columnIndex = column == null ? 0 : table.indexOf(column);
515 setBounds(row.getBounds(columnIndex));
519 * Returns the index of the column over which the TableCursor is positioned.
521 * @return the column index for the current position
523 * @exception SWTException <ul>
524 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
525 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
528 public int getColumn() {
530 return column == null ? 0 : table.indexOf(column);
533 * Returns the background color that the receiver will use to draw.
535 * @return the receiver's background color
538 public Color getBackground() {
540 if (background == null) {
541 return getDisplay().getSystemColor(BACKGROUND);
546 * Returns the foreground color that the receiver will use to draw.
548 * @return the receiver's foreground color
551 public Color getForeground() {
553 if (foreground == null) {
554 return getDisplay().getSystemColor(FOREGROUND);
559 * Returns the row over which the TableCursor is positioned.
561 * @return the item for the current position, or <code>null</code> if none
563 * @exception SWTException <ul>
564 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
565 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
568 public TableItem getRow() {
573 * Sets the receiver's background color to the color specified
574 * by the argument, or to the default system color for the control
575 * if the argument is null.
577 * Note: This operation is a hint and may be overridden by the platform.
578 * For example, on Windows the background of a Button cannot be changed.
580 * @param color the new color (or null)
582 * @exception IllegalArgumentException <ul>
583 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
585 * @exception SWTException <ul>
586 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
587 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
591 public void setBackground (Color color) {
593 super.setBackground(getBackground());
597 * Sets the receiver's foreground color to the color specified
598 * by the argument, or to the default system color for the control
599 * if the argument is null.
601 * Note: This operation is a hint and may be overridden by the platform.
603 * @param color the new color (or null)
605 * @exception IllegalArgumentException <ul>
606 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
608 * @exception SWTException <ul>
609 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
610 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
614 public void setForeground (Color color) {
616 super.setForeground(getForeground());
620 * Positions the TableCursor over the cell at the given row and column in the parent table.
622 * @param row the index of the row for the cell to select
623 * @param column the index of column for the cell to select
625 * @exception SWTException <ul>
626 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
627 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
631 public void setSelection(int row, int column) {
633 int columnCount = table.getColumnCount();
634 int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1;
636 || row >= table.getItemCount()
638 || column > maxColumnIndex)
639 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
640 setRowColumn(row, column, false);
643 * Positions the TableCursor over the cell at the given row and column in the parent table.
645 * @param row the TableItem of the row for the cell to select
646 * @param column the index of column for the cell to select
648 * @exception SWTException <ul>
649 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
650 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
654 public void setSelection(TableItem row, int column) {
656 int columnCount = table.getColumnCount();
657 int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1;
661 || column > maxColumnIndex)
662 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
663 setRowColumn(table.indexOf(row), column, false);
665 void unhookRowColumnListeners() {
666 if (column != null) {
667 column.removeListener(SWT.Dispose, disposeColumnListener);
668 column.removeListener(SWT.Move, resizeListener);
669 column.removeListener(SWT.Resize, resizeListener);
673 row.removeListener(SWT.Dispose, disposeItemListener);