Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9a0b4de9b5ed6419903a8dfac46b70024d150dac (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (c) 2014, 2015 CEA, 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 (CEA) - Initial API and implementation
 *   Christian W. Damus - bug 458736
 *
 */
package org.eclipse.papyrus.uml.modelrepair.ui;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.providers.AdapterFactoryHierarchicContentProvider;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResourceSet;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
import org.eclipse.papyrus.infra.ui.util.UIUtil;
import org.eclipse.papyrus.infra.widgets.editors.TreeSelectorDialog;
import org.eclipse.papyrus.uml.modelrepair.Activator;
import org.eclipse.papyrus.uml.modelrepair.internal.stereotypes.ZombieStereotypesDescriptor;
import org.eclipse.papyrus.uml.modelrepair.ui.providers.NestedProfilesAdapterFactory;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.uml2.uml.Profile;

import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;


/**
 * This is the ZombieStereotypeDialogPresenter type. Enjoy.
 */
public class ZombieStereotypeDialogPresenter implements IZombieStereotypePresenter {

	private final Shell parentShell;

	private final ResourceSet modelSet;

	private final List<ZombieStereotypesDescriptor> zombieDescriptors = Lists.newArrayListWithExpectedSize(1);

	private final BrowseProfileSupplier dynamicProfileSupplier = new BrowseProfileSupplier();

	private volatile Runnable presentation;

	private Future<?> presentationFuture;

	private final Lock lock = new ReentrantLock();

	private final Condition pendingCond = lock.newCondition();

	private volatile boolean pending = false;

	private final ExecutorService uiExecutor;

	private ExecutorService pendingExecutor;

	public ZombieStereotypeDialogPresenter(Shell parentShell, ResourceSet modelSet) {
		super();

		this.parentShell = parentShell;
		this.modelSet = modelSet;

		uiExecutor = UIUtil.createUIExecutor(parentShell.getDisplay());
	}

	public void dispose() {
		detachPresentation();
		uiExecutor.shutdown();
		if (pendingExecutor != null) {
			pendingExecutor.shutdown();
		}
	}

	void detachPresentation() {
		lock.lock();
		try {
			zombieDescriptors.clear();
			presentation = null;
		} finally {
			lock.unlock();
		}
	}

	public Function<? super EPackage, Profile> getDynamicProfileSupplier() {
		return dynamicProfileSupplier;
	}

	public void addZombies(ZombieStereotypesDescriptor zombies) {
		lock.lock();
		try {
			zombieDescriptors.add(zombies);
			engagePresentation();
		} finally {
			lock.unlock();
		}
	}

	public void asyncAddZombies(Runnable runnable) {
		lock.lock();
		try {
			uiExecutor.execute(runnable);

			// Bump the presentation along until after this runnable has executed
			if (presentationFuture != null) {
				presentationFuture.cancel(false);
				presentation = null;
			}
			engagePresentation();
		} finally {
			lock.unlock();
		}
	}

	/**
	 * Engages the presentation of zombies if it is not already engaged.
	 * 
	 * @precondition The {@link #lock} is locked
	 */
	private void engagePresentation() {
		if (presentation == null) {
			if (!isPending()) {
				internalSetPending(true);
			}

			presentation = new Runnable() {

				public void run() {
					List<ZombieStereotypesDescriptor> zombies;

					lock.lock();
					try {
						if (presentation != this) {
							internalSetPending(false);
							return;
						}
						zombies = ImmutableList.copyOf(zombieDescriptors);
						detachPresentation();
					} finally {
						lock.unlock();
					}

					try {
						if (!zombies.isEmpty()) {
							try {
								ZombieStereotypesDialog zombieDialog = new ZombieStereotypesDialog(parentShell, modelSet, zombies);
								dynamicProfileSupplier.setParentWindow(zombieDialog);
								zombieDialog.setBlockOnOpen(true);
								zombieDialog.open();
							} catch (ServiceException e) {
								StatusManager.getManager().handle(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to open model repair dialog.", e), StatusManager.SHOW);
							} finally {
								dynamicProfileSupplier.setParentWindow(null);
							}
						}
					} finally {
						internalSetPending(false);
					}
				}
			};

			presentationFuture = uiExecutor.submit(presentation);
		}
	}

	public boolean isPending() {
		return pending;
	}

	public void awaitPending(boolean expected) throws InterruptedException {
		lock.lock();
		try {
			while (pending != expected) {
				pendingCond.await();
			}
		} finally {
			lock.unlock();
		}
	}

	public void onPendingDone(final Runnable runnable) {
		lock.lock();
		try {
			if (!pending) {
				// Just do it now
				runnable.run();
			} else {
				// Schedule a wait
				if (pendingExecutor == null) {
					pendingExecutor = Executors.newSingleThreadExecutor();
				}

				pendingExecutor.execute(new Runnable() {

					public void run() {
						try {
							awaitPending(false);
							runnable.run();
						} catch (InterruptedException e) {
							// Oh, well. This runnable won't happen
						}
					}
				});
			}
		} finally {
			lock.unlock();
		}
	}

	private void internalSetPending(boolean pending) {
		lock.lock();
		try {
			this.pending = pending;
			pendingCond.signalAll();
		} finally {
			lock.unlock();
		}
	}

	boolean hasNestedProfiles(Profile profile) {
		return Iterators.any(profile.eAllContents(), Predicates.instanceOf(Profile.class));
	}

	//
	// Nested types
	//

	private class BrowseProfileSupplier implements Function<EPackage, Profile> {

		private Window parentWindow;

		void setParentWindow(Window parentWindow) {
			this.parentWindow = parentWindow;
		}

		public Profile apply(EPackage schema) {
			Profile result = null;

			LabelProviderService labelProvider = null;
			boolean localProvider = false;
			try {
				labelProvider = ServiceUtilsForResourceSet.getInstance().getService(LabelProviderService.class, modelSet);
			} catch (ServiceException e) {
				labelProvider = new LabelProviderServiceImpl();
				localProvider = true;
			}

			final BrowseProfilesDialog dlg = new BrowseProfilesDialog(parentWindow.getShell(), schema, labelProvider);
			Future<URI> task = uiExecutor.submit(new Callable<URI>() {

				public URI call() {
					dlg.setBlockOnOpen(true);
					dlg.open();
					return dlg.getSelectedProfileURI();
				}
			});

			try {
				URI selectedProfileURI = task.get();
				if (selectedProfileURI != null) {
					result = EMFHelper.load(modelSet, selectedProfileURI, Profile.class);

					if ((result != null) && hasNestedProfiles(result)) {
						// Need to select a particular profile from this resource
						final TreeSelectorDialog selectorDlg = new TreeSelectorDialog(parentWindow.getShell());

						NestedProfilesAdapterFactory factory = new NestedProfilesAdapterFactory();
						ITreeContentProvider content = new AdapterFactoryHierarchicContentProvider(factory);
						ILabelProvider labels = new AdapterFactoryLabelProvider(factory);

						try {
							selectorDlg.setContentProvider(content);
							selectorDlg.setLabelProvider(labels);
							final Object dlgInput = result.eResource();

							Future<Profile> specific = uiExecutor.submit(new Callable<Profile>() {

								public Profile call() throws Exception {
									selectorDlg.setTitle("Select a Profile");
									selectorDlg.setDescription("Select a Profile from within the chosen resource.");
									selectorDlg.setHelpAvailable(false);
									selectorDlg.setBlockOnOpen(true);
									selectorDlg.setInput(dlgInput);
									selectorDlg.open();

									Profile result = null;

									Object[] selected = selectorDlg.getResult();
									if ((selected != null) && (selected.length > 0)) {
										result = (Profile) selected[0];
									}

									return result;
								}
							});

							result = specific.get();
						} finally {
							content.dispose();
							labels.dispose();
							factory.dispose();
						}
					}
				}
			} catch (ExecutionException e) {
				Activator.log.error("Profile selection dialog presentation failed.", e); //$NON-NLS-1$
			} catch (InterruptedException e) {
				Activator.log.error("Profile selection dialog presentation interrupted.", e); //$NON-NLS-1$
			} finally {
				if (localProvider) {
					try {
						labelProvider.disposeService();
					} catch (ServiceException e) {
						Activator.log.error(e);
					}
				}
			}

			return result;
		}
	}
}

Back to the top