Skip to content

Commit

Permalink
pcorlessGH-272 Fixes some exceptions (especially when no color labels…
Browse files Browse the repository at this point in the history
… are defined), fixes moveComponent to avoid StackOverflow, minor cleanup
  • Loading branch information
Guillaume Tâche committed Jun 13, 2024
1 parent 362adc6 commit 5e32489
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5064,7 +5064,7 @@ public void actionPerformed(ActionEvent event) {
"viewer.dialog.error.exception.msg",
message);
SwingUtilities.invokeLater(doSwingWork);
logger.log(Level.FINE, "Error processing action event.", e);
logger.log(Level.SEVERE, "Error processing action event.", e);
}

if (!cancelSetFocus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,9 @@ private void checkGroupLabelChange(MarkupAnnotationPanel.SortColumn sortColumn,
}

private String findColor(Color color) {
if (colorLabels != null) {
for (DragDropColorList.ColorLabel colorLabel : colorLabels) {
if (color.equals(colorLabel.getColor())) {
return colorLabel.getLabel();
}
for (DragDropColorList.ColorLabel colorLabel : colorLabels) {
if (color.equals(colorLabel.getColor())) {
return colorLabel.getLabel();
}
}
// see if we can't return a hex color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,10 @@ protected void applyAnnotationStatusLabel(Annotation annotation) {

ArrayList<DragDropColorList.ColorLabel> colorLabels = DragDropColorList.retrieveColorLabels();
String colorLabelString = null;
if (colorLabels != null) {
for (DragDropColorList.ColorLabel colorLabel : colorLabels) {
if (annotation.getColor() != null && colorLabel.getColor().equals(annotation.getColor())) {
colorLabelString = colorLabel.getLabel();
break;
}
for (DragDropColorList.ColorLabel colorLabel : colorLabels) {
if (annotation.getColor() != null && colorLabel.getColor().equals(annotation.getColor())) {
colorLabelString = colorLabel.getLabel();
break;
}
}
StringBuilder statusLabel = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
import org.icepdf.core.pobjects.annotations.PopupAnnotation;
import org.icepdf.core.pobjects.annotations.TextMarkupAnnotation;
import org.icepdf.core.util.SystemProperties;
import org.icepdf.ri.common.views.*;
import org.icepdf.ri.common.views.AbstractPageViewComponent;
import org.icepdf.ri.common.views.AnnotationComponent;
import org.icepdf.ri.common.views.Controller;
import org.icepdf.ri.common.views.DocumentViewController;
import org.icepdf.ri.common.views.PageViewComponentImpl;
import org.icepdf.ri.common.views.annotations.AbstractAnnotationComponent;
import org.icepdf.ri.common.views.annotations.MarkupAnnotationComponent;
import org.icepdf.ri.common.views.annotations.PopupAnnotationComponent;
Expand All @@ -41,7 +45,12 @@
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.UUID;
import java.util.logging.Level;


Expand Down Expand Up @@ -192,11 +201,14 @@ public void actionPerformed(final ActionEvent e) {
}
}

@Override
public Color getColor() {
if (getAnnotationComponent() != null && getAnnotationComponent().getAnnotation() != null) {
return getAnnotationComponent().getAnnotation().getColor();
} else {
if (getAnnotationComponent() == null ||
getAnnotationComponent().getAnnotation() == null ||
getAnnotationComponent().getAnnotation().getColor() == null) {
return popupBackgroundColor;
} else {
return getAnnotationComponent().getAnnotation().getColor();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void windowClosing(final WindowEvent e) {
}

public void saveChanges() {
if (annotationSummaryPanel.getController().hasChanged()) {
if (annotationSummaryPanel.getController().hasChanged() && annotationSummaryPanel.getController().canSave()) {
final MessageFormat formatter = new MessageFormat(
messageBundle.getString("viewer.summary.dialog.saveOnClose.noUpdates.msg").replace("'", "''"));
final String dialogMessage = formatter.format(new Object[]{controller.getDocument().getDocumentOrigin()});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.UUID;
import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;

/**
*
*/
Expand All @@ -43,14 +45,12 @@ public class ColorLabelPanel extends JPanel {

public ColorLabelPanel(final Frame frame, final DragDropColorList.ColorLabel colorLabel, final SummaryController summaryController) {
this.controller = createColorPanelController();
this.colorLabel = colorLabel;
this.summaryController = summaryController;
this.colorLabel = requireNonNull(colorLabel);
this.summaryController = requireNonNull(summaryController);

// setup the gui
setLayout(new BorderLayout());
if (colorLabel != null) {
add(new JLabel("<html><h3>" + colorLabel.getLabel() + "</h3></html>", JLabel.CENTER), BorderLayout.NORTH);
}
add(new JLabel("<html><h3>" + colorLabel.getLabel() + "</h3></html>", JLabel.CENTER), BorderLayout.NORTH);
draggableAnnotationPanel = createDraggableAnnotationPanel(frame);
draggableAnnotationPanel.addMouseWheelListener(e -> draggableAnnotationPanel.getParent()
.dispatchEvent(SwingUtilities.convertMouseEvent(draggableAnnotationPanel, e, draggableAnnotationPanel.getParent())));
Expand Down Expand Up @@ -86,7 +86,9 @@ public void compact(final UUID uuid) {

public AnnotationSummaryBox addAnnotation(final MarkupAnnotation markupAnnotation, final int y) {
final PopupAnnotation popupAnnotation = markupAnnotation.getPopupAnnotation();
if (popupAnnotation != null) {
if (popupAnnotation == null) {
return null;
} else {
final List<AbstractPageViewComponent> pageComponents =
summaryController.getController().getDocumentViewController().getDocumentViewModel().getPageComponents();
final int pageIndex = markupAnnotation.getPageIndex();
Expand All @@ -96,17 +98,17 @@ public AnnotationSummaryBox addAnnotation(final MarkupAnnotation markupAnnotatio
popupAnnotationComponent.setVisible(true);
popupAnnotationComponent.removeMouseListeners();
final AnnotationSummaryGroup parent = summaryController.getGroupManager().getParentOf(markupAnnotation);
if (parent != null) {
parent.addComponent(popupAnnotationComponent);
} else {
if (parent == null) {
draggableAnnotationPanel.add(popupAnnotationComponent, y);
draggableAnnotationPanel.revalidate();
draggableAnnotationPanel.repaint();
} else {
parent.addComponent(popupAnnotationComponent);
}
popupAnnotationComponent.fireComponentMoved(false, false, UUID.randomUUID());
return popupAnnotationComponent;
} else return null;
} else return null;
}
}

protected AnnotationSummaryBox createSummaryBox(final PopupAnnotation popupAnnotation,
Expand Down Expand Up @@ -139,10 +141,10 @@ public void addComponent(final AnnotationSummaryComponent component) {
}

public AnnotationSummaryBox addAnnotation(final MarkupAnnotation markupAnnotation) {
if (!markupAnnotation.isInReplyTo()) {
return addAnnotation(markupAnnotation, -1);
} else {
if (markupAnnotation.isInReplyTo()) {
return null;
} else {
return addAnnotation(markupAnnotation, -1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@

import java.awt.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.logging.Logger;

Expand Down Expand Up @@ -61,7 +65,7 @@ public Component add(final AnnotationSummaryComponent c, final int pos, final bo

public boolean contains(final MarkupAnnotation annotation) {
return Arrays.stream(getComponents())
.filter(c -> c instanceof AnnotationSummaryComponent)
.filter(AnnotationSummaryComponent.class::isInstance)
.flatMap(c -> ((AnnotationSummaryComponent) c).getAnnotations().stream())
.anyMatch(a -> a.getPObjectReference().equals(annotation.getPObjectReference()));
}
Expand All @@ -72,10 +76,10 @@ public Component add(final Component c, final int pos) {
panelController.setNeverDragged(false);
}
if (panelController.isNeverDragged()) {
if (pos != -1) {
super.add(c, pos);
} else {
if (pos == -1) {
super.add(c);
} else {
super.add(c, pos);
}
} else {
if (pos != -1) {
Expand All @@ -100,7 +104,7 @@ public Component add(final Component c, final int pos) {

public int getFirstPosForComponents(final Collection<Component> components) {
final List<Component> sorted = getSortedList(components, true);
return sorted != null ? getPositionFor(sorted.get(0)) : -1;
return sorted == null ? -1 : getPositionFor(sorted.get(0));
}

public int getPositionFor(final Component c) {
Expand Down Expand Up @@ -211,24 +215,22 @@ public void checkForOverlap(final UUID uuid) {
final Component[] comps = getComponents();
Arrays.sort(comps, new ComponentBoundsCompare());
if (comps.length > 1) {
checkForOverlap(uuid, comps[0], Arrays.copyOfRange(comps, 1, comps.length));
checkForOverlap(uuid, comps, 1);
}
}

private static void checkForOverlap(final UUID uuid, final Component refComp, final Component[] components) {
if (components.length > 0) {
final Component curComp = components[0];
private static void checkForOverlap(final UUID uuid, final Component[] components, final int index) {
if (index < components.length) {
final Component refComp = components[index - 1];
final Component curComp = components[index];
final Rectangle bounds = curComp.getBounds();
if (refComp.getBounds().intersects(bounds) || refComp.getY() + refComp.getHeight() + 10 > curComp.getY()) {
// over top but just below the top so we shift it back down.
curComp.setLocation(bounds.x, refComp.getY() + refComp.getHeight() + 10);
((AnnotationSummaryComponent) curComp).fireComponentMoved(false, true, uuid);
}
if (components.length > 1) {
checkForOverlap(uuid, curComp, Arrays.copyOfRange(components, 1, components.length));
}
checkForOverlap(uuid, components, index + 1);
}

}

public void compact(final UUID uuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.*;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -362,54 +368,46 @@ public void mouseMoved(final MouseEvent e) {
}
}

public void moveComponent(final Component dragComponent, final boolean snap) {
if (dragComponent != null) {
final int padding = 10;
private void moveComponent(final Component draggedComponent, final boolean snap) {
if (draggedComponent != null) {
panel.sortComponents();
final Component[] comps = panel.getComponents();
final int draggedIndex = findComponentAt(comps, dragComponent);
final Rectangle dragBounds = dragComponent.getBounds();
Component comp;
// adjust for any overlap
for (int i = 0; i < comps.length; i++) {
comp = comps[i];
if (i == draggedIndex) {
continue;
}
if (comp.getBounds().contains(dragBounds) && comp instanceof AnnotationSummaryGroup) {
summaryController.getGroupManager().moveIntoGroup((AnnotationSummaryComponent) dragComponent,
((AnnotationSummaryGroup) comp).getColor(), comp.getName());
return;
} else if (comp.getBounds().intersects(dragBounds)) {
// over top but just below the top so we shift it back down.
if (comp.getY() < dragBounds.y) {
dragComponent.setLocation(dragBounds.x, comp.getY() + comp.getHeight() + padding);
moveComponent(dragComponent, snap);
} else {
comp.setLocation(dragBounds.x, dragComponent.getY() + dragComponent.getHeight() + padding);
moveComponent(comp, snap);
}
}
}
innerMove(draggedComponent, snap);
}
}

if (draggedIndex == 0) {
// make sure the component y > padding
if (dragComponent.getY() < padding) {
dragComponent.setLocation(dragComponent.getX(), padding);
moveComponent(dragComponent, snap);
}
private void innerMove(final Component draggedComponent, final boolean snap) {
final int padding = 10;
final Component[] comps = panel.getComponents();
final int draggedIndex = findComponentAt(comps, draggedComponent);
final Rectangle dragBounds = draggedComponent.getBounds();
final Collection<AnnotationSummaryComponent> moved = new ArrayList<>(comps.length);
if (draggedIndex == 0 && dragBounds.getY() < padding) {
draggedComponent.setLocation(draggedComponent.getX(), padding);
} else if (draggedIndex > 0) {
final Component previousComponent = comps[draggedIndex - 1];
if (previousComponent instanceof AnnotationSummaryGroup && previousComponent.getBounds().contains(dragBounds)) {
summaryController.getGroupManager().moveIntoGroup((AnnotationSummaryComponent) draggedComponent,
((AnnotationSummaryGroup) previousComponent).getColor(), previousComponent.getName());
} else if (previousComponent.getBounds().intersects(dragBounds)) {
draggedComponent.setLocation(dragBounds.x, previousComponent.getY() + previousComponent.getHeight() + padding);
moved.add((AnnotationSummaryComponent) draggedComponent);
}
if (draggedIndex >= 1) {
comp = comps[draggedIndex - 1];
final int offset = dragComponent.getY() - (comp.getY() + comp.getHeight());
if (offset < padding) {
dragComponent.setLocation(dragComponent.getX(), dragComponent.getY() + (padding - offset));
moveComponent(dragComponent, snap);
}

for (int i = draggedIndex + 1; i < comps.length; i++) {
final Component currentComponent = comps[i];
for (int j = draggedIndex; j < i; ++j) {
final Component precedingComponent = comps[j];
if (currentComponent.getBounds().intersects(precedingComponent.getBounds())) {
final Component previousComponent = comps[i - 1];
currentComponent.setLocation(previousComponent.getX(), previousComponent.getY() + previousComponent.getHeight() + padding);
moved.add((AnnotationSummaryComponent) currentComponent);
}
}
panel.revalidate();
((AnnotationSummaryComponent) dragComponent).fireComponentMoved(snap, true, UUID.randomUUID());
}

panel.revalidate();
moved.forEach(c -> c.fireComponentMoved(snap, true, UUID.randomUUID()));
}

private int findComponentAt(final Component[] comps, final Component dragComponent) {
Expand Down Expand Up @@ -441,10 +439,12 @@ private AnnotationSummaryComponent getDeepestComponentAt(final Component c, fina
final Component child = c.getComponentAt(p);
if (child instanceof AnnotationSummaryComponent) {
final Point newP = SwingUtilities.convertPoint(c, p, child);
if (child != c) {
if (child == c) {
return (AnnotationSummaryComponent) child;
} else {
final AnnotationSummaryComponent deepest = getDeepestComponentAt(child, newP);
return deepest == null ? (AnnotationSummaryComponent) child : deepest;
} else return (AnnotationSummaryComponent) child;
}
} else if (c instanceof AnnotationSummaryComponent) {
return (AnnotationSummaryComponent) c;
} else return null;
Expand All @@ -463,17 +463,19 @@ private List<AnnotationSummaryComponent> getAllComponentsAt(final Component c, f
final Component child = c.getComponentAt(p);
if (child instanceof AnnotationSummaryComponent) {
//Sometimes StackOverflow for some reason
if (!list.contains(child)) {
if (list.contains(child)) {
return list;
} else {
list.add((AnnotationSummaryComponent) child);
final Point newP = SwingUtilities.convertPoint(c, p, child);
return getAllComponentsAt(child, newP, list);
} else return list;
}
} else return list;
}

public AnnotationSummaryComponent findComponentFor(final Predicate<AnnotationSummaryBox> filter) {
final List<AnnotationSummaryComponent> components = Arrays.stream(panel.getComponents())
.map(c -> (AnnotationSummaryComponent) c).collect(Collectors.toList());
.map(AnnotationSummaryComponent.class::cast).collect(Collectors.toList());
final List<AnnotationSummaryComponent> found = components.stream().map(c -> {
if (c instanceof AnnotationSummaryBox) {
return filter.test((AnnotationSummaryBox) c) ? c : null;
Expand Down
Loading

0 comments on commit 5e32489

Please sign in to comment.