Created a separated 'externaltools' plugin: initial check-in
[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.
31          * This key must be a boolean preference in the preference store.
32          */
33         private String fPreferenceKey = null;
34         /**
35          * The message displayed to the user, with the toggle button
36          */
37         private String fToggleMessage = null;
38         private Button fToggleButton = null;
39         /**
40          * The preference store which will be affected by the toggle button
41          */
42         IPreferenceStore fStore = null;
43
44         public MessageDialogWithToggle(Shell parentShell, String dialogTitle, Image image, String message, int dialogImageType, String[] dialogButtonLabels, int defaultIndex, String preferenceKey, String toggleMessage, IPreferenceStore store) {
45                 super(parentShell, dialogTitle, image, message, dialogImageType, dialogButtonLabels, defaultIndex);
46                 fStore = store;
47                 fPreferenceKey = preferenceKey;
48                 fToggleMessage = toggleMessage;
49         }
50         
51         protected Control createDialogArea(Composite parent) {
52                 Composite dialogArea = (Composite) super.createDialogArea(parent);
53                 fToggleButton = createCheckButton(dialogArea, fToggleMessage);
54                 getToggleButton().setSelection(fStore.getBoolean(fPreferenceKey));
55                 return dialogArea;
56         }
57
58         /**
59          * Creates a button with the given label and sets the default
60          * configuration data.
61          */
62         protected Button createCheckButton(Composite parent, String label) {
63                 Button button= new Button(parent, SWT.CHECK | SWT.LEFT);
64                 button.setText(label);
65
66                 GridData data = new GridData(SWT.NONE);
67                 data.horizontalSpan= 2;
68                 data.horizontalAlignment= GridData.CENTER;
69                 button.setLayoutData(data);
70                 button.setFont(parent.getFont());
71
72                 return button;
73         }
74
75         /**
76          * When the OK button is pressed, store the preference.
77          * 
78          * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
79          */
80         protected void buttonPressed(int id) {
81                 if (id == IDialogConstants.OK_ID) {  // was the OK button pressed?
82                         storePreference();
83                 }
84                 super.buttonPressed(id);
85         }
86
87         /**
88          * Store the preference based on the user's selection
89          */
90         protected void storePreference() {
91                 fStore.setValue(fPreferenceKey, getToggleButton().getSelection());
92         }
93
94         /**
95          * Returns the button used to toggle the dialog preference
96          * 
97          * @return Button the preference toggle button
98          */
99         protected Button getToggleButton() {
100                 return fToggleButton;
101         }
102         
103         /**
104          * Convenience method to open a simple confirm (OK/Cancel) dialog.
105          *
106          * @param parent the parent shell of the dialog, or <code>null</code> if none
107          * @param title the dialog's title, or <code>null</code> if none
108          * @param message the message
109          * @return <code>true</code> if the user presses the OK button,
110          *    <code>false</code> otherwise
111          */
112         public static boolean openConfirm(Shell parent, String title, String message, String preferenceKey, String toggleMessage, IPreferenceStore store) {
113                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(
114                         parent,
115                         title,
116                         null,   // accept the default window icon
117                         message,
118                         QUESTION,
119                         new String[] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL},
120                         0,              // OK is the default
121                         preferenceKey,
122                         toggleMessage,
123                         store);
124                 return dialog.open() == 0;
125         }
126         /**
127          * Convenience method to open a standard error dialog.
128          *
129          * @param parent the parent shell of the dialog, or <code>null</code> if none
130          * @param title the dialog's title, or <code>null</code> if none
131          * @param message the message
132          */
133         public static void openError(Shell parent, String title, String message, String preferenceKey, String toggleMessage, IPreferenceStore store) {
134                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(
135                         parent,
136                         title,
137                         null,   // accept the default window icon
138                         message,
139                         ERROR,
140                         new String[] {IDialogConstants.OK_LABEL},
141                         0,              // ok is the default
142                         preferenceKey,
143                         toggleMessage,
144                         store);
145                 dialog.open();
146         }
147         /**
148          * Convenience method to open a standard information dialog.
149          *
150          * @param parent the parent shell of the dialog, or <code>null</code> if none
151          * @param title the dialog's title, or <code>null</code> if none
152          * @param message the message
153          */
154         public static void openInformation(
155                 Shell parent,
156                 String title,
157                 String message, String preferenceKey, String toggleMessage, IPreferenceStore store) {
158                         MessageDialogWithToggle dialog =
159                                 new MessageDialogWithToggle(parent, title, null, // accept the default window icon
160                 message, INFORMATION, new String[] { IDialogConstants.OK_LABEL }, 0,            // ok is the default 
161                 preferenceKey, toggleMessage, store);
162                 dialog.open();
163         }
164         /**
165          * Convenience method to open a simple Yes/No question dialog.
166          *
167          * @param parent the parent shell of the dialog, or <code>null</code> if none
168          * @param title the dialog's title, or <code>null</code> if none
169          * @param message the message
170          * @return <code>true</code> if the user presses the OK button,
171          *    <code>false</code> otherwise
172          */
173         public static boolean openQuestion(Shell parent, String title, String message, String preferenceKey, String toggleMessage, IPreferenceStore store) {
174                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(
175                         parent,
176                         title,
177                         null,   // accept the default window icon
178                         message,
179                         QUESTION,
180                         new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL},
181                         0,              // yes is the default
182                         preferenceKey,
183                         toggleMessage,
184                         store);
185                 return dialog.open() == 0;
186         }
187         /**
188          * Convenience method to open a standard warning dialog.
189          *
190          * @param parent the parent shell of the dialog, or <code>null</code> if none
191          * @param title the dialog's title, or <code>null</code> if none
192          * @param message the message
193          */
194         public static void openWarning(Shell parent, String title, String message, String preferenceKey, String toggleMessage, IPreferenceStore store) {
195                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(
196                         parent,
197                         title,
198                         null,   // accept the default window icon
199                         message,
200                         WARNING,
201                         new String[] {IDialogConstants.OK_LABEL},
202                         0,              // ok is the default
203                         preferenceKey,
204                         toggleMessage,
205                         store);
206                 dialog.open();
207         }
208
209 }