/**
 * 
 */
package de.uni_jena.iaa.linktype.atomic.editors.grapheditor.commands;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.core.runtime.Assert;
import org.eclipse.gef.commands.Command;

import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SStructuredNode;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SAnnotation;

/**
 * @author Stephan Druskat
 *
 */
public class StructuredNodeAnnotateCommand extends Command {
	
	private SStructuredNode model;
	private TreeMap<String, Pair<String, String>> annotations = new TreeMap<String, Pair<String, String>>();
	
	@Override 
	public void execute() {
		// Remove all annotations; FIXME: Save them in TreeMap<String, Pair<String,String>> oldAnnotations for undo
		for (Iterator<SAnnotation> iterator = model.getSAnnotations().iterator(); iterator.hasNext();) {
			SAnnotation anno = (SAnnotation) iterator.next();
			String name = anno.getName();
			model.removeLabel(name);
			Assert.isTrue(!(model.getSAnnotations().contains(name))); // FIXME: Refactor to unit test method
		}
		// Create annotations from input
		for (Entry<String, Pair<String, String>> anno : getAnnotations().entrySet()) {
			model.createSAnnotation(anno.getValue().getLeft(), anno.getKey(), anno.getValue().getRight());
		}
	}
	
	@Override // FIXME: Remove once undo is implemented
	public boolean canUndo() {
		return false;
	}
			 
	public void setModel(SStructuredNode model) {
		this.model = model;
	}

	/**
	 * @return the annotations
	 */
	public TreeMap<String,Pair<String,String>> getAnnotations() {
		return annotations;
	}

	/**
	 * @param annotations the annotations to set
	 */
	public void setAnnotations(TreeMap<String, Pair<String, String>> annotations) {
		this.annotations = annotations;
	}

}

@@ -1007,169 +1007,439 @@
 %0A%09%09%09
-String name = anno.getName();%0A%09%09%09model.removeLabel(name);%0A%09%09%09Assert.isTrue(!(model.getSAnnotations().contains(name))); // FIXME: Refactor to unit test method%0A%09%09%7D
+if (anno.getNamespace() != null) %7B%0A%09%09%09%09model.removeLabel(anno.getNamespace(), anno.getName());%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09model.removeLabel(anno.getName());%0A%09%09%09%7D%0A%09%09%09String name = anno.getName();%0A%09%09%09Assert.isTrue(!(model.getSAnnotations().contains(name))); // FIXME: Refactor to unit test method%0A%09%09%09Assert.isTrue(!(model.getSAnnotations().contains(anno))); // FIXME: Refactor to unit test method%0A%09%09%7D%0A%09%09System.err.println(model.getLabels());
 %0A%09%09/

Fix bug where annotations do not get removed from an element where the namespace is not nullb