Refactory
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpdt / externaltools / internal / ui / MessageDialogWithToggle.java
1 package net.sourceforge.phpdt.externaltools.internal.ui;
2
3 /**********************************************************************
4  Copyright (c) 2000, 2002 IBM Corp.  All rights reserved.
5  This file is made available under the terms of the Common Public License v1.0
6  which accompanies this distribution, and is available at
7  http://www.eclipse.org/legal/cpl-v10.html
8  **********************************************************************/
9
10 import org.eclipse.jface.dialogs.IDialogConstants;
11 import org.eclipse.jface.dialogs.MessageDialog;
12 import org.eclipse.jface.preference.IPreferenceStore;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Shell;
20
21 /**
22  * An message dialog which allows the user to set a boolean preference.
23  * 
24  * This is typically used to set a preference that determines if the dialog
25  * should be shown in the future
26  */
27 public class MessageDialogWithToggle extends MessageDialog {
28
29         /**
30          * The preference key which is set by the toggle button. This key must be a
31          * boolean preference in the preference store.
32          */
33         private String fPreferenceKey = null;
34
35         /**
36          * The message displayed to the user, with the toggle button
37          */
38         private String fToggleMessage = null;
39
40         private Button fToggleButton = null;
41
42         /**
43          * The preference store which will be affected by the toggle button
44          */
45         IPreferenceStore fStore = null;
46
47         public MessageDialogWithToggle(Shell parentShell, String dialogTitle,
48                         Image image, String message, int dialogImageType,
49                         String[] dialogButtonLabels, int defaultIndex,
50                         String preferenceKey, String toggleMessage, IPreferenceStore store) {
51                 super(parentShell, dialogTitle, image, message, dialogImageType,
52                                 dialogButtonLabels, defaultIndex);
53                 fStore = store;
54                 fPreferenceKey = preferenceKey;
55                 fToggleMessage = toggleMessage;
56         }
57
58         protected Control createDialogArea(Composite parent) {
59                 Composite dialogArea = (Composite) super.createDialogArea(parent);
60                 fToggleButton = createCheckButton(dialogArea, fToggleMessage);
61                 getToggleButton().setSelection(fStore.getBoolean(fPreferenceKey));
62                 return dialogArea;
63         }
64
65         /**
66          * Creates a button with the given label and sets the default configuration
67          * data.
68          */
69         protected Button createCheckButton(Composite parent, String label) {
70                 Button button = new Button(parent, SWT.CHECK | SWT.LEFT);
71                 button.setText(label);
72
73                 GridData data = new GridData(SWT.NONE);
74                 data.horizontalSpan = 2;
75                 data.horizontalAlignment = GridData.CENTER;
76                 button.setLayoutData(data);
77                 button.setFont(parent.getFont());
78
79                 return button;
80         }
81
82         /**
83          * When the OK button is pressed, store the preference.
84          * 
85          * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
86          */
87         protected void buttonPressed(int id) {
88                 if (id == IDialogConstants.OK_ID) { // was the OK button pressed?
89                         storePreference();
90                 }
91                 super.buttonPressed(id);
92         }
93
94         /**
95          * Store the preference based on the user's selection
96          */
97         protected void storePreference() {
98                 fStore.setValue(fPreferenceKey, getToggleButton().getSelection());
99         }
100
101         /**
102          * Returns the button used to toggle the dialog preference
103          * 
104          * @return Button the preference toggle button
105          */
106         protected Button getToggleButton() {
107                 return fToggleButton;
108         }
109
110         /**
111          * Convenience method to open a simple confirm (OK/Cancel) dialog.
112          * 
113          * @param parent
114          *            the parent shell of the dialog, or <code>null</code> if none
115          * @param title
116          *            the dialog's title, or <code>null</code> if none
117          * @param message
118          *            the message
119          * @return <code>true</code> if the user presses the OK button,
120          *         <code>false</code> otherwise
121          */
122         public static boolean openConfirm(Shell parent, String title,
123                         String message, String preferenceKey, String toggleMessage,
124                         IPreferenceStore store) {
125                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent,
126                                 title, null, // accept the default window icon
127                                 message, QUESTION, new String[] { IDialogConstants.OK_LABEL,
128                                                 IDialogConstants.CANCEL_LABEL }, 0, // OK is the default
129                                 preferenceKey, toggleMessage, store);
130                 return dialog.open() == 0;
131         }
132
133         /**
134          * Convenience method to open a standard error dialog.
135          * 
136          * @param parent
137          *            the parent shell of the dialog, or <code>null</code> if none
138          * @param title
139          *            the dialog's title, or <code>null</code> if none
140          * @param message
141          *            the message
142          */
143         public static void openError(Shell parent, String title, String message,
144                         String preferenceKey, String toggleMessage, IPreferenceStore store) {
145                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent,
146                                 title, null, // accept the default window icon
147                                 message, ERROR, new String[] { IDialogConstants.OK_LABEL }, 0, // ok
148                                                                                                                                                                 // is
149                                                                                                                                                                 // the
150                                                                                                                                                                 // default
151                                 preferenceKey, toggleMessage, store);
152                 dialog.open();
153         }
154
155         /**
156          * Convenience method to open a standard information dialog.
157          * 
158          * @param parent
159          *            the parent shell of the dialog, or <code>null</code> if none
160          * @param title
161          *            the dialog's title, or <code>null</code> if none
162          * @param message
163          *            the message
164          */
165         public static void openInformation(Shell parent, String title,
166                         String message, String preferenceKey, String toggleMessage,
167                         IPreferenceStore store) {
168                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent,
169                                 title,
170                                 null, // accept the default window icon
171                                 message, INFORMATION,
172                                 new String[] { IDialogConstants.OK_LABEL }, 0, // ok is the
173                                                                                                                                 // default
174                                 preferenceKey, toggleMessage, store);
175                 dialog.open();
176         }
177
178         /**
179          * Convenience method to open a simple Yes/No question dialog.
180          * 
181          * @param parent
182          *            the parent shell of the dialog, or <code>null</code> if none
183          * @param title
184          *            the dialog's title, or <code>null</code> if none
185          * @param message
186          *            the message
187          * @return <code>true</code> if the user presses the OK button,
188          *         <code>false</code> otherwise
189          */
190         public static boolean openQuestion(Shell parent, String title,
191                         String message, String preferenceKey, String toggleMessage,
192                         IPreferenceStore store) {
193                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent,
194                                 title, null, // accept the default window icon
195                                 message, QUESTION, new String[] { IDialogConstants.YES_LABEL,
196                                                 IDialogConstants.NO_LABEL }, 0, // yes is the default
197                                 preferenceKey, toggleMessage, store);
198                 return dialog.open() == 0;
199         }
200
201         /**
202          * Convenience method to open a standard warning dialog.
203          * 
204          * @param parent
205          *            the parent shell of the dialog, or <code>null</code> if none
206          * @param title
207          *            the dialog's title, or <code>null</code> if none
208          * @param message
209          *            the message
210          */
211         public static void openWarning(Shell parent, String title, String message,
212                         String preferenceKey, String toggleMessage, IPreferenceStore store) {
213                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent,
214                                 title,
215                                 null, // accept the default window icon
216                                 message, WARNING, new String[] { IDialogConstants.OK_LABEL },
217                                 0, // ok is the default
218                                 preferenceKey, toggleMessage, store);
219                 dialog.open();
220         }
221
222 }