1 /*******************************************************************************
2  * Copyright (c) 2017 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.correction;
15 
16 import org.eclipse.jface.dialogs.*;
17 import org.eclipse.pde.internal.ui.PDEPlugin;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.*;
25 
26 
27 /**
28  * Copied from org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog. Uses
29  * pde ui settings in this class
30  */
31 public class OptionalMessageDialog extends MessageDialog {
32 
33 	// String constants for widgets
34 	private static final String CHECKBOX_TEXT = "Do not show this &message again";
35 
36 	// Dialog store id constants
37 	private static final String STORE_ID = "OptionalMessageDialog.hide."; //$NON-NLS-1$
38 
39 	public static final int NOT_SHOWN = IDialogConstants.CLIENT_ID + 1;
40 
41 	private final String fId;
42 	private final String fCheckBoxText;
43 
44 	private Button fHideDialogCheckBox;
45 
46 	/**
47 	 * Opens the dialog but only if the user hasn't chosen to hide it.
48 	 *
49 	 * @return the index of the pressed button or {@link SWT#DEFAULT} if the dialog
50 	 *         got dismissed without pressing a button (e.g. via Esc) or
51 	 *         {{@link #NOT_SHOWN} if the dialog was not shown
52 	 */
open(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex)53 	public static int open(String id, Shell parent, String title, Image titleImage, String message, int dialogType,
54 			String[] buttonLabels, int defaultButtonIndex) {
55 		return open(id, parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex,
56 				CHECKBOX_TEXT);
57 	}
58 
59 	/**
60 	 * Opens the dialog but only if the user hasn't chosen to hide it.
61 	 *
62 	 * @return the index of the pressed button or {@link SWT#DEFAULT} if the dialog
63 	 *         got dismissed without pressing a button (e.g. via Esc) or
64 	 *         {{@link #NOT_SHOWN} if the dialog was not shown
65 	 */
open(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex, String checkboxText)66 	public static int open(String id, Shell parent, String title, Image titleImage, String message, int dialogType,
67 			String[] buttonLabels, int defaultButtonIndex, String checkboxText) {
68 		if (!isDialogEnabled(id)) {
69 			return OptionalMessageDialog.NOT_SHOWN;
70 		}
71 
72 		MessageDialog dialog = new OptionalMessageDialog(id, parent, title, titleImage, message, dialogType,
73 				buttonLabels, defaultButtonIndex, checkboxText);
74 		return dialog.open();
75 	}
76 
OptionalMessageDialog(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex)77 	protected OptionalMessageDialog(String id, Shell parent, String title, Image titleImage, String message,
78 			int dialogType, String[] buttonLabels, int defaultButtonIndex) {
79 		this(id, parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex, CHECKBOX_TEXT);
80 	}
81 
OptionalMessageDialog(String id, Shell parent, String title, Image titleImage, String message, int dialogType, String[] buttonLabels, int defaultButtonIndex, String checkBoxText)82 	protected OptionalMessageDialog(String id, Shell parent, String title, Image titleImage, String message,
83 			int dialogType, String[] buttonLabels, int defaultButtonIndex, String checkBoxText) {
84 		super(parent, title, titleImage, message, dialogType, buttonLabels, defaultButtonIndex);
85 		fId = id;
86 		fCheckBoxText = checkBoxText;
87 	}
88 
89 	@Override
createCustomArea(Composite parent)90 	protected Control createCustomArea(Composite parent) {
91 		Composite composite = new Composite(parent, SWT.NONE);
92 		GridLayout layout = new GridLayout();
93 		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
94 		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
95 		layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
96 		composite.setLayout(layout);
97 		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
98 
99 		fHideDialogCheckBox = new Button(composite, SWT.CHECK | SWT.LEFT);
100 		fHideDialogCheckBox.setText(fCheckBoxText);
101 		fHideDialogCheckBox.addSelectionListener(new SelectionAdapter() {
102 			@Override
103 			public void widgetSelected(SelectionEvent e) {
104 				setDialogEnabled(fId, !((Button) e.widget).getSelection());
105 			}
106 		});
107 		applyDialogFont(fHideDialogCheckBox);
108 		return fHideDialogCheckBox;
109 	}
110 
111 	/*
112 	 * @see org.eclipse.jface.dialogs.MessageDialog#buttonPressed(int)
113 	 *
114 	 * @since 3.8.1
115 	 */
116 	@Override
buttonPressed(int buttonId)117 	protected void buttonPressed(int buttonId) {
118 		super.buttonPressed(buttonId);
119 		String[] buttonLabels = getButtonLabels();
120 		int returnCode = getReturnCode();
121 		if (returnCode < buttonLabels.length && IDialogConstants.CANCEL_LABEL.equals(buttonLabels[returnCode])) {
122 			setDialogEnabled(fId, true); // don't store if cancelled
123 		}
124 	}
125 
126 	/*
127 	 * @see org.eclipse.jface.dialogs.MessageDialog#handleShellCloseEvent()
128 	 *
129 	 * @since 3.8.1
130 	 */
131 	@Override
handleShellCloseEvent()132 	protected void handleShellCloseEvent() {
133 		super.handleShellCloseEvent();
134 		setDialogEnabled(fId, true); // don't store if closed without pressing a button
135 	}
136 
137 	// --------------- Configuration handling --------------
138 
139 	/**
140 	 * Returns this dialog
141 	 *
142 	 * @return the settings to be used
143 	 */
getDialogSettings()144 	private static IDialogSettings getDialogSettings() {
145 		IDialogSettings settings = PDEPlugin.getDefault().getDialogSettings();
146 		settings = settings.getSection(STORE_ID);
147 		if (settings == null) {
148 			settings = PDEPlugin.getDefault().getDialogSettings().addNewSection(STORE_ID);
149 		}
150 		return settings;
151 	}
152 
153 	/**
154 	 * Answers whether the optional dialog is enabled and should be shown.
155 	 */
isDialogEnabled(String key)156 	public static boolean isDialogEnabled(String key) {
157 		IDialogSettings settings = getDialogSettings();
158 		return !settings.getBoolean(key);
159 	}
160 
161 	/**
162 	 * Sets whether the optional dialog is enabled and should be shown.
163 	 */
setDialogEnabled(String key, boolean isEnabled)164 	public static void setDialogEnabled(String key, boolean isEnabled) {
165 		IDialogSettings settings = getDialogSettings();
166 		settings.put(key, !isEnabled);
167 	}
168 
169 	/**
170 	 * Clears all remembered information about hidden dialogs
171 	 */
clearAllRememberedStates()172 	public static void clearAllRememberedStates() {
173 		IDialogSettings settings = PDEPlugin.getDefault().getDialogSettings();
174 		settings.addNewSection(STORE_ID);
175 	}
176 }
177