Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 58e7b7a7fd3c26dce2362935d9ac16d461e723ea (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
/*****************************************************************************
 * 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:
 *  Saadia DHOUIB (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.export.handlers;


import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.core.resource.NotFoundException;
import org.eclipse.papyrus.uml.export.Activator;
import org.eclipse.papyrus.uml.export.dialog.ProfilesToExportSelectionDialog;
import org.eclipse.papyrus.uml.export.messages.Messages;
import org.eclipse.papyrus.uml.export.util.ProfileUtil;
import org.eclipse.papyrus.uml.export.util.WSFileUtil;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionDelegate;
import org.eclipse.uml2.uml.Profile;


// TODO: Auto-generated Javadoc
/**
 * The Class EditProfileApplicationAction.
 */
public class EditProfileApplicationAction extends ActionDelegate implements IActionDelegate {


	/**
	 * Selected model files.
	 */
	protected List<IFile> files;

	/**
	 * Gets the i file.
	 *
	 * @param selectedObj
	 *            the selected obj
	 * @return the i file
	 */
	public static IFile getIFile(Object selectedObj) {
		IFile result = null;
		if (selectedObj instanceof IFile) {
			result = (IFile) selectedObj;
		}
		// Try to adapt
		if (result == null && selectedObj instanceof IAdaptable) {
			result = (IFile) ((IAdaptable) selectedObj).getAdapter(IFile.class);
		}
		// adapt in ifile
		if (result == null) {
			result = (IFile) Platform.getAdapterManager().getAdapter(selectedObj, IFile.class);
		}
		if (result == null) {
			// try to check if it is a collection
			Collection<?> collec = (Collection<?>) Platform.getAdapterManager().getAdapter(selectedObj, Collection.class);
			if (collec != null) {
				for (Object o : collec) {
					if (o instanceof IFile) {
						IFile f = (IFile) o;
						if ("uml".equals(f.getFileExtension())) {
							result = f;
							break;
						}
					}
				}
			}
		}
		return result != null && "uml".equals(result.getFileExtension()) ? result : null;
	}

	/**
	 * Checks if is profile application is editable.
	 *
	 * @param selectedObj
	 *            the selected obj
	 * @return true, if is profile application editable
	 */
	public static boolean isProfileApplicationEditable(Object selectedObj) {
		IFile file = getIFile(selectedObj);
		if (file != null) {
			ResourceSet resourceSet = new ResourceSetImpl();
			Resource umlResource = WSFileUtil.loadResource(file.getFullPath().toString(), resourceSet);
			if (umlResource.getContents().isEmpty()) {
				// The root doesn't exist.
				return false;
			}
			EObject root = umlResource.getContents().get(0);
			Object[] appliedProfiles = ProfileUtil.getAppliedProfilesFromWS(root);
			if (appliedProfiles != null) {
				for (int i = 0; i < appliedProfiles.length; i++) {
					String installedProfileURI = findCorrespondingInstalledProfile((Profile) appliedProfiles[i]);
					if (installedProfileURI != null) {
						return true;
					}

				}
			}
		}
		return false;

	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
	 * @generated
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			files = ((IStructuredSelection) selection).toList();
		}
	}

	/**
	 * Run.
	 *
	 * @param action
	 *            the action
	 * @see org.eclipse.ui.actions.ActionDelegate#run(org.eclipse.jface.action.IAction)
	 */

	@Override
	public void run(IAction action) {
		if (files != null) {
			Iterator<IFile> filesIt = files.iterator();
			final IFile file = filesIt.next();
			ResourceSet resourceSet = new ResourceSetImpl();
			ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
			try {
				manager.connect(file.getFullPath(), null);
			} catch (CoreException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			final ITextFileBuffer buffer = manager.getTextFileBuffer(file.getFullPath());
			final IDocument doc = buffer.getDocument();
			final FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(doc);
			final MultiTextEdit multiEdit = new MultiTextEdit();
			final Resource umlResource = WSFileUtil.loadResource(file.getFullPath().toString(), resourceSet);
			if (umlResource.getContents().isEmpty()) {
				// The root doesn't exist.
				try {
					throw new NotFoundException(Messages.EditProfileApplicationAction_0);
				} catch (NotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			EObject root = umlResource.getContents().get(0);
			Object[] appliedProfiles = ProfileUtil.getAppliedProfilesFromWS(root);
			ArrayList<Object> appliedLocalProfiles = new ArrayList<Object>();
			for (int i = 0; i < appliedProfiles.length; i++) {
				String installedProfileURI = findCorrespondingInstalledProfile((Profile) appliedProfiles[i]);
				if (installedProfileURI != null) {
					appliedLocalProfiles.add(appliedProfiles[i]);
				}

			}
			if (appliedLocalProfiles != null && !appliedLocalProfiles.isEmpty()) {
				Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
				final ProfilesToExportSelectionDialog dialog = new ProfilesToExportSelectionDialog(shell, true, appliedLocalProfiles.toArray(), "Change profile application from local to installed");

				dialog.open();
				if (Window.OK == dialog.getReturnCode()) {

					IRunnableWithProgress operation = new IRunnableWithProgress() {


						@Override
						public void run(IProgressMonitor monitor) {
							Object[] profiles = dialog.getResult();
							for (int i = 0; i < profiles.length; i++) {
								if (profiles[i] instanceof Profile) {
									// Absolute profile URI (platform:/resource/)
									URI localProfileURI = ((Profile) profiles[i]).eResource().getURI();

									// Absolute model URI (platform:/resource)
									IPath filePath = file.getFullPath();
									URI modelURI = umlResource.getURI();

									// Relative profile URI
									URI relativeProfileURI = localProfileURI.deresolve(modelURI);

									String installedProfileURI = findCorrespondingInstalledProfile((Profile) profiles[i]);
									if (installedProfileURI != null) {

										try {
											WSFileUtil.replaceString(localProfileURI.toString(), installedProfileURI, adapter, doc, 0, multiEdit);
											WSFileUtil.replaceString(relativeProfileURI.toString(), installedProfileURI, adapter, doc, 0, multiEdit);
										} catch (BadLocationException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}

									}
								}


							}
							if (multiEdit.hasChildren()) {
								try {
									multiEdit.apply(doc);
								} catch (MalformedTreeException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (BadLocationException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								try {
									buffer.commit(null, true);
								} catch (CoreException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}


							}
						}
					};
					try {
						PlatformUI.getWorkbench().getProgressService().run(true, true, operation);
					} catch (InvocationTargetException e) {
						IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
						org.eclipse.papyrus.uml.export.Activator.getDefault().getLog().log(status);
					} catch (InterruptedException e) {
						IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
						org.eclipse.papyrus.uml.export.Activator.getDefault().getLog().log(status);
					}
				}
			} else {
				// launch an error message
				Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
				MessageDialog.openError(shell, "Error", "No profile application to edit");
			}
		}


	}





	/**
	 * Find corresponding installed profile.
	 *
	 * @param profile
	 *            the profile
	 * @return the corresponding installed profile uri
	 */
	public static String findCorrespondingInstalledProfile(Profile profile) {
		// TODO Auto-generated method stub
		IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode("org.eclipse.papyrus.uml.export"); //$NON-NLS-1$
		org.osgi.service.prefs.Preferences node = preferences.node("installedProfilesUriCorrespondance");
		String installedProfileURI = node.get(profile.eResource().getURI().toString(), "default"); //$NON-NLS-1$
		if ((!installedProfileURI.equals("default"))) {
			URI uri = URI.createURI(installedProfileURI);
			try {
				ResourceSet resourceSet = new ResourceSetImpl();
				Resource resource = resourceSet.getResource(uri, true);
				// verify that the profile is indeed installed in the platform before replacing the uri
				if ((resource.isLoaded()) && (!(resource.getContents().isEmpty()))) {
					// System.err.println(installedProfileURI);
					return installedProfileURI;
				}
			} catch (WrappedException ex) {
				ex.printStackTrace();
			}
		} else {
			return null;
		}
		return null;

	}



}

Back to the top