From eb5b63a12df2ca3361b68c0de6b1900d178f8d57 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Fri, 9 Jun 2023 15:21:27 +0200 Subject: [PATCH] #4060 - Clean up redundant code in annotation handlers - Remove redundant code - Clean up code --- .../actions/CreateSpanAnnotationHandler.java | 47 +++++-------------- ...FillSlotWithExistingAnnotationHandler.java | 18 ++++--- .../FillSlotWithNewAnnotationHandler.java | 26 ++++------ .../actions/MoveSpanAnnotationHandler.java | 4 +- .../diam/editor/actions/ScrollToHandler.java | 4 +- .../CreateSpanAnnotationHandlerTest.java | 4 +- 6 files changed, 35 insertions(+), 68 deletions(-) diff --git a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/CreateSpanAnnotationHandler.java b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/CreateSpanAnnotationHandler.java index b47f653709e..16bc5417a56 100644 --- a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/CreateSpanAnnotationHandler.java +++ b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/CreateSpanAnnotationHandler.java @@ -27,16 +27,12 @@ import org.apache.wicket.request.Request; import org.springframework.core.annotation.Order; -import de.tudarmstadt.ukp.clarin.webanno.api.annotation.page.AnnotationPageBase; import de.tudarmstadt.ukp.clarin.webanno.support.JSONUtil; import de.tudarmstadt.ukp.inception.diam.editor.config.DiamAutoConfig; import de.tudarmstadt.ukp.inception.diam.model.ajax.DefaultAjaxResponse; import de.tudarmstadt.ukp.inception.diam.model.compact.CompactRangeList; import de.tudarmstadt.ukp.inception.rendering.editorstate.AnnotatorState; import de.tudarmstadt.ukp.inception.rendering.model.Range; -import de.tudarmstadt.ukp.inception.rendering.selection.Selection; -import de.tudarmstadt.ukp.inception.rendering.vmodel.VID; -import de.tudarmstadt.ukp.inception.schema.adapter.AnnotationException; /** *

@@ -60,9 +56,14 @@ public String getCommand() public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest) { try { - AnnotationPageBase page = getPage(); - CAS cas = page.getEditorCas(); - actionSpan(aTarget, aRequest.getRequestParameters(), cas); + var page = getPage(); + var cas = page.getEditorCas(); + var state = getAnnotatorState(); + var range = getRangeFromRequest(state, aRequest.getRequestParameters(), cas); + + state.getSelection().selectSpan(cas, range.getBegin(), range.getEnd()); + page.getAnnotationActionHandler().actionCreateOrUpdate(aTarget, cas); + return new DefaultAjaxResponse(getAction(aRequest)); } catch (Exception e) { @@ -70,43 +71,17 @@ public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest) } } - private void actionSpan(AjaxRequestTarget aTarget, IRequestParameters aRequestParameters, - CAS aCas) - throws IOException, AnnotationException - { - AnnotationPageBase page = (AnnotationPageBase) aTarget.getPage(); - AnnotatorState state = getAnnotatorState(); - - // This is the span the user has marked in the browser in order to create a new slot-filler - // annotation OR the span of an existing annotation which the user has selected. - var userSelectedSpan = getOffsetsFromRequest(state, aRequestParameters, aCas); - - if (state.isSlotArmed()) { - // When filling a slot, the current selection is *NOT* changed. The - // Span annotation which owns the slot that is being filled remains - // selected! - page.getAnnotationActionHandler().actionFillSlot(aTarget, aCas, - userSelectedSpan.getBegin(), userSelectedSpan.getEnd(), VID.NONE_ID); - return; - } - - Selection selection = state.getSelection(); - selection.selectSpan(aCas, userSelectedSpan.getBegin(), userSelectedSpan.getEnd()); - page.getAnnotationActionHandler().actionCreateOrUpdate(aTarget, aCas); - } - /** * Extract offset information from the current request. These are either offsets of an existing * selected annotations or offsets contained in the request for the creation of a new * annotation. */ - static Range getOffsetsFromRequest(AnnotatorState aState, IRequestParameters request, CAS aCas) + static Range getRangeFromRequest(AnnotatorState aState, IRequestParameters request, CAS aCas) throws IOException { - String offsets = request.getParameterValue(PARAM_OFFSETS).toString(); + var offsets = request.getParameterValue(PARAM_OFFSETS).toString(); - CompactRangeList offsetLists = JSONUtil.getObjectMapper().readValue(offsets, - CompactRangeList.class); + var offsetLists = JSONUtil.getObjectMapper().readValue(offsets, CompactRangeList.class); int annotationBegin = aState.getWindowBeginOffset() + offsetLists.get(0).getBegin(); int annotationEnd = aState.getWindowBeginOffset() diff --git a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/FillSlotWithExistingAnnotationHandler.java b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/FillSlotWithExistingAnnotationHandler.java index 1c9f9af380e..ff7aed02ee6 100644 --- a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/FillSlotWithExistingAnnotationHandler.java +++ b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/FillSlotWithExistingAnnotationHandler.java @@ -17,12 +17,10 @@ */ package de.tudarmstadt.ukp.inception.diam.editor.actions; -import org.apache.uima.cas.CAS; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.request.Request; import org.springframework.core.annotation.Order; -import de.tudarmstadt.ukp.clarin.webanno.api.annotation.page.AnnotationPageBase; import de.tudarmstadt.ukp.inception.diam.editor.config.DiamAutoConfig; import de.tudarmstadt.ukp.inception.diam.model.ajax.DefaultAjaxResponse; @@ -48,13 +46,12 @@ public String getCommand() public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest) { try { - AnnotationPageBase page = getPage(); - CAS cas = page.getEditorCas(); - - // When filling a slot, the current selection is *NOT* changed. The - // Span annotation which owns the slot that is being filled remains - // selected! - page.getAnnotationActionHandler().actionFillSlot(aTarget, cas, 0, 0, getVid(aRequest)); + var page = getPage(); + var cas = page.getEditorCas(); + var slotFillerId = getVid(aRequest); + // When filling a slot, the current selection is *NOT* changed. The Span annotation + // which owns the slot that is being filled remains selected! + page.getAnnotationActionHandler().actionFillSlot(aTarget, cas, slotFillerId); return new DefaultAjaxResponse(getAction(aRequest)); } @@ -66,6 +63,7 @@ public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest) @Override public boolean accepts(Request aRequest) { - return super.accepts(aRequest) && getAnnotatorState().isSlotArmed(); + return super.accepts(aRequest) && getAnnotatorState().isSlotArmed() + && getVid(aRequest).isSet(); } } diff --git a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/FillSlotWithNewAnnotationHandler.java b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/FillSlotWithNewAnnotationHandler.java index aefe6d378f5..89164f24f02 100644 --- a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/FillSlotWithNewAnnotationHandler.java +++ b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/FillSlotWithNewAnnotationHandler.java @@ -31,8 +31,6 @@ import de.tudarmstadt.ukp.inception.diam.model.ajax.DefaultAjaxResponse; import de.tudarmstadt.ukp.inception.diam.model.compact.CompactRange; import de.tudarmstadt.ukp.inception.diam.model.compact.CompactRangeList; -import de.tudarmstadt.ukp.inception.rendering.editorstate.AnnotatorState; -import de.tudarmstadt.ukp.inception.rendering.vmodel.VID; import de.tudarmstadt.ukp.inception.schema.adapter.AnnotationException; /** @@ -77,17 +75,14 @@ private void actionSpan(AjaxRequestTarget aTarget, IRequestParameters aRequestPa CAS aCas) throws IOException, AnnotationException { - AnnotationPageBase page = (AnnotationPageBase) aTarget.getPage(); - // This is the span the user has marked in the browser in order to create a new slot-filler // annotation OR the span of an existing annotation which the user has selected. - CompactRange userSelectedSpan = getOffsetsFromRequest(aTarget, aRequestParameters, aCas); + var range = getRangeFromRequest(aTarget, aRequestParameters, aCas); - // When filling a slot, the current selection is *NOT* changed. The - // Span annotation which owns the slot that is being filled remains - // selected! - page.getAnnotationActionHandler().actionFillSlot(aTarget, aCas, userSelectedSpan.getBegin(), - userSelectedSpan.getEnd(), VID.NONE_ID); + // When filling a slot, the current selection is *NOT* changed. The Span annotation which + // owns the slot that is being filled remains selected! + getPage().getAnnotationActionHandler().actionFillSlot(aTarget, aCas, range.getBegin(), + range.getEnd()); } /** @@ -95,18 +90,17 @@ private void actionSpan(AjaxRequestTarget aTarget, IRequestParameters aRequestPa * selected annotations or offsets contained in the request for the creation of a new * annotation. */ - private CompactRange getOffsetsFromRequest(AjaxRequestTarget aTarget, - IRequestParameters request, CAS aCas) + private CompactRange getRangeFromRequest(AjaxRequestTarget aTarget, IRequestParameters request, + CAS aCas) throws IOException { // Create new span annotation - in this case we get the offset information from the // request - String offsets = request.getParameterValue(PARAM_OFFSETS).toString(); + var offsets = request.getParameterValue(PARAM_OFFSETS).toString(); - CompactRangeList offsetLists = JSONUtil.getObjectMapper().readValue(offsets, - CompactRangeList.class); + var offsetLists = JSONUtil.getObjectMapper().readValue(offsets, CompactRangeList.class); - AnnotatorState state = getAnnotatorState(); + var state = getAnnotatorState(); int annotationBegin = state.getWindowBeginOffset() + offsetLists.get(0).getBegin(); int annotationEnd = state.getWindowBeginOffset() + offsetLists.get(offsetLists.size() - 1).getEnd(); diff --git a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/MoveSpanAnnotationHandler.java b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/MoveSpanAnnotationHandler.java index e8f505e4a28..b919788a9f7 100644 --- a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/MoveSpanAnnotationHandler.java +++ b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/MoveSpanAnnotationHandler.java @@ -17,7 +17,7 @@ */ package de.tudarmstadt.ukp.inception.diam.editor.actions; -import static de.tudarmstadt.ukp.inception.diam.editor.actions.CreateSpanAnnotationHandler.getOffsetsFromRequest; +import static de.tudarmstadt.ukp.inception.diam.editor.actions.CreateSpanAnnotationHandler.getRangeFromRequest; import java.io.IOException; @@ -70,7 +70,7 @@ public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest) CAS cas = page.getEditorCas(); var vid = getVid(aRequest); AnnotatorState state = getAnnotatorState(); - var range = getOffsetsFromRequest(state, aRequest.getRequestParameters(), cas); + var range = getRangeFromRequest(state, aRequest.getRequestParameters(), cas); moveSpan(aTarget, cas, vid, range); return new DefaultAjaxResponse(getAction(aRequest)); } diff --git a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/ScrollToHandler.java b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/ScrollToHandler.java index f6ef197c744..755e9dcc050 100644 --- a/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/ScrollToHandler.java +++ b/inception/inception-diam/src/main/java/de/tudarmstadt/ukp/inception/diam/editor/actions/ScrollToHandler.java @@ -17,7 +17,7 @@ */ package de.tudarmstadt.ukp.inception.diam.editor.actions; -import static de.tudarmstadt.ukp.inception.diam.editor.actions.CreateSpanAnnotationHandler.getOffsetsFromRequest; +import static de.tudarmstadt.ukp.inception.diam.editor.actions.CreateSpanAnnotationHandler.getRangeFromRequest; import org.apache.uima.cas.CAS; import org.apache.wicket.ajax.AjaxRequestTarget; @@ -58,7 +58,7 @@ public DefaultAjaxResponse handle(AjaxRequestTarget aTarget, Request aRequest) IRequestParameters requestParameters = aRequest.getRequestParameters(); if (!requestParameters.getParameterValue(PARAM_OFFSETS).isEmpty()) { - Range offsets = getOffsetsFromRequest(getAnnotatorState(), requestParameters, cas); + Range offsets = getRangeFromRequest(getAnnotatorState(), requestParameters, cas); page.getAnnotationActionHandler().actionJump(aTarget, offsets.getBegin(), offsets.getEnd()); } diff --git a/inception/inception-diam/src/test/java/de/tudarmstadt/ukp/inception/diam/editor/actions/CreateSpanAnnotationHandlerTest.java b/inception/inception-diam/src/test/java/de/tudarmstadt/ukp/inception/diam/editor/actions/CreateSpanAnnotationHandlerTest.java index a1b5cd17b41..841eb173e7c 100644 --- a/inception/inception-diam/src/test/java/de/tudarmstadt/ukp/inception/diam/editor/actions/CreateSpanAnnotationHandlerTest.java +++ b/inception/inception-diam/src/test/java/de/tudarmstadt/ukp/inception/diam/editor/actions/CreateSpanAnnotationHandlerTest.java @@ -62,7 +62,7 @@ void thatSelectionIsClippedToDocumentBoundaries_begin() throws Exception params.setParameterValue(CreateSpanAnnotationHandler.PARAM_OFFSETS, toJsonString(new CompactRangeList(new CompactRange(begin, end)))); - var range = sut.getOffsetsFromRequest(state, params, cas); + var range = sut.getRangeFromRequest(state, params, cas); assertThat(range.getBegin()).isEqualTo(0); assertThat(range.getEnd()).isEqualTo(cas.getDocumentText().length()); @@ -81,7 +81,7 @@ void thatSelectionIsClippedToDocumentBoundaries_end() throws Exception params.setParameterValue(CreateSpanAnnotationHandler.PARAM_OFFSETS, toJsonString(new CompactRangeList(new CompactRange(begin, end)))); - var range = sut.getOffsetsFromRequest(state, params, cas); + var range = sut.getRangeFromRequest(state, params, cas); assertThat(range.getBegin()).isEqualTo(0); assertThat(range.getEnd()).isEqualTo(end);