use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState 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);
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.
the class DocumentNamePanel method getLabel.
private String getLabel() {
StringBuilder sb = new StringBuilder();
AnnotatorState state = getModelObject();
if (state.getProject() != null) {
sb.append(state.getProject().getName());
}
sb.append("/");
if (state.getDocument() != null) {
sb.append(state.getDocument().getName());
}
if (RuntimeConfigurationType.DEVELOPMENT.equals(getApplication().getConfigurationType())) {
sb.append(" (");
if (state.getProject() != null) {
sb.append(state.getProject().getId());
}
sb.append("/");
if (state.getDocument() != null) {
sb.append(state.getDocument().getId());
}
sb.append(")");
}
return sb.toString();
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.
the class AnnotationDetailEditorPanel method loadFeatureEditorModels.
public void loadFeatureEditorModels(JCas aJCas, AjaxRequestTarget aTarget) throws AnnotationException {
LOG.trace("loadFeatureEditorModels()");
AnnotatorState state = getModelObject();
Selection selection = state.getSelection();
List<FeatureState> featureStates = state.getFeatureStates();
for (FeatureState featureState : featureStates) {
if (StringUtils.isNotBlank(featureState.feature.getLinkTypeName())) {
featureState.value = new ArrayList<>();
}
}
try {
if (selection.isSpan()) {
annotationFeatureForm.updateLayersDropdown();
}
if (selection.getAnnotation().isSet()) {
// If an existing annotation was selected, take the feature editor model values from
// there
AnnotationFS annoFs = selectByAddr(aJCas, state.getSelection().getAnnotation().getId());
// Try obtaining the layer from the feature structure
AnnotationLayer layer;
try {
layer = annotationService.getLayer(state.getProject(), annoFs);
state.setSelectedAnnotationLayer(layer);
LOG.trace(String.format("loadFeatureEditorModels() selectedLayer set from selection: %s", state.getSelectedAnnotationLayer().getUiName()));
} catch (NoResultException e) {
clearFeatureEditorModels(aTarget);
throw new IllegalStateException("Unknown layer [" + annoFs.getType().getName() + "]", e);
}
// selected span annotation
if (!selection.isArc() && !state.getPreferences().isRememberLayer()) {
state.setSelectedAnnotationLayer(layer);
}
loadFeatureEditorModelsCommon(aTarget, aJCas, layer, annoFs, null);
} else {
if (selection.isArc()) {
// Avoid creation of arcs on locked layers
if (state.getSelectedAnnotationLayer() != null && state.getSelectedAnnotationLayer().isReadonly()) {
state.setSelectedAnnotationLayer(new AnnotationLayer());
} else {
loadFeatureEditorModelsCommon(aTarget, aJCas, state.getSelectedAnnotationLayer(), null, state.getRememberedArcFeatures());
}
} else {
loadFeatureEditorModelsCommon(aTarget, aJCas, state.getSelectedAnnotationLayer(), null, state.getRememberedSpanFeatures());
}
}
annotationFeatureForm.updateRememberLayer();
if (aTarget != null) {
aTarget.add(annotationFeatureForm);
}
} catch (Exception e) {
throw new AnnotationException(e);
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.
the class AnnotationDetailEditorPanel method setSlot.
@SuppressWarnings("unchecked")
private void setSlot(AjaxRequestTarget aTarget, JCas aJCas, int aAnnotationId) {
AnnotatorState state = getModelObject();
// Set an armed slot
if (!state.getSelection().isArc() && state.isSlotArmed()) {
List<LinkWithRoleModel> links = (List<LinkWithRoleModel>) state.getFeatureState(state.getArmedFeature()).value;
LinkWithRoleModel link = links.get(state.getArmedSlot());
link.targetAddr = aAnnotationId;
link.label = selectByAddr(aJCas, aAnnotationId).getCoveredText();
}
// Auto-commit if working on existing annotation
if (state.getSelection().getAnnotation().isSet()) {
try {
actionCreateOrUpdate(aTarget, aJCas);
} catch (Exception e) {
handleException(this, aTarget, e);
}
}
state.clearArmedSlot();
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.
the class AnnotationDetailEditorPanel method createNewAnnotation.
private void createNewAnnotation(AjaxRequestTarget aTarget, TypeAdapter aAdapter, JCas aJCas) throws AnnotationException, IOException {
AnnotatorState state = getModelObject();
if (state.getSelection().isArc()) {
if (aAdapter instanceof SpanAdapter) {
error("Layer [" + aAdapter.getLayer().getUiName() + "] does not support arc annotation.");
aTarget.addChildren(getPage(), IFeedback.class);
} else if (aAdapter instanceof ArcAdapter) {
createNewRelationAnnotation((ArcAdapter) aAdapter, aJCas);
} else if (aAdapter instanceof ChainAdapter) {
createNewChainLinkAnnotation((ChainAdapter) aAdapter, aJCas);
} else {
throw new IllegalStateException("I don't know how to use [" + aAdapter.getClass().getSimpleName() + "] in this situation.");
}
} else {
if (aAdapter instanceof SpanAdapter) {
createNewSpanAnnotation(aTarget, (SpanAdapter) aAdapter, aJCas);
} else if (aAdapter instanceof ChainAdapter) {
createNewChainElement((ChainAdapter) aAdapter, aJCas);
} else {
throw new IllegalStateException("I don't know how to use [" + aAdapter.getClass().getSimpleName() + "] in this situation.");
}
}
}
Aggregations