From 048ec0a0f33ce00dea80b188aa223dd2aae25327 Mon Sep 17 00:00:00 2001 From: Patrick Corless Date: Tue, 23 Jan 2024 21:51:03 -0700 Subject: [PATCH] GH-330 make sure we clean up glue and popup components in the DocumentViewModel --- .../views/AbstractDocumentViewModel.java | 32 +++++++++++++++++-- .../ri/common/views/DocumentViewModel.java | 4 ++- .../common/views/PageViewComponentImpl.java | 9 ++---- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/AbstractDocumentViewModel.java b/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/AbstractDocumentViewModel.java index 237eafd45..2d4d65b50 100644 --- a/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/AbstractDocumentViewModel.java +++ b/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/AbstractDocumentViewModel.java @@ -19,9 +19,12 @@ import org.icepdf.core.pobjects.Document; import org.icepdf.core.pobjects.Page; import org.icepdf.core.pobjects.PageTree; +import org.icepdf.core.pobjects.Reference; import org.icepdf.ri.common.UndoCaretaker; import org.icepdf.ri.common.views.annotations.AbstractAnnotationComponent; +import org.icepdf.ri.common.views.annotations.MarkupGlueComponent; import org.icepdf.ri.common.views.annotations.PageViewAnnotationComponent; +import org.icepdf.ri.common.views.annotations.PopupAnnotationComponent; import javax.swing.*; import java.awt.*; @@ -113,10 +116,35 @@ public void addDocumentViewAnnotationComponent(AbstractPageViewComponent pageVie } @Override - public void removeDocumentViewAnnotationComponent(AbstractPageViewComponent pageViewComponent, PageViewAnnotationComponent annotationComponent) { - if (!documentViewAnnotationComponents.containsKey(pageViewComponent)) { + public void removeDocumentViewAnnotationComponent(DocumentView parentDocumentView, + AbstractPageViewComponent pageViewComponent, + PageViewAnnotationComponent annotationComponent) { + if (documentViewAnnotationComponents.containsKey(pageViewComponent)) { List components = documentViewAnnotationComponents.get(pageViewComponent); components.remove(annotationComponent); + // find and remove glue component + if (annotationComponent instanceof PopupAnnotationComponent) { + PageViewAnnotationComponent markForRemoval = null; + for (PageViewAnnotationComponent pageViewAnnotationComponent : components) { + if (pageViewAnnotationComponent instanceof MarkupGlueComponent) { + MarkupGlueComponent glueComponent = (MarkupGlueComponent) pageViewAnnotationComponent; + PopupAnnotationComponent popupannotationComponent = + (PopupAnnotationComponent) annotationComponent; + Reference glueParentReference = + glueComponent.getPopupAnnotationComponent().getAnnotation().getPObjectReference(); + Reference parentReference = + popupannotationComponent.getAnnotation().getPObjectReference(); + if (glueParentReference.equals(parentReference)) { + markForRemoval = pageViewAnnotationComponent; + break; + } + } + } + if (markForRemoval != null) { + components.remove(markForRemoval); + parentDocumentView.remove((Component) markForRemoval); + } + } } } diff --git a/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/DocumentViewModel.java b/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/DocumentViewModel.java index eef703a2b..a0c870849 100644 --- a/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/DocumentViewModel.java +++ b/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/DocumentViewModel.java @@ -196,7 +196,9 @@ public interface DocumentViewModel { * Removes an annotation component the list of floating annotation components that are painted in the DocumentView. * @param annotationComponent annotation component to remove */ - void removeDocumentViewAnnotationComponent(AbstractPageViewComponent pageViewComponent, PageViewAnnotationComponent annotationComponent); + void removeDocumentViewAnnotationComponent(DocumentView parentDocumentView, + AbstractPageViewComponent pageViewComponent, + PageViewAnnotationComponent annotationComponent); void removeAllFloatingAnnotationComponent(AbstractPageViewComponent pageViewComponent); /** diff --git a/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/PageViewComponentImpl.java b/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/PageViewComponentImpl.java index 3a04531f3..e638301eb 100644 --- a/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/PageViewComponentImpl.java +++ b/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/views/PageViewComponentImpl.java @@ -1,11 +1,6 @@ package org.icepdf.ri.common.views; -import org.icepdf.core.pobjects.Catalog; -import org.icepdf.core.pobjects.Destination; -import org.icepdf.core.pobjects.NameTree; -import org.icepdf.core.pobjects.Page; -import org.icepdf.core.pobjects.PageTree; -import org.icepdf.core.pobjects.Reference; +import org.icepdf.core.pobjects.*; import org.icepdf.core.pobjects.annotations.Annotation; import org.icepdf.core.pobjects.annotations.ChoiceWidgetAnnotation; import org.icepdf.core.pobjects.annotations.FreeTextAnnotation; @@ -626,7 +621,7 @@ private void addPopupAnnotationComponentGlue(MarkupAnnotationComponent markupAnn private void removePopupAnnotationComponent(PopupAnnotationComponent popupAnnotationComponent) { parentDocumentView.remove(popupAnnotationComponent); - documentViewModel.removeDocumentViewAnnotationComponent(this, popupAnnotationComponent); + documentViewModel.removeDocumentViewAnnotationComponent(parentDocumentView, this, popupAnnotationComponent); ArrayList components = documentViewModel.getDocumentViewAnnotationComponents(this); // don't forget to remove the glue component for (PageViewAnnotationComponent component : components) {