Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 34aaa265c05b0142242653e0f58b2674011680a9 (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
/*******************************************************************************
 * Copyright (c) 2004, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Patrik Suzzi <psuzzi@gmail.com> - Bug 489250
 *******************************************************************************/
package org.eclipse.ui.internal.activities.ws;

import com.ibm.icu.text.MessageFormat;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.activities.IActivity;
import org.eclipse.ui.activities.IActivityManager;
import org.eclipse.ui.activities.NotDefinedException;
import org.eclipse.ui.activities.WorkbenchTriggerPointAdvisor;

/**
 * Dialog that will prompt the user and confirm that they wish to activate a set
 * of activities.
 *
 * @since 3.0
 */
public class EnablementDialog extends Dialog {

	/**
	 * The translation bundle in which to look up internationalized text.
	 */
	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(EnablementDialog.class.getName());

	private Button dontAskButton;

	private Set<String> activitiesToEnable = new HashSet<>(7);

	private Collection<String> activityIds;

	private boolean dontAsk;

	private Button detailsButton;

	boolean showDetails = false;

	private Composite detailsComposite;

	private Label detailsLabel;

	private String selectedActivity;

	private Text detailsText;

	private Properties strings;

	/**
	 * Create a new instance of the receiver.
	 *
	 * @param parentShell the parent shell
	 * @param activityIds the candidate activities
	 * @param strings     string overrides
	 */
	public EnablementDialog(Shell parentShell, Collection<String> activityIds, Properties strings) {
		super(parentShell);
		this.activityIds = activityIds;
		this.strings = strings;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		Font dialogFont = parent.getFont();
		composite.setFont(dialogFont);
		Label text = new Label(composite, SWT.NONE);
		text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		text.setFont(dialogFont);
		IActivityManager manager = PlatformUI.getWorkbench().getActivitySupport().getActivityManager();

		if (activityIds.size() == 1) {
			String activityId = activityIds.iterator().next();
			activitiesToEnable.add(activityId);
			selectedActivity = activityId;

			IActivity activity = manager.getActivity(activityId);
			String activityText;
			try {
				activityText = activity.getName();
			} catch (NotDefinedException e) {
				activityText = activity.getId();
			}
			text.setText(MessageFormat.format(RESOURCE_BUNDLE.getString("requiresSingle"), //$NON-NLS-1$
					activityText));

			text = new Label(composite, SWT.NONE);
			text.setText(strings.getProperty(WorkbenchTriggerPointAdvisor.PROCEED_SINGLE,
					RESOURCE_BUNDLE.getString(WorkbenchTriggerPointAdvisor.PROCEED_SINGLE)));
			text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			text.setFont(dialogFont);
		} else {
			text.setText(RESOURCE_BUNDLE.getString("requiresMulti")); //$NON-NLS-1$
			Set<String> activityIdsCopy = new HashSet<>(activityIds);
			CheckboxTableViewer viewer = CheckboxTableViewer.newCheckList(composite,
					SWT.CHECK | SWT.BORDER | SWT.SINGLE);
			viewer.setContentProvider(new ActivityContentProvider());
			viewer.setLabelProvider(new ActivityLabelProvider(manager));
			viewer.setInput(activityIdsCopy);
			viewer.setCheckedElements(activityIdsCopy.toArray());
			viewer.addCheckStateListener(event -> {
				if (event.getChecked()) {
					activitiesToEnable.add((String) event.getElement());
				} else {
					activitiesToEnable.remove(event.getElement());
				}

				getButton(Window.OK).setEnabled(!activitiesToEnable.isEmpty());
			});
			viewer.addSelectionChangedListener(event -> {
				selectedActivity = (String) event.getStructuredSelection().getFirstElement();
				setDetails();
			});
			activitiesToEnable.addAll(activityIdsCopy);

			viewer.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			viewer.getControl().setFont(dialogFont);

			text = new Label(composite, SWT.NONE);
			text.setText(strings.getProperty(WorkbenchTriggerPointAdvisor.PROCEED_MULTI,
					RESOURCE_BUNDLE.getString(WorkbenchTriggerPointAdvisor.PROCEED_MULTI)));
			text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			text.setFont(dialogFont);
		}
		Label seperator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		seperator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		dontAskButton = new Button(composite, SWT.CHECK);
		dontAskButton.setSelection(false);
		dontAskButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		dontAskButton.setText(strings.getProperty(WorkbenchTriggerPointAdvisor.DONT_ASK,
				RESOURCE_BUNDLE.getString(WorkbenchTriggerPointAdvisor.DONT_ASK)));
		dontAskButton.setFont(dialogFont);

		detailsComposite = new Composite(composite, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		detailsComposite.setLayout(layout);
		detailsLabel = new Label(detailsComposite, SWT.NONE);
		detailsLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		detailsLabel.setFont(dialogFont);

		detailsText = new Text(detailsComposite, SWT.WRAP | SWT.V_SCROLL | SWT.BORDER | SWT.READ_ONLY);
		detailsText.setLayoutData(new GridData(GridData.FILL_BOTH));
		detailsText.setFont(dialogFont);

		setDetails();

		GridData data = new GridData(GridData.FILL_BOTH);
		detailsComposite.setLayoutData(data);
		setDetailHints();

		return composite;
	}

	/**
	 * Set the text of the detail label and text area.
	 */
	protected void setDetails() {
		if (selectedActivity == null) {
			detailsLabel.setText(strings.getProperty(WorkbenchTriggerPointAdvisor.NO_DETAILS,
					RESOURCE_BUNDLE.getString(WorkbenchTriggerPointAdvisor.NO_DETAILS)));
			detailsText.setText(""); //$NON-NLS-1$
		} else {
			IActivity activity = PlatformUI.getWorkbench().getActivitySupport().getActivityManager()
					.getActivity(selectedActivity);
			String name;
			try {
				name = activity.getName();
			} catch (NotDefinedException e1) {
				name = selectedActivity;
			}
			String desc;
			try {
				desc = activity.getDescription();
			} catch (NotDefinedException e) {
				desc = RESOURCE_BUNDLE.getString("noDescAvailable"); //$NON-NLS-1$
			}
			detailsLabel.setText(MessageFormat.format(RESOURCE_BUNDLE.getString("detailsLabel"), name)); //$NON-NLS-1$
			detailsText.setText(desc);
		}
	}

	/**
	 *
	 */
	protected void setDetailHints() {
		GridData data = (GridData) detailsComposite.getLayoutData();
		if (showDetails) {
			Composite parent = detailsComposite.getParent();
			data.widthHint = parent.getSize().x - ((GridLayout) parent.getLayout()).marginWidth * 2;
			data.heightHint = convertHeightInCharsToPixels(5);
		} else {
			data.widthHint = 0;
			data.heightHint = 0;
		}
	}

	/**
	 * Set the label of the detail button based on whether we're currently showing
	 * the description text.
	 */
	private void setDetailButtonLabel() {
		if (!showDetails) {
			detailsButton.setText(RESOURCE_BUNDLE.getString("showDetails")); //$NON-NLS-1$
		} else {
			detailsButton.setText(RESOURCE_BUNDLE.getString("hideDetails")); //$NON-NLS-1$
		}
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(RESOURCE_BUNDLE.getString("title")); //$NON-NLS-1$
	}

	/**
	 * @return Returns whether the user has declared that there is to be no further
	 *         prompting for the supplied activities
	 */
	public boolean getDontAsk() {
		return dontAsk;
	}

	/**
	 * @return Returns the activities to enable
	 */
	public Set<String> getActivitiesToEnable() {
		return activitiesToEnable;
	}

	@Override
	protected void okPressed() {
		dontAsk = dontAskButton.getSelection();
		super.okPressed();
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		detailsButton = createButton(parent, IDialogConstants.DETAILS_ID, "", false); //$NON-NLS-1$
		setDetailButtonLabel();
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.DETAILS_ID) {
			detailsPressed();
			return;
		}
		super.buttonPressed(buttonId);
	}

	/**
	 * Handles selection of the Details button.
	 */
	private void detailsPressed() {
		showDetails = !showDetails;
		setDetailButtonLabel();
		setDetailHints();
		setDetails();
		((Composite) getDialogArea()).layout(true);
		getShell().setSize(getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
}

Back to the top