Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da75e2676c49b563e5484536c30ff1220f59d6db (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
/*****************************************************************************
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.properties.databinding;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.properties.databinding.custom.RemoveAllCustomStyleListValueCommand;


public class RemoveAllCSSStyleSheetValueCommand extends RemoveAllCustomStyleListValueCommand {

	protected final Set<EObject> deletedEObjects = new HashSet<EObject>();

	public RemoveAllCSSStyleSheetValueCommand(EditingDomain domain, View view, String styleName, EClass eClass, EStructuralFeature feature, Collection<?> values) {
		super(domain, view, styleName, eClass, feature, values);
	}

	@Override
	public void execute() {
		for(Object value : values) {
			if(value instanceof EObject) {
				EObject styleSheet = (EObject)value;
				Collection<EStructuralFeature.Setting> references = EMFHelper.getUsages(styleSheet);
				//We're removing the last reference to this styleSheet (Only if the stylesheet is contained
				//in the same resource as the view referencing it... We don't modify external models)
				if(references.size() == 1 && styleSheet.eResource() == view.eResource()) {
					deletedEObjects.add(styleSheet);
					styleSheet.eResource().getContents().remove(styleSheet);
				}
			}
		}

		super.execute();
	}

	@Override
	public void undo() {
		for(Object value : values) {
			if(value instanceof EObject) {
				EObject eObject = (EObject)value;
				if(deletedEObjects.contains(eObject)) {
					//We destroyed the StyleSheet EObject: recreate it in its previous resource
					view.eResource().getContents().add(eObject);
				}
			}
		}
		super.undo();
	}

}

Back to the top