Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2c6fe84a62e147b4beec64c31e96c014ee5f2699 (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
/*******************************************************************************
 *  Copyright (c) 2000, 2009 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     Anton Leherbauer (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Shell;

import org.eclipse.cdt.ui.CUIPlugin;

import org.eclipse.cdt.internal.ui.CUIMessages;

/**
 * This is a <code>MessageDialog</code> which allows the user
 * to choose that the dialog isn't shown again the next time.
 */ 
public class OptionalMessageDialog extends MessageDialog {
	// Dialog store id constants
	private static final String STORE_ID= "OptionalMessageDialog.hide."; //$NON-NLS-1$
	private static final String KEY_DETAIL = ".detail"; //$NON-NLS-1$

	public static final int NOT_SHOWN= IDialogConstants.CLIENT_ID + 1;
	public static final int NO_DETAIL= -1;
	
	private Button fHideDialogCheckBox;
	private String fId;
	private String fHideMessage;

	/**
	 * Opens the dialog but only if the user hasn't chosen to hide it.
	 * Returns <code>NOT_SHOWN</code> if the dialog was not shown.
	 */
	public static int open(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex) {
		if (!isDialogEnabled(id))
			return OptionalMessageDialog.NOT_SHOWN;
		
		MessageDialog dialog= new OptionalMessageDialog(id, parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex);
		return dialog.open();
	}

	protected OptionalMessageDialog(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex) {
		super(parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex);
		fId= id;
		switch(dialogType) {
		case QUESTION:
        case QUESTION_WITH_CANCEL:
        	fHideMessage= CUIMessages.OptionalMessageDialog_rememberDecision;
        	break;
        default:
        	fHideMessage= CUIMessages.OptionalMessageDialog_dontShowAgain;
		}
	}

	@Override
	protected Control createCustomArea(Composite parent) {
		Composite composite= new Composite(parent, SWT.NONE);
		GridLayout layout= new GridLayout();
		layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		fHideDialogCheckBox= new Button(composite, SWT.CHECK | SWT.LEFT);
		fHideDialogCheckBox.setText(fHideMessage);
		fHideDialogCheckBox.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setDialogEnabled(fId, !((Button)e.widget).getSelection());
			}
		});
		applyDialogFont(fHideDialogCheckBox);
		return fHideDialogCheckBox;
	}
	
	//--------------- Configuration handling --------------
	
	/**
	 * Returns this dialog
	 * 
	 * @return the settings to be used
	 */
	private static IDialogSettings getDialogSettings() {
		IDialogSettings settings= CUIPlugin.getDefault().getDialogSettings();
		settings= settings.getSection(STORE_ID);
		if (settings == null)
			settings= CUIPlugin.getDefault().getDialogSettings().addNewSection(STORE_ID);
		return settings;
	}
		
	/**
	 * Answers whether the optional dialog is enabled and should be shown.
	 */
	public static boolean isDialogEnabled(String key) {
		IDialogSettings settings= getDialogSettings();
		return !settings.getBoolean(key);
	}
	
	/**
	 * Sets a detail for the dialog.
	 */
	public static void setDialogDetail(String key, int detail) {
		IDialogSettings settings= getDialogSettings();
		settings.put(key+KEY_DETAIL, detail);
	}

	/**
	 * Returns the detail for this dialog, or NO_DETAIL, if none.
	 */
	public static int getDialogDetail(String key) {
		IDialogSettings settings= getDialogSettings();
		try {
			return settings.getInt(key+KEY_DETAIL);
		} catch (NumberFormatException e) {
			return NO_DETAIL;
		}
	}
	
	/**
	 * Sets whether the optional dialog is enabled and should be shown.
	 */
	public static void setDialogEnabled(String key, boolean isEnabled) {
		IDialogSettings settings= getDialogSettings();
		settings.put(key, !isEnabled);
	}

	/**
	 * Clears all remembered information about hidden dialogs
	 */
	public static void clearAllRememberedStates() {
		IDialogSettings settings= CUIPlugin.getDefault().getDialogSettings();
		settings.addNewSection(STORE_ID);
	}
}

Back to the top