Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 10b3216b13310ec9367f247081890d6ccb8d925c (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
/*****************************************************************************
 * 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.common.databinding.custom;

import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.gmf.runtime.notation.NamedStyle;
import org.eclipse.gmf.runtime.notation.NotationFactory;
import org.eclipse.gmf.runtime.notation.View;


public abstract class AbstractCustomStyleListValueCommand extends AbstractCommand {

	protected View view;

	protected EClass styleClass;

	protected String styleName;

	protected boolean needsCreate;

	protected EStructuralFeature styleFeature;

	protected EditingDomain domain;

	protected Command command;

	protected NamedStyle style;

	public AbstractCustomStyleListValueCommand(EditingDomain domain, View view, String styleName, EClass styleClass, EStructuralFeature styleFeature) {
		this.view = view;
		this.styleName = styleName;
		this.styleClass = styleClass;
		this.styleFeature = styleFeature;
		this.domain = domain;
	}

	@Override
	public void execute() {
		// FIXME: Related to CompoundCommand vs StrictCompoundCommand.
		// Sometimes, canExecute() is not called, and the command is not prepared
		if (!isPrepared) {
			prepare();
			isPrepared = true;
		}

		if (needsCreate) {
			view.getStyles().add(style);
		}
		command.execute();
	}

	@Override
	public void redo() {
		execute();
	}

	@Override
	public boolean prepare() {
		style = view.getNamedStyle(styleClass, styleName);

		// The style may be volatile (generated by CSS). If it doesn't belong to the view, it will need to be attached.
		if (needsCreate = (style == null)) {
			style = (NamedStyle) NotationFactory.eINSTANCE.create(styleClass);
			style.setName(styleName);
		} else if (style.eResource() == null) {
			needsCreate = true;
		}

		command = createCommand();

		// return true;
		return command.canExecute();
	}

	protected abstract Command createCommand();

	@Override
	public void undo() {
		EList currentList = (EList) (style.eGet(styleFeature));
		command.undo();
		if (needsCreate) {
			view.getStyles().remove(style);
		}
	}

}

Back to the top