Skip to content

Commit

Permalink
GH-252 Delete all user owned annotations (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
gtache committed Jun 13, 2024
1 parent 8b150f4 commit aca9737
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.icepdf.core.pobjects.*;
import org.icepdf.core.pobjects.graphics.GraphicsState;
import org.icepdf.core.util.Library;
import org.icepdf.core.util.SystemProperties;

import java.awt.*;
import java.awt.geom.GeneralPath;
Expand All @@ -28,6 +29,7 @@
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;

/**
* As mentioned in 12.5.2, "Annotation Dictionaries," the meaning of an
Expand Down Expand Up @@ -164,6 +166,8 @@ public abstract class MarkupAnnotation extends Annotation {
*/
public static final Name EXT_GSTATE_NAME = new Name("ip1");

private static final Pattern REPLY_PATTERN = Pattern.compile("(?:Re: ?)+");

/**
* (Optional; PDF 1.4) An array of numbers in the range 0.0 to 1.0 specifying
* the interior color that shall be used to fill the annotation’s line endings
Expand Down Expand Up @@ -472,4 +476,8 @@ public void setMarkupBounds(ArrayList<Shape> markupBounds) {
this.markupBounds = markupBounds;
}


public boolean isCurrentUserOwner() {
return REPLY_PATTERN.matcher(getTitleText()).replaceAll("").equals(SystemProperties.USER_NAME);
}
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<source>9</source>
<target>9</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public class SwingController extends ComponentAdapter
private JToggleButton zoomDynamicToolButton;
private JToggleButton selectToolButton;
// main annotation toolbar
private JButton deleteAllAnnotationsButton;
private AnnotationColorToggleButton highlightAnnotationToolButton;
private JToggleButton redactionAnnotationToolButton;
private JToggleButton linkAnnotationToolButton;
Expand Down Expand Up @@ -1233,6 +1234,26 @@ public void setAnnotationEditingModeToolButton(JToggleButton btn) {
btn.addActionListener(this);
}

/**
* Called by SwingViewerBuilder, so that Controller can setup event handling
*
* @param btn button to assign
*/
public void setDeleteAllButton(final JButton btn) {
deleteAllAnnotationsButton = btn;
btn.addActionListener(e -> {
documentViewController.getDocumentViewModel().getPageComponents().forEach(pvc -> {
final List<AbstractAnnotationComponent> comps = ((PageViewComponentImpl) pvc).getAnnotationComponents();
if (comps != null) {
final Collection<AnnotationComponent> toDelete = comps.stream().filter(comp -> comp instanceof MarkupAnnotationComponent
&& ((MarkupAnnotation) comp.getAnnotation()).isCurrentUserOwner()).collect(Collectors.toSet());
documentViewController.deleteAnnotations(toDelete);
reflectUndoCommands();
}
});
});
}

/**
* Called by SwingViewerBuilder, so that Controller can setup event handling
*
Expand Down Expand Up @@ -1703,6 +1724,7 @@ protected void reflectStateInComponents() {
setEnabled(zoomDynamicToolButton, opened && !pdfCollection);
setEnabled(textSelectToolButton, opened && canExtract && !pdfCollection);
setEnabled(selectToolButton, opened && canModify && !pdfCollection);
setEnabled(deleteAllAnnotationsButton, opened && canModify && !pdfCollection && !IS_READONLY);
setEnabled(highlightAnnotationToolButton, opened && canModify && !pdfCollection && !IS_READONLY);
setEnabled(redactionAnnotationToolButton, opened && canModify && !pdfCollection && !IS_READONLY);
setEnabled(strikeOutAnnotationToolButton, opened && canModify && !pdfCollection && !IS_READONLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ public JToolBar buildCompleteToolBar(boolean embeddableComponent) {
if (propertiesManager.checkAndStoreBooleanProperty(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_TOOL))
addToToolBar(toolbar, buildToolToolBar());
if (propertiesManager.checkAndStoreBooleanProperty(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION))
addToToolBar(toolbar, buildAnnotationlToolBar());
addToToolBar(toolbar, buildAnnotationToolBar());
if (propertiesManager.checkAndStoreBooleanProperty(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_FORMS))
addToToolBar(toolbar, buildFormsToolBar());
if (propertiesManager.checkAndStoreBooleanProperty(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_SEARCH))
Expand Down Expand Up @@ -1591,7 +1591,7 @@ public JToolBar buildToolToolBar() {
return toolbar;
}

public JToolBar buildAnnotationlToolBar() {
public JToolBar buildAnnotationToolBar() {
JToolBar toolbar = new JToolBar();
commonToolBarSetup(toolbar, false);
if (propertiesManager.checkAndStoreBooleanProperty(
Expand Down Expand Up @@ -1642,6 +1642,10 @@ public JToolBar buildAnnotationlToolBar() {
ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION_TEXT)) {
addToToolBar(toolbar, buildTextAnnotationToolButton(iconSize));
}
if (propertiesManager.checkAndStoreBooleanProperty(
ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION_DELETE)) {
addToToolBar(toolbar, buildDeleteAllAnnotationsButton(iconSize));
}
if (propertiesManager.checkAndStoreBooleanProperty(
ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION_REDACTION)) {
addToToolBar(toolbar, buildRedactionAnnotationToolButton(iconSize));
Expand All @@ -1662,7 +1666,6 @@ public JToolBar buildAnnotationlToolBar() {
ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION_PREVIEW)) {
addToToolBar(toolbar, buildAnnotationPreviewButton(iconSize));
}

return toolbar;
}

Expand Down Expand Up @@ -1776,6 +1779,17 @@ public JToggleButton buildSelectToolButton(final Images.IconSize imageSize) {
return btn;
}

public JButton buildDeleteAllAnnotationsButton(final Images.IconSize imageSize) {
final JButton btn = makeToolbarButton(
messageBundle.getString("viewer.toolbar.tool.delete.all.label"),
messageBundle.getString("viewer.toolbar.tool.delete.all.tooltip"),
"delete_all_annotations", imageSize, buttonFont);
if (viewerController != null && btn != null) {
viewerController.setDeleteAllButton(btn);
}
return btn;
}

public AbstractButton buildHighlightAnnotationToolButton(final Images.IconSize imageSize) {
AnnotationColorToggleButton btn = makeAnnotationToggleButton(
messageBundle.getString("viewer.toolbar.tool.highlight.label"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import org.icepdf.core.SecurityCallback;
import org.icepdf.core.pobjects.Destination;
import org.icepdf.core.pobjects.Document;
import org.icepdf.ri.common.views.annotations.AbstractAnnotationComponent;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.util.Collection;
import java.util.Set;


/**
Expand Down Expand Up @@ -230,4 +233,6 @@ public interface DocumentViewController {
void firePropertyChange(String event, int oldValue, int newValue);

void firePropertyChange(String event, Object oldValue, Object newValue);

void deleteAnnotations(Collection<AnnotationComponent> annotations);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -1395,6 +1396,35 @@ public void deleteAnnotation(AnnotationComponent annotationComponent) {
}
}

@Override
public void deleteAnnotations(final Collection<AnnotationComponent> annotations) {
if (documentViewModel != null) {
final List<Memento> addMementos = new ArrayList<>(annotations.size());
final List<Memento> deleteMementos = new ArrayList<>(annotations.size());
annotations.forEach(ac -> {
// parent component
final PageViewComponent pageComponent =
ac.getPageViewComponent();

if (annotationCallback != null) {
annotationCallback.removeAnnotation(pageComponent, ac);
}
addMementos.add(new AnnotationState(ac, AnnotationState.Operation.ADD));
deleteMementos.add(new AnnotationState(ac, AnnotationState.Operation.DELETE));

// fire event notification
firePropertyChange(PropertyConstants.ANNOTATION_DELETED, ac, null);
});
documentViewModel.addMemento(new CombinedMemento(addMementos), new CombinedMemento(deleteMementos));

// clear previously selected annotation and fire event.
assignSelectedAnnotation(null);

// repaint the view.
documentView.repaint();
}
}

public void addNewDestination(Destination destination) {
if (documentViewModel != null && destination != null) {
Library library = document.getCatalog().getLibrary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public AnnotationState(final AnnotationComponent annotationComponent, final Oper
this.annotationComponent = requireNonNull(annotationComponent);
this.operation = requireNonNull(operation);
this.userSpaceRectangle = annotationComponent.getAnnotation().getUserSpaceRectangle();
this.popupState = annotationComponent instanceof MarkupAnnotationComponent ? new PopupState(((MarkupAnnotationComponent) annotationComponent).getPopupAnnotationComponent()) : null;
this.popupState = annotationComponent instanceof MarkupAnnotationComponent ?
new PopupState(((MarkupAnnotationComponent) annotationComponent).getPopupAnnotationComponent()) : null;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public abstract class MarkupAnnotationComponent<T extends MarkupAnnotation> exte
"org.icepdf.core.annotations.interactive.popup.enabled", true);
}

// Used to keep a reference to the popupAnnotationComponent while the annotation is deleted, in case of an undo
//Used to keep a reference to the popupAnnotationComponent while the annotation is deleted, in case of an undo
private PopupAnnotationComponent popupAnnotationComponent;

public MarkupAnnotationComponent(T annotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public final class ViewerPropertiesManager {
public static final String PROPERTY_ANNOTATION_EDITING_MODE_ENABLED = "application.annotation.editing.mode.enabled";
// Individual controls for the annotation toolbar button commands
public static final String PROPERTY_SHOW_TOOLBAR_ANNOTATION_SELECTION = "application.toolbar.annotation.selection.enabled";
public static final String PROPERTY_SHOW_TOOLBAR_ANNOTATION_DELETE = "application.toolbar.annotation.delete.enabled";
public static final String PROPERTY_SHOW_TOOLBAR_ANNOTATION_HIGHLIGHT = "application.toolbar.annotation.highlight.enabled";
public static final String PROPERTY_SHOW_TOOLBAR_ANNOTATION_REDACTION = "application.toolbar.annotation.redaction" +
".enabled";
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ viewer.toolbar.tool.text.label=
viewer.toolbar.tool.text.tooltip=Text Select Tool
viewer.toolbar.tool.select.label=
viewer.toolbar.tool.select.tooltip=Select Tool
viewer.toolbar.tool.delete.all.label=Delete
viewer.toolbar.tool.delete.all.tooltip=Delete all annotations
viewer.toolbar.tool.link.label=
viewer.toolbar.tool.link.tooltip=Link Annotation Tool
viewer.toolbar.tool.highlight.label=Highlight
Expand Down

0 comments on commit aca9737

Please sign in to comment.