KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dialogs > OptionalMessageDialog


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.ui.dialogs;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.IDialogSettings;
26 import org.eclipse.jface.dialogs.MessageDialog;
27
28 import org.eclipse.jdt.internal.ui.JavaPlugin;
29 import org.eclipse.jdt.internal.ui.JavaUIMessages;
30
31 /**
32  * This is a <code>MessageDialog</code> which allows the user
33  * to choose that the dialog isn't shown again the next time.
34  */

35 public class OptionalMessageDialog extends MessageDialog {
36     
37     // String constants for widgets
38
private static final String JavaDoc CHECKBOX_TEXT= JavaUIMessages.OptionalMessageDialog_dontShowAgain;
39
40     // Dialog store id constants
41
private static final String JavaDoc STORE_ID= "OptionalMessageDialog.hide."; //$NON-NLS-1$
42

43     public static final int NOT_SHOWN= IDialogConstants.CLIENT_ID + 1;
44     
45     private Button fHideDialogCheckBox;
46     private String JavaDoc fId;
47
48     /**
49      * Opens the dialog but only if the user hasn't choosen to hide it.
50      * Returns <code>NOT_SHOWN</code> if the dialog was not shown.
51      */

52     public static int open(String JavaDoc id, Shell parent, String JavaDoc title, Image titleImage, String JavaDoc message, int dialogType, String JavaDoc[] buttonLabels, int defaultButtonIndex) {
53         if (!isDialogEnabled(id))
54             return OptionalMessageDialog.NOT_SHOWN;
55         
56         MessageDialog dialog= new OptionalMessageDialog(id, parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex);
57         return dialog.open();
58     }
59
60     protected OptionalMessageDialog(String JavaDoc id, Shell parent, String JavaDoc title, Image titleImage, String JavaDoc message, int dialogType, String JavaDoc[] buttonLabels, int defaultButtonIndex) {
61         super(parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex);
62         fId= id;
63     }
64
65     protected Control createCustomArea(Composite parent) {
66         Composite composite= new Composite(parent, SWT.NONE);
67         GridLayout layout= new GridLayout();
68         layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
69         layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
70         layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
71         composite.setLayout(layout);
72         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
73         
74         fHideDialogCheckBox= new Button(composite, SWT.CHECK | SWT.LEFT);
75         fHideDialogCheckBox.setText(CHECKBOX_TEXT);
76         fHideDialogCheckBox.addSelectionListener(new SelectionAdapter() {
77             public void widgetSelected(SelectionEvent e) {
78                 setDialogEnabled(fId, !((Button)e.widget).getSelection());
79             }
80         });
81         applyDialogFont(fHideDialogCheckBox);
82         return fHideDialogCheckBox;
83     }
84     
85     //--------------- Configuration handling --------------
86

87     /**
88      * Returns this dialog
89      *
90      * @return the settings to be used
91      */

92     private static IDialogSettings getDialogSettings() {
93         IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
94         settings= settings.getSection(STORE_ID);
95         if (settings == null)
96             settings= JavaPlugin.getDefault().getDialogSettings().addNewSection(STORE_ID);
97         return settings;
98     }
99         
100     /**
101      * Answers whether the optional dialog is enabled and should be shown.
102      */

103     public static boolean isDialogEnabled(String JavaDoc key) {
104         IDialogSettings settings= getDialogSettings();
105         return !settings.getBoolean(key);
106     }
107     
108     /**
109      * Sets whether the optional dialog is enabled and should be shown.
110      */

111     public static void setDialogEnabled(String JavaDoc key, boolean isEnabled) {
112         IDialogSettings settings= getDialogSettings();
113         settings.put(key, !isEnabled);
114     }
115
116     /**
117      * Clears all remembered information about hidden dialogs
118      */

119     public static void clearAllRememberedStates() {
120         IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
121         settings.addNewSection(STORE_ID);
122     }
123 }
124
Popular Tags