KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > ErrorDialogWithToggle


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

11 package org.eclipse.jdt.internal.debug.ui;
12
13  
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.jface.dialogs.ErrorDialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Shell;
24
25 /**
26  * An error dialog which allows the user to set
27  * a boolean preference.
28  *
29  * This is typically used to set a preference that
30  * determines if the dialog should be shown in
31  * the future
32  */

33 public class ErrorDialogWithToggle extends ErrorDialog {
34
35     /**
36      * The preference key which is set by the toggle button.
37      * This key must be a boolean preference in the preference store.
38      */

39     private String JavaDoc fPreferenceKey= null;
40     /**
41      * The message displayed to the user, with the toggle button
42      */

43     private String JavaDoc fToggleMessage= null;
44     private Button fToggleButton= null;
45     /**
46      * The preference store which will be affected by the toggle button
47      */

48     IPreferenceStore fStore= null;
49
50     public ErrorDialogWithToggle(Shell parentShell, String JavaDoc dialogTitle, String JavaDoc message, IStatus status, String JavaDoc preferenceKey, String JavaDoc toggleMessage, IPreferenceStore store) {
51         super(parentShell, dialogTitle, message, status, IStatus.WARNING | IStatus.ERROR | IStatus.INFO);
52         fStore= store;
53         fPreferenceKey= preferenceKey;
54         fToggleMessage= toggleMessage;
55     }
56
57     protected Control createDialogArea(Composite parent) {
58         Composite dialogComposite= (Composite) super.createDialogArea(parent);
59         dialogComposite.setFont(parent.getFont());
60         setToggleButton(createCheckButton(dialogComposite, fToggleMessage));
61         getToggleButton().setSelection(!fStore.getBoolean(fPreferenceKey));
62         applyDialogFont(dialogComposite);
63         return dialogComposite;
64     }
65     
66     /**
67      * Creates a button with the given label and sets the default
68      * configuration data.
69      */

70     private Button createCheckButton(Composite parent, String JavaDoc label) {
71         Button button= new Button(parent, SWT.CHECK | SWT.LEFT);
72         button.setText(label);
73
74         GridData data = new GridData(SWT.NONE);
75         data.horizontalSpan= 2;
76         data.horizontalAlignment= GridData.CENTER;
77         button.setLayoutData(data);
78         button.setFont(parent.getFont());
79         
80         return button;
81     }
82
83     protected void buttonPressed(int id) {
84         if (id == IDialogConstants.OK_ID) { // was the OK button pressed?
85
storePreference();
86         }
87         super.buttonPressed(id);
88     }
89     
90     private void storePreference() {
91         fStore.setValue(fPreferenceKey, !getToggleButton().getSelection());
92     }
93
94     protected Button getToggleButton() {
95         return fToggleButton;
96     }
97
98     protected void setToggleButton(Button button) {
99         fToggleButton = button;
100     }
101     
102     /**
103      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
104      */

105     protected void createButtonsForButtonBar(Composite parent) {
106         super.createButtonsForButtonBar(parent);
107         getButton(IDialogConstants.OK_ID).setFocus();
108     }
109 }
110
Popular Tags