Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemi Schnekenburger2014-12-08 14:04:17 +0000
committerGerrit Code Review @ Eclipse.org2014-12-08 14:04:17 +0000
commit69a2e15172bc8c85785d2aaacb27a6f29a5b36bb (patch)
tree1676cc6731bbdc0d2b8597707b5f91b301372783 /plugins
parentd4346a8b044e282042d5c6280f775a07fc619d7c (diff)
parent58ef03c6e67e5ede1318e8de48895e4c02b06429 (diff)
downloadorg.eclipse.papyrus-69a2e15172bc8c85785d2aaacb27a6f29a5b36bb.tar.gz
org.eclipse.papyrus-69a2e15172bc8c85785d2aaacb27a6f29a5b36bb.tar.xz
org.eclipse.papyrus-69a2e15172bc8c85785d2aaacb27a6f29a5b36bb.zip
Merge "436952: [Submodel] Deletion of a model fragment does not delete the model fragment resources https://bugs.eclipse.org/bugs/show_bug.cgi?id=436952" into streams/1.0-maintenance
Diffstat (limited to 'plugins')
-rw-r--r--plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractModel.java15
-rw-r--r--plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/EMFLogicalModel.java283
-rw-r--r--plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/IModel.java10
-rw-r--r--plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ModelSet.java11
-rw-r--r--plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/additional/AdditionalResourcesModel.java39
-rw-r--r--plugins/infra/services/org.eclipse.papyrus.infra.services.controlmode/src/org/eclipse/papyrus/infra/services/controlmode/commands/CreateControlResource.java246
6 files changed, 389 insertions, 215 deletions
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractModel.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractModel.java
index cd984f9b525..4d9940c47a5 100644
--- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractModel.java
+++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractModel.java
@@ -8,12 +8,15 @@
*
* Contributors:
* Remi Schnekenburger (CEA LIST) - Initial API and implementation
+ * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 436952
*
*****************************************************************************/
package org.eclipse.papyrus.infra.core.resource;
import java.util.List;
+import java.util.Set;
+import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
/**
@@ -118,4 +121,16 @@ public abstract class AbstractModel implements IModel {
public boolean isModelFor(Object element) {
return false;
}
+
+
+ /**
+ * @see org.eclipse.papyrus.infra.core.resource.IModel#cleanModel(java.util.Set)
+ *
+ * @param toDeleteOnSave
+ */
+ @Override
+ public void cleanModel(Set<URI> resourcesToDelete) {
+ // Nothing to do
+
+ }
}
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/EMFLogicalModel.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/EMFLogicalModel.java
index 653351be6a5..123cc50a34b 100644
--- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/EMFLogicalModel.java
+++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/EMFLogicalModel.java
@@ -1,114 +1,169 @@
-/*****************************************************************************
- * Copyright (c) 2013, 2014 CEA LIST 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:
- * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
- * Christian W. Damus (CEA) - bug 437052
- *
- *****************************************************************************/
-package org.eclipse.papyrus.infra.core.resource;
-
-import java.io.IOException;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.papyrus.infra.core.Activator;
-
-/**
- * An IModel which is an abstraction for a set of consistent EMF Resources
- *
- * @author Camille Letavernier
- *
- */
-public abstract class EMFLogicalModel extends AbstractBaseModel implements IEMFModel {
-
- protected final Set<Resource> resources = new HashSet<Resource>();
-
- public Set<Resource> getResources() {
- return resources;
- }
-
- @Override
- protected void configureResource(Resource resourceToConfigure) {
- super.configureResource(resourceToConfigure);
- if (resourceToConfigure != null) {
- resources.add(resourceToConfigure);
- }
- }
-
- protected boolean isRelatedResource(Resource resource) {
- if (resource == null) {
- return false;
- }
-
- return resource.getURI().fileExtension().equals(getModelFileExtension());
- }
-
- @Override
- public void saveModel() throws IOException {
- final ModelSet set = getModelManager();
-
- for (Resource resource : getResources()) {
- if (set.shouldSave(resource)) {
- try {
- resource.save(null);
- } catch (IOException ex) {
- // If an exception occurs, we should not prevent other resources from being saved.
- // This would probably make things even worse. Catch and log.
- Activator.log.error(ex);
- }
- }
- }
- }
-
- @Override
- public void handle(Resource resource) {
- if (isRelatedResource(resource)) {
- configureResource(resource);
- }
- }
-
- @Override
- public void unload() {
- super.unload();
- getResources().clear();
- }
-
- @Override
- public void setModelURI(URI uriWithoutExtension) {
- for (Resource resource : getResources()) {
- if (isControlled(resource)) {
- updateURI(resource, uriWithoutExtension);
- }
- }
- super.setModelURI(uriWithoutExtension);
- }
-
- protected void updateURI(Resource resource, URI uriWithoutExtension) {
- URI oldBaseURI = this.resource.getURI();
- URI newBaseURI = uriWithoutExtension.appendFileExtension(getModelFileExtension());
-
- URI currentFullURI = resource.getURI();
- URI currentRelativeURI = currentFullURI.deresolve(oldBaseURI);
- URI newFullURI = currentRelativeURI.resolve(newBaseURI);
-
- resource.setURI(newFullURI);
- }
-
- @Override
- public boolean isModelFor(Object element) {
- if (element instanceof EObject) {
- return resources.contains(((EObject) element).eResource());
- }
- return resources.contains(element);
- }
-
-}
+/*****************************************************************************
+ * Copyright (c) 2013, 2014 CEA LIST, 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:
+ * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
+ * Christian W. Damus (CEA) - bug 437052
+ * Christian W. Damus - bug 399859
+ * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 436952
+ *
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.core.resource;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+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.papyrus.infra.core.Activator;
+
+/**
+ * An IModel which is an abstraction for a set of consistent EMF Resources
+ *
+ * @author Camille Letavernier
+ *
+ */
+public abstract class EMFLogicalModel extends AbstractBaseModel implements IEMFModel {
+
+ protected final Set<Resource> resources = new HashSet<Resource>();
+
+ public Set<Resource> getResources() {
+ pruneDeletedResources();
+ return resources;
+ }
+
+ protected void pruneDeletedResources() {
+ ResourceSet rset = getModelManager();
+ for (Iterator<Resource> iter = resources.iterator(); iter.hasNext();) {
+ if (iter.next().getResourceSet() != rset) {
+ // This resource was deleted
+ iter.remove();
+ }
+ }
+ }
+
+ @Override
+ protected void configureResource(Resource resourceToConfigure) {
+ super.configureResource(resourceToConfigure);
+ if (resourceToConfigure != null) {
+ resources.add(resourceToConfigure);
+ }
+ }
+
+ protected boolean isRelatedResource(Resource resource) {
+ if (resource == null) {
+ return false;
+ }
+
+ return resource.getURI().fileExtension().equals(getModelFileExtension());
+ }
+
+ @Override
+ public void saveModel() throws IOException {
+ final ModelSet set = getModelManager();
+
+ for (Resource resource : getResources()) {
+ if (set.shouldSave(resource)) {
+ try {
+ resource.save(null);
+ } catch (IOException ex) {
+ // If an exception occurs, we should not prevent other resources from being saved.
+ // This would probably make things even worse. Catch and log.
+ Activator.log.error(ex);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void handle(Resource resource) {
+ if (isRelatedResource(resource)) {
+ configureResource(resource);
+ }
+ }
+
+ @Override
+ public void unload() {
+ super.unload();
+ getResources().clear();
+ }
+
+ @Override
+ public void setModelURI(URI uriWithoutExtension) {
+ for (Resource resource : getResources()) {
+ if (isControlled(resource)) {
+ updateURI(resource, uriWithoutExtension);
+ }
+ }
+ super.setModelURI(uriWithoutExtension);
+ }
+
+ protected void updateURI(Resource resource, URI uriWithoutExtension) {
+ URI oldBaseURI = this.resource.getURI();
+ URI newBaseURI = uriWithoutExtension.appendFileExtension(getModelFileExtension());
+
+ URI currentFullURI = resource.getURI();
+ URI currentRelativeURI = currentFullURI.deresolve(oldBaseURI);
+ URI newFullURI = currentRelativeURI.resolve(newBaseURI);
+
+ resource.setURI(newFullURI);
+ }
+
+ @Override
+ public boolean isModelFor(Object element) {
+ if (element instanceof EObject) {
+ return resources.contains(((EObject) element).eResource());
+ }
+ return resources.contains(element);
+ }
+
+ /**
+ * Clean model.
+ *
+ * @param resourcesToDelete
+ * the resource to delete
+ * @see org.eclipse.papyrus.infra.core.resource.IModel#cleanModel(java.util.Set)
+ */
+ @Override
+ public void cleanModel(Set<URI> resourcesToDelete) {
+
+ if (!resourcesToDelete.isEmpty()) {
+
+ // Initialise exploration
+ Iterator<Resource> modelResourcesIterator = getResources().iterator();
+ List<Resource> referencedDeletedResources = new ArrayList<Resource>();
+
+ while (modelResourcesIterator.hasNext()) {
+
+ // Verify if current resource will be deleted
+ Resource currentResource = modelResourcesIterator.next();
+ if (resourcesToDelete.contains(currentResource.getURI())) {
+
+ referencedDeletedResources.add(currentResource);
+ }
+
+ }
+
+ // Remove all bad references
+ if (!referencedDeletedResources.isEmpty()) {
+ getResources().removeAll(referencedDeletedResources);
+ }
+
+
+ }
+
+ }
+
+}
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/IModel.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/IModel.java
index 09e5825e8fb..2e4d9ed0ca3 100644
--- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/IModel.java
+++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/IModel.java
@@ -10,6 +10,8 @@
* Contributors:
* CEA LIST - Initial API and implementation
* Christian W. Damus (CEA) - manage models by URI, not IFile (CDO)
+ * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 436952
+ *
*
*****************************************************************************/
package org.eclipse.papyrus.infra.core.resource;
@@ -194,4 +196,12 @@ public interface IModel {
* @return
*/
public boolean isModelFor(Object element);
+
+ /**
+ * Clean model of deleted or uncontrolled references.
+ *
+ * @param resourcesToDelete
+ * the to delete on save
+ */
+ public void cleanModel(Set<URI> resourcesToDelete);
}
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ModelSet.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ModelSet.java
index 3ef7414db4c..152d35d96c7 100644
--- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ModelSet.java
+++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ModelSet.java
@@ -17,6 +17,7 @@
* Christian W. Damus (CEA) - bug 429826
* Christian W. Damus (CEA) - bug 432813
* Christian W. Damus (CEA) - bug 437052
+ * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 436952
*
*****************************************************************************/
package org.eclipse.papyrus.infra.core.resource;
@@ -752,6 +753,10 @@ public class ModelSet extends ResourceSetImpl {
for (IModel model : modelList) {
try {
if (!(model instanceof AdditionalResourcesModel)) {
+
+ // Bug 436952 : Clean referenced resources by models
+ model.cleanModel(getResourcesToDeleteOnSave());
+
model.saveModel();
monitor.worked(1);
}
@@ -762,6 +767,7 @@ public class ModelSet extends ResourceSetImpl {
}
}
try {
+ additional.cleanModel(getResourcesToDeleteOnSave());
additional.saveModel();
} catch (Exception ex) {
Activator.log.error(ex);
@@ -774,6 +780,8 @@ public class ModelSet extends ResourceSetImpl {
}
}
+
+
/**
* @return {@link ModelSet#toDeleteOnSave}
*/
@@ -790,6 +798,7 @@ public class ModelSet extends ResourceSetImpl {
URI uri = uriIterator.next();
if (validateDeleteResource(uri)) {
+
if (deleteResource(uri)) {
uriIterator.remove();
}
@@ -1162,7 +1171,7 @@ public class ModelSet extends ResourceSetImpl {
}
break;
case Notification.REMOVE:
- object = notification.getNewValue();
+ object = notification.getOldValue();
if (object instanceof Resource) {
resourcesToLoadState.remove((object));
}
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/additional/AdditionalResourcesModel.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/additional/AdditionalResourcesModel.java
index 2b0415ad04d..825ea8c04fb 100644
--- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/additional/AdditionalResourcesModel.java
+++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/additional/AdditionalResourcesModel.java
@@ -12,13 +12,18 @@
* Christian W. Damus (CEA) - manage models by URI, not IFile (CDO)
* Christian W. Damus (CEA LIST) - support control mode in CDO resources
* Christian W. Damus (CEA) - bug 437052
+ * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 436952
+ *
*
*****************************************************************************/
package org.eclipse.papyrus.infra.core.resource.additional;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
import java.util.Set;
import org.eclipse.core.runtime.IPath;
@@ -156,4 +161,38 @@ public class AdditionalResourcesModel extends AbstractModel implements IModel {
return res;
}
+ /**
+ * @see org.eclipse.papyrus.infra.core.resource.AbstractModel#cleanModel(java.util.Set)
+ *
+ * @param resourcesToDelete
+ */
+ @Override
+ public void cleanModel(Set<URI> resourcesToDelete) {
+ if (!resourcesToDelete.isEmpty()) {
+
+ // Initialise exploration
+ Iterator<Resource> modelResourcesIterator = modelSet.getResources().iterator();
+ List<Resource> referencedDeletedResources = new ArrayList<Resource>();
+
+ while (modelResourcesIterator.hasNext()) {
+
+ // Verify if current resource will be deleted
+ Resource currentResource = modelResourcesIterator.next();
+ if (resourcesToDelete.contains(currentResource.getURI())) {
+
+ referencedDeletedResources.add(currentResource);
+ }
+
+ }
+
+ // Remove all bad references
+ if (!referencedDeletedResources.isEmpty()) {
+ modelSet.getResources().removeAll(referencedDeletedResources);
+ }
+
+
+ }
+
+ }
+
}
diff --git a/plugins/infra/services/org.eclipse.papyrus.infra.services.controlmode/src/org/eclipse/papyrus/infra/services/controlmode/commands/CreateControlResource.java b/plugins/infra/services/org.eclipse.papyrus.infra.services.controlmode/src/org/eclipse/papyrus/infra/services/controlmode/commands/CreateControlResource.java
index e5068461254..19c87d2f89b 100644
--- a/plugins/infra/services/org.eclipse.papyrus.infra.services.controlmode/src/org/eclipse/papyrus/infra/services/controlmode/commands/CreateControlResource.java
+++ b/plugins/infra/services/org.eclipse.papyrus.infra.services.controlmode/src/org/eclipse/papyrus/infra/services/controlmode/commands/CreateControlResource.java
@@ -1,100 +1,146 @@
-/*****************************************************************************
- * 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 (Atos) arthur.daussy@atos.net - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.infra.services.controlmode.commands;
-
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.gmf.runtime.common.core.command.CommandResult;
-import org.eclipse.papyrus.infra.core.resource.ModelSet;
-import org.eclipse.papyrus.infra.services.controlmode.ControlModeRequest;
-
-/**
- * Command used to create new controlled resource.
- * This command will create the new resources. It will also handle correctly undo and redo
- *
- * @author adaussy
- *
- */
-public class CreateControlResource extends AbstractControlResourceCommand {
-
- /**
- * @param request
- * {@link CreateControlResource#request}
- */
- public CreateControlResource(ControlModeRequest request) {
- super(request, "Create a control resource", null);
- }
-
- /**
- * @param request
- * {@link CreateControlResource#request}
- * @param newFileExtension
- * {@link CreateControlResource#newFileExtension}
- */
- public CreateControlResource(ControlModeRequest request, String newFileExtension) {
- this(request);
- this.newFileExtension = newFileExtension;
- }
-
- @Override
- protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
- Resource resource = getResourceSet().getResource(getTargetUri(), false);
- boolean resourceInSet = resource != null;
- if (resourceInSet) {
- return CommandResult.newErrorCommandResult("The resource is already in the resource set");
- }
- Resource newResource = getResourceSet().createResource(getTargetUri());
- if (newResource == null) {
- return CommandResult.newErrorCommandResult("Unable to create new resource to control");
- }
- // Set the new created target to the request if other command need it
- getRequest().setTargetResource(newResource, getFileExtension());
- // Force modified to true to force serialization
- newResource.setModified(true);
- // In case the resource has been uncontrolled before the it's still the resource to delete on save of the model set. So it has to be removed
- getRequest().getModelSet().getResourcesToDeleteOnSave().remove(newResource.getURI());
- return CommandResult.newOKCommandResult(newResource);
- }
-
- @Override
- protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
- IStatus superStatus = super.doUndo(monitor, info);
- // delete the related file
- Resource oldResource = getRequest().getTargetResource(getFileExtension());
- getResourceSet().getResources().remove(oldResource);
- ModelSet modelSet = getRequest().getModelSet();
- if (modelSet == null) {
- return CommandResult.newErrorCommandResult("Unable to get model set").getStatus();
- }
- modelSet.getResourcesToDeleteOnSave().add(oldResource.getURI());
- return superStatus;
- }
-
- @Override
- protected IStatus doRedo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
- // Re add the resources to the resourceSet
- Resource oldResource = getRequest().getTargetResource(getFileExtension());
- getResourceSet().getResources().add(oldResource);
- ModelSet modelSet = getRequest().getModelSet();
- if (modelSet == null) {
- return CommandResult.newErrorCommandResult("Unable to get model set").getStatus();
- }
- modelSet.getResourcesToDeleteOnSave().remove(oldResource.getURI());
- oldResource.setModified(true);
- return super.doRedo(monitor, info);
- }
-}
+/*****************************************************************************
+ * Copyright (c) 2013, 2014 Atos, 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:
+ * Arthur Daussy (Atos) arthur.daussy@atos.net - Initial API and implementation
+ * Christian W. Damus - bug 399859
+ * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 436952
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.services.controlmode.commands;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.papyrus.infra.core.resource.ModelSet;
+import org.eclipse.papyrus.infra.services.controlmode.ControlModeRequest;
+
+/**
+ * Command used to create new controlled resource.
+ * This command will create the new resources. It will also handle correctly undo and redo
+ *
+ * @author adaussy
+ *
+ */
+public class CreateControlResource extends AbstractControlResourceCommand {
+
+ /**
+ * @param request
+ * {@link CreateControlResource#request}
+ */
+ public CreateControlResource(ControlModeRequest request) {
+ super(request, "Create a control resource", null);
+ }
+
+ /**
+ * @param request
+ * {@link CreateControlResource#request}
+ * @param newFileExtension
+ * {@link CreateControlResource#newFileExtension}
+ */
+ public CreateControlResource(ControlModeRequest request, String newFileExtension) {
+ this(request);
+ this.newFileExtension = newFileExtension;
+ }
+
+ @Override
+ protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
+ Resource resource = getResourceSet().getResource(getTargetUri(), false);
+ boolean resourceInSet = resource != null;
+ if (resourceInSet) {
+ if (failedToLoadBecauseNonExistent(resource) || isNotYetDeteted(resource)) {
+ /*
+ * It doesn't exist or it will be deleted during the save, so by re-creating it we may actually help to fix unresolved proxies
+ * (such as from an out-of-date sash model)
+ */
+ resource.getResourceSet().getResources().remove(resource);
+ resource = null;
+ resourceInSet = false;
+ } else {
+ return CommandResult.newErrorCommandResult("The resource is already in the resource set");
+ }
+ }
+ Resource newResource = getResourceSet().createResource(getTargetUri());
+ if (newResource == null) {
+ return CommandResult.newErrorCommandResult("Unable to create new resource to control");
+ }
+ // Set the new created target to the request if other command need it
+ getRequest().setTargetResource(newResource, getFileExtension());
+ // Force modified to true to force serialization
+ newResource.setModified(true);
+ // In case the resource has been uncontrolled before the it's still the resource to delete on save of the model set. So it has to be removed
+ getRequest().getModelSet().getResourcesToDeleteOnSave().remove(newResource.getURI());
+ return CommandResult.newOKCommandResult(newResource);
+ }
+
+ /**
+ * Checks if the resource is not yet detete.
+ *
+ * @param resource
+ * the resource
+ * @return true, if is not yet detete
+ */
+ protected boolean isNotYetDeteted(Resource resource) {
+ boolean result = false;
+ ResourceSet resourceSet = getResourceSet();
+ if (resourceSet instanceof ModelSet) {
+ result = ((ModelSet) resourceSet).getResourcesToDeleteOnSave().contains(resource.getURI());
+ }
+ return result;
+ }
+
+ /**
+ * Failed to load because non existent.
+ *
+ * @param resource
+ * the resource
+ * @return true, if successful
+ */
+ protected boolean failedToLoadBecauseNonExistent(Resource resource) {
+ boolean result = false;
+
+ if (resource.getContents().isEmpty() && !resource.getErrors().isEmpty()) {
+ // Does it exist to load it?
+ result = !resource.getResourceSet().getURIConverter().exists(resource.getURI(), null);
+ }
+
+ return result;
+ }
+
+ @Override
+ protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+ IStatus superStatus = super.doUndo(monitor, info);
+ // delete the related file
+ Resource oldResource = getRequest().getTargetResource(getFileExtension());
+ getResourceSet().getResources().remove(oldResource);
+ ModelSet modelSet = getRequest().getModelSet();
+ if (modelSet == null) {
+ return CommandResult.newErrorCommandResult("Unable to get model set").getStatus();
+ }
+ modelSet.getResourcesToDeleteOnSave().add(oldResource.getURI());
+ return superStatus;
+ }
+
+ @Override
+ protected IStatus doRedo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+ // Re add the resources to the resourceSet
+ Resource oldResource = getRequest().getTargetResource(getFileExtension());
+ getResourceSet().getResources().add(oldResource);
+ ModelSet modelSet = getRequest().getModelSet();
+ if (modelSet == null) {
+ return CommandResult.newErrorCommandResult("Unable to get model set").getStatus();
+ }
+ modelSet.getResourcesToDeleteOnSave().remove(oldResource.getURI());
+ oldResource.setModified(true);
+ return super.doRedo(monitor, info);
+ }
+}

Back to the top