Skip to main content
summaryrefslogtreecommitdiffstats
blob: 127a8b1ba52023eb82f871d301cc5c9b94a2dd24 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/***********************************************************************
 * Copyright (c) 2008 Anyware Technologies
 * 
 * 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:
 *    Jacques Lescot (Anyware Technologies) - initial API and implementation
 *    Mathieu Velten (Atos Origin) mathieu.velten@atosorigin.com
 **********************************************************************/
package org.eclipse.papyrus.views.documentation.eannotation;

import java.util.Set;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EModelElement;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.papyrus.views.documentation.IDocumentationChangedListener;
import org.eclipse.papyrus.views.documentation.Messages;

/**
 * A GEF Command use to update documentation EAnnotation when an EModelElement is selected.
 * 
 * @author <a href="mailto:jacques.lescot@anyware-tech.com">Jacques LESCOT</a>
 * @author <a href="mailto:sebastien.gabel@c-s.fr">Sebastien GABEL</a>
 * @author Mathieu Velten (Atos Origin) mathieu.velten@atosorigin.com
 */
public class ChangeEAnnotationCommand extends AbstractCommand {

	private EModelElement element;

	private String newValue;

	private String oldValue;
	
	private String source;
	
	private String key;
	
	private Set<IDocumentationChangedListener> documentationChangedListeners;

	/**
	 * Constructor
	 * 
	 * @param element
	 *        the EModelElement
	 * @param newValue
	 *        the new documentation as a String
	 */
	public ChangeEAnnotationCommand(EModelElement element, String source, String key, String newValue, Set<IDocumentationChangedListener> documentationChangedListeners) {
		super(Messages.ChangeDocCommandLabel);
		this.element = element;
		this.source = source;
		this.key = key;
		this.newValue = newValue;
		this.documentationChangedListeners = documentationChangedListeners;
	}

	

	@Override
	public boolean canExecute() {
		return true;
	}



	@Override
	public boolean canUndo() {
		return true;
	}



	/**
	 * {@inheritDoc}
	 */
	public void execute() {
		// stores the previous doc
		oldValue = null;
		EAnnotation annotation = element.getEAnnotation(source);
		if(annotation != null) {
			oldValue = annotation.getDetails().get(key);
		}

		redo();
	}

	/**
	 * {@inheritDoc}
	 */
	public void redo() {
		changeDocumentation(element, newValue);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void undo() {
		changeDocumentation(element, oldValue);
	}
	
	private void notifyListeners(String currentDoc) {
		for (IDocumentationChangedListener listener : documentationChangedListeners) {
			listener.documentationChanged(element);
		}
	}

	/**
	 * Set the documentation for the given Model Element
	 * 
	 * @param elt
	 *        the element to document
	 * @param newDoc
	 *        the documentation text
	 */
	protected IStatus changeDocumentation(EModelElement elt, String newDoc) {
		EAnnotation annotation = elt.getEAnnotation(source);
		if(newDoc != null && !"".equals(newDoc)) { //$NON-NLS-1$
			// creates EAnnotation if needed
			if(annotation == null) {
				annotation = EcoreFactory.eINSTANCE.createEAnnotation();
				annotation.setSource(source);
				elt.getEAnnotations().add(annotation);
			}

			annotation.getDetails().put(key, newDoc);
		} else {
			// remove the documentation
			if(annotation != null) {
				annotation.getDetails().remove(key);

				// remove the EAnnotation if empty
				if(annotation.getDetails().isEmpty()) {
					elt.getEAnnotations().remove(annotation);
				}
			}
		}
		notifyListeners(newDoc);
		
		return Status.OK_STATUS;
	}

}

Back to the top