01: /**
02: *
03: */package net.refractions.udig.internal.ui;
04:
05: import org.eclipse.jface.dialogs.Dialog;
06: import org.eclipse.swt.SWT;
07: import org.eclipse.swt.widgets.Button;
08: import org.eclipse.swt.widgets.Composite;
09: import org.eclipse.swt.widgets.Control;
10: import org.eclipse.swt.widgets.Display;
11: import org.eclipse.swt.widgets.Event;
12: import org.eclipse.swt.widgets.Shell;
13:
14: /**
15: * This class is insane the good part is the push and findbuttons as helpers for testing.
16: *
17: */
18: public class DialogDriver {
19:
20: public static final long DELAY = 100;
21:
22: /**
23: * This should be null if no errors occurred otherwise there should be debug
24: * message
25: */
26: public String error;
27:
28: public static void pushButton(Dialog dialog, int id) {
29: Shell shell = dialog.getShell();
30: Button button = findButton(shell.getChildren(), id, shell);
31: if (!button.isEnabled())
32: throw new RuntimeException(
33: "Error button to press is not enabled"); //$NON-NLS-1$
34: button.notifyListeners(SWT.Selection, new Event());
35: }
36:
37: public static Button findButton(Dialog dialog, int id) {
38: Shell shell = dialog.getShell();
39: Button found = findButton(shell.getChildren(), id, shell);
40: if (found != null)
41: return found;
42:
43: Display display = Display.getCurrent();
44: shell = display.getActiveShell();
45: found = findButton(shell.getChildren(), id, shell);
46: if (found != null)
47: return found;
48:
49: Shell[] shells = display.getShells();
50: for (Shell shell2 : shells) {
51: found = findButton(shell2.getChildren(), id, shell2);
52: if (found != null)
53: return found;
54: }
55: return null;
56: }
57:
58: public static Button findButton(Control[] children, int id,
59: Shell shell) {
60: if (((Integer) shell.getDefaultButton().getData()).intValue() == id) //$NON-NLS-1$
61: return shell.getDefaultButton();
62:
63: for (Control child : children) {
64: if (child instanceof Button) {
65: Button button = (Button) child;
66: Object data = button.getData();
67: if (data != null) {
68: if (((Integer) data).intValue() == id)
69: return button;
70: }
71: }
72: if (child instanceof Composite) {
73: Composite composite = (Composite) child;
74: Button button = findButton(composite.getChildren(), id,
75: shell);
76: if (button != null)
77: return button;
78: }
79: }
80: return null;
81: }
82:
83: }
|