Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6825c85ef1e7fa7fdb3915d1c277927919df5910 (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
package org.eclipse.papyrus.readonly.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.utils.BusinessModelResolver;
import org.eclipse.papyrus.infra.onefile.model.IPapyrusFile;
import org.eclipse.papyrus.infra.onefile.model.PapyrusModelHelper;
import org.eclipse.papyrus.infra.onefile.utils.OneFileUtils;
import org.eclipse.papyrus.readonly.ReadOnlyManager;
import org.eclipse.ui.PlatformUI;

public class EnableWriteHandler extends AbstractHandler {

	public Object execute(ExecutionEvent event) throws ExecutionException {
		EObject elem = getSelectedElement();
		if(elem != null && elem.eResource() != null && elem.eResource().getResourceSet() != null) {
			Resource res = elem.eResource();
			ResourceSet rs = res.getResourceSet();

			IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(res.getURI().toPlatformString(true)));
			IPapyrusFile papFile = PapyrusModelHelper.getPapyrusModelFactory().createIPapyrusFile(file);
			IFile[] associatedFiles = OneFileUtils.getAssociatedFiles(papFile);

			ReadOnlyManager.enableWrite(associatedFiles);

			for(IFile associatedFile : associatedFiles) {
				URI associatedUri = URI.createPlatformResourceURI(associatedFile.getFullPath().toString(), true);
				Resource associatedResource = rs.getResource(associatedUri, true);
				if(associatedResource != null) {
					associatedResource.setModified(true);
				}
			}
		}
		return null;
	}

	protected EObject getSelectedElement() {
		ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
		if(selection instanceof IStructuredSelection) {
			Object obj = ((IStructuredSelection)selection).getFirstElement();
			return resolveSemanticObject(obj);
		}
		return null;
	}

	/**
	 * Resolve semantic element
	 * 
	 * @param object
	 *        the object to resolve
	 * @return <code>null</code> or the semantic element associated to the
	 *         specified object
	 */
	protected EObject resolveSemanticObject(Object object) {
		Object businessObject = BusinessModelResolver.getInstance().getBusinessModel(object);
		if(businessObject instanceof EObject) {
			return (EObject)businessObject;
		}
		return null;
	}
}

Back to the top