Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 62b05b4926523c467ea8cfd7fb688c66e35dc3e7 (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
/*
 * 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 463631
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905
 *
 */
package org.eclipse.papyrus.infra.ui.internal.emf.readonly.handlers;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.papyrus.infra.core.resource.AbstractReadOnlyHandler;
import org.eclipse.papyrus.infra.core.resource.IReadOnlyHandler2;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ReadOnlyAxis;
import org.eclipse.papyrus.infra.emf.readonly.internal.ControlledResourceTracker;
import org.eclipse.papyrus.infra.ui.editor.reload.IReloadContextProvider;
import org.eclipse.papyrus.infra.ui.internal.emf.messages.Messages;
import org.eclipse.swt.widgets.Display;

import com.google.common.base.Optional;


/**
 * A {@linkplain IReadOnlyHandler2 read-only handler} for objects in referenced models, which by default shouldn't be editable in the context of the
 * model referencing them. This is a discretion-based handler.
 */
public class ReferencedModelReadOnlyHandler extends AbstractReadOnlyHandler implements IReloadContextProvider {

	private final ControlledResourceTracker controlledResourceTracker;

	private final Set<URI> writableReferencedModels = new HashSet<URI>();

	private boolean interactive = true;

	public ReferencedModelReadOnlyHandler(EditingDomain editingDomain) {
		super(editingDomain);

		controlledResourceTracker = ControlledResourceTracker.getInstance(editingDomain);
	}

	/**
	 * Queries whether I interact with the user to confirm making resources writable. I am interactive by default.
	 *
	 * @return whether I am interactive
	 */
	public boolean isInteractive() {
		return interactive;
	}

	/**
	 * Sets whether I interact with the user to confirm making resources writable.
	 *
	 * @param interactive
	 *            whether I am interactive
	 */
	public void setInteractive(boolean interactive) {
		this.interactive = interactive;
	}

	@Override
	public Optional<Boolean> anyReadOnly(Set<ReadOnlyAxis> axes, URI[] uris) {
		Optional<Boolean> result = Optional.absent();

		if (axes.contains(ReadOnlyAxis.DISCRETION)) {
			final URIConverter converter = getEditingDomain().getResourceSet().getURIConverter();

			for (int i = 0; i < uris.length; i++) {
				// Clients may pass object URIs (including fragments), so trim to a resource URI because we operate on the resource level
				URI next = uris[i].trimFragment();

				// If the resource doesn't exist, then it can't be opened in some other editor, so
				// we needn't be concerned about editing it in the context of a referencing model
				if (!writableReferencedModels.contains(next.trimFileExtension()) && isNotModelSetMainModel(next) && converter.exists(next, null)) {
					result = Optional.of(true);
					break;
				}
			}
		}

		return result;
	}

	@Override
	public Optional<Boolean> canMakeWritable(Set<ReadOnlyAxis> axes, URI[] uris) {
		Optional<Boolean> result = Optional.absent();

		if (axes.contains(ReadOnlyAxis.DISCRETION)) {
			for (int i = 0; i < uris.length; i++) {
				// Clients may pass object URIs (including fragments), so trim to a resource URI because we operate on the resource level
				URI next = uris[i].trimFragment();

				if (isNotModelSetMainModel(next)) {
					result = Optional.of(true);
				} else {
					// If it's not something I handle, then bomb
					result = Optional.of(false);
					break;
				}
			}
		}

		return result;
	}

	@Override
	public Optional<Boolean> makeWritable(Set<ReadOnlyAxis> axes, URI[] uris) {
		Optional<Boolean> result = Optional.absent();

		if (axes.contains(ReadOnlyAxis.DISCRETION)) {
			final List<URI> toMakeWritable = new ArrayList<URI>(uris.length);

			for (int i = 0; i < uris.length; i++) {
				// Clients may pass object URIs (including fragments), so trim to a resource URI because we operate on the resource level
				URI next = uris[i].trimFragment();

				if (isNotModelSetMainModel(next)) {
					toMakeWritable.add(next);
				}
			}

			if (!toMakeWritable.isEmpty()) {
				final boolean[] enableWrite = { !isInteractive() };

				if (isInteractive()) {
					Display currentDisplay = Display.getCurrent();
					if (currentDisplay == null) {
						currentDisplay = Display.getDefault();
					}
					currentDisplay.syncExec(new Runnable() {

						@Override
						public void run() {
							StringBuilder message = new StringBuilder(Messages.ReferencedModelReadOnlyHandler_promptMsg);
							for (URI uri : toMakeWritable) {
								String path;
								if (uri.isPlatformResource()) {
									path = uri.toPlatformString(true);
								} else if (uri.isFile()) {
									path = uri.toFileString();
								} else {
									path = uri.toString();
								}

								message.append(path);
								message.append("\n"); //$NON-NLS-1$
							}
							enableWrite[0] = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), Messages.ReferencedModelReadOnlyHandler_promptTitle, message.toString());
						}
					});
				}

				if (enableWrite[0]) {
					for (URI next : toMakeWritable) {
						writableReferencedModels.add(next.trimFileExtension());
						fireReadOnlyStateChanged(ReadOnlyAxis.DISCRETION, next, true);
					}
				}

				result = Optional.of(enableWrite[0]);
			}
		}

		return result;
	}

	protected boolean isNotModelSetMainModel(URI uri) {
		// Default to false result because, if the context is not a ModelSet, the question doesn't make sense and we should not restrict editing
		boolean result = false;

		ResourceSet rset = getEditingDomain().getResourceSet();
		if (rset instanceof ModelSet) {
			ModelSet modelSet = (ModelSet) rset;
			Set<URI> rootURIs = resolveRootResourceURIs(modelSet, uri);

			if (!rootURIs.isEmpty()) {
				URI next = rootURIs.iterator().next();
				result = modelSet.isUserModelResource(next)
						&& !rootURIs.contains(modelSet.getURIWithoutExtension());
			}
		}

		return result;
	}

	/**
	 * Trace a potential controlled unit's root resources.
	 *
	 * @param modelSet
	 *            the contextual model-set
	 * @param uri
	 *            a resource URI
	 *
	 * @return the corresponding root resource URI, which is just the original {@code uri} if either it isn't a controlled unit or we cannot tell
	 */
	protected Set<URI> resolveRootResourceURIs(ModelSet modelSet, URI uri) {
		return controlledResourceTracker.getRootResourceURIs(uri);
	}

	@Override
	public Object createReloadContext() {
		return writableReferencedModels;
	}

	@Override
	@SuppressWarnings("unchecked")
	public void restore(Object reloadContext) {
		writableReferencedModels.addAll((Set<URI>) reloadContext);
	}
}

Back to the top