Example usage for org.eclipse.swt.widgets Label getAccessible

List of usage examples for org.eclipse.swt.widgets Label getAccessible

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Label getAccessible.

Prototype

public Accessible getAccessible() 

Source Link

Document

Returns the accessible object for the receiver.

Usage

From source file:AccessibleControlListenerAdding.java

License:asdf

public static void main(String[] args) {
    final Display display = new Display();

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Label label = new Label(shell, SWT.NONE);
    label.setText("asdf");

    Accessible accessible = label.getAccessible();
    accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
        public void getState(AccessibleControlEvent e) {
            super.getState(e);
            System.out.println("AccessibleControlListener");
        }/*from   w  w  w  .j a v  a  2 s  .c o m*/
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }

    display.dispose();
}

From source file:AccessibleListenerAdding.java

License:asdf

public static void main(String[] args) {
    final Display display = new Display();

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Label label = new Label(shell, SWT.NONE);
    label.setText("asdf");

    Accessible accessible = label.getAccessible();
    accessible.addAccessibleListener(new AccessibleAdapter() {
        public void getName(AccessibleEvent e) {
            super.getName(e);
            e.result = "Speak this instead of the text";
            System.out.println("Accessible");

        }/*www . j  a va  2 s  .  co  m*/
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }

    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet350.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 350");
    shell.setLayout(new GridLayout(2, false));
    shell.setText("Accessible Relations");

    Label nameLabel = new Label(shell, SWT.NONE);
    nameLabel.setText("Name:");
    Text nameText = new Text(shell, SWT.BORDER);
    nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Accessible accNameLabel = nameLabel.getAccessible();
    Accessible accNameText = nameText.getAccessible();
    accNameLabel.addRelation(ACC.RELATION_LABEL_FOR, accNameText);
    accNameText.addRelation(ACC.RELATION_LABELLED_BY, accNameLabel);

    Group addressGroup = new Group(shell, SWT.NONE);
    addressGroup.setText("Address");
    addressGroup.setLayout(new GridLayout(2, false));
    addressGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

    Label streetLabel = new Label(addressGroup, SWT.NONE);
    streetLabel.setText("Street:");
    Text streetText = new Text(addressGroup, SWT.BORDER);
    streetText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Accessible accStreetLabel = streetLabel.getAccessible();
    Accessible accStreetText = streetText.getAccessible();
    accStreetLabel.addRelation(ACC.RELATION_LABEL_FOR, accStreetText);
    accStreetText.addRelation(ACC.RELATION_LABELLED_BY, accStreetLabel);

    Label cityLabel = new Label(addressGroup, SWT.NONE);
    cityLabel.setText("City:");
    Text cityText = new Text(addressGroup, SWT.BORDER);
    cityText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Accessible accCityLabel = cityLabel.getAccessible();
    Accessible accCityText = cityText.getAccessible();
    accCityLabel.addRelation(ACC.RELATION_LABEL_FOR, accCityText);
    accCityText.addRelation(ACC.RELATION_LABELLED_BY, accCityLabel);

    Accessible accAddressGroup = addressGroup.getAccessible();
    accStreetText.addRelation(ACC.RELATION_MEMBER_OF, accAddressGroup);
    accStreetText.addRelation(ACC.RELATION_LABELLED_BY, accAddressGroup);
    accCityText.addRelation(ACC.RELATION_MEMBER_OF, accAddressGroup);
    accCityText.addRelation(ACC.RELATION_LABELLED_BY, accAddressGroup);

    shell.pack();//from w ww.  j a  va2s.  co  m
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet334.java

public static void main(String[] arg) {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Snippet 334");
    shell.setLayout(new GridLayout(2, false));
    Label label = new Label(shell, SWT.NONE);
    label.setText("Test:");
    canvas = new Canvas(shell, SWT.MULTI | SWT.BORDER);
    final Caret caret = new Caret(canvas, SWT.NONE);
    canvas.addPaintListener(e -> {/*ww  w .  j av  a 2  s . c  o  m*/
        GC gc = e.gc;
        gc.drawText(text, 10, 10);
        caret.setBounds(10, 10, 2, gc.getFontMetrics().getHeight());
        Rectangle rect = canvas.getClientArea();
        if (canvas.isFocusControl()) {
            gc.drawFocus(rect.x, rect.y, rect.width, rect.height);
        }
    });
    canvas.addTraverseListener(e -> {
        switch (e.detail) {
        case SWT.TRAVERSE_TAB_NEXT:
        case SWT.TRAVERSE_TAB_PREVIOUS:
            e.doit = true; // enable traversal
            break;
        }
    });

    // key listener enables traversal out)
    canvas.addKeyListener(keyPressedAdapter(e -> {
    }));

    canvas.addFocusListener(focusGainedAdapter(event -> canvas.redraw()));
    canvas.addFocusListener(focusLostAdapter(event -> canvas.redraw()));
    canvas.addMouseListener(mouseDownAdapter(e -> canvas.setFocus()));
    Accessible acc = canvas.getAccessible();
    acc.addRelation(ACC.RELATION_LABELLED_BY, label.getAccessible());
    acc.addAccessibleControlListener(new AccessibleControlAdapter() {
        @Override
        public void getRole(AccessibleControlEvent e) {
            e.detail = ACC.ROLE_TEXT;
        }

        @Override
        public void getLocation(AccessibleControlEvent e) {
            Rectangle rect = canvas.getBounds();
            Point pt = shell.toDisplay(rect.x, rect.y);
            e.x = pt.x;
            e.y = pt.y;
            e.width = rect.width;
            e.height = rect.height;
        }

        @Override
        public void getValue(AccessibleControlEvent e) {
            e.result = text;
        }

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

        @Override
        public void getChildCount(AccessibleControlEvent e) {
            e.detail = 0;
        }

        @Override
        public void getState(AccessibleControlEvent e) {
            e.detail = ACC.STATE_NORMAL | ACC.STATE_FOCUSABLE;
            if (canvas.isFocusControl())
                e.detail |= ACC.STATE_FOCUSED | ACC.STATE_SELECTABLE;
        }
    });
    acc.addAccessibleTextListener(new AccessibleTextExtendedAdapter() {
        @Override
        public void getSelectionRange(AccessibleTextEvent e) {
            // select the first 4 characters for testing
            e.offset = 0;
            e.length = 4;
        }

        @Override
        public void getCaretOffset(AccessibleTextEvent e) {
            e.offset = 0;
        }

        @Override
        public void getTextBounds(AccessibleTextEvent e) {
            // for now, assume that start = 0 and end = text.length
            GC gc = new GC(canvas);
            Point extent = gc.textExtent(text);
            gc.dispose();
            Rectangle rect = display.map(canvas, null, 10, 10, extent.x, extent.y);
            e.x = rect.x;
            e.y = rect.y;
            e.width = rect.width;
            e.height = rect.height;
        }

        @Override
        public void getText(AccessibleTextEvent e) {
            int start = 0, end = text.length();
            switch (e.type) {
            case ACC.TEXT_BOUNDARY_ALL:
                start = e.start;
                end = e.end;
                break;
            case ACC.TEXT_BOUNDARY_CHAR:
                start = e.count >= 0 ? e.start + e.count : e.start - e.count;
                start = Math.max(0, Math.min(end, start));
                end = start;
                e.count = start - e.start;
                e.start = start;
                e.end = start;
                break;
            case ACC.TEXT_BOUNDARY_LINE:
                int offset = e.count <= 0 ? e.start : e.end;
                offset = Math.min(offset, text.length());
                int lineCount = 0;
                int index = 0;
                while (index != -1) {
                    lineCount++;
                    index = text.indexOf("\n", index);
                    if (index != -1)
                        index++;
                }
                e.count = e.count < 0 ? Math.max(e.count, -lineCount) : Math.min(e.count, lineCount);
                index = 0;
                int lastIndex = 0;
                String[] lines = new String[lineCount];
                for (int i = 0; i < lines.length; i++) {
                    index = text.indexOf("\n", index);
                    lines[i] = index != -1 ? text.substring(lastIndex, index) : text.substring(lastIndex);
                    lastIndex = index;
                    index++;
                }
                int len = 0;
                int lineAtOffset = 0;
                for (int i = 0; i < lines.length; i++) {
                    len += lines[i].length();
                    if (len >= e.offset) {
                        lineAtOffset = i;
                        break;
                    }
                }
                int result = Math.max(0, Math.min(lineCount - 1, lineAtOffset + e.count));
                e.count = result - lineAtOffset;
                e.result = lines[result];
                break;
            }
            e.result = text.substring(start, end);
        }

        @Override
        public void getSelectionCount(AccessibleTextEvent e) {
            e.count = 1;
        }

        @Override
        public void getSelection(AccessibleTextEvent e) {
            // there is only 1 selection, so index = 0
            getSelectionRange(e);
            e.start = e.offset;
            e.end = e.offset + e.length;
        }

        @Override
        public void getRanges(AccessibleTextEvent e) {
            // for now, ignore bounding box
            e.start = 0;
            e.end = text.length() - 1;
        }

        @Override
        public void getCharacterCount(AccessibleTextEvent e) {
            e.count = text.length();
        }

        @Override
        public void getVisibleRanges(AccessibleTextEvent e) {
            e.start = 0;
            e.end = text.length() - 1;
        }
    });
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet340.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 340");
    shell.setLayout(new GridLayout());
    shell.setText("Description Relation Example");

    // (works with either a Label or a READ_ONLY Text)
    final Label liveLabel = new Label(shell, SWT.BORDER | SWT.READ_ONLY);
    //   final Text liveLabel = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
    liveLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    liveLabel.setText("Live region messages go here");

    new Label(shell, SWT.NONE).setText("Type an integer from 1 to 4:");

    final Text textField = new Text(shell, SWT.SINGLE | SWT.BORDER);
    textField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    textField.addModifyListener(e -> {
        String textValue = textField.getText();
        String message = textValue + " is not valid input.";
        try {/*from   w  ww.j  a v  a 2s . c  om*/
            int value = Integer.parseInt(textValue);
            switch (value) {
            case 1:
                message = "One for the money,";
                break;
            case 2:
                message = "Two for the show,";
                break;
            case 3:
                message = "Three to get ready,";
                break;
            case 4:
                message = "And four to go!";
                break;
            }
        } catch (NumberFormatException ex) {
        }
        liveLabel.setText(message);
        textField.getAccessible().sendEvent(ACC.EVENT_DESCRIPTION_CHANGED, null);
        textField.setSelection(0, textField.getCharCount());
    });
    textField.getAccessible().addRelation(ACC.RELATION_DESCRIBED_BY, liveLabel.getAccessible());
    liveLabel.getAccessible().addRelation(ACC.RELATION_DESCRIPTION_FOR, textField.getAccessible());
    textField.getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getDescription(AccessibleEvent event) {
            event.result = liveLabel.getText();
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}