Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 54347 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/ui/internal/ide/messages.properties (+1 lines)
Lines 1190-1195 Link Here
1190
PathVariableSelectionDialog.ExtensionDialog.description = Choose extension to {0}
1190
PathVariableSelectionDialog.ExtensionDialog.description = Choose extension to {0}
1191
1191
1192
MessageDialogWithToggle.defaultToggleMessage = &Do not show this message again
1192
MessageDialogWithToggle.defaultToggleMessage = &Do not show this message again
1193
UserPreferencePromptDialog.defaultToggleMessage = &Do not show this message again
1193
1194
1194
# ==============================================================================
1195
# ==============================================================================
1195
# Editor Framework
1196
# Editor Framework
(-)src/org/eclipse/ui/internal/ide/dialogs/UserPreferencePromptDialog.java (+381 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.ui.internal.ide.dialogs;
13
14
import org.eclipse.jface.dialogs.IDialogConstants;
15
import org.eclipse.jface.dialogs.MessageDialog;
16
import org.eclipse.jface.preference.IPreferenceStore;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.graphics.Image;
21
import org.eclipse.swt.layout.GridData;
22
import org.eclipse.swt.widgets.Button;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Control;
25
import org.eclipse.swt.widgets.Shell;
26
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
27
28
29
/**
30
 * A message dialog which also allows the user to adjust a toggle setting.
31
 * 
32
 * This is typically used to allow the user to indicate whether the dialog
33
 * should be shown in the future.
34
 */
35
public class UserPreferencePromptDialog extends MessageDialog {
36
	public static final String ALWAYS = "always"; //$NON-NLS-1$
37
	public static final String NEVER = "never"; //$NON-NLS-1$
38
	public static final String PROMPT = "prompt"; //$NON-NLS-1$
39
	public static final String OK = "ok"; //$NON-NLS-1$
40
	
41
	
42
	/**
43
	 * The message displayed to the user, with the toggle button
44
	 */
45
	protected String toggleMessage = null;
46
	protected boolean toggleState = false;
47
	protected Button toggleButton = null;
48
	
49
	protected Button[] buttons;
50
	protected String[] buttonLabels;
51
	protected int defaultButtonIndex;
52
	
53
	/**
54
	 * The preference store which will be affected by the toggle button.
55
	 */
56
	protected IPreferenceStore prefStore = null;
57
	
58
	/**
59
	 * The preference store key that will be used to store the value.
60
	 */
61
	protected String prefKey = null;
62
	 
63
	
64
	/**
65
	 * Creates a message dialog with a toggle.
66
	 * See the superclass constructor for info on the other parameters.
67
	 * 
68
	 * @param toggleMessage the message for the toggle control, or <code>null</code> 
69
	 *   for the default message ("Do not show this message again").
70
	 * @param toggleState the initial state for the toggle 
71
	 * 
72
	 */
73
	public UserPreferencePromptDialog(Shell parentShell, String dialogTitle, Image image, String message, int dialogImageType, String[] dialogButtonLabels, int defaultIndex, String toggleMessage, boolean toggleState) {
74
		super(parentShell, dialogTitle, image, message, dialogImageType, dialogButtonLabels, defaultIndex);
75
		this.toggleMessage = toggleMessage;
76
		this.toggleState = toggleState; 
77
		this.buttonLabels = dialogButtonLabels;
78
	}
79
	
80
	/**
81
	 * Returns the toggle state.  This can be called even after the dialog
82
	 * is closed.
83
	 * 
84
	 * @return <code>true</code> if the toggle button is checked, 
85
	 *   <code>false</code> if not
86
	 */
87
	public boolean getToggleState() {
88
		return toggleState;
89
	}
90
91
	/**
92
	 * @param prefKey The prefKey to set.
93
	 */
94
	public void setPrefKey(String prefKey) {
95
		this.prefKey = prefKey;
96
	}
97
	/**
98
	 * @param prefStore The prefStore to set.
99
	 */
100
	public void setPrefStore(IPreferenceStore prefStore) {
101
		this.prefStore = prefStore;
102
	}
103
	
104
	/* (non-Javadoc)
105
	 * Method declared in Dialog.
106
	 */
107
	protected Control createDialogArea(Composite parent) {
108
		Composite dialogArea = (Composite) super.createDialogArea(parent);
109
		toggleButton = createToggleButton(dialogArea);
110
		return dialogArea;
111
	}
112
	
113
	/* 
114
	 * (non-Javadoc)
115
	 * Method declared on Dialog.
116
	 */
117
	protected void createButtonsForButtonBar(Composite parent) {
118
		buttons = new Button[buttonLabels.length];
119
		for (int i = 0; i < buttonLabels.length; i++) {
120
			int id = i;
121
			String label = buttonLabels[i];
122
			if (label == IDialogConstants.OK_LABEL) {
123
				id = IDialogConstants.OK_ID;
124
			} else if (label == IDialogConstants.YES_LABEL) {
125
				id = IDialogConstants.YES_ID;
126
			} else if (label == IDialogConstants.NO_LABEL) {
127
				id = IDialogConstants.NO_ID;
128
			} else if (label == IDialogConstants.CANCEL_LABEL) {
129
				id = IDialogConstants.CANCEL_ID;
130
			}
131
			Button button = createButton(parent, id, label, defaultButtonIndex == i);
132
			buttons[i] = button;
133
		}
134
	}
135
	
136
	
137
	/**
138
	 * Creates a toggle button with the toggle message and state.
139
	 */
140
	protected Button createToggleButton(Composite parent) {
141
		final Button button= new Button(parent, SWT.CHECK | SWT.LEFT);
142
		String text = toggleMessage; 
143
		if (text == null) {
144
			text = IDEWorkbenchMessages.getString("UserPreferencePromptDialog.defaultToggleMessage"); //$NON-NLS-1$
145
		}
146
		button.setText(text);
147
		button.setSelection(toggleState);
148
		
149
		GridData data = new GridData(SWT.NONE);
150
		data.horizontalSpan= 2;
151
		data.horizontalAlignment= GridData.CENTER;
152
		button.setLayoutData(data);
153
		button.setFont(parent.getFont());
154
		
155
		button.addSelectionListener(new SelectionAdapter() {
156
			public void widgetSelected(SelectionEvent e) {
157
				toggleState = button.getSelection();
158
			}
159
			
160
		});
161
		return button;
162
	}
163
	
164
	/**
165
	 * Returns the toggle button.
166
	 * 
167
	 * @return the toggle button
168
	 */
169
	protected Button getToggleButton() {
170
		return toggleButton;
171
	}
172
	
173
	
174
	
175
	/* (non-Javadoc)
176
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
177
	 */
178
	protected void buttonPressed(int buttonId) {
179
		super.buttonPressed(buttonId);
180
		
181
		if (buttonId != IDialogConstants.CANCEL_ID && toggleState && prefStore != null && prefKey != null) {
182
			if (buttonId == IDialogConstants.YES_ID) {
183
				prefStore.setValue(prefKey, ALWAYS);
184
			} else if (buttonId == IDialogConstants.NO_ID) {
185
				prefStore.setValue(prefKey, NEVER);
186
			} else if (buttonId == IDialogConstants.OK_ID) {
187
				prefStore.setValue(prefKey, ALWAYS);
188
			}
189
		}
190
	}
191
	
192
	/**
193
	 * Convenience method to open a simple confirm (OK/Cancel) dialog.
194
	 *
195
	 * @param parent the parent shell of the dialog, or <code>null</code> if none
196
	 * @param title the dialog's title, or <code>null</code> if none
197
	 * @param message the message
198
	 * @param toggleMessage the message for the toggle control, or <code>null</code> 
199
	 *   for the default message ("Don't show me this message again").
200
	 * @param toggleState the initial state for the toggle 
201
	 * @param store the IPreference store in which the user's preference should be persisted
202
	 * @param key the key to use when persisting the user's preference
203
	 * @return the dialog, after being closed by the user, which the client can
204
	 * 		only call <code>getReturnCode()</code> or <code>getToggleState()</code>
205
	 */
206
	public static UserPreferencePromptDialog openOkCancelConfirm(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) {
207
		UserPreferencePromptDialog dialog = new UserPreferencePromptDialog(
208
				parent,
209
				title,
210
				null,	// accept the default window icon
211
				message,
212
				QUESTION,
213
				new String[] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL},
214
				0,		// OK is the default
215
				toggleMessage,
216
				toggleState);
217
		dialog.prefStore = store;
218
		dialog.prefKey = key;
219
		dialog.open();
220
		return dialog;
221
	}
222
223
	
224
	/**
225
	 * Convenience method to open a standard error dialog.
226
	 *
227
	 * @param parent the parent shell of the dialog, or <code>null</code> if none
228
	 * @param title the dialog's title, or <code>null</code> if none
229
	 * @param message the message
230
	 * @param toggleMessage the message for the toggle control, or <code>null</code> 
231
	 *   for the default message ("Don't show me this message again").
232
	 * @param toggleState the initial state for the toggle 
233
	 * @param store the IPreference store in which the user's preference should be persisted
234
	 * @param key the key to use when persisting the user's preference
235
 	 * @return the dialog, after being closed by the user, which the client can
236
	 * 		only call <code>getReturnCode()</code> or <code>getToggleState()</code>
237
	 */
238
	public static UserPreferencePromptDialog openError(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) {
239
		UserPreferencePromptDialog dialog = new UserPreferencePromptDialog(
240
				parent,
241
				title,
242
				null,	// accept the default window icon
243
				message,
244
				ERROR,
245
				new String[] {IDialogConstants.OK_LABEL},
246
				0,		// ok is the default
247
				toggleMessage,
248
				toggleState);
249
		dialog.prefStore = store;
250
		dialog.prefKey = key;
251
		dialog.open();
252
		return dialog;
253
	}
254
	
255
	/**
256
	 * Convenience method to open a standard information dialog.
257
	 *
258
	 * @param parent the parent shell of the dialog, or <code>null</code> if none
259
	 * @param title the dialog's title, or <code>null</code> if none
260
	 * @param message the message
261
	 * @param toggleMessage the message for the toggle control, or <code>null</code> 
262
	 *   for the default message ("Don't show me this message again").
263
	 * @param toggleState the initial state for the toggle 
264
 	 * @param store the IPreference store in which the user's preference should be persisted
265
	 * @param key the key to use when persisting the user's preference
266
267
	 * @return the dialog, after being closed by the user, which the client can
268
	 * 		only call <code>getReturnCode()</code> or <code>getToggleState()</code>
269
	 */
270
	public static UserPreferencePromptDialog openInformation(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) {
271
		UserPreferencePromptDialog dialog = new UserPreferencePromptDialog(
272
				parent, 
273
				title, 
274
				null, // accept the default window icon
275
				message, 
276
				INFORMATION, 
277
				new String[] { IDialogConstants.OK_LABEL }, 
278
				0,		// ok is the default 
279
				toggleMessage,
280
				toggleState);
281
		dialog.prefStore = store;
282
		dialog.prefKey = key;
283
		dialog.open();
284
		return dialog;
285
	}
286
	
287
	/**
288
	 * Convenience method to open a simple Yes/No question dialog.
289
	 *
290
	 * @param parent the parent shell of the dialog, or <code>null</code> if none
291
	 * @param title the dialog's title, or <code>null</code> if none
292
	 * @param message the message
293
	 * @param toggleMessage the message for the toggle control, or <code>null</code> 
294
	 *   for the default message ("Don't show me this message again").
295
	 * @param toggleState the initial state for the toggle
296
 	 * @param store the IPreference store in which the user's preference should be persisted
297
	 * @param key the key to use when persisting the user's preference
298
 
299
	 * @return the dialog, after being closed by the user, which the client can
300
	 * 		only call <code>getReturnCode()</code> or <code>getToggleState()</code>
301
	 */
302
	public static UserPreferencePromptDialog openYesNoQuestion(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) {
303
		UserPreferencePromptDialog dialog = new UserPreferencePromptDialog(
304
				parent,
305
				title,
306
				null,	// accept the default window icon
307
				message,
308
				QUESTION,
309
				new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL},
310
				0,		// yes is the default
311
				toggleMessage,
312
				toggleState);
313
		dialog.prefStore = store;
314
		dialog.prefKey = key;
315
		dialog.open();
316
		return dialog;
317
	}
318
319
	/**
320
	 * Convenience method to open a simple question Yes/No/Cancel dialog.
321
	 *
322
	 * @param parent the parent shell of the dialog, or <code>null</code> if none
323
	 * @param title the dialog's title, or <code>null</code> if none
324
	 * @param message the message
325
	 * @param toggleMessage the message for the toggle control, or <code>null</code> 
326
	 *   for the default message ("Don't show me this message again").
327
	 * @param toggleState the initial state for the toggle 
328
	 * @param store the IPreference store in which the user's preference should be persisted
329
	 * @param key the key to use when persisting the user's preference
330
	 * @return the dialog, after being closed by the user, which the client can
331
	 * 		only call <code>getReturnCode()</code> or <code>getToggleState()</code>
332
	 */
333
	public static UserPreferencePromptDialog openYesNoCancelQuestion(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) {
334
		UserPreferencePromptDialog dialog = new UserPreferencePromptDialog(
335
				parent,
336
				title,
337
				null,	// accept the default window icon
338
				message,
339
				QUESTION,
340
				new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL},
341
				0,		// YES is the default
342
				toggleMessage,
343
				toggleState);
344
		dialog.prefStore = store;
345
		dialog.prefKey = key;
346
		dialog.open();
347
		return dialog;
348
	}
349
	
350
	/**
351
	 * Convenience method to open a standard warning dialog.
352
	 *
353
	 * @param parent the parent shell of the dialog, or <code>null</code> if none
354
	 * @param title the dialog's title, or <code>null</code> if none
355
	 * @param message the message
356
	 * @param toggleMessage the message for the toggle control, or <code>null</code> 
357
	 *   for the default message ("Don't show me this message again").
358
	 * @param toggleState the initial state for the toggle
359
 	 * @param store the IPreference store in which the user's preference should be persisted
360
	 * @param key the key to use when persisting the user's preference	  
361
	 * @return the dialog, after being closed by the user, which the client can
362
	 * 		only call <code>getReturnCode()</code> or <code>getToggleState()</code>
363
	 */
364
	public static UserPreferencePromptDialog openWarning(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) {
365
		UserPreferencePromptDialog dialog = new UserPreferencePromptDialog(
366
				parent,
367
				title,
368
				null,	// accept the default window icon
369
				message,
370
				WARNING,
371
				new String[] {IDialogConstants.OK_LABEL},
372
				0,		// ok is the default
373
				toggleMessage,
374
				toggleState);
375
		dialog.open();
376
		dialog.prefStore = store;
377
		dialog.prefKey = key;
378
		return dialog;
379
	}
380
	
381
}

Return to bug 54347