Search in sources :

Example 1 with ColoringStrategyType

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType in project webanno by webanno.

the class PreferencesUtil method loadPreferences.

/**
 * Set annotation preferences of users for a given project such as window size, annotation
 * layers,... reading from the file system.
 *
 * @param aUsername
 *            The {@link User} for whom we need to read the preference (preferences are stored
 *            per user)
 * @param aRepositoryService the repository service.
 * @param aAnnotationService the annotation service.
 * @param aBModel
 *            The {@link AnnotatorState} that will be populated with preferences from the
 *            file
 * @param aMode the mode.
 * @throws BeansException hum?
 * @throws IOException hum?
 */
public static void loadPreferences(String aUsername, SettingsService aSettingsService, ProjectService aRepositoryService, AnnotationSchemaService aAnnotationService, AnnotatorState aBModel, Mode aMode) throws BeansException, IOException {
    AnnotationPreference preference = new AnnotationPreference();
    BeanWrapper wrapper = new BeanWrapperImpl(preference);
    // get annotation preference from file system
    try {
        Properties props = aRepositoryService.loadUserSettings(aUsername, aBModel.getProject());
        for (Entry<Object, Object> entry : props.entrySet()) {
            String property = entry.getKey().toString();
            int index = property.indexOf(".");
            String propertyName = property.substring(index + 1);
            String mode = property.substring(0, index);
            if (wrapper.isWritableProperty(propertyName) && mode.equals(aMode.getName())) {
                if (AnnotationPreference.class.getDeclaredField(propertyName).getGenericType() instanceof ParameterizedType) {
                    if (entry.getValue().toString().startsWith("[")) {
                        // its a list
                        List<String> value = Arrays.asList(StringUtils.replaceChars(entry.getValue().toString(), "[]", "").split(","));
                        if (!value.get(0).equals("")) {
                            wrapper.setPropertyValue(propertyName, value);
                        }
                    } else if (entry.getValue().toString().startsWith("{")) {
                        // its a map
                        String s = StringUtils.replaceChars(entry.getValue().toString(), "{}", "");
                        Map<String, String> value = Arrays.stream(s.split(",")).map(x -> x.split("=")).collect(Collectors.toMap(x -> x[0], x -> x[1]));
                        wrapper.setPropertyValue(propertyName, value);
                    }
                } else {
                    wrapper.setPropertyValue(propertyName, entry.getValue());
                }
            }
        }
        // set layers according to preferences
        List<AnnotationLayer> enabledLayers = aAnnotationService.listAnnotationLayer(aBModel.getProject()).stream().filter(// only allow enabled layers
        l -> l.isEnabled()).collect(Collectors.toList());
        List<Long> hiddenLayerIds = preference.getHiddenAnnotationLayerIds();
        enabledLayers = enabledLayers.stream().filter(l -> !hiddenLayerIds.contains(l.getId())).collect(Collectors.toList());
        aBModel.setAnnotationLayers(enabledLayers);
        // Get color preferences for each layer, init with legacy if not found
        Map<Long, ColoringStrategyType> colorPerLayer = preference.getColorPerLayer();
        for (AnnotationLayer layer : aAnnotationService.listAnnotationLayer(aBModel.getProject())) {
            if (!colorPerLayer.containsKey(layer.getId())) {
                colorPerLayer.put(layer.getId(), ColoringStrategyType.LEGACY);
            }
        }
    }// no preference found
     catch (Exception e) {
        // If no layer preferences are defined,
        // then just assume all enabled layers are preferred
        List<AnnotationLayer> enabledLayers = aAnnotationService.listAnnotationLayer(aBModel.getProject()).stream().filter(// only allow enabled layers
        l -> l.isEnabled()).collect(Collectors.toList());
        aBModel.setAnnotationLayers(enabledLayers);
        preference.setWindowSize(aSettingsService.getNumberOfSentences());
        // add default coloring strategy
        Map<Long, ColoringStrategyType> colorPerLayer = new HashMap<>();
        for (AnnotationLayer layer : aBModel.getAnnotationLayers()) {
            colorPerLayer.put(layer.getId(), ColoringStrategy.getBestInitialStrategy(aAnnotationService, layer, preference));
        }
        preference.setColorPerLayer(colorPerLayer);
    }
    aBModel.setPreferences(preference);
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) ProjectService(de.tudarmstadt.ukp.clarin.webanno.api.ProjectService) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) BeanWrapper(org.springframework.beans.BeanWrapper) IOException(java.io.IOException) HashMap(java.util.HashMap) BeansException(org.springframework.beans.BeansException) ColoringStrategy(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy) Mode(de.tudarmstadt.ukp.clarin.webanno.model.Mode) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) SettingsService(de.tudarmstadt.ukp.clarin.webanno.api.SettingsService) AnnotationSchemaService(de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) AnnotationPreference(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotationPreference) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) Map(java.util.Map) Entry(java.util.Map.Entry) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) ColoringStrategyType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Properties(java.util.Properties) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) ParameterizedType(java.lang.reflect.ParameterizedType) BeanWrapper(org.springframework.beans.BeanWrapper) List(java.util.List) AnnotationPreference(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotationPreference) HashMap(java.util.HashMap) Map(java.util.Map) ColoringStrategyType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType)

Example 2 with ColoringStrategyType

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType in project webanno by webanno.

the class AnnotationPreferencesDialogContent method createLayerContainer.

private ListView<AnnotationLayer> createLayerContainer() {
    return new ListView<AnnotationLayer>("annotationLayers") {

        private static final long serialVersionUID = -4040731191748923013L;

        @Override
        protected void populateItem(ListItem<AnnotationLayer> item) {
            // add checkbox
            // get initial state
            AnnotationPreference pref = stateModel.getObject().getPreferences();
            List<Long> hiddenLayerIds = pref.getHiddenAnnotationLayerIds();
            boolean isPreferredToShow = !hiddenLayerIds.contains(item.getModelObject().getId());
            CheckBox layer_cb = new CheckBox("annotationLayerActive", Model.of(isPreferredToShow));
            layer_cb.add(new AjaxEventBehavior("change") {

                private static final long serialVersionUID = 8378489004897115519L;

                @Override
                protected void onEvent(AjaxRequestTarget target) {
                    // check state & live update preferences
                    List<Long> hiddenLayerIds = stateModel.getObject().getPreferences().getHiddenAnnotationLayerIds();
                    // get and switch state of checkbox
                    boolean isPreferredToShow = layer_cb.getModelObject();
                    layer_cb.setModelObject(!isPreferredToShow);
                    // live update preferences
                    if (isPreferredToShow) {
                        // prefer to deactivate layer
                        hiddenLayerIds.add(item.getModelObject().getId());
                    } else {
                        // prefer to activate layer
                        hiddenLayerIds.remove(item.getModelObject().getId());
                    }
                }
            });
            item.add(layer_cb);
            // add coloring strategy combobox
            ChoiceRenderer<ColoringStrategyType> choiceRenderer = new ChoiceRenderer<>("descriptiveName");
            List<ColoringStrategyType> choices = new ArrayList<ColoringStrategyType>(EnumSet.allOf(ColoringStrategyType.class));
            Model<ColoringStrategyType> initialSelected = Model.of(form.getModelObject().colorPerLayer.get(item.getModelObject().getId()));
            DropDownChoice<ColoringStrategyType> layer_color = new DropDownChoice<ColoringStrategyType>("layercoloring", initialSelected, choices, choiceRenderer);
            layer_color.add(new AjaxFormComponentUpdatingBehavior("change") {

                private static final long serialVersionUID = 1060397773470276585L;

                @Override
                protected void onUpdate(AjaxRequestTarget target) {
                    AnnotationLayer current_layer = item.getModelObject();
                    ColoringStrategyType selectedColor = layer_color.getModelObject();
                    form.getModelObject().colorPerLayer.put(current_layer.getId(), selectedColor);
                }
            });
            item.add(layer_color);
            // add label
            Label lbl = new Label("annotationLayerDesc", item.getModelObject().getUiName());
            item.add(lbl);
        }
    };
}
Also used : AjaxFormComponentUpdatingBehavior(org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior) AjaxEventBehavior(org.apache.wicket.ajax.AjaxEventBehavior) ArrayList(java.util.ArrayList) Label(org.apache.wicket.markup.html.basic.Label) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) ListView(org.apache.wicket.markup.html.list.ListView) DropDownChoice(org.apache.wicket.markup.html.form.DropDownChoice) CheckBox(org.apache.wicket.markup.html.form.CheckBox) ChoiceRenderer(org.apache.wicket.markup.html.form.ChoiceRenderer) ArrayList(java.util.ArrayList) List(java.util.List) ListItem(org.apache.wicket.markup.html.list.ListItem) AnnotationPreference(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotationPreference) ColoringStrategyType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType)

Aggregations

ColoringStrategyType (de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType)2 AnnotationPreference (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotationPreference)2 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)2 List (java.util.List)2 AnnotationSchemaService (de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService)1 ProjectService (de.tudarmstadt.ukp.clarin.webanno.api.ProjectService)1 SettingsService (de.tudarmstadt.ukp.clarin.webanno.api.SettingsService)1 ColoringStrategy (de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy)1 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)1 Mode (de.tudarmstadt.ukp.clarin.webanno.model.Mode)1 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)1 IOException (java.io.IOException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1 Collectors (java.util.stream.Collectors)1