Skip to content
Snippets Groups Projects
Commit 074a9904 authored by Simon Barner's avatar Simon Barner
Browse files

- If the creation of new annotations should be wrapped into a command, but the...

- If the creation of new annotations should be wrapped into a command, but the annotation already exists, avoid the usage of a command and just directly return the (already existing) annotation.
- This avoids superfluous model modifications (i.e., the project marked dirty and requires saving)
refs 2369
parent 2fc61624
No related branches found
No related tags found
No related merge requests found
......@@ -41,32 +41,34 @@ import org.fortiss.tooling.kernel.service.IPersistencyService;
* @author diewald
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 2CBB6FE26D2ACA2DC628D90CB72A9D3D
* @ConQAT.Rating YELLOW Hash: 502B780FCA9B38E1D3626217E5A689BF
*/
public class AnnotationUtils {
/**
* Returns the annotation of a given {@link EClass} from a given {@code modelElement}. If the
* annotation does not exist yet, it is created and added to the specification list of the
* {@code modelElement}.
* Returns the annotation of the particular {@code clazz} from a given {@code modelElement}.
*/
private static <T extends IAnnotatedSpecification> T getAnnotationInternal(
final IModelElement modelElement, Class<T> clazz) {
return pickFirstInstanceOf(clazz, modelElement.getSpecifications());
}
/**
* Creates an annotation of a particular {@code eClass} and adds it the specification list of
* the {@code modelElement}.
*/
@SuppressWarnings("unchecked")
private static <T extends IAnnotatedSpecification> T getAnnotation(
final IModelElement modelElement, EClass eClass) {
private static <T extends IAnnotatedSpecification> void createAnnotationInternal(
final IModelElement modelElement, Class<T> clazz) {
final Class<IAnnotatedSpecification> instanceClass =
(Class<IAnnotatedSpecification>)eClass.getInstanceClass();
final T annotation =
(T)pickFirstInstanceOf(instanceClass, modelElement.getSpecifications());
final EClass eClass = getEClassForClass(clazz);
if(annotation != null) {
return annotation;
if(eClass == null) {
return;
}
final T newAnnotation = (T)EcoreUtil.create(eClass);
modelElement.getSpecifications().add(newAnnotation);
return newAnnotation;
}
/**
......@@ -112,35 +114,37 @@ public class AnnotationUtils {
* into a command.
*/
public static <T extends IAnnotatedSpecification> T getAnnotation(
final IModelElement modelElement, Class<T> clazz, boolean wrapIntoCommand) {
final IModelElement modelElement, final Class<T> clazz, boolean wrapIntoCommand) {
final EClass eClass = getEClassForClass(clazz);
T annotation = getAnnotationInternal(modelElement, clazz);
if(annotation != null) {
return annotation;
}
if(eClass != null) {
if(wrapIntoCommand) {
ITopLevelElement modelContext =
IPersistencyService.INSTANCE.getTopLevelElementFor(modelElement);
modelContext.runAsCommand(new Runnable() {
@Override
public void run() {
// TODO (see #2228 / #2208) Keep this code that ensures that non-existing
// annotations are lazily instantiated until #2228 is resolved. It should be
// noted that each time an annotation is constructed from this context, an
// undo/redo command will be created that is not comprehensible for the user
// (see #2208).
getAnnotation(modelElement, eClass);
}
});
}
// Retrieve annotation / create if the creation does not need to be wrapped into a
// command.
return getAnnotation(modelElement, eClass);
if(wrapIntoCommand) {
ITopLevelElement modelContext =
IPersistencyService.INSTANCE.getTopLevelElementFor(modelElement);
modelContext.runAsCommand(new Runnable() {
@Override
public void run() {
// TODO (see #2228 / #2208) Keep this code that ensures that non-existing
// annotations are lazily instantiated until #2228 is resolved. It should be
// noted that each time an annotation is constructed from this context, an
// undo/redo command will be created that is not comprehensible for the user
// (see #2208).
createAnnotationInternal(modelElement, clazz);
}
});
} else {
// Create if the creation does not need to be wrapped into a command.
createAnnotationInternal(modelElement, clazz);
}
return null;
// Return new annotation (be it created in a command, or directly). This will return null in
// case the creation of the new annotation was not successful.
return getAnnotationInternal(modelElement, clazz);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment