From 5e1e5e49cdb33b29f6524570a9f39e9921762074 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Tue, 19 Dec 2023 19:24:01 +0100 Subject: [PATCH] #2696 - Document-level recommendations - Clean up a bit reducing complexity --- ...DocumentMetadataAnnotationDetailPanel.java | 19 +++---- ...umentMetadataAnnotationSelectionPanel.java | 56 +++++++------------ .../sidebar/DocumentMetadataSidebar.java | 8 +-- 3 files changed, 31 insertions(+), 52 deletions(-) diff --git a/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataAnnotationDetailPanel.java b/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataAnnotationDetailPanel.java index 3a698656b30..c4ad20ca5b4 100644 --- a/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataAnnotationDetailPanel.java +++ b/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataAnnotationDetailPanel.java @@ -92,22 +92,21 @@ public class DocumentMetadataAnnotationDetailPanel private final IModel user; private final ListView featureList; private final AnnotationActionHandler actionHandler; - private final AnnotatorState state; + private final IModel state; public DocumentMetadataAnnotationDetailPanel(String aId, IModel aModel, - IModel aDocument, IModel aUser, CasProvider aCasProvider, - IModel aProject, AnnotationPage aAnnotationPage, - AnnotationActionHandler aActionHandler, AnnotatorState aState) + CasProvider aCasProvider, AnnotationPage aAnnotationPage, + AnnotationActionHandler aActionHandler, IModel aState) { super(aId, aModel); setOutputMarkupPlaceholderTag(true); - sourceDocument = aDocument; - user = aUser; + sourceDocument = aState.map(AnnotatorState::getDocument); + user = aState.map(AnnotatorState::getUser); annotationPage = aAnnotationPage; jcasProvider = aCasProvider; - project = aProject; + project = aState.map(AnnotatorState::getProject); actionHandler = aActionHandler; state = aState; @@ -341,7 +340,7 @@ private static final class IsSidebarAction public void toggleVisibility() { - state.clearArmedSlot(); + state.getObject().clearArmedSlot(); setVisible(!isVisible()); } @@ -353,8 +352,8 @@ public void onLinkFeatureDeletedEvent(LinkFeatureDeletedEvent aEvent) CAS cas = jcasProvider.get(); AnnotationFS fs = ICasUtil.selectAnnotationByAddr(cas, aEvent.getLinkWithRoleModel().targetAddr); - state.getSelection().selectSpan(fs); - if (state.getSelection().getAnnotation().isSet()) { + state.getObject().getSelection().selectSpan(fs); + if (state.getObject().getSelection().getAnnotation().isSet()) { actionHandler.actionDelete(target); findParent(AnnotationPageBase.class).actionRefreshDocument(aEvent.getTarget()); diff --git a/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataAnnotationSelectionPanel.java b/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataAnnotationSelectionPanel.java index d6096e279cb..35efe2b6bdd 100644 --- a/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataAnnotationSelectionPanel.java +++ b/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataAnnotationSelectionPanel.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.io.Serializable; +import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -42,7 +43,7 @@ import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.list.ListItem; import org.apache.wicket.markup.html.list.ListView; -import org.apache.wicket.markup.html.panel.Panel; +import org.apache.wicket.markup.html.panel.GenericPanel; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.LoadableDetachableModel; @@ -52,7 +53,6 @@ import org.slf4j.LoggerFactory; import org.wicketstuff.event.annotation.OnEvent; -import de.tudarmstadt.ukp.clarin.webanno.api.annotation.page.AnnotationPageBase; import de.tudarmstadt.ukp.clarin.webanno.api.casstorage.CasProvider; import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature; import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer; @@ -81,12 +81,11 @@ import de.tudarmstadt.ukp.inception.ui.core.docanno.layer.DocumentMetadataLayerSupport; public class DocumentMetadataAnnotationSelectionPanel - extends Panel + extends GenericPanel { private static final long serialVersionUID = 8318858582025740458L; - private static final Logger LOG = LoggerFactory - .getLogger(DocumentMetadataAnnotationSelectionPanel.class); + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private static final String CID_LABEL = "label"; private static final String CID_TYPE = "type"; @@ -104,7 +103,6 @@ public class DocumentMetadataAnnotationSelectionPanel private final AnnotationPage annotationPage; private final CasProvider jcasProvider; - private final IModel project; private final IModel sourceDocument; private final IModel user; private final IModel selectedLayer; @@ -114,26 +112,26 @@ public class DocumentMetadataAnnotationSelectionPanel private WebMarkupContainer selectedAnnotation; private DocumentMetadataAnnotationDetailPanel selectedDetailPanel; private int createdAnnotationAddress; - private final AnnotatorState state; + private final IModel state; - public DocumentMetadataAnnotationSelectionPanel(String aId, IModel aProject, - IModel aDocument, IModel aUser, CasProvider aCasProvider, + public DocumentMetadataAnnotationSelectionPanel(String aId, CasProvider aCasProvider, AnnotationPage aAnnotationPage, AnnotationActionHandler aActionHandler, - AnnotatorState aState) + IModel aState) { - super(aId, aProject); + super(aId, aState.map(AnnotatorState::getProject)); setOutputMarkupPlaceholderTag(true); + state = aState; + + sourceDocument = aState.map(AnnotatorState::getDocument); + user = aState.map(AnnotatorState::getUser); + annotationPage = aAnnotationPage; - sourceDocument = aDocument; - user = aUser; jcasProvider = aCasProvider; - project = aProject; selectedLayer = Model.of(listCreatableMetadataLayers().stream().findFirst().orElse(null)); var availableLayers = LoadableDetachableModel.of(this::listCreatableMetadataLayers); actionHandler = aActionHandler; - state = aState; annotations = LoadableDetachableModel.of(this::listAnnotations); var content = new WebMarkupContainer("content"); @@ -168,11 +166,6 @@ public DocumentMetadataAnnotationSelectionPanel(String aId, IModel aPro } - public Project getModelObject() - { - return (Project) getDefaultModelObject(); - } - private void actionCreate(AjaxRequestTarget aTarget) throws AnnotationException, IOException { try { @@ -188,7 +181,7 @@ private void actionCreate(AjaxRequestTarget aTarget) throws AnnotationException, aTarget.add(annotationsContainer); - findParent(AnnotationPageBase.class).actionRefreshDocument(aTarget); + annotationPage.actionRefreshDocument(aTarget); } catch (Exception e) { handleException(this, aTarget, e); @@ -202,9 +195,10 @@ private void actionDelete(AjaxRequestTarget aTarget, annotationPage.ensureIsEditable(); // Load the boiler-plate + var project = getModelObject(); var cas = jcasProvider.get(); var fs = ICasUtil.selectFsByAddr(cas, aDetailPanel.getModelObject().getId()); - var layer = annotationService.findLayer(project.getObject(), fs); + var layer = annotationService.findLayer(project, fs); var adapter = annotationService.getAdapter(layer); // Perform actual actions @@ -220,7 +214,7 @@ private void actionDelete(AjaxRequestTarget aTarget, } remove(aDetailPanel); - findParent(AnnotationPageBase.class).actionRefreshDocument(aTarget); + annotationPage.actionRefreshDocument(aTarget); aTarget.add(annotationsContainer); } catch (Exception e) { @@ -265,13 +259,6 @@ private ListView createAnnotationList() { private static final long serialVersionUID = -6833373063896777785L; - /** - * Determines if new annotations should be rendered visible or not. For the - * initialization of existing annotations this value should be false. Afterwards when - * manually creating new annotations it should be true to immediately open them - * afterwards. If there are no annotations at initialization it is initialized with true - * else false. - */ @Override protected void populateItem(ListItem aItem) { @@ -283,8 +270,7 @@ protected void populateItem(ListItem aItem) aItem.add(container); var detailPanel = new DocumentMetadataAnnotationDetailPanel(CID_ANNOTATION_DETAILS, - Model.of(vid), sourceDocument, user, jcasProvider, project, annotationPage, - actionHandler, state); + Model.of(vid), jcasProvider, annotationPage, actionHandler, state); aItem.add(detailPanel); container.add(new LambdaAjaxEventBehavior(CLICK, @@ -411,8 +397,7 @@ private List listAnnotations() public void onDocumentMetadataEvent(DocumentMetadataEvent aEvent) { aEvent.getRequestTarget().ifPresent(target -> target.add(annotationsContainer)); - findParent(AnnotationPageBase.class) - .actionRefreshDocument(aEvent.getRequestTarget().orElse(null)); + annotationPage.actionRefreshDocument(aEvent.getRequestTarget().orElse(null)); } @OnEvent @@ -427,8 +412,7 @@ public void onFeatureValueUpdated(FeatureValueUpdatedEvent aEvent) } }); - findParent(AnnotationPageBase.class) - .actionRefreshDocument((aEvent.getRequestTarget().orElse(null))); + annotationPage.actionRefreshDocument((aEvent.getRequestTarget().orElse(null))); } protected static void handleException(Component aComponent, AjaxRequestTarget aTarget, diff --git a/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataSidebar.java b/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataSidebar.java index 427a00d4cbb..eeeb89667a8 100644 --- a/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataSidebar.java +++ b/inception/inception-layer-docmetadata/src/main/java/de/tudarmstadt/ukp/inception/ui/core/docanno/sidebar/DocumentMetadataSidebar.java @@ -36,11 +36,7 @@ public DocumentMetadataSidebar(String aId, IModel aModel, { super(aId, aModel, aActionHandler, aCasProvider, aAnnotationPage); - var project = aModel.map(AnnotatorState::getProject); - var sourceDocument = aModel.map(AnnotatorState::getDocument); - var user = aModel.map(AnnotatorState::getUser); - - add(new DocumentMetadataAnnotationSelectionPanel("annotations", project, sourceDocument, - user, aCasProvider, aAnnotationPage, aActionHandler, getModelObject())); + add(new DocumentMetadataAnnotationSelectionPanel("annotations", aCasProvider, + aAnnotationPage, aActionHandler, aModel)); } }