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.
 
  31          * This key must be a boolean preference in the preference store.
 
  33         private String fPreferenceKey = null;
 
  35          * The message displayed to the user, with the toggle button
 
  37         private String fToggleMessage = null;
 
  38         private Button fToggleButton = null;
 
  40          * The preference store which will be affected by the toggle button
 
  42         IPreferenceStore fStore = null;
 
  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);
 
  47                 fPreferenceKey = preferenceKey;
 
  48                 fToggleMessage = toggleMessage;
 
  51         protected Control createDialogArea(Composite parent) {
 
  52                 Composite dialogArea = (Composite) super.createDialogArea(parent);
 
  53                 fToggleButton = createCheckButton(dialogArea, fToggleMessage);
 
  54                 getToggleButton().setSelection(fStore.getBoolean(fPreferenceKey));
 
  59          * Creates a button with the given label and sets the default
 
  62         protected Button createCheckButton(Composite parent, String label) {
 
  63                 Button button= new Button(parent, SWT.CHECK | SWT.LEFT);
 
  64                 button.setText(label);
 
  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());
 
  76          * When the OK button is pressed, store the preference.
 
  78          * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
 
  80         protected void buttonPressed(int id) {
 
  81                 if (id == IDialogConstants.OK_ID) {  // was the OK button pressed?
 
  84                 super.buttonPressed(id);
 
  88          * Store the preference based on the user's selection
 
  90         protected void storePreference() {
 
  91                 fStore.setValue(fPreferenceKey, getToggleButton().getSelection());
 
  95          * Returns the button used to toggle the dialog preference
 
  97          * @return Button the preference toggle button
 
  99         protected Button getToggleButton() {
 
 100                 return fToggleButton;
 
 104          * Convenience method to open a simple confirm (OK/Cancel) dialog.
 
 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
 
 112         public static boolean openConfirm(Shell parent, String title, String message, String preferenceKey, String toggleMessage, IPreferenceStore store) {
 
 113                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(
 
 116                         null,   // accept the default window icon
 
 119                         new String[] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL},
 
 120                         0,              // OK is the default
 
 124                 return dialog.open() == 0;
 
 127          * Convenience method to open a standard error dialog.
 
 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
 
 133         public static void openError(Shell parent, String title, String message, String preferenceKey, String toggleMessage, IPreferenceStore store) {
 
 134                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(
 
 137                         null,   // accept the default window icon
 
 140                         new String[] {IDialogConstants.OK_LABEL},
 
 141                         0,              // ok is the default
 
 148          * Convenience method to open a standard information dialog.
 
 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
 
 154         public static void openInformation(
 
 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);
 
 165          * Convenience method to open a simple Yes/No question dialog.
 
 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
 
 173         public static boolean openQuestion(Shell parent, String title, String message, String preferenceKey, String toggleMessage, IPreferenceStore store) {
 
 174                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(
 
 177                         null,   // accept the default window icon
 
 180                         new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL},
 
 181                         0,              // yes is the default
 
 185                 return dialog.open() == 0;
 
 188          * Convenience method to open a standard warning dialog.
 
 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
 
 194         public static void openWarning(Shell parent, String title, String message, String preferenceKey, String toggleMessage, IPreferenceStore store) {
 
 195                 MessageDialogWithToggle dialog = new MessageDialogWithToggle(
 
 198                         null,   // accept the default window icon
 
 201                         new String[] {IDialogConstants.OK_LABEL},
 
 202                         0,              // ok is the default