Author: joern Date: Mon Oct 10 10:59:39 2011 New Revision: 1180872 URL: http://svn.apache.org/viewvc?rev=1180872&view=rev Log: UIMA-2245 Moved annotation style settigns methods to the Annotation Editor (and away form the Cas Document Provider) and defined a new storage format for an Annotation Style.
Modified: uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationStyle.java uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/CasDocumentProvider.java uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/styleview/AnnotationStyleViewPage.java uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/ui/property/EditorAnnotationPropertyPage.java Modified: uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java?rev=1180872&r1=1180871&r2=1180872&view=diff ============================================================================== --- uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java (original) +++ uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java Mon Oct 10 10:59:39 2011 @@ -446,7 +446,7 @@ public final class AnnotationEditor exte // ask document provider for this // getAnnotation must be added to cas document provider - AnnotationStyle style = getCasDocumentProvider().getAnnotationStyle(getEditorInput(), + AnnotationStyle style = getAnnotationStyle( eclipseAnnotation.getAnnotationFS().getType()); return style.getLayer(); @@ -1046,6 +1046,41 @@ public final class AnnotationEditor exte } /** + * Retrieves an <code>AnnotationStyle</code> from the underlying storage. + * + * @param element + * @param type + * @return + */ + public AnnotationStyle getAnnotationStyle(Type type) { + if (type == null) + throw new IllegalArgumentException("type parameter must not be null!"); + + IPreferenceStore prefStore = getCasDocumentProvider(). + getTypeSystemPreferenceStore(getEditorInput()); + + return AnnotationStyle.getAnnotationStyleFromStore(prefStore, type.getName()); + } + + + /** + * Sets an annotation style. + * + * Note: Internal usage only! + * + * @param element + * @param style + */ + // TODO: Disk must be accessed for every changed annotation style + // add a second method which can take all changed styles + public void setAnnotationStyle(AnnotationStyle style) { + IPreferenceStore prefStore = getCasDocumentProvider().getTypeSystemPreferenceStore(getEditorInput()); + AnnotationStyle.putAnnotatationStyleToStore(prefStore, style); + + getCasDocumentProvider().saveTypeSystemPreferenceStore(getEditorInput()); + } + + /** * Set the shown annotation status of a type. * * @param type @@ -1053,7 +1088,7 @@ public final class AnnotationEditor exte * it not shown */ private void showAnnotationType(Type type, boolean isVisible) { - AnnotationStyle style = getCasDocumentProvider().getAnnotationStyle(getEditorInput(), type); + AnnotationStyle style = getAnnotationStyle(type); if (isVisible) { @@ -1086,8 +1121,7 @@ public final class AnnotationEditor exte boolean isKeepLineSpacing = false; for(Type shownType : shownAnnotationTypes) { - AnnotationStyle potentialTagStyle = - getCasDocumentProvider().getAnnotationStyle(getEditorInput(), shownType); + AnnotationStyle potentialTagStyle = getAnnotationStyle(shownType); if (AnnotationStyle.Style.TAG.equals(potentialTagStyle.getStyle())) { isKeepLineSpacing = true; Modified: uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationStyle.java URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationStyle.java?rev=1180872&r1=1180871&r2=1180872&view=diff ============================================================================== --- uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationStyle.java (original) +++ uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationStyle.java Mon Oct 10 10:59:39 2011 @@ -20,6 +20,10 @@ package org.apache.uima.caseditor.editor; import java.awt.Color; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jface.preference.IPreferenceStore; /** @@ -212,4 +216,123 @@ public final class AnnotationStyle { annotationStyle += " Config: " + getConfiguration(); return annotationStyle; } + + // TODO: Format must be redefined, so that only one key/string pair is needed to save it! + + // key -> type name + ."style" + // value -> key/value pairs -> key=value; key=value; + // split on ";" and then split on = to parse values into a map + // maybe make a util which can save a String, String map to a line, and load it from String ... + private static String serializeProperties(Map<String, String> properties) { + + StringBuilder configString = new StringBuilder(); + + for (Map.Entry<String, String> entry : properties.entrySet()) { + configString.append(entry.getKey().trim()); + configString.append("="); + configString.append(entry.getValue().trim()); + configString.append(";"); + } + + return configString.toString(); + } + + private static Map<String, String> parseProperties(String line) { + Map<String, String> properties = new HashMap<String, String>(); + + String keyValueStrings[] = line.split(";"); + + for (String keyValueString : keyValueStrings) { + String keyValuePair[] = keyValueString.split("="); + + if (keyValuePair.length == 2) { + properties.put(keyValuePair[0], keyValuePair[1]); + } + } + + return properties; + } + + /** + * Note: This method must not be called by user code! It is only public because the migration + * code in the Cas Editor Ide Plugin needs to access this method. + * + * @param store + * @param style + */ + public static void putAnnotatationStyleToStore(IPreferenceStore store, AnnotationStyle style) { + + Color color = new Color(style.getColor().getRed(), style.getColor().getGreen(), + style.getColor().getBlue()); + + Map<String, String> styleProperties = new HashMap<String, String>(); + + styleProperties.put("color", Integer.toString(color.getRGB())); + styleProperties.put("strategy", style.getStyle().toString()); + styleProperties.put("layer", Integer.toString(style.getLayer())); + + if (style.getConfiguration() != null) { + styleProperties.put("config", style.getConfiguration()); + } + + store.putValue(style.getAnnotation() + ".style", serializeProperties(styleProperties)); + } + + /** + * Retrieves an annotation style from the provided preference store. + * <p> + * Note: This method must not be called by user code! It is only public because the migration + * code in the Cas Editor Ide Plugin needs to access this method. + * + * @param store + * @param typeName + * @return + */ + public static AnnotationStyle getAnnotationStyleFromStore(IPreferenceStore store, String typeName) { + + Map<String, String> styleProperties = parseProperties(store.getString(typeName + ".style")); + + AnnotationStyle.Style style = AnnotationStyle.Style.UNDERLINE; + + String styleString = styleProperties.get("strategy"); + if (styleString != null && styleString.length() != 0) { + // TODO: Might throw exception, catch it and use default! + try { + style = AnnotationStyle.Style.valueOf(styleString); + } + catch (IllegalArgumentException e) { + } + } + + Color color = Color.RED; + + String colorString = styleProperties.get("color"); + if (colorString != null && colorString.length() != 0) { + try { + int colorInteger = Integer.parseInt(colorString); + color = new Color(colorInteger); + } + catch (NumberFormatException e) { + } + } + + int layer = 0; + + String layerString = styleProperties.get("layer"); + + if (layerString != null && layerString.length() != 0) { + try { + layer = Integer.parseInt(layerString); + } + catch (NumberFormatException e) { + } + } + + String configuration = styleProperties.get("config"); + + if (configuration != null && configuration.length() != 0) + configuration = null; + + return new AnnotationStyle(typeName, style, color, layer, configuration); + } } Modified: uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/CasDocumentProvider.java URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/CasDocumentProvider.java?rev=1180872&r1=1180871&r2=1180872&view=diff ============================================================================== --- uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/CasDocumentProvider.java (original) +++ uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/CasDocumentProvider.java Mon Oct 10 10:59:39 2011 @@ -19,7 +19,6 @@ package org.apache.uima.caseditor.editor; -import java.awt.Color; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -89,45 +88,17 @@ public abstract class CasDocumentProvide return elementErrorStatus.get(element); } + // Problem: Keys maybe should be pre-fixed depending on the plugin which is storing values public abstract IPreferenceStore getTypeSystemPreferenceStore(Object element); - /** - * Retrieves an <code>AnnotationStyle</code> from the underlying storage. - * - * Note: Internal usage only! - * - * @param element - * @param type - * @return - */ - public AnnotationStyle getAnnotationStyle(Object element, Type type) { - - if (type == null) - throw new IllegalArgumentException("type parameter must not be null!"); - - IPreferenceStore prefStore = getTypeSystemPreferenceStore(element); - - return getAnnotationStyleFromStore(prefStore, type.getName()); - } - - /** - * Sets an annotation style. - * - * Note: Internal usage only! - * - * @param element - * @param style - */ - // TODO: Disk must be accessed for every changed annotation style - // add a second method which can take all changed styles - public void setAnnotationStyle(Object element, AnnotationStyle style) { - IPreferenceStore prefStore = getTypeSystemPreferenceStore(element); - putAnnotatationStyleToStore(prefStore, style); - } + // Might fail silently, only log an error + public abstract void saveTypeSystemPreferenceStore(Object element); - // TODO: We also need a set method here - protected Collection<String> getShownTypes(Object element) { + // TODO: We also need a set method here + // TODO: This is somehow duplicated, once in the editor annotation status + // and once in the + Collection<String> getShownTypes(Object element) { PreferenceStore prefStore = (PreferenceStore) getTypeSystemPreferenceStore(element); Set<String> shownTypes = new HashSet<String>(); @@ -142,16 +113,23 @@ public abstract class CasDocumentProvide return shownTypes; } - protected void addShownType(Object element, Type type) { + // TODO: Move to Annotation Editor + void addShownType(Object element, Type type) { IPreferenceStore prefStore = getTypeSystemPreferenceStore(element); prefStore.setValue(type.getName() + ".isShown", Boolean.TRUE.toString()); + + saveTypeSystemPreferenceStore(element); } - protected void removeShownType(Object element, Type type) { + // TODO: Move to Annotation Editor + void removeShownType(Object element, Type type) { IPreferenceStore prefStore = getTypeSystemPreferenceStore(element); prefStore.setValue(type.getName() + ".isShown", Boolean.FALSE.toString()); + + saveTypeSystemPreferenceStore(element); } + // TODO: Redesign the editor annotation status ... maybe this could be a session and ts scoped pref store? protected abstract EditorAnnotationStatus getEditorAnnotationStatus(Object element); protected abstract void setEditorAnnotationStatus(Object element, @@ -165,76 +143,19 @@ public abstract class CasDocumentProvide // per project ??? // Is there one doc provider instance per editor, or one for many editors ?! - protected static void putAnnotatationStyleToStore(IPreferenceStore store, AnnotationStyle style) { - - Color color = new Color(style.getColor().getRed(), style.getColor().getGreen(), - style.getColor().getBlue()); - - // TODO: Define appendixes in constants ... - store.putValue(style.getAnnotation() + ".style.color", Integer.toString(color.getRGB())); - store.putValue(style.getAnnotation() + ".style.strategy", style.getStyle().toString()); - store.putValue(style.getAnnotation() + ".style.layer", Integer.toString(style.getLayer())); - - if (style.getConfiguration() != null) - store.putValue(style.getAnnotation() + ".style.config", style.getConfiguration()); - } - - // method to get annotation style from pref store - private AnnotationStyle getAnnotationStyleFromStore(IPreferenceStore store, String typeName) { - - AnnotationStyle.Style style = AnnotationStyle.Style.UNDERLINE; - - String styleString = store.getString(typeName + ".style.strategy"); - if (styleString.length() != 0) { - // TODO: Might throw exception, catch it and use default! - try { - style = AnnotationStyle.Style.valueOf(styleString); - } - catch (IllegalArgumentException e) { - } - } - - Color color = Color.RED; - - String colorString = store.getString(typeName + ".style.color"); - if (colorString.length() != 0) { - try { - int colorInteger = Integer.parseInt(colorString); - color = new Color(colorInteger); - } - catch (NumberFormatException e) { - } - } - - int layer = 0; - - String layerString = store.getString(typeName + ".style.layer"); - - if (layerString.length() != 0) { - try { - layer = Integer.parseInt(layerString); - } - catch (NumberFormatException e) { - } - } - - String configuration = store.getString(typeName + ".style.config"); - - if (configuration.length() != 0) - configuration = null; - - return new AnnotationStyle(typeName, style, color, layer, configuration); - } - - + // TODO: Replace this with preference listener + // TODO: Write a Annotation Style Listener which outputs the current events, + // based on preference store changes ... non-relevant chanegs need to be filtered! public void addAnnotationStyleListener(Object element, IAnnotationStyleListener listener) { annotationStyleListeners.add(listener); } +//TODO: Replace this with preference listener public void removeAnnotationStyleListener(Object element, IAnnotationStyleListener listener) { annotationStyleListeners.remove(listener); } +//TODO: Replace this with preference listener public void fireAnnotationStyleChanged(Object element, Collection<AnnotationStyle> styles) { for (IAnnotationStyleListener listener : annotationStyleListeners) { listener.annotationStylesChanged(styles); Modified: uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/styleview/AnnotationStyleViewPage.java URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/styleview/AnnotationStyleViewPage.java?rev=1180872&r1=1180871&r2=1180872&view=diff ============================================================================== --- uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/styleview/AnnotationStyleViewPage.java (original) +++ uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/styleview/AnnotationStyleViewPage.java Mon Oct 10 10:59:39 2011 @@ -154,7 +154,7 @@ class AnnotationStyleViewPage extends Pa Type type = typeNode.getAnnotationType(); // TODO: Get this information trough the editor ... its easier - AnnotationStyle style = editor.getCasDocumentProvider().getAnnotationStyle(editor.getEditorInput(), type); + AnnotationStyle style = editor.getAnnotationStyle(type); if (TYPE_NAME_COLUMN == columnIndex) { return type.getShortName().trim(); @@ -176,8 +176,8 @@ class AnnotationStyleViewPage extends Pa AnnotationTypeNode typeNode = (AnnotationTypeNode) element; Type type = typeNode.getAnnotationType(); - // TODO: Get this information trough the editor ... its easier - AnnotationStyle style = editor.getCasDocumentProvider().getAnnotationStyle(editor.getEditorInput(), type); + + AnnotationStyle style = editor.getAnnotationStyle(type); return new Color(Display.getCurrent(), style.getColor().getRed(), style.getColor().getGreen(), style.getColor().getBlue()); Modified: uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/ui/property/EditorAnnotationPropertyPage.java URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/ui/property/EditorAnnotationPropertyPage.java?rev=1180872&r1=1180871&r2=1180872&view=diff ============================================================================== --- uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/ui/property/EditorAnnotationPropertyPage.java (original) +++ uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/ui/property/EditorAnnotationPropertyPage.java Mon Oct 10 10:59:39 2011 @@ -37,9 +37,7 @@ public class EditorAnnotationPropertyPag @Override protected AnnotationStyle getAnnotationStyle(Type type) { - // TODO: Use the editor directly ... - return getEditor().getCasDocumentProvider().getAnnotationStyle( - getEditor().getEditorInput(), type); + return getEditor().getAnnotationStyle(type); } @Override @@ -53,13 +51,16 @@ public class EditorAnnotationPropertyPag // TODO: Add method to change all styles at once, instead of writing // the dotCorpus file for changed style for (AnnotationStyle style : changedStyles) { - getEditor().getCasDocumentProvider().setAnnotationStyle( - getEditor().getEditorInput(), style); + getEditor().setAnnotationStyle(style); } + // TODO: This should not be necessary anymore! Synchronization should be done via preference store! getEditor().getCasDocumentProvider().fireAnnotationStyleChanged(getEditor().getEditorInput(), changedStyles); + // TODO: Trigger saving of preference store! + // getEditor().getCasDocumentProvider().saveTypeSystemPreferenceStore(getEditor().getEditorInput()); + return true; } }