Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1d87e223a2820f73be31f906525ed2a9146eae7c (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
/*****************************************************************************
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.emf.commands;

import java.util.Collection;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;

/**
 * 
 * This command allows to remove elements from a resource
 * 
 */
public class RemoveFromResourcecommand extends RecordingCommand {

	/**
	 * the managed resource
	 */
	final private Resource resource;

	/**
	 * the element to remove from the resource
	 */
	final private EObject toRemove;

	/**
	 * the list of the elements to remove from the resource
	 */
	final private Collection<EObject> elementsToRemove;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param domain
	 *        the editing domain
	 * @param resource
	 *        the resource
	 * @param toRemove
	 *        the element to remove
	 */
	public RemoveFromResourcecommand(final TransactionalEditingDomain domain, final Resource resource, final EObject toRemove) {
		this(domain, resource, toRemove, null);
	}

	/**
	 * 
	 * Constructor.
	 * 
	 * @param domain
	 *        the editing domain
	 * @param resource
	 *        the resource
	 * @param elementsToRemove
	 *        the list of the elements to remove
	 */
	public RemoveFromResourcecommand(final TransactionalEditingDomain domain, final Resource resource, final Collection<EObject> elementsToRemove) {
		this(domain, resource, null, elementsToRemove);
	}

	/**
	 * 
	 * Constructor.
	 * 
	 * @param domain
	 *        the editing domain
	 * @param resource
	 *        the resource
	 * @param toRemove
	 *        the element to remove
	 * @param elementsToRemove
	 *        the list of the elements to remove
	 */
	protected RemoveFromResourcecommand(final TransactionalEditingDomain domain, final Resource resource, final EObject toRemove, final Collection<EObject> elementsToRemove) {
		super(domain, "Remove an EObject from a resource");
		this.resource = resource;
		this.toRemove = toRemove;
		this.elementsToRemove = elementsToRemove;
	}

	/**
	 * 
	 * remove the element(s) of the resource
	 * 
	 */
	@Override
	protected void doExecute() {
		try {
			if(this.toRemove != null) {
				this.resource.getContents().remove(toRemove);
			}

			if(this.elementsToRemove != null) {
				this.resource.getContents().removeAll(elementsToRemove);
			}
		} catch (Exception e) {
			int i = 0;
			i++;
		}
	}


	/**
	 * 
	 * @see org.eclipse.emf.common.command.AbstractCommand#canExecute()
	 * 
	 * @return
	 */
	@Override
	public boolean canExecute() {
		boolean result = super.canExecute();
		if(toRemove == null && elementsToRemove == null) {
			return false;
		}
		return result;
	}

}

Back to the top