Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c90c61a8eed6bd8cf886f0a7ccc582b07954b23a (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
/*******************************************************************************
 * Copyright (c) 2013 Atos.
 * 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:
 *     Arthur Daussy <a href="mailto:arthur.daussy@atos.net"> - initial API and implementation
 ******************************************************************************/
package org.eclipse.papyrus.controlmode.request;

import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.emf.type.core.requests.AbstractEditCommandRequest;
import org.eclipse.papyrus.controlmode.interfaces.ControlModeRequestParameters;
import org.eclipse.papyrus.resource.ModelSet;

/**
 * Request used to control or uncontrol an element.
 * 
 * @author adaussy
 * 
 */
public class ControlModeRequest extends AbstractEditCommandRequest implements ControlModeRequestParameters {

	/**
	 * Object to control or uncontrol
	 */
	private EObject targetObject;

	/**
	 * New target {@link URI}.
	 * In the case of control command. This point to the new created resource.
	 * In the case of uncontrol command. This point to the container resource
	 */
	private URI newURI;

	/**
	 * Old source {@link URI}.
	 * In the case of control command. This point to the new created resource.
	 * In the case of uncontrol command. This point to the container resource
	 */
	private URI sourceURI;

	/**
	 * @return {@link ControlModeRequest#sourceURI}
	 */
	public URI getSourceURI() {
		return sourceURI;
	}

	/**
	 * True if the request aim to control an element
	 * False otherwise
	 */
	private boolean isControlRequest;


	/**
	 * Constructor used to create an Uncontrol request (No new {@link URI} specified)
	 * 
	 * @param editingDomain
	 *        Current {@link TransactionalEditingDomain}
	 * @param targetObject
	 *        {@link EObject} to control
	 */
	public ControlModeRequest(TransactionalEditingDomain editingDomain, EObject targetObject) {
		this(editingDomain, targetObject, getTargetURI(targetObject));
		this.isControlRequest = false;
	}

	private static URI getTargetURI(EObject targetObject) {
		EObject eContainer = targetObject.eContainer();
		if(eContainer != null) {
			return eContainer.eResource().getURI();
		}
		return null;
	}

	/**
	 * @return {@link ControlModeRequest#isControlRequest}
	 */
	public boolean isControlRequest() {
		return isControlRequest;
	}

	/**
	 * Set the {@link ControlModeRequest#isControlRequest}
	 * 
	 * @param isControlRequest
	 */
	public void setControlRequest(boolean isControlRequest) {
		this.isControlRequest = isControlRequest;
	}

	/**
	 * Constructor used to create an control request to the new URI
	 * 
	 * @param editingDomain
	 *        Current {@link TransactionalEditingDomain}
	 * @param targetObject
	 *        {@link EObject} to control
	 * @param newURI
	 *        {@link URI} targeting the new resource about to be created
	 */
	public ControlModeRequest(TransactionalEditingDomain editingDomain, EObject objectToControl, URI newURI) {
		super(editingDomain);
		Assert.isNotNull(objectToControl);
		this.targetObject = objectToControl;
		this.newURI = newURI;
		if(newURI != null) {
			setTargetResource(editingDomain.getResourceSet().getResource(newURI, false), newURI.fileExtension());
		}
		Resource r = objectToControl.eResource();
		Assert.isNotNull(r);
		this.sourceURI = r.getURI();
		this.isControlRequest = true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest#getEditHelperContext()
	 */
	public Object getEditHelperContext() {
		return targetObject;
	}

	/**
	 * @return {@link ControlModeRequest#targetObject}
	 */
	public EObject getTargetObject() {
		return targetObject;
	}

	/**
	 * @return {@link ControlModeRequest#newURI}
	 */
	public URI getNewURI() {
		return newURI;
	}


	/**
	 * See {@link ControlModeRequest#isUIAction()}
	 * 
	 * @param value
	 */
	public void setIsUIAction(boolean value) {
		setParameter(IS_UI_ACTION, value);
	}

	/**
	 * Indicate that this request as been launch by user.
	 * That's means that the manager can use User Interface (such as dialog) do display information to user
	 * 
	 * @param value
	 */
	public boolean isUIAction() {
		Object result = getParameter(IS_UI_ACTION);
		return (Boolean)(result == null ? false : result);
	}

	/**
	 * Register the target resource for a specific type (define by file extension)
	 * 
	 * @param resource
	 *        Target resource
	 * @param extension
	 *        Resource File Extension
	 */
	public void setTargetResource(Resource resource, String extension) {
		setParameter(getTargetResourceKey(extension), resource);
	}

	/**
	 * Get the target resource for this kind of file extension
	 * 
	 * @param extension
	 * @return
	 */
	public Resource getTargetResource(String extension) {
		Object result = getParameter(getTargetResourceKey(extension));
		return (Resource)(result == null ? null : result);
	}

	/**
	 * Get the source resource for this kind of file extension
	 * 
	 * @param extension
	 * @return
	 */
	public Resource getSourceResource(String extension) {
		Object result = getParameter(getSourceResourceKey(extension));
		return (Resource)(result == null ? null : result);
	}

	/**
	 * Register the source resource for a specific type (define by file extension)
	 * 
	 * @param resource
	 *        Target resource
	 * @param extension
	 *        Resource File Extension
	 */
	public void setSourceResource(Resource resource, String extension) {
		setParameter(getSourceResourceKey(extension), resource);
	}

	/**
	 * Get the key used to register this file extension into the parameters for target
	 * 
	 * @param extension
	 *        File extension
	 * @return
	 */
	protected String getTargetResourceKey(String extension) {
		return getResourceKey(TARGET_RESOURCE, extension);
	}

	/**
	 * Get the key used to register this file extension into the parameters for source
	 * 
	 * @param extension
	 *        File Extenion
	 * @return
	 */
	protected String getSourceResourceKey(String extension) {
		return getResourceKey(SOURCE_RESOURCE, extension);
	}

	protected String getResourceKey(String key, String extension) {
		return key + extension;
	}

	/**
	 * @return the current {@link ModelSet} in use
	 */
	public ModelSet getModelSet() {
		return (ModelSet)getEditingDomain().getResourceSet();
	}
	
	/**
	 * Create a request for creating a request for controlling a EObject. This request is aware that the action is User Interface action
	 * 
	 * @param editingDomain
	 * @param objectToControl
	 * @param newURI
	 * @return
	 */
	public static ControlModeRequest createUIControlModelRequest(TransactionalEditingDomain editingDomain, EObject objectToControl, URI newURI) {
		ControlModeRequest request = new ControlModeRequest(editingDomain, objectToControl, newURI);
		request.setIsUIAction(true);
		return request;
	}

	/**
	 * Create a request for creating a request for controling a EObject. This request is aware that the action is User Interface action
	 * 
	 * @param editingDomain
	 * @param objectToControl
	 * @return
	 */
	public static ControlModeRequest createUIUncontrolModelRequest(TransactionalEditingDomain editingDomain, EObject objectToControl) {
		ControlModeRequest request = new ControlModeRequest(editingDomain, objectToControl);
		request.setIsUIAction(true);
		return request;
	}
}

Back to the top