Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f2383fbf03c4ab300b732da2930b2c4d6288a4dd (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
/*****************************************************************************
 * Copyright (c) 2014 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.decoratormodel.internal.ui.wizards;

import static org.eclipse.papyrus.uml.decoratormodel.internal.ui.wizards.AbstractProfileApplicationsPage.align;
import static org.eclipse.papyrus.uml.decoratormodel.internal.ui.wizards.AbstractProfileApplicationsPage.button;
import static org.eclipse.papyrus.uml.decoratormodel.internal.ui.wizards.AbstractProfileApplicationsPage.fill;
import static org.eclipse.papyrus.uml.decoratormodel.internal.ui.wizards.AbstractProfileApplicationsPage.label;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.uml.decoratormodel.internal.ui.messages.Messages;
import org.eclipse.papyrus.uml.decoratormodel.internal.ui.preferences.ProfileExternalizationUIPreferences;
import org.eclipse.papyrus.uml.decoratormodel.internal.ui.providers.DecoratorModelLabelProvider;
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.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.uml2.uml.Package;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;

/**
 * @author damus
 *
 */
public class DecoratorModelSelectionPage extends WizardPage {
	private boolean showDontShowThis;

	protected CheckboxTableViewer table;

	private ResourceSet resourceSet;
	private Set<URI> input;
	private Set<IFile> initialSelections;

	private final EventBus bus;

	public DecoratorModelSelectionPage(EventBus bus) {
		this(bus, null);
	}

	public DecoratorModelSelectionPage(EventBus bus, ImageDescriptor titleImage) {
		this("selection", Messages.DecoratorModelSelectionPage_0, bus, titleImage); //$NON-NLS-1$
	}

	protected DecoratorModelSelectionPage(String name, String title, EventBus bus, ImageDescriptor titleImage) {
		super(name, title, titleImage);

		this.bus = bus;
	}

	@Override
	public void createControl(Composite parent) {
		Composite main = new Composite(parent, SWT.NONE);
		main.setLayout(new GridLayout());

		label(main, Messages.DecoratorModelSelectionPage_1);
		table = new CheckboxTableViewer(new Table(main, SWT.BORDER | SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL));
		fill(table.getTable(), true, true);
		table.setLabelProvider(new DelegatingStyledCellLabelProvider(new DecoratorModelLabelProvider(resourceSet)));
		table.setContentProvider(new AvailableResourcesContentProvider());
		table.setInput(getInput());
		if (initialSelections != null) {
			table.setCheckedElements(Iterables.toArray(initialSelections, IFile.class));
		}
		table.addCheckStateListener(new ICheckStateListener() {

			@Override
			public void checkStateChanged(CheckStateChangedEvent event) {
				validatePage();
			}
		});

		createSelectionButtons(main);

		if (showDontShowThis()) {
			createPreferenceArea(main);
		}

		setControl(main);

		validatePage();
	}

	IFile[] getCheckedFiles() {
		List<?> checked = (table == null) //
		? (initialSelections == null) ? Collections.emptyList() : ImmutableList.copyOf(initialSelections) //
				: Arrays.asList(table.getCheckedElements());
		return Iterables.toArray(Iterables.filter(checked, IFile.class), IFile.class);
	}

	protected void createSelectionButtons(Composite parent) {
		Composite buttons = new Composite(parent, SWT.NONE);
		align(buttons, SWT.TRAIL);

		RowLayout layout = new RowLayout();
		layout.marginLeft = 0;
		layout.marginTop = 0;
		layout.marginRight = 0;
		layout.marginBottom = 0;
		buttons.setLayout(layout);

		createSpecialSelectionButtons(buttons);

		class ButtonHandler extends SelectionAdapter {
			final boolean select;

			ButtonHandler(boolean select) {
				this.select = select;
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setAllChecked(select);
				validatePage();
			}
		}

		button(buttons, Messages.DecoratorModelSelectionPage_2).addSelectionListener(new ButtonHandler(true));
		button(buttons, Messages.DecoratorModelSelectionPage_3).addSelectionListener(new ButtonHandler(false));
	}

	protected void createSpecialSelectionButtons(Composite buttons) {
		// Pass
	}

	protected void createPreferenceArea(Composite parent) {
		final boolean[] preference = { false };
		final Button dontShowThisButton = new Button(parent, SWT.CHECK);
		dontShowThisButton.setText(Messages.DecoratorModelSelectionPage_4);

		dontShowThisButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				preference[0] = dontShowThisButton.getSelection();
			}
		});
		dontShowThisButton.getShell().addDisposeListener(new DisposeListener() {

			@Override
			public void widgetDisposed(DisposeEvent e) {
				storeDontShowThisPreference(preference[0]);
			}
		});
	}

	protected void storeDontShowThisPreference(boolean dontShowThisAgain) {
		ProfileExternalizationUIPreferences.setAutoPromptToLoadProfileApplications(!dontShowThisAgain);
	}

	public void setIsAutomaticPrompt(boolean autoPrompt) {
		showDontShowThis = autoPrompt;
	}

	protected boolean showDontShowThis() {
		return showDontShowThis;
	}

	public void setInput(Package package_, Set<URI> input) {
		this.input = input;

		if (package_ != null) {
			resourceSet = EMFHelper.getResourceSet(package_);
		}

		if (table != null) {
			table.setInput(input);
		}

		validatePage();
	}

	public Set<URI> getInput() {
		return input;
	}

	public void select(Iterable<URI> resources) {
		Set<IFile> files = urisToFiles(resources);
		if (!files.equals(this.initialSelections)) {
			this.initialSelections = urisToFiles(resources);

			if (table != null) {
				table.setCheckedElements(Iterables.toArray(files, IFile.class));
			}

			bus.post(initialSelections);

			validatePage();
		}
	}

	void validatePage() {
		setErrorMessage(null);
		IFile[] checked = getCheckedFiles();
		bus.post(getCheckedFiles());

		if ((input == null) || input.isEmpty()) {
			setErrorMessage(Messages.DecoratorModelSelectionPage_5);
			setPageComplete(false);
		} else if (checked.length == 0) {
			setPageComplete(false);
		} else {
			setPageComplete(true);
		}
	}

	static Set<IFile> urisToFiles(Iterable<URI> uris) {
		Set<IFile> result = Sets.newLinkedHashSet();
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		for (URI next : uris) {
			if (next.isPlatformResource()) {
				IFile file = root.getFile(new Path(next.toPlatformString(true)));
				if ((file != null) && file.exists()) {
					result.add(file);
				}
			}
		}

		return result;
	}

	static IFile[] urisToFilesArray(Iterable<URI> uris) {
		return Iterables.toArray(urisToFiles(uris), IFile.class);
	}

	//
	// Nested types
	//

	static class AvailableResourcesContentProvider implements ITreeContentProvider {
		private final IFile[] none = {};

		@Override
		public Object[] getElements(Object inputElement) {
			IFile[] result = none;

			if (inputElement instanceof Collection<?>) {
				result = urisToFilesArray(Iterables.filter((Collection<?>) inputElement, URI.class));
				Arrays.sort(result, new Comparator<IFile>() {
					@Override
					public int compare(IFile o1, IFile o2) {
						return o1.getName().compareTo(o2.getName());
					}
				});
			}

			return result;
		}

		@Override
		public void dispose() {
			// Pass
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// Pass
		}

		@Override
		public Object[] getChildren(Object parentElement) {
			// There is no tree structure
			return none;
		}

		@Override
		public Object getParent(Object element) {
			// There is no tree structure
			return null;
		}

		@Override
		public boolean hasChildren(Object element) {
			// There is no tree structure
			return false;
		}
	}

}

Back to the top