Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet363.java110
1 files changed, 110 insertions, 0 deletions
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet363.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet363.java
new file mode 100644
index 0000000000..88e5dd12b9
--- /dev/null
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet363.java
@@ -0,0 +1,110 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.swt.snippets;
+
+/*
+ * Accessibility example snippet: Declare a message area to be a "live region",
+ * and send "changed" events to a screen reader when the message area is updated.
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ */
+import org.eclipse.swt.*;
+import org.eclipse.swt.accessibility.*;
+import org.eclipse.swt.events.*;
+import org.eclipse.swt.graphics.*;
+import org.eclipse.swt.layout.*;
+import org.eclipse.swt.widgets.*;
+
+public class Snippet363 {
+ static final String errorMessage = "Error: Number expected.";
+ static Label icon;
+ static Image errorIcon;
+ static Text liveLabel;
+
+public static void main(String [] args) {
+ Display display = new Display();
+ errorIcon = display.getSystemImage(SWT.ICON_ERROR);
+ Shell shell = new Shell(display);
+ shell.setLayout(new GridLayout(2, false));
+ shell.setText("LiveRegion Test");
+
+ icon = new Label(shell, SWT.NONE);
+ icon.setLayoutData(new GridData(32, 32));
+
+ liveLabel = new Text(shell, SWT.READ_ONLY);
+ GC gc = new GC(liveLabel);
+ Point pt = gc.textExtent(errorMessage);
+ GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
+ data.minimumWidth = pt.x + gc.getFontMetrics().getAverageCharWidth() * 2;
+ gc.dispose();
+ liveLabel.setLayoutData(data);
+ liveLabel.setText("");
+ liveLabel.getAccessible().addAccessibleAttributeListener(new AccessibleAttributeAdapter() {
+ public void getAttributes(AccessibleAttributeEvent e) {
+ e.attributes = new String[] {
+ "container-live", "polite",
+ "live", "polite",
+ "container-live-role", "status",
+ };
+ }
+ });
+
+ final Label textFieldLabel = new Label(shell, SWT.NONE);
+ textFieldLabel.setText("Type a number:");
+ textFieldLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
+
+ final Text textField = new Text(shell, SWT.SINGLE | SWT.BORDER);
+ textField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
+
+ final Button okButton = new Button(shell, SWT.PUSH);
+ okButton.setText("OK");
+ okButton.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false, 2, 1));
+ okButton.setEnabled(false);
+
+ textField.addModifyListener(new ModifyListener() {
+ public void modifyText(ModifyEvent e) {
+ boolean isNumber = false;
+ String textValue = textField.getText();
+ try {
+ Integer.parseInt(textValue);
+ isNumber = true;
+ setMessageText(false, "Thank-you");
+ } catch (NumberFormatException ex) {
+ if (textValue.isEmpty()) {
+ setMessageText(false, "");
+ } else {
+ setMessageText(true, "Error: Number expected.");
+ }
+ }
+ okButton.setEnabled(isNumber);
+ }
+ });
+
+ textField.setFocus();
+ shell.pack();
+ shell.open();
+ while (!shell.isDisposed()) {
+ if (!display.readAndDispatch()) display.sleep();
+ }
+ display.dispose();
+}
+
+static void setMessageText(boolean error, String newMessage) {
+ String oldMessage = liveLabel.getText();
+ icon.setImage(error ? errorIcon : null);
+ liveLabel.setText(newMessage);
+ liveLabel.getAccessible().sendEvent(ACC.EVENT_ATTRIBUTE_CHANGED, null);
+ liveLabel.getAccessible().sendEvent(ACC.EVENT_TEXT_CHANGED, new Object[] {ACC.TEXT_DELETE, 0, oldMessage.length(), oldMessage});
+ liveLabel.getAccessible().sendEvent(ACC.EVENT_TEXT_CHANGED, new Object[] {ACC.TEXT_INSERT, 0, newMessage.length(), newMessage});
+}
+
+}

Back to the top