]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2012 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 CHECKBOX_TEXT= JavaUIMessages.OptionalMessageDialog_dontShowAgain; | |
39 | ||
40 | // Dialog store id constants | |
41 | private static final String STORE_ID= "OptionalMessageDialog.hide."; //$NON-NLS-1$ | |
42 | ||
43 | public static final int NOT_SHOWN= IDialogConstants.CLIENT_ID + 1; | |
44 | ||
45 | private final String fId; | |
46 | private final String fCheckBoxText; | |
47 | ||
48 | private Button fHideDialogCheckBox; | |
49 | ||
50 | ||
51 | /** | |
52 | * Opens the dialog but only if the user hasn't chosen to hide it. | |
53 | * | |
54 | * @return the index of the pressed button or {@link SWT#DEFAULT} if the dialog got dismissed | |
55 | * without pressing a button (e.g. via Esc) or {{@link #NOT_SHOWN} if the dialog was not | |
56 | * shown | |
57 | */ | |
58 | public static int open(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex) { | |
59 | return open(id, parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex, CHECKBOX_TEXT); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Opens the dialog but only if the user hasn't chosen to hide it. | |
64 | * | |
65 | * @return the index of the pressed button or {@link SWT#DEFAULT} if the dialog got dismissed | |
66 | * without pressing a button (e.g. via Esc) or {{@link #NOT_SHOWN} if the dialog was not | |
67 | * shown | |
68 | */ | |
69 | public static int open(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex, String checkboxText) { | |
70 | if (!isDialogEnabled(id)) | |
71 | return OptionalMessageDialog.NOT_SHOWN; | |
72 | ||
73 | MessageDialog dialog= new OptionalMessageDialog(id, parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex, checkboxText); | |
74 | return dialog.open(); | |
75 | } | |
76 | ||
77 | protected OptionalMessageDialog(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex) { | |
78 | this(id, parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex, CHECKBOX_TEXT); | |
79 | } | |
80 | ||
81 | protected OptionalMessageDialog(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex, String checkBoxText) { | |
82 | super(parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex); | |
83 | fId= id; | |
84 | fCheckBoxText= checkBoxText; | |
85 | } | |
86 | ||
87 | @Override | |
88 | protected Control createCustomArea(Composite parent) { | |
89 | Composite composite= new Composite(parent, SWT.NONE); | |
90 | GridLayout layout= new GridLayout(); | |
91 | layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); | |
92 | layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); | |
93 | layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); | |
94 | composite.setLayout(layout); | |
95 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
96 | ||
97 | fHideDialogCheckBox= new Button(composite, SWT.CHECK | SWT.LEFT); | |
98 | fHideDialogCheckBox.setText(fCheckBoxText); | |
99 | fHideDialogCheckBox.addSelectionListener(new SelectionAdapter() { | |
100 | @Override | |
101 | public void widgetSelected(SelectionEvent e) { | |
102 | setDialogEnabled(fId, !((Button)e.widget).getSelection()); | |
103 | } | |
104 | }); | |
105 | applyDialogFont(fHideDialogCheckBox); | |
106 | return fHideDialogCheckBox; | |
107 | } | |
108 | ||
109 | /* | |
110 | * @see org.eclipse.jface.dialogs.MessageDialog#buttonPressed(int) | |
111 | * @since 3.8.1 | |
112 | */ | |
113 | @Override | |
114 | protected void buttonPressed(int buttonId) { | |
115 | super.buttonPressed(buttonId); | |
116 | if (IDialogConstants.CANCEL_LABEL.equals(getButtonLabels()[getReturnCode()])) | |
117 | setDialogEnabled(fId, true); // don't store if cancelled | |
118 | } | |
119 | ||
120 | /* | |
121 | * @see org.eclipse.jface.dialogs.MessageDialog#handleShellCloseEvent() | |
122 | * @since 3.8.1 | |
123 | */ | |
124 | @Override | |
125 | protected void handleShellCloseEvent() { | |
126 | super.handleShellCloseEvent(); | |
127 | setDialogEnabled(fId, true); // don't store if closed without pressing a button | |
128 | } | |
129 | ||
130 | //--------------- Configuration handling -------------- | |
131 | ||
132 | /** | |
133 | * Returns this dialog | |
134 | * | |
135 | * @return the settings to be used | |
136 | */ | |
137 | private static IDialogSettings getDialogSettings() { | |
138 | IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings(); | |
139 | settings= settings.getSection(STORE_ID); | |
140 | if (settings == null) | |
141 | settings= JavaPlugin.getDefault().getDialogSettings().addNewSection(STORE_ID); | |
142 | return settings; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Answers whether the optional dialog is enabled and should be shown. | |
147 | */ | |
148 | public static boolean isDialogEnabled(String key) { | |
149 | IDialogSettings settings= getDialogSettings(); | |
150 | return !settings.getBoolean(key); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Sets whether the optional dialog is enabled and should be shown. | |
155 | */ | |
156 | public static void setDialogEnabled(String key, boolean isEnabled) { | |
157 | IDialogSettings settings= getDialogSettings(); | |
158 | settings.put(key, !isEnabled); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Clears all remembered information about hidden dialogs | |
163 | */ | |
164 | public static void clearAllRememberedStates() { | |
165 | IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings(); | |
166 | settings.addNewSection(STORE_ID); | |
167 | } | |
168 | } |