Search in sources :

Example 1 with AccessibleTableEvent

use of org.eclipse.swt.accessibility.AccessibleTableEvent in project eclipse.platform.swt by eclipse.

the class CTable method initAccessibility.

void initAccessibility() {
    // TODO: does this all work if CTable is virtual?
    final Accessible accessibleTable = getAccessible();
    accessibleTable.addAccessibleListener(new AccessibleAdapter() {

        @Override
        public void getName(AccessibleEvent e) {
            /* CTables take their name from the preceding Label, if any. */
            Control[] siblings = getParent().getChildren();
            for (int i = 0; i < siblings.length; i++) {
                if (i != 0 && siblings[i] == CTable.this) {
                    Control control = siblings[i - 1];
                    if (control instanceof Label) {
                        e.result = ((Label) control).getText();
                    }
                }
            }
        }

        @Override
        public void getHelp(AccessibleEvent e) {
            /* A CTable's toolTip text (if any) can be used as its help text. */
            e.result = getToolTipText();
        }
    });
    accessibleTable.addAccessibleControlListener(new AccessibleControlAdapter() {

        /* Child IDs are assigned as follows:
		 * - column header ids (if any) are numbered from 0 to columnCount - 1
		 * - cell ids are numbered in row-major order (starting from columnCount if there are columns)
		 * Accessibles are returned in getChild.
		 */
        @Override
        public void getChild(AccessibleControlEvent e) {
            int childID = e.childID;
            // childID == index
            if (childID == ACC.CHILDID_CHILD_AT_INDEX)
                childID = e.detail;
            if (columns.length > 0 && 0 <= childID && childID < columns.length) {
                // header cell
                CTableColumn column = columns[childID];
                e.accessible = column.getAccessible(accessibleTable);
            } else {
                // item cell
                int columnCount = columns.length > 0 ? columns.length : 1;
                if (columns.length > 0)
                    childID -= columnCount;
                if (0 <= childID && childID < itemsCount * columnCount) {
                    int rowIndex = childID / columnCount;
                    int columnIndex = childID - rowIndex * columnCount;
                    e.accessible = items[rowIndex].getAccessible(accessibleTable, columnIndex);
                }
            }
        }

        @Override
        public void getChildAtPoint(AccessibleControlEvent e) {
            Point point = toControl(e.x, e.y);
            if (columns.length > 0 && point.y < getHeaderHeight()) {
                // header cell
                int columnIndex = computeColumnIntersect(point.x, 0);
                if (columnIndex != -1) {
                    CTableColumn column = columns[columnIndex];
                    e.accessible = column.getAccessible(accessibleTable);
                }
            } else {
                // item cell
                int columnIndex = columns.length > 0 ? computeColumnIntersect(point.x, 0) : 0;
                if (columnIndex != -1) {
                    int rowIndex = (point.y - getHeaderHeight()) / itemHeight + topIndex;
                    if (0 <= rowIndex && rowIndex < itemsCount) {
                        if (items[rowIndex].getHitBounds().contains(point)) {
                            /* considers the x value */
                            e.accessible = items[rowIndex].getAccessible(accessibleTable, columnIndex);
                        }
                    }
                }
            }
        }

        @Override
        public void getChildCount(AccessibleControlEvent e) {
            e.detail = columns.length > 0 ? columns.length + itemsCount * columns.length : itemsCount;
        }

        @Override
        public void getChildren(AccessibleControlEvent e) {
            int childIdCount = columns.length > 0 ? columns.length + itemsCount * columns.length : itemsCount;
            Object[] children = new Object[childIdCount];
            for (int i = 0; i < childIdCount; i++) {
                children[i] = Integer.valueOf(i);
            }
            e.children = children;
        }

        @Override
        public void getFocus(AccessibleControlEvent e) {
            e.childID = isFocusControl() ? ACC.CHILDID_SELF : ACC.CHILDID_NONE;
        }

        @Override
        public void getLocation(AccessibleControlEvent e) {
            Rectangle location = null;
            Point pt = null;
            int childID = e.childID;
            if (childID == ACC.CHILDID_SELF) {
                // table
                location = getBounds();
                pt = getParent().toDisplay(location.x, location.y);
            } else if (columns.length > 0 && 0 <= childID && childID < columns.length) {
                // header cell
                CTableColumn column = columns[childID];
                location = new Rectangle(column.getX(), 0, column.getWidth(), getHeaderHeight());
                pt = toDisplay(location.x, location.y);
            } else {
                // item cell
                int columnCount = columns.length > 0 ? columns.length : 1;
                if (columns.length > 0)
                    childID -= columnCount;
                if (0 <= childID && childID < itemsCount * columnCount) {
                    int rowIndex = childID / columnCount;
                    int columnIndex = childID - rowIndex * columnCount;
                    location = items[rowIndex].getBounds(columnIndex);
                    pt = toDisplay(location.x, location.y);
                }
            }
            if (location != null && pt != null) {
                e.x = pt.x;
                e.y = pt.y;
                e.width = location.width;
                e.height = location.height;
            }
        }

        @Override
        public void getRole(AccessibleControlEvent e) {
            e.detail = e.childID == ACC.CHILDID_SELF ? ACC.ROLE_TABLE : ACC.ROLE_TABLECELL;
        }

        @Override
        public void getSelection(AccessibleControlEvent e) {
            int columnCount = columns.length > 0 ? columns.length : 1;
            int[] selectionIndices = getSelectionIndices();
            Object[] selectedChildren = new Object[selectionIndices.length * columnCount];
            for (int i = 0; i < selectionIndices.length; i++) {
                int row = selectionIndices[i];
                for (int col = 0; col < columnCount; col++) {
                    selectedChildren[i] = Integer.valueOf(row * columnCount + col);
                }
            }
            e.children = selectedChildren;
        }

        @Override
        public void getState(AccessibleControlEvent e) {
            int state = ACC.STATE_NORMAL;
            int childID = e.childID;
            if (childID == ACC.CHILDID_SELF) {
                // table
                state |= ACC.STATE_FOCUSABLE;
                if (isFocusControl()) {
                    state |= ACC.STATE_FOCUSED;
                }
            } else if (columns.length > 0 && 0 <= childID && childID < columns.length) {
                // header cell
                /* CTable does not support header cell focus or selection. */
                state |= ACC.STATE_SIZEABLE;
            } else {
                // item cell
                int columnCount = columns.length > 0 ? columns.length : 1;
                if (columns.length > 0)
                    childID -= columnCount;
                if (0 <= childID && childID < itemsCount * columnCount) {
                    /* CTable does not support cell selection (only row selection). */
                    int rowIndex = childID / columnCount;
                    state |= ACC.STATE_SELECTABLE;
                    if (isFocusControl()) {
                        state |= ACC.STATE_FOCUSABLE;
                    }
                    if (items[rowIndex].isSelected()) {
                        state |= ACC.STATE_SELECTED;
                        if (isFocusControl()) {
                            state |= ACC.STATE_FOCUSED;
                        }
                    }
                }
            }
            e.detail = state;
        }
    });
    accessibleTable.addAccessibleTableListener(new AccessibleTableAdapter() {

        @Override
        public void deselectColumn(AccessibleTableEvent e) {
        /* CTable does not support column selection. */
        }

        @Override
        public void deselectRow(AccessibleTableEvent e) {
            deselect(e.row);
            e.result = ACC.OK;
        }

        @Override
        public void getCaption(AccessibleTableEvent e) {
            // TODO: What is a caption? How does it differ from name? Should app supply?
            e.result = "This is the Custom Table's Test Caption";
        }

        @Override
        public void getCell(AccessibleTableEvent e) {
            int index = e.row;
            if (0 <= index && index < itemsCount) {
                CTableItem row = items[index];
                index = e.column;
                if (columns.length == 0 || 0 <= index && index < columns.length) {
                    e.accessible = row.getAccessible(accessibleTable, index);
                }
            }
        }

        @Override
        public void getColumnCount(AccessibleTableEvent e) {
            int columnCount = columns.length > 0 ? columns.length : 1;
            e.count = columnCount;
        }

        @Override
        public void getColumnDescription(AccessibleTableEvent e) {
            // TODO: What is a description? How does it differ from name? Should app supply?
            e.result = "This is the Custom Table's Test Description for column " + e.column;
        }

        // public void getColumnHeader(AccessibleTableEvent e) {
        // e.accessible = header.getAccessible();
        // }
        @Override
        public void getColumnHeaderCells(AccessibleTableEvent e) {
            if (columns.length == 0) {
                /* The CTable is being used as a list, and there are no headers. */
                e.accessibles = null;
            } else {
                Accessible[] accessibles = new Accessible[columns.length];
                for (int i = 0; i < columns.length; i++) {
                    CTableColumn column = columns[i];
                    accessibles[i] = column.getAccessible(accessibleTable);
                }
                e.accessibles = accessibles;
            }
        }

        @Override
        public void getRowCount(AccessibleTableEvent e) {
            e.count = itemsCount;
        }

        @Override
        public void getRowDescription(AccessibleTableEvent e) {
            // TODO: What is a description? How does it differ from name? Should app supply?
            e.result = "This is the Custom Table's Test Description for row " + e.row;
        }

        @Override
        public void getRowHeader(AccessibleTableEvent e) {
        /* CTable does not support row headers. */
        }

        @Override
        public void getSelectedCellCount(AccessibleTableEvent e) {
            int columnCount = columns.length > 0 ? columns.length : 1;
            e.count = selectedItems.length * columnCount;
        }

        @Override
        public void getSelectedCells(AccessibleTableEvent e) {
            int columnCount = columns.length > 0 ? columns.length : 1;
            Accessible[] accessibles = new Accessible[selectedItems.length * columnCount];
            for (int r = 0; r < selectedItems.length; r++) {
                CTableItem row = selectedItems[r];
                for (int c = 0; c < columnCount; c++) accessibles[r + c] = row.getAccessible(accessibleTable, c);
            }
            e.accessibles = accessibles;
        }

        @Override
        public void getSelectedColumnCount(AccessibleTableEvent e) {
            e.count = 0;
        /* CTable does not support column selection. */
        }

        @Override
        public void getSelectedColumns(AccessibleTableEvent e) {
        /* CTable does not support column selection. */
        }

        @Override
        public void getSelectedRowCount(AccessibleTableEvent e) {
            e.count = selectedItems.length;
        }

        @Override
        public void getSelectedRows(AccessibleTableEvent e) {
            int[] selectedIndices = new int[selectedItems.length];
            for (int i = 0; i < selectedItems.length; i++) {
                selectedIndices[i] = selectedItems[i].index;
            }
            e.selected = selectedIndices;
        }

        @Override
        public void getSummary(AccessibleTableEvent e) {
            // TODO: What is a summary? How does it differ from name? Should app supply?
            e.result = "This is the Custom Table's Summary";
        }

        @Override
        public void isColumnSelected(AccessibleTableEvent e) {
            e.isSelected = false;
        /* CTable does not support column selection. */
        }

        @Override
        public void isRowSelected(AccessibleTableEvent e) {
            e.isSelected = isSelected(e.row);
        }

        @Override
        public void selectColumn(AccessibleTableEvent e) {
        /* CTable does not support column selection. */
        }

        @Override
        public void selectRow(AccessibleTableEvent e) {
            select(e.row);
            e.result = ACC.OK;
        }

        @Override
        public void setSelectedColumn(AccessibleTableEvent e) {
        /* CTable does not support column selection. */
        }

        @Override
        public void setSelectedRow(AccessibleTableEvent e) {
            setSelection(e.row);
            e.result = ACC.OK;
        }
    });
}
Also used : AccessibleControlEvent(org.eclipse.swt.accessibility.AccessibleControlEvent) AccessibleTableEvent(org.eclipse.swt.accessibility.AccessibleTableEvent) AccessibleControlAdapter(org.eclipse.swt.accessibility.AccessibleControlAdapter) Label(org.eclipse.swt.widgets.Label) Rectangle(org.eclipse.swt.graphics.Rectangle) AccessibleAdapter(org.eclipse.swt.accessibility.AccessibleAdapter) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Control(org.eclipse.swt.widgets.Control) AccessibleTableAdapter(org.eclipse.swt.accessibility.AccessibleTableAdapter) AccessibleEvent(org.eclipse.swt.accessibility.AccessibleEvent) Accessible(org.eclipse.swt.accessibility.Accessible)

Aggregations

Accessible (org.eclipse.swt.accessibility.Accessible)1 AccessibleAdapter (org.eclipse.swt.accessibility.AccessibleAdapter)1 AccessibleControlAdapter (org.eclipse.swt.accessibility.AccessibleControlAdapter)1 AccessibleControlEvent (org.eclipse.swt.accessibility.AccessibleControlEvent)1 AccessibleEvent (org.eclipse.swt.accessibility.AccessibleEvent)1 AccessibleTableAdapter (org.eclipse.swt.accessibility.AccessibleTableAdapter)1 AccessibleTableEvent (org.eclipse.swt.accessibility.AccessibleTableEvent)1 Point (org.eclipse.swt.graphics.Point)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 Control (org.eclipse.swt.widgets.Control)1 Label (org.eclipse.swt.widgets.Label)1