KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > parts > MessageDialogWithToggle


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.parts;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.preference.IPreferenceStore;
27
28 import org.eclipse.ui.internal.WorkbenchMessages;
29
30 /**
31  * A message dialog which also allows the user to adjust a toggle setting.
32  *
33  * This is typically used to allow the user to indicate whether the dialog
34  * should be shown in the future.
35  */

36 public class MessageDialogWithToggle extends MessageDialog {
37     
38     /**
39      * The message displayed to the user, with the toggle button
40      */

41     private String JavaDoc toggleMessage = null;
42     private boolean toggleState = false;
43     private Button toggleButton = null;
44     
45     /**
46      * The preference store which will be affected by the toggle button
47      */

48     IPreferenceStore fStore = null;
49
50     /**
51      * Creates a message dialog with a toggle.
52      * See the superclass constructor for info on the other parameters.
53      *
54      * @param toggleMessage the message for the toggle control, or <code>null</code>
55      * for the default message ("Do not show this message again").
56      * @param toggleState the initial state for the toggle
57      *
58      */

59     public MessageDialogWithToggle(Shell parentShell, String JavaDoc dialogTitle, Image image, String JavaDoc message, int dialogImageType, String JavaDoc[] dialogButtonLabels, int defaultIndex, String JavaDoc toggleMessage, boolean toggleState) {
60         super(parentShell, dialogTitle, image, message, dialogImageType, dialogButtonLabels, defaultIndex);
61         this.toggleMessage = toggleMessage;
62         this.toggleState = toggleState;
63     }
64
65     /**
66      * Returns the toggle state. This can be called even after the dialog
67      * is closed.
68      *
69      * @return <code>true</code> if the toggle button is checked,
70      * <code>false</code> if not
71      */

72     public boolean getToggleState() {
73         return toggleState;
74     }
75     
76     /* (non-Javadoc)
77      * Method declared in Dialog.
78      */

79     protected Control createDialogArea(Composite parent) {
80         Composite dialogArea = (Composite) super.createDialogArea(parent);
81         toggleButton = createToggleButton(dialogArea);
82         return dialogArea;
83     }
84
85     /**
86      * Creates a toggle button with the toggle message and state.
87      */

88     protected Button createToggleButton(Composite parent) {
89         final Button button= new Button(parent, SWT.CHECK | SWT.LEFT);
90         String JavaDoc text = toggleMessage;
91         if (text == null) {
92             text = WorkbenchMessages.getString("MessageDialogWithToggle.defaultToggleMessage"); //$NON-NLS-1$
93
}
94         button.setText(text);
95         button.setSelection(toggleState);
96         
97         GridData data = new GridData(SWT.NONE);
98         data.horizontalSpan= 2;
99         data.horizontalAlignment= GridData.CENTER;
100         button.setLayoutData(data);
101         button.setFont(parent.getFont());
102
103         button.addSelectionListener(new SelectionAdapter() {
104             public void widgetSelected(SelectionEvent e) {
105                 toggleState = button.getSelection();
106             }
107
108         });
109         return button;
110     }
111     
112     /**
113      * Returns the toggle button.
114      *
115      * @return the toggle button
116      */

117     protected Button getToggleButton() {
118         return toggleButton;
119     }
120     
121     /**
122      * Convenience method to open a simple confirm (OK/Cancel) dialog.
123      *
124      * @param parent the parent shell of the dialog, or <code>null</code> if none
125      * @param title the dialog's title, or <code>null</code> if none
126      * @param message the message
127      * @param toggleMessage the message for the toggle control, or <code>null</code>
128      * for the default message ("Don't show me this message again").
129      * @param toggleState the initial state for the toggle
130      * @return the dialog, after being closed by the user, which the client can
131      * only call <code>getReturnCode()</code> or <code>getToggleState()</code>
132      */

133     public static MessageDialogWithToggle openConfirm(Shell parent, String JavaDoc title, String JavaDoc message, String JavaDoc toggleMessage, boolean toggleState) {
134         MessageDialogWithToggle dialog = new MessageDialogWithToggle(
135             parent,
136             title,
137             null, // accept the default window icon
138
message,
139             QUESTION,
140             new String JavaDoc[] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL},
141             0, // OK is the default
142
toggleMessage,
143             toggleState);
144         dialog.open();
145         return dialog;
146     }
147     
148     /**
149      * Convenience method to open a standard error dialog.
150      *
151      * @param parent the parent shell of the dialog, or <code>null</code> if none
152      * @param title the dialog's title, or <code>null</code> if none
153      * @param message the message
154      * @param toggleMessage the message for the toggle control, or <code>null</code>
155      * for the default message ("Don't show me this message again").
156      * @param toggleState the initial state for the toggle
157      * @return the dialog, after being closed by the user, which the client can
158      * only call <code>getReturnCode()</code> or <code>getToggleState()</code>
159      */

160     public static MessageDialogWithToggle openError(Shell parent, String JavaDoc title, String JavaDoc message, String JavaDoc toggleMessage, boolean toggleState) {
161         MessageDialogWithToggle dialog = new MessageDialogWithToggle(
162             parent,
163             title,
164             null, // accept the default window icon
165
message,
166             ERROR,
167             new String JavaDoc[] {IDialogConstants.OK_LABEL},
168             0, // ok is the default
169
toggleMessage,
170             toggleState);
171         dialog.open();
172         return dialog;
173     }
174     
175     /**
176      * Convenience method to open a standard information dialog.
177      *
178      * @param parent the parent shell of the dialog, or <code>null</code> if none
179      * @param title the dialog's title, or <code>null</code> if none
180      * @param message the message
181      * @param toggleMessage the message for the toggle control, or <code>null</code>
182      * for the default message ("Don't show me this message again").
183      * @param toggleState the initial state for the toggle
184      * @return the dialog, after being closed by the user, which the client can
185      * only call <code>getReturnCode()</code> or <code>getToggleState()</code>
186      */

187     public static MessageDialogWithToggle openInformation(Shell parent, String JavaDoc title, String JavaDoc message, String JavaDoc toggleMessage, boolean toggleState) {
188         MessageDialogWithToggle dialog = new MessageDialogWithToggle(
189             parent,
190             title,
191             null, // accept the default window icon
192
message,
193             INFORMATION,
194             new String JavaDoc[] { IDialogConstants.OK_LABEL },
195             0, // ok is the default
196
toggleMessage,
197             toggleState);
198         dialog.open();
199         return dialog;
200     }
201     
202     /**
203      * Convenience method to open a simple Yes/No question dialog.
204      *
205      * @param parent the parent shell of the dialog, or <code>null</code> if none
206      * @param title the dialog's title, or <code>null</code> if none
207      * @param message the message
208      * @param toggleMessage the message for the toggle control, or <code>null</code>
209      * for the default message ("Don't show me this message again").
210      * @param toggleState the initial state for the toggle
211      * @return the dialog, after being closed by the user, which the client can
212      * only call <code>getReturnCode()</code> or <code>getToggleState()</code>
213      */

214     public static MessageDialogWithToggle openQuestion(Shell parent, String JavaDoc title, String JavaDoc message, String JavaDoc toggleMessage, boolean toggleState) {
215         MessageDialogWithToggle dialog = new MessageDialogWithToggle(
216             parent,
217             title,
218             null, // accept the default window icon
219
message,
220             QUESTION,
221             new String JavaDoc[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL},
222             0, // yes is the default
223
toggleMessage,
224             toggleState);
225         dialog.open();
226         return dialog;
227     }
228     
229     /**
230      * Convenience method to open a standard warning dialog.
231      *
232      * @param parent the parent shell of the dialog, or <code>null</code> if none
233      * @param title the dialog's title, or <code>null</code> if none
234      * @param message the message
235      * @param toggleMessage the message for the toggle control, or <code>null</code>
236      * for the default message ("Don't show me this message again").
237      * @param toggleState the initial state for the toggle
238      * @return the dialog, after being closed by the user, which the client can
239      * only call <code>getReturnCode()</code> or <code>getToggleState()</code>
240      */

241     public static MessageDialogWithToggle openWarning(Shell parent, String JavaDoc title, String JavaDoc message, String JavaDoc toggleMessage, boolean toggleState) {
242         MessageDialogWithToggle dialog = new MessageDialogWithToggle(
243             parent,
244             title,
245             null, // accept the default window icon
246
message,
247             WARNING,
248             new String JavaDoc[] {IDialogConstants.OK_LABEL},
249             0, // ok is the default
250
toggleMessage,
251             toggleState);
252         dialog.open();
253         return dialog;
254     }
255
256 }
257
Popular Tags