Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ec1985fdf309eb8b4a861374384ad04e942a15b4 (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
/*******************************************************************************
 * Copyright (c) 2012 CEA LIST.
 * 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:
 *     Nicolas Bros (Mia-Software) - Bug 372865 - FacetSet selection dialog
 *     Gregoire Dupe (Mia-Software) - Bug 372626 - Aggregates
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.efacet.ui.internal.dialogs;

import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetSet;
import org.eclipse.papyrus.emf.facet.efacet.ui.internal.Activator;
import org.eclipse.papyrus.emf.facet.efacet.ui.internal.Messages;
import org.eclipse.papyrus.emf.facet.efacet.ui.internal.exported.dialog.IFacetSetSelectionDialogInternal;
import org.eclipse.papyrus.emf.facet.efacet.ui.internal.widget.FacetSetSelectionControl;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.dialog.IDialogCallback;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.dialog.IDialogCallbackWithPreCommit;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionStatusDialog;

/**
 * A dialog to select {@link FacetSet}s
 *
 * @param <T>
 *            the type of a pre-commit dialog
 */
public class FacetSetSelectionDialog<T> extends SelectionStatusDialog implements IFacetSetSelectionDialogInternal<T> {

	// note: the dialog should be large enough to see the validator message
	private static final int DEFAULT_WIDTH = 800;
	private static final int DEFAULT_HEIGHT = 600;

	private final Collection<FacetSet> available;
	private final int selectionMaxSize;
	private final boolean allowEmpty;
	private final IDialogCallbackWithPreCommit<List<FacetSet>, Boolean, T> callback;
	private FacetSetSelectionControl selectionControl;

	public FacetSetSelectionDialog(final Collection<FacetSet> available,
			final int selectionMaxSize, final boolean allowEmpty,
			final IDialogCallbackWithPreCommit<List<FacetSet>, Boolean, T> callback, final Shell parentShell) {
		super(parentShell);
		Assert.isLegal(callback != null, "available cannot be null"); //$NON-NLS-1$
		Assert.isLegal(callback != null, "callback cannot be null"); //$NON-NLS-1$
		Assert.isLegal(selectionMaxSize > 0, "selectionMaxSize must be > 0"); //$NON-NLS-1$
		this.available = available;
		this.selectionMaxSize = selectionMaxSize;
		this.allowEmpty = allowEmpty;
		this.callback = callback;
		setHelpAvailable(false);
	}

	@Override
	protected Control createDialogArea(final Composite parent) {
		final Composite composite = (Composite) super.createDialogArea(parent);
		final Runnable onChange = new Runnable() {
			public void run() {
				updateValidationStatus();
			}
		};
		this.selectionControl = createFacetSetSelectionControl(composite, onChange);
		this.selectionControl.createContents();
		this.selectionControl.setAvailableFacetSets(this.available);
		updateValidationStatus();
		return composite;
	}

	protected FacetSetSelectionControl createFacetSetSelectionControl(final Composite parentComposite, final Runnable onChange) {
		return new FacetSetSelectionControl(parentComposite, this.selectionMaxSize, this.allowEmpty, onChange);
	}

	@Override
	protected void createButtonsForButtonBar(final Composite parent) {
		createButton(parent, IDialogConstants.SELECT_ALL_ID, Messages.FacetSetSelectionDialog_selectAll, false);
		createButton(parent, IDialogConstants.DESELECT_ALL_ID, Messages.FacetSetSelectionDialog_deselectAll, false);
		super.createButtonsForButtonBar(parent);
	}

	@Override
	protected void buttonPressed(final int buttonId) {
		super.buttonPressed(buttonId);
		if (IDialogConstants.SELECT_ALL_ID == buttonId) {
			selectAll();
		} else if (IDialogConstants.DESELECT_ALL_ID == buttonId) {
			deselectAll();
		}
	}

	protected void updateValidationStatus() {
		updateStatus(this.selectionControl.getValidationStatus());
	}

	@Override
	protected void configureShell(final Shell shell) {
		super.configureShell(shell);
		if (this.selectionMaxSize > 1) {
			shell.setText(Messages.FacetSetSelectionDialog_titleSelectFacetSets);
		} else {
			shell.setText(Messages.FacetSetSelectionDialog_titleSelectFacetSet);
		}
	}

	@Override
	protected boolean isResizable() {
		return true;
	}

	@Override
	protected IDialogSettings getDialogBoundsSettings() {
		final String sectionName = this.getClass().getName() + ".settings"; //$NON-NLS-1$
		final IDialogSettings settings = Activator.getDefault().getDialogSettings();
		IDialogSettings section = settings.getSection(sectionName);
		if (section == null) {
			section = settings.addNewSection(sectionName);
			// cf Dialog#DIALOG_WIDTH
			section.put("DIALOG_WIDTH", FacetSetSelectionDialog.DEFAULT_WIDTH); //$NON-NLS-1$
			section.put("DIALOG_HEIGHT", FacetSetSelectionDialog.DEFAULT_HEIGHT); //$NON-NLS-1$
		}
		return section;
	}

	public boolean isErrorStatus() {
		final IStatus validationStatus = this.selectionControl.getValidationStatus();
		return validationStatus == null || validationStatus.getSeverity() >= IStatus.ERROR;
	}

	public T pressOk() {
		final IDialogCallback<Boolean> precommitCallBack = new IDialogCallback<Boolean>() {
			public void committed(final Boolean precommitResult) {
				commit(precommitResult);
			}
		};
		final T dialog = this.callback.openPrecommitDialog(
				getSelectedFacetSets(),
				precommitCallBack);
		if (dialog == null) {
			commit(Boolean.TRUE);
		}
		return dialog;
	}

	public void pressCancel() {
		setReturnCode(Window.CANCEL);
		close();
	}

	@Override
	protected void okPressed() {
		pressOk();
	}

	@Override
	protected void cancelPressed() {
		pressCancel();
	}

	protected void commit(final Boolean precommitResult) {
		// since the callback is called from client code, we need to ensure we are running on the UI thread
		Display.getDefault().syncExec(new Runnable() {
			public void run() {
				commitOnUIThread(precommitResult);
			}
		});
	}

	protected void commitOnUIThread(final Boolean precommitResult) {
		final List<FacetSet> selectedFacetSets = getSelectedFacetSets();
		setReturnCode(Window.OK);
		close();
		this.callback.committed(selectedFacetSets, precommitResult);
	}

	@Override
	public int open() {
		// so that this dialog can be manipulated programmatically
		setBlockOnOpen(false);
		return super.open();
	}

	public List<FacetSet> getSelectedFacetSets() {
		if (this.selectionControl.isDisposed()) {
			throw new UnsupportedOperationException("No selection available because the dialog is closed"); //$NON-NLS-1$
		}
		return this.selectionControl.getSelectedFacetSets();
	}

	public void setSelectedFacetSets(final List<? extends FacetSet> newSelection) {
		this.selectionControl.setSelectedFacetSets(newSelection);
		updateValidationStatus();
	}

	public void selectAll() {
		this.selectionControl.selectAll();
		updateValidationStatus();
	}

	public void deselectAll() {
		this.selectionControl.deselectAll();
		updateValidationStatus();
	}

	public boolean isOkButtonEnabled() {
		return getOkButton().isEnabled();
	}

	@Override
	protected void computeResult() {
		// nothing : the dialog is not used in a standard way : use the callback
	}

	public TreeViewer getTreeViewer() {
		return this.selectionControl.getTreeViewer();
	}
}

Back to the top