use of de.tudarmstadt.ukp.clarin.webanno.api.ProjectService 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.ProjectService in project webanno by webanno.
the class AnnotationPage method commonInit.
private void commonInit() {
setModel(Model.of(new AnnotatorStateImpl(Mode.ANNOTATION)));
// Ensure that a user is set
getModelObject().setUser(userRepository.getCurrentUser());
add(createUrlFragmentBehavior());
add(annotationEditor = createAnnotationEditor());
add(createRightSidebar());
add(createLeftSidebar());
add(createDocumentInfoLabel());
add(getOrCreatePositionInfoLabel());
add(openDocumentsModal = new OpenDocumentDialog("openDocumentsModal", getModel(), getAllowedProjects()) {
private static final long serialVersionUID = 5474030848589262638L;
@Override
public void onDocumentSelected(AjaxRequestTarget aTarget) {
actionLoadDocument(aTarget);
}
});
add(preferencesModal = new AnnotationPreferencesDialog("preferencesDialog", getModel()));
preferencesModal.setOnChangeAction(this::actionCompletePreferencesChange);
add(exportDialog = new ExportDocumentDialog("exportDialog", getModel()));
add(guidelinesDialog = new GuidelinesDialog("guidelinesDialog", getModel()));
Form<Void> gotoPageTextFieldForm = new Form<>("gotoPageTextFieldForm");
gotoPageTextField = new NumberTextField<>("gotoPageText", Model.of(1), Integer.class);
// FIXME minimum and maximum should be obtained from the annotator state
gotoPageTextField.setMinimum(1);
gotoPageTextField.setOutputMarkupId(true);
gotoPageTextFieldForm.add(gotoPageTextField);
LambdaAjaxSubmitLink gotoPageLink = new LambdaAjaxSubmitLink("gotoPageLink", gotoPageTextFieldForm, this::actionGotoPage);
gotoPageTextFieldForm.setDefaultButton(gotoPageLink);
gotoPageTextFieldForm.add(gotoPageLink);
add(gotoPageTextFieldForm);
add(new LambdaAjaxLink("initialLoadComplete", this::actionInitialLoadComplete));
add(new LambdaAjaxLink("showOpenDocumentDialog", this::actionShowOpenDocumentDialog));
add(new ActionBarLink("showPreferencesDialog", this::actionShowPreferencesDialog));
add(new ActionBarLink("showGuidelinesDialog", guidelinesDialog::show));
add(new ActionBarLink("showExportDialog", exportDialog::show).onConfigure(_this -> {
AnnotatorState state = AnnotationPage.this.getModelObject();
_this.setVisible(state.getProject() != null && (isAdmin(state.getProject(), projectService, state.getUser()) || !state.getProject().isDisableExport()));
}));
add(new ActionBarLink("showPreviousDocument", t -> actionShowPreviousDocument(t)).add(new InputBehavior(new KeyType[] { KeyType.Shift, KeyType.Page_up }, EventType.click)));
add(new ActionBarLink("showNextDocument", t -> actionShowNextDocument(t)).add(new InputBehavior(new KeyType[] { KeyType.Shift, KeyType.Page_down }, EventType.click)));
add(new ActionBarLink("showNext", t -> actionShowNextPage(t)).add(new InputBehavior(new KeyType[] { KeyType.Page_down }, EventType.click)));
add(new ActionBarLink("showPrevious", t -> actionShowPreviousPage(t)).add(new InputBehavior(new KeyType[] { KeyType.Page_up }, EventType.click)));
add(new ActionBarLink("showFirst", t -> actionShowFirstPage(t)).add(new InputBehavior(new KeyType[] { KeyType.Home }, EventType.click)));
add(new ActionBarLink("showLast", t -> actionShowLastPage(t)).add(new InputBehavior(new KeyType[] { KeyType.End }, EventType.click)));
add(new ActionBarLink("toggleScriptDirection", this::actionToggleScriptDirection));
add(createOrGetResetDocumentDialog());
add(createOrGetResetDocumentLink());
add(finishDocumentDialog = new ConfirmationDialog("finishDocumentDialog", new StringResourceModel("FinishDocumentDialog.title", this, null), new StringResourceModel("FinishDocumentDialog.text", this, null)));
add(finishDocumentLink = new LambdaAjaxLink("showFinishDocumentDialog", this::actionFinishDocument) {
private static final long serialVersionUID = 874573384012299998L;
@Override
protected void onConfigure() {
super.onConfigure();
AnnotatorState state = AnnotationPage.this.getModelObject();
setEnabled(state.getDocument() != null && !documentService.isAnnotationFinished(state.getDocument(), state.getUser()));
}
});
finishDocumentIcon = new FinishImage("finishImage", getModel());
finishDocumentIcon.setOutputMarkupId(true);
finishDocumentLink.add(finishDocumentIcon);
}
Aggregations