1 package net.sourceforge.phpdt.externaltools.internal.ui;
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 **********************************************************************/
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;
22 * An message dialog which allows the user to set a boolean preference.
24 * This is typically used to set a preference that determines if the dialog
25 * should be shown in the future
27 public class MessageDialogWithToggle extends MessageDialog {
30 * The preference key which is set by the toggle button. This key must be a
31 * boolean preference in the preference store.
33 private String fPreferenceKey = null;
36 * The message displayed to the user, with the toggle button
38 private String fToggleMessage = null;
40 private Button fToggleButton = null;
43 * The preference store which will be affected by the toggle button
45 IPreferenceStore fStore = null;
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);
54 fPreferenceKey = preferenceKey;
55 fToggleMessage = toggleMessage;
58 protected Control createDialogArea(Composite parent) {
59 Composite dialogArea = (Composite) super.createDialogArea(parent);
60 fToggleButton = createCheckButton(dialogArea, fToggleMessage);
61 getToggleButton().setSelection(fStore.getBoolean(fPreferenceKey));
66 * Creates a button with the given label and sets the default configuration
69 protected Button createCheckButton(Composite parent, String label) {
70 Button button = new Button(parent, SWT.CHECK | SWT.LEFT);
71 button.setText(label);
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());
83 * When the OK button is pressed, store the preference.
85 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
87 protected void buttonPressed(int id) {
88 if (id == IDialogConstants.OK_ID) { // was the OK button pressed?
91 super.buttonPressed(id);
95 * Store the preference based on the user's selection
97 protected void storePreference() {
98 fStore.setValue(fPreferenceKey, getToggleButton().getSelection());
102 * Returns the button used to toggle the dialog preference
104 * @return Button the preference toggle button
106 protected Button getToggleButton() {
107 return fToggleButton;
111 * Convenience method to open a simple confirm (OK/Cancel) dialog.
114 * the parent shell of the dialog, or <code>null</code> if none
116 * the dialog's title, or <code>null</code> if none
119 * @return <code>true</code> if the user presses the OK button,
120 * <code>false</code> otherwise
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;
134 * Convenience method to open a standard error dialog.
137 * the parent shell of the dialog, or <code>null</code> if none
139 * the dialog's title, or <code>null</code> if none
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
151 preferenceKey, toggleMessage, store);
156 * Convenience method to open a standard information dialog.
159 * the parent shell of the dialog, or <code>null</code> if none
161 * the dialog's title, or <code>null</code> if none
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,
170 null, // accept the default window icon
171 message, INFORMATION,
172 new String[] { IDialogConstants.OK_LABEL }, 0, // ok is the
174 preferenceKey, toggleMessage, store);
179 * Convenience method to open a simple Yes/No question dialog.
182 * the parent shell of the dialog, or <code>null</code> if none
184 * the dialog's title, or <code>null</code> if none
187 * @return <code>true</code> if the user presses the OK button,
188 * <code>false</code> otherwise
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;
202 * Convenience method to open a standard warning dialog.
205 * the parent shell of the dialog, or <code>null</code> if none
207 * the dialog's title, or <code>null</code> if none
211 public static void openWarning(Shell parent, String title, String message,
212 String preferenceKey, String toggleMessage, IPreferenceStore store) {
213 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent,
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);