KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > gui > swt > SWTOptionDialog


1 package com.sslexplorer.agent.client.gui.swt;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.events.SelectionAdapter;
5 import org.eclipse.swt.events.SelectionEvent;
6 import org.eclipse.swt.layout.GridData;
7 import org.eclipse.swt.layout.GridLayout;
8 import org.eclipse.swt.layout.RowLayout;
9 import org.eclipse.swt.widgets.Button;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Dialog;
12 import org.eclipse.swt.widgets.Display;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Shell;
15
16 public class SWTOptionDialog extends Dialog {
17     private Object JavaDoc message;
18     private String JavaDoc okText;
19     private String JavaDoc cancelText;
20     private boolean ok;
21     private Shell shell;
22     private boolean open;
23
24     /**
25      * Constructor
26      */

27     public SWTOptionDialog(Shell parent, int style, String JavaDoc okText, String JavaDoc cancelText, String JavaDoc title, Object JavaDoc message) {
28         // Let users override the default styles
29
super(parent, style);
30         this.okText = okText;
31         this.cancelText = cancelText;
32         setText(title);
33         setMessage(message);
34
35         // Create the dialog window
36
shell = new Shell(getParent(), getStyle());
37         shell.setText(getText());
38     }
39
40     /**
41      * Gets the message. May be a string or {@link Composite}.
42      *
43      * @return String
44      */

45     public Object JavaDoc getMessage() {
46         return message;
47     }
48
49     /**
50      * Sets the message. May be a string or {@link Composite}.
51      *
52      * @param message the new message
53      */

54     public void setMessage(Object JavaDoc message) {
55         this.message = message;
56     }
57
58     /**
59      * Opens the dialog and returns the input
60      *
61      * @return ok
62      */

63     public boolean open() {
64         createContents(shell);
65         shell.pack();
66         SWTUtil.center(shell);
67         open = true;
68         shell.open();
69         Display display = getParent().getDisplay();
70         while (open) {
71             if (!display.readAndDispatch()) {
72                 display.sleep();
73             }
74         }
75         // Return the entered value, or null
76
return ok;
77     }
78     
79     /**
80      * Close the dialog
81      */

82     public void close() {
83         if(!shell.isDisposed())
84             shell.close();
85     }
86
87     /**
88      * Creates the dialog's contents
89      *
90      * @param shell the dialog window
91      */

92     private void createContents(final Shell shell) {
93         shell.setLayout(new GridLayout(1, true));
94
95         Composite c = new Composite(shell, 0);
96         GridLayout gridLayout = new GridLayout ();
97         c.setLayout (gridLayout);
98
99         if (message instanceof String JavaDoc) {
100             Label label = new Label(shell, SWT.NONE);
101             label.setText((String JavaDoc) message);
102             GridData data = new GridData ();
103             data.horizontalAlignment = GridData.CENTER;
104             data.grabExcessHorizontalSpace = true;
105             label.setLayoutData (data);
106         } else if (message != null) {
107             GridData data = new GridData(GridData.FILL_BOTH);
108             ((Composite) message).setLayoutData(data);
109         }
110         
111         Composite c2 = new Composite(shell, 0);
112         RowLayout rowLayout = new RowLayout ();
113         c2.setLayout (rowLayout);
114
115         // Create the OK button and add a handler
116
// so that pressing it will set input
117
// to the entered value
118
Button ok = new Button(c2, SWT.PUSH);
119         ok.setText(okText);
120         ok.addSelectionListener(new SelectionAdapter() {
121             public void widgetSelected(SelectionEvent event) {
122                 SWTOptionDialog.this.ok = true;
123                 open = false;
124             }
125         });
126
127         // Create the cancel button and add a handler
128
// so that pressing it will set input to null
129
if (cancelText != null) {
130             Button cancel = new Button(c2, SWT.PUSH);
131             cancel.setText(cancelText);
132             cancel.addSelectionListener(new SelectionAdapter() {
133                 public void widgetSelected(SelectionEvent event) {
134                     open = false;
135                 }
136             });
137         }
138         
139         GridData data = new GridData ();
140         data.horizontalAlignment = GridData.CENTER;
141         data.grabExcessHorizontalSpace = true;
142         c2.setLayoutData(data);
143         
144         shell.pack();
145
146         // Set the OK button as the default, so
147
// user can type input and press Enter
148
// to dismiss
149
shell.setDefaultButton(ok);
150     }
151
152     public Shell getShell() {
153         return shell;
154     }
155 }
156
Popular Tags