Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ee98c6cd8250c890ed1401840b136f18fec790e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.jface.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.tcf.te.ui.swt.activator.UIPlugin;
import org.eclipse.tcf.te.ui.swt.nls.Messages;
import org.eclipse.ui.PlatformUI;

/**
 * Message dialog with "do not show again" and optional help button. The Dialog
 * stores the selected button result automatically, when "do not show again" was
 * selected. All stored values can be cleared in the Target Explorer preferences
 * root page.
 * <p>
 * Additional information (e.g. last opening time stamp for license warning) can
 * be stored using <code>set/getAdditionalDialogInfo()</code>, that should
 * also be cleared with the states.
 */
public class OptionalMessageDialog extends MessageDialogWithToggle {

	// section name for the dialog settings stored by this dialog
	private static final String DIALOG_ID = "OptionalMessageDialog"; //$NON-NLS-1$

	// context help id for the dialog
	private String contextHelpId;
	// the key where the result is stored within the dialog settings section
	private String key;

	/**
	 * Constructor. Message dialog with "do not show again" and optional help
	 * button. The dialog automatically stores the pressed button when "do not
	 * show again" was selected. The next time the dialogs <code>open()</code>
	 * method is called it returns the stored value without opening the dialog.
	 * When the cancel button was pressed, _NO_ value will be stored.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param image
	 *            The window icon or <code>null</code> if default icon should
	 *            be used.
	 * @param message
	 *            The dialog message text.
	 * @param imageType
	 *            The dialog image type (QUESTION, INFORMATION, WARNING, ERROR).
	 * @param buttonLabels
	 *            The labels for buttons.
	 * @param defaultIndex
	 *            The default button index.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 */
	public OptionalMessageDialog(Shell parentShell, String title, Image image, String message, int imageType, String[] buttonLabels, int defaultIndex, String key, String contextHelpId) {

		super(parentShell,
		      title,
		      image,
		      message,
		      imageType,
		      buttonLabels != null ? buttonLabels : new String [] { IDialogConstants.OK_LABEL },
		      defaultIndex,
		      Messages.getString(DIALOG_ID + (imageType == QUESTION ? "_rememberMyDecision_label" : "_doNotShowAgain_label")), //$NON-NLS-1$ //$NON-NLS-2$
		      false);

		this.contextHelpId = contextHelpId;
		this.key = key == null || key.trim().length() == 0 ? null : key.trim();

		if (contextHelpId != null) {
			PlatformUI.getWorkbench().getHelpSystem().setHelp(parentShell, contextHelpId);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
	 */
	@Override
	protected boolean isResizable() {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IconAndMessageDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createButtonBar(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(2, false);
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		layout.horizontalSpacing = 0;
		composite.setLayout(layout);
		GridData gd = new GridData(SWT.FILL, SWT.BOTTOM, true, false);
		if (parent.getLayout() instanceof GridLayout) {
			gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns;
		}
		composite.setLayoutData(gd);
		composite.setFont(parent.getFont());

		// create help control if needed
		if (contextHelpId != null) {
			Control helpControl = createHelpControl(composite);
			((GridData)helpControl.getLayoutData()).horizontalIndent = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		}

		Control buttonSection = super.createButtonBar(composite);
		((GridData)buttonSection.getLayoutData()).grabExcessHorizontalSpace = true;
		return composite;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.MessageDialog#createButton(org.eclipse.swt.widgets.Composite, int, java.lang.String, boolean)
	 */
	@Override
	protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
		// Allow to re-adjust the button id's. Base implementation is matching
		// the button labels against the well known labels defined by IDialogConstants.
		// For labels not defined there, the implementation set id's starting with 256.
		return super.createButton(parent, adjustButtonIdForLabel(id, label), label, defaultButton);
	}

	/**
	 * Adjust the button id to use for the button with the given label.
	 * <p>
	 * <b>Note:</b>Base implementation is matching the button labels against the well known
	 * labels defined by {@link IDialogConstants}. For labels not defined there, the implementation
	 * set id's starting with 256.
	 * <p>
	 * The default implementation returns the button id unmodified.
	 *
	 * @param id The suggested button id.
	 * @param label The button label.
	 * @return The effective button id.
	 */
	protected int adjustButtonIdForLabel(int id, String label) {
		return id;
	}

	private Control createHelpControl(Composite parent) {
		Image helpImage = JFaceResources.getImage(DLG_IMG_HELP);
		if (helpImage != null) {
			return createHelpImageButton(parent, helpImage);
		}
		return createHelpLink(parent);
	}

	/*
	 * Creates a button with a help image. This is only used if there
	 * is an image available.
	 */
	private ToolBar createHelpImageButton(Composite parent, Image image) {
		ToolBar toolBar = new ToolBar(parent, SWT.FLAT | SWT.NO_FOCUS);
		((GridLayout)parent.getLayout()).numColumns++;
		toolBar.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		final Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_HAND);
		toolBar.setCursor(cursor);
		toolBar.addDisposeListener(new DisposeListener() {
			@Override
            public void widgetDisposed(DisposeEvent e) {
				cursor.dispose();
			}
		});
		ToolItem item = new ToolItem(toolBar, SWT.NONE);
		item.setImage(image);
		item.setToolTipText(JFaceResources.getString("helpToolTip")); //$NON-NLS-1$
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				helpPressed();
			}
		});
		return toolBar;
	}

	/*
	 * Creates a help link. This is used when there is no help image
	 * available.
	 */
	private Link createHelpLink(Composite parent) {
		Link link = new Link(parent, SWT.WRAP | SWT.NO_FOCUS);
		((GridLayout)parent.getLayout()).numColumns++;
		link.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		link.setText("<a>" + IDialogConstants.HELP_LABEL + "</a>"); //$NON-NLS-1$ //$NON-NLS-2$
		link.setToolTipText(IDialogConstants.HELP_LABEL);
		link.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				helpPressed();
			}
		});
		return link;
	}

	/**
	 * Invoked if the help button is pressed.
	 */
	/* default */ void helpPressed() {
		if (getShell() != null) {
			Control c = getShell().getDisplay().getFocusControl();
			while (c != null) {
				if (c.isListening(SWT.Help)) {
					c.notifyListeners(SWT.Help, new Event());
					break;
				}
				c = c.getParent();
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.MessageDialog#createCustomArea(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createCustomArea(Composite parent) {
		if (contextHelpId != null) {
			PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, contextHelpId);
		}
		Label label = new Label(parent, SWT.NULL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		return label;
	}

	/**
	 * Opens the dialog only, if no dialog result is stored and this dialog
	 * should be displayed. If a dialog result is stored, this state will be
	 * returned without opening the dialog. When the dialog is closed and "do
	 * not show again" was selected, the result will be stored.
	 *
	 * @see org.eclipse.jface.window.Window#open()
	 */
	@Override
	public int open() {
		int result = getDialogResult(key);
		if (result < 0) {
			result = super.open();
			if (getToggleState() && result >= 0 && result != IDialogConstants.CANCEL_ID) {
				setDialogResult(key, result);
			}
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.MessageDialogWithToggle#setToggleButton(org.eclipse.swt.widgets.Button)
	 */
	@Override
	protected void setToggleButton(Button button) {
		// if no key is given, no toggle button should be displayed
		if (button != null && key != null) {
			super.setToggleButton(button);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.MessageDialogWithToggle#createToggleButton(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Button createToggleButton(Composite parent) {
		// if no key is given, no toggle button should be created
		if (key != null) {
			return super.createToggleButton(parent);
		}
		return null;
	}
	/**
	 * Opens a question dialog with OK and CANCEL.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openOkCancelDialog(Shell parentShell, String title, String message, String key, String contextHelpId) {
		return openOkCancelDialog(parentShell, title, message, null, key, contextHelpId);
	}

	/**
	 * Opens a question dialog with OK and CANCEL.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param buttonLabel
	 * 			  An string array listing the labels of the message dialog buttons. If <code>null</code>, the default
	 *            labeling, typically &quot;OK&quot; for a single button message dialog, will be applied.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openOkCancelDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) {
		if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL };
		OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, QUESTION, buttonLabel, 0, key, contextHelpId);
		return dialog.open();
	}

	/**
	 * Opens a question dialog with YES, NO and CANCEL.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openYesNoCancelDialog(Shell parentShell, String title, String message, String key, String contextHelpId) {
		return openYesNoCancelDialog(parentShell, title, message, null, key, contextHelpId);
	}

	/**
	 * Opens a question dialog with YES, NO and CANCEL.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param buttonLabel
	 * 			  An string array listing the labels of the message dialog buttons. If <code>null</code>, the default
	 *            labeling, typically &quot;OK&quot; for a single button message dialog, will be applied.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openYesNoCancelDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) {
		if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL };
		OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, QUESTION, buttonLabel, 0, key, contextHelpId);
		return dialog.open();
	}

	/**
	 * Opens a question dialog with YES and NO.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openYesNoDialog(Shell parentShell, String title, String message, String key, String contextHelpId) {
		return openYesNoDialog(parentShell, title, message, null, key, contextHelpId);
	}

	/**
	 * Opens a question dialog with YES and NO.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param buttonLabel
	 * 			  An string array listing the labels of the message dialog buttons. If <code>null</code>, the default
	 *            labeling, typically &quot;OK&quot; for a single button message dialog, will be applied.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openYesNoDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) {
		if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL };
		OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, QUESTION, buttonLabel, 0, key, contextHelpId);
		return dialog.open();
	}

	/**
	 * Opens a info dialog with OK.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openInformationDialog(Shell parentShell, String title, String message, String key, String contextHelpId) {
		return openInformationDialog(parentShell, title, message, null, key, contextHelpId);
	}

	/**
	 * Opens a info dialog with OK.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param buttonLabel
	 * 			  An string array listing the labels of the message dialog buttons. If <code>null</code>, the default
	 *            labeling, typically &quot;OK&quot; for a single button message dialog, will be applied.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openInformationDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) {
		if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.OK_LABEL };
		OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, INFORMATION, buttonLabel, 0, key, contextHelpId);
		return dialog.open();
	}

	/**
	 * Opens a warning dialog with OK.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openWarningDialog(Shell parentShell, String title, String message, String key, String contextHelpId) {
		return openWarningDialog(parentShell, title, message, null, key, contextHelpId);
	}

	/**
	 * Opens a warning dialog with OK.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param buttonLabel
	 * 			  An string array listing the labels of the message dialog buttons. If <code>null</code>, the default
	 *            labeling, typically &quot;OK&quot; for a single button message dialog, will be applied.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openWarningDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) {
		if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.OK_LABEL };
		OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, WARNING, buttonLabel, 0, key, contextHelpId);
		return dialog.open();
	}

	/**
	 * Opens a error dialog with OK.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openErrorDialog(Shell parentShell, String title, String message, String key, String contextHelpId) {
		return openErrorDialog(parentShell, title, message, null, key, contextHelpId);
	}

	/**
	 * Opens a error dialog with OK.
	 *
	 * @param parentShell
	 *            The shell.
	 * @param title
	 *            The title for the message dialog.
	 * @param message
	 *            The dialog message text.
	 * @param buttonLabel
	 * 			  An string array listing the labels of the message dialog buttons. If <code>null</code>, the default
	 *            labeling, typically &quot;OK&quot; for a single button message dialog, will be applied.
	 * @param key
	 *            The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>").
	 * @param contextHelpId
	 *            The optional help context id. If <code>null</code>, no help
	 *            button will be shown.
	 * @return The stored or selected result.
	 */
	public static int openErrorDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) {
		if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.OK_LABEL };
		OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, ERROR, buttonLabel, 0, key, contextHelpId);
		return dialog.open();
	}

	/*
	 * Get the dialog settings section or create it when it is not available.
	 */
	private static IDialogSettings getDialogSettings() {
		IDialogSettings settings = UIPlugin.getDefault().getDialogSettings();
		settings = settings.getSection(DIALOG_ID);
		if (settings == null)
			settings = UIPlugin.getDefault().getDialogSettings().addNewSection(DIALOG_ID);
		return settings;
	}

	/**
	 * Get the stored result for this key. If the dialog should be opened, -1
	 * will be returned.
	 *
	 * @param key
	 *            The key for the stored result.
	 * @return The stored result or -1 of the dialog should be opened.
	 */
	public static int getDialogResult(String key) {
		IDialogSettings settings = getDialogSettings();
		try {
			return settings.getInt(key + ".result"); //$NON-NLS-1$
		}
		catch (NumberFormatException e) {
		}
		return -1;
	}

	/**
	 * Get the stored toggle state for this key.
	 * If no state is stored, <code>false</code> will be returned.
	 *
	 * @param key
	 *            The key for the stored toggle state.
	 * @return The stored result or -1 of the dialog should be opened.
	 */
	public static boolean getDialogToggleState(String key) {
		IDialogSettings settings = getDialogSettings();
		return settings.getBoolean(key + ".toggleState"); //$NON-NLS-1$
	}

	/**
	 * Get the stored info for this key.
	 *
	 * @param key
	 *            The key for the stored info.
	 * @return The stored info or <code>null</code>.
	 */
	public static String getAdditionalDialogInfo(String key) {
		IDialogSettings settings = getDialogSettings();
		return settings.get(key + ".additionalInfo"); //$NON-NLS-1$
	}

	/**
	 * Set the dialog result for this key. If the result is < 0, the string
	 * "PROMPT" will be stored.
	 *
	 * @param key
	 *            The key to store the result.
	 * @param result
	 *            The result that should be stored.
	 */
	public static void setDialogResult(String key, int result) {
		IDialogSettings settings = getDialogSettings();
		if (result < 0) {
			settings.put(key + ".result", PROMPT); //$NON-NLS-1$
		}
		else {
			settings.put(key + ".result", result); //$NON-NLS-1$
		}
	}

	/**
	 * Set the dialog toggle state for this key.
	 *
	 * @param key
	 *            The key to store the toggle state.
	 * @param result
	 *            The toggle state that should be stored.
	 */
	public static void setDialogToggleState(String key, boolean state) {
		IDialogSettings settings = getDialogSettings();
		settings.put(key + ".toggleState", state); //$NON-NLS-1$
	}

	/**
	 * Set additional info for this key.
	 *
	 * @param key
	 *            The key to store the additional info.
	 * @param value
	 *            The additional info that should be stored.
	 */
	public static void setAdditionalDialogInfo(String key, String value) {
		IDialogSettings settings = getDialogSettings();
		settings.put(key + ".additionalInfo", value); //$NON-NLS-1$
	}

	/**
	 * Clears all stored information for this dialogs
	 */
	public static void clearAllRememberedStates() {
		IDialogSettings settings = UIPlugin.getDefault().getDialogSettings();
		settings.addNewSection(DIALOG_ID);
	}
}

Back to the top