Skip to content

Commit

Permalink
GH-330 make sure we clean up glue and popup components in the Documen…
Browse files Browse the repository at this point in the history
…tViewModel
  • Loading branch information
Patrick Corless committed Jan 24, 2024
1 parent 123a704 commit 048ec0a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<PageViewAnnotationComponent> 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);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<PageViewAnnotationComponent> components = documentViewModel.getDocumentViewAnnotationComponents(this);
// don't forget to remove the glue component
for (PageViewAnnotationComponent component : components) {
Expand Down

0 comments on commit 048ec0a

Please sign in to comment.