Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d5ad6e46b2b3778f7d216d95f2dffed640a0008a (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
/*****************************************************************************
 * 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 org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
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.RemoveCustomStyleListValueCommand;


public class RemoveCSSStyleSheetCommand extends RemoveCustomStyleListValueCommand {

	protected Resource previousResource;

	public RemoveCSSStyleSheetCommand(EditingDomain domain, View view, String styleName, EClass eClass, EStructuralFeature feature, Object value) {
		super(domain, view, styleName, eClass, feature, value);
	}

	@Override
	public void execute() {
		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()) {
				previousResource = styleSheet.eResource();
				styleSheet.eResource().getContents().remove(styleSheet);
			}
		}

		super.execute();
	}

	@Override
	public void undo() {
		//We destroyed the StyleSheet EObject: recreate it in its previous resource
		if(previousResource != null) {
			previousResource.getContents().add((EObject)value);
		}
		super.undo();
	}

}

Back to the top