Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCamille Letavernier2013-07-23 15:20:37 +0000
committerCamille Letavernier2013-07-23 15:20:37 +0000
commita9891afd0c4481d1f4a576872b164754a0b894dd (patch)
tree871e61175f31f536240bdb3ae6ab943fd23a80e7 /plugins
parentbde534316c175510e621fd30bd47b1cfb116f071 (diff)
downloadorg.eclipse.papyrus-a9891afd0c4481d1f4a576872b164754a0b894dd.tar.gz
org.eclipse.papyrus-a9891afd0c4481d1f4a576872b164754a0b894dd.tar.xz
org.eclipse.papyrus-a9891afd0c4481d1f4a576872b164754a0b894dd.zip
408491: Papyrus shall enable to easily switch between local and
registered profiles. https://bugs.eclipse.org/bugs/show_bug.cgi?id=408491 Initial working prototype
Diffstat (limited to 'plugins')
-rw-r--r--plugins/infra/org.eclipse.papyrus.infra.modelrepair/plugin.xml2
-rw-r--r--plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/readonly/ReadOnlyHelper.java40
-rw-r--r--plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/ModelRepairDialog.java197
-rw-r--r--plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/providers/ResourceLinksContentProvider.java48
4 files changed, 249 insertions, 38 deletions
diff --git a/plugins/infra/org.eclipse.papyrus.infra.modelrepair/plugin.xml b/plugins/infra/org.eclipse.papyrus.infra.modelrepair/plugin.xml
index 493807b515b..fd5e6722731 100644
--- a/plugins/infra/org.eclipse.papyrus.infra.modelrepair/plugin.xml
+++ b/plugins/infra/org.eclipse.papyrus.infra.modelrepair/plugin.xml
@@ -41,7 +41,7 @@
<command
defaultHandler="org.eclipse.papyrus.uml.modelrepair.handler.ModelRepairHandler"
id="org.eclipse.papyrus.uml.modelrepair.editlinks"
- name="Edit model links">
+ name="Edit model dependencies">
</command>
</extension>
diff --git a/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/readonly/ReadOnlyHelper.java b/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/readonly/ReadOnlyHelper.java
new file mode 100644
index 00000000000..f182c3da819
--- /dev/null
+++ b/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/readonly/ReadOnlyHelper.java
@@ -0,0 +1,40 @@
+package org.eclipse.papyrus.uml.modelrepair.readonly;
+
+import java.util.Iterator;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.edit.domain.EditingDomain;
+import org.eclipse.emf.edit.domain.IEditingDomainProvider;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.papyrus.infra.core.resource.IReadOnlyHandler;
+import org.eclipse.papyrus.infra.emf.readonly.ReadOnlyManager;
+
+import com.google.common.base.Optional;
+
+
+public class ReadOnlyHelper {
+
+ public static boolean isReadOnly(Resource r) {
+ EditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE.getEditingDomain(r.getResourceSet());
+ if(domain == null && r.getResourceSet() instanceof IEditingDomainProvider) {
+ domain = ((IEditingDomainProvider)r.getResourceSet()).getEditingDomain();
+ }
+ IReadOnlyHandler readOnlyHandler = ReadOnlyManager.getReadOnlyHandler(domain);
+
+ Iterator<EObject> iterator = r.getAllContents();
+ while(iterator.hasNext()) {
+ Optional<Boolean> readOnly = readOnlyHandler.isReadOnly(iterator.next());
+ if(!readOnly.isPresent()) {
+ return false;
+ }
+
+ if(!readOnly.get()) {
+ return false;
+ }
+ }
+
+ //True if each element is read only
+ return true;
+ }
+}
diff --git a/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/ModelRepairDialog.java b/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/ModelRepairDialog.java
index 21770074453..370f00d426b 100644
--- a/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/ModelRepairDialog.java
+++ b/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/ModelRepairDialog.java
@@ -12,10 +12,24 @@
package org.eclipse.papyrus.uml.modelrepair.ui;
import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.transaction.RecordingCommand;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ILabelProvider;
@@ -23,6 +37,7 @@ import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.uml.modelrepair.Activator;
+import org.eclipse.papyrus.uml.modelrepair.readonly.ReadOnlyHelper;
import org.eclipse.papyrus.uml.modelrepair.ui.providers.ResourceLabelProvider;
import org.eclipse.papyrus.uml.modelrepair.ui.providers.ResourceLinksContentProvider;
import org.eclipse.swt.SWT;
@@ -37,6 +52,7 @@ import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
@@ -73,9 +89,22 @@ public class ModelRepairDialog extends TrayDialog {
self.setLayout(new GridLayout(1, false));
self.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
- Label label = new Label(self, SWT.NONE);
- label.setText("Press F2 or double click on an element to change its URI");
- label.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
+ Label descriptionLabel = new Label(self, SWT.NONE);
+ String description = "Press F2 or double click on an element to change its URI\n";
+ description += "Edit a root element to modify all dependencies to the selected resource\n";
+ description += "Edit a child element to modify only the dependencies from the root resource to the selected one\n";
+ descriptionLabel.setText(description);
+
+ descriptionLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
+
+ Label warningLabel = new Label(self, SWT.NONE);
+ String warning = "/!\\ Modifying the dependencies between models may result into an inconsistent state and a corrupted model /!\\\n";
+ warning += "Most corrupted model can be repaired with this tool";
+ warningLabel.setText(warning);
+ warningLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED));
+
+ warningLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
+
viewer = new TreeViewer(self, SWT.FULL_SELECTION | SWT.BORDER);
tree = viewer.getTree();
@@ -175,6 +204,9 @@ public class ModelRepairDialog extends TrayDialog {
Object element = currentSelection.getData();
textEditor.setText(((ILabelProvider)viewer.getLabelProvider()).getText(element));
editor.setEditor(textEditor, currentSelection);
+ textEditor.setFocus();
+ textEditor.forceFocus();
+ textEditor.setSelection(textEditor.getText().length()); //At the end
}
protected void validate() {
@@ -185,25 +217,162 @@ public class ModelRepairDialog extends TrayDialog {
return;
}
- Object element = currentSelection.getData();
+ TransactionalEditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE.getEditingDomain(modelSet);
+ domain.getCommandStack().execute(new RecordingCommand(domain, "Change URI") {
- Resource resource;
- if(element instanceof URI) {
- resource = modelSet.getResource((URI)element, false);
- } else if(element instanceof Resource) {
- resource = (Resource)element;
- } else {
+ @Override
+ protected void doExecute() {
+ doValidate();
+ }
+
+ });
+
+
+ viewer.refresh();
+ }
+
+ protected void doValidate() {
+ URI newURI = URI.createURI(textEditor.getText());
+
+ Resource fromResource = null;
+
+ Resource toResource;
+
+ if(currentSelection.getParentItem() == null) { //We selected a root, which is a resource
+ Object element = currentSelection.getData();
+
+ if(element instanceof Resource) {
+ toResource = (Resource)element;
+ } else {
+ return;
+ }
+ } else { //We selected a child, which is a URI. Its parent is a Resource.
+ TreeItem parentItem = currentSelection.getParentItem();
+ Object parentData = parentItem.getData();
+ if(parentData instanceof Resource) {
+ fromResource = (Resource)parentData;
+ } else {
+ return;
+ }
+
+ Object element = currentSelection.getData();
+
+ if(element instanceof URI) {
+ toResource = modelSet.getResource((URI)element, false);
+ } else {
+ return;
+ }
+ }
+
+ if(toResource == null) {
return;
}
- if(resource == null) {
+ if(toResource.getURI().equals(newURI)) {
return;
}
- URI newURI = URI.createURI(textEditor.getText());
- resource.setURI(newURI);
+ Set<Resource> resourcesToEdit = new HashSet<Resource>();
- viewer.refresh();
+ if(fromResource == null) {
+ resourcesToEdit.addAll(modelSet.getResources());
+ resourcesToEdit.remove(toResource);
+ } else {
+ resourcesToEdit.add(fromResource);
+ }
+
+ for(Resource currentResource : resourcesToEdit) {
+
+ if(ReadOnlyHelper.isReadOnly(currentResource)) {
+ continue;
+ }
+
+ Iterator<EObject> allContentsIterator = currentResource.getAllContents();
+
+ while(allContentsIterator.hasNext()) {
+ EObject eObject;
+
+ eObject = allContentsIterator.next();
+
+ if(eObject.eIsProxy()) {
+ continue;
+ }
+
+ for(EReference reference : eObject.eClass().getEAllReferences()) {
+ if(reference.isTransient()) {
+ continue;
+ }
+
+ if(reference.isContainment()) {
+ continue;
+ }
+
+ Object value = eObject.eGet(reference);
+ if(value instanceof EObject) {
+ EObject targetEObject = (EObject)value;
+ if(targetEObject.eResource() == toResource) {
+ EClass targetEClass = targetEObject.eClass();
+ EObject newEObject = targetEClass.getEPackage().getEFactoryInstance().create(targetEClass);
+ if(newEObject instanceof InternalEObject) {
+ String eObjectFragment = targetEObject.eResource().getURIFragment(targetEObject);
+ URI eObjectURI = newURI.appendFragment(eObjectFragment);
+ ((InternalEObject)newEObject).eSetProxyURI(eObjectURI);
+
+ try {
+ System.out.println("Replace " + EcoreUtil.getURI(targetEObject) + " with " + EcoreUtil.getURI(newEObject));
+ eObject.eSet(reference, newEObject);
+ } catch (Exception ex) {
+ Activator.log.error(ex);
+ }
+ }
+ }
+ } else if(value instanceof Collection<?>) {
+ Map<Integer, EObject> indexToNewValue = new HashMap<Integer, EObject>();
+
+ Collection<Object> collection = (Collection<Object>)value;
+
+ int i = 0;
+ for(Object collectionElement : (Collection<?>)value) {
+ if(collectionElement instanceof EObject) {
+ EObject targetEObject = (EObject)collectionElement;
+ if(targetEObject.eResource() == toResource) {
+ EClass targetEClass = targetEObject.eClass();
+ EObject newEObject = targetEClass.getEPackage().getEFactoryInstance().create(targetEClass);
+ if(newEObject instanceof InternalEObject) {
+ String eObjectFragment = targetEObject.eResource().getURIFragment(targetEObject);
+ URI eObjectURI = newURI.appendFragment(eObjectFragment);
+ ((InternalEObject)newEObject).eSetProxyURI(eObjectURI);
+
+ System.out.println("Replace " + EcoreUtil.getURI(targetEObject) + " with " + EcoreUtil.getURI(newEObject));
+ indexToNewValue.put(i, newEObject);
+ }
+ }
+ }
+
+ i++;
+ }
+
+ Object[] allValues = collection.toArray();
+
+ if(indexToNewValue.isEmpty()) {
+ continue;
+ }
+
+ for(Map.Entry<Integer, EObject> entry : indexToNewValue.entrySet()) {
+ // System.out.println("Replace " + allValues[entry.getKey()] + " with " + entry.getValue());
+ allValues[entry.getKey()] = entry.getValue();
+ }
+
+ try {
+ collection.clear();
+ collection.addAll(Arrays.asList(allValues));
+ } catch (Exception ex) {
+ // Activator.log.warn(ex.getMessage());
+ }
+ }
+ }
+ }
+ }
}
@Override
diff --git a/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/providers/ResourceLinksContentProvider.java b/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/providers/ResourceLinksContentProvider.java
index 67559599d75..740698b7de6 100644
--- a/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/providers/ResourceLinksContentProvider.java
+++ b/plugins/infra/org.eclipse.papyrus.infra.modelrepair/src/org/eclipse/papyrus/uml/modelrepair/ui/providers/ResourceLinksContentProvider.java
@@ -11,17 +11,12 @@ 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.emf.ecore.util.EContentsEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
-import org.eclipse.emf.edit.domain.EditingDomain;
-import org.eclipse.emf.edit.domain.IEditingDomainProvider;
-import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
-import org.eclipse.papyrus.infra.core.resource.IReadOnlyHandler;
-import org.eclipse.papyrus.infra.emf.readonly.ReadOnlyManager;
-
-import com.google.common.base.Optional;
+import org.eclipse.papyrus.uml.modelrepair.readonly.ReadOnlyHelper;
public class ResourceLinksContentProvider implements ITreeContentProvider {
@@ -70,13 +65,22 @@ public class ResourceLinksContentProvider implements ITreeContentProvider {
}
public boolean hasChildren(Object element) {
+ if(element instanceof Resource) {
+ Resource resource = (Resource)element;
+ if(ReadOnlyHelper.isReadOnly(resource)) {
+ return false;
+ }
+ }
return getChildren(element).length > 0;
}
protected Map<Resource, Set<URI>> allLinks() {
- if(allLinks != null) {
- return allLinks;
- }
+ //Do not cache, to recompute the dependencies after a refresh
+ //This may be expensive. TODO: Manually clean the cache before refresh.
+
+ // if(allLinks != null) {
+ // return allLinks;
+ // }
allLinks = new HashMap<Resource, Set<URI>>();
@@ -84,13 +88,6 @@ public class ResourceLinksContentProvider implements ITreeContentProvider {
return allLinks;
}
- EditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE.getEditingDomain(input);
- if(input instanceof IEditingDomainProvider) {
- domain = ((IEditingDomainProvider)input).getEditingDomain();
- }
- IReadOnlyHandler readOnlyHandler = ReadOnlyManager.getReadOnlyHandler(domain);
-
-
for(Resource resource : input.getResources()) {
Set<URI> allReferencedURIs = new HashSet<URI>();
@@ -100,13 +97,15 @@ public class ResourceLinksContentProvider implements ITreeContentProvider {
while(allContents.hasNext()) {
EObject nextElement = allContents.next();
- Optional<Boolean> isReadOnly = readOnlyHandler.isReadOnly(nextElement);
- if(isReadOnly.isPresent() && isReadOnly.get()) {
- continue;
- }
+ // Optional<Boolean> isReadOnly = readOnlyHandler.isReadOnly(nextElement);
+ // if(isReadOnly.isPresent() && isReadOnly.get()) {
+ // continue;
+ // }
List<EObject> allReferencedEObjects = nextElement.eCrossReferences();
- for(EObject referencedEObject : allReferencedEObjects) {
+ EContentsEList.FeatureIterator<EObject> iterator = (EContentsEList.FeatureIterator<EObject>)allReferencedEObjects.iterator();
+ while(iterator.hasNext()) {
+ EObject referencedEObject = iterator.next();
if(referencedEObject.eIsProxy()) {
allReferencedURIs.add(EcoreUtil.getURI(referencedEObject));
} else {
@@ -116,6 +115,10 @@ public class ResourceLinksContentProvider implements ITreeContentProvider {
//Exclude local references
if(referencedEObject.eResource() != resource) {
+ if(iterator.feature().isTransient()) {
+ continue;
+ }
+
allReferencedURIs.add(referencedEObject.eResource().getURI());
}
}
@@ -133,5 +136,4 @@ public class ResourceLinksContentProvider implements ITreeContentProvider {
return allLinks;
}
-
}

Back to the top