Skip to content

Commit

Permalink
GH-174 initial test for PopupAnnotation printing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Jan 12, 2023
1 parent 7683110 commit 5e487c0
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ else if (border != null) {
public boolean isBorder() {
boolean borderWidth = false;
Object border = getObject(BORDER_KEY);
if (border != null && border instanceof List) {
if (border instanceof List) {
List borderProps = (List) border;
if (borderProps.size() == 3) {
borderWidth = ((Number) borderProps.get(2)).floatValue() > 0;
Expand All @@ -1250,8 +1250,9 @@ public void render(Graphics2D origG, int renderHintType,
return;
if (renderHintType == GraphicsRenderingHints.SCREEN && !allowScreenNormalMode())
return;
if (renderHintType == GraphicsRenderingHints.PRINT && !allowPrintNormalMode())
return;
// need an override on this as popups aren't usually printed.
// if (renderHintType == GraphicsRenderingHints.PRINT && !allowPrintNormalMode())
// return;

//System.out.println("render(-) " + this);
Rectangle2D.Float rect = getUserSpaceRectangle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import org.icepdf.core.pobjects.graphics.GraphicsState;
import org.icepdf.core.util.Library;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/**
Expand Down Expand Up @@ -302,6 +306,25 @@ public PDate getCreationDate() {
return creationDate;
}

/**
* Format the creation date using the given FormatStyle
* @param formatStyle date output style used by DateTimeFormatter
* @return formatted creation date if available, empty String otherwise
*/
public String getFormattedCreationDate(FormatStyle formatStyle) {
LocalDateTime creationDate = getCreationDate().asLocalDateTime();
if (creationDate == null) return "";
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDateTime(formatStyle)
.withLocale(Locale.getDefault());
return creationDate.format(formatter);
}

public String getFormattedTitleText() {
String titleText = getTitleText();
return titleText != null ? titleText : "";
}

public boolean isInReplyTo() {
return library.getObject(entries, IRT_KEY) != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import org.icepdf.core.pobjects.StateManager;
import org.icepdf.core.util.Library;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.time.format.FormatStyle;
import java.util.HashMap;
import java.util.logging.Logger;

Expand Down Expand Up @@ -65,6 +68,9 @@ public class PopupAnnotation extends Annotation {

protected MarkupAnnotation parent;

private boolean resetPopupPaintables = true;
private JPanel popupPaintablesPanel;

public PopupAnnotation(Library l, HashMap h) {
super(l, h);
}
Expand Down Expand Up @@ -119,6 +125,80 @@ public static PopupAnnotation getInstance(Library library,
return popupAnnotation;
}

@Override
protected void renderAppearanceStream(Graphics2D g2d) {
GraphicsConfiguration graphicsConfiguration = g2d.getDeviceConfiguration();
if (graphicsConfiguration.getDevice().getType() == GraphicsDevice.TYPE_PRINTER) {
// make sure we have parent content print.
String contents = getParent() != null ? getParent().getContents() : "";
if (contents != null && resetPopupPaintables) {
buildPopupPaintables();
}
// todo likely also test for minimized to be useful?
if (contents != null){
paintPopupPaintables(g2d);
}
}
}

private void paintPopupPaintables(Graphics2D g2d) {
Rectangle2D.Float origRect = getUserSpaceRectangle();
Rectangle2D rect = new Rectangle2D.Double(0, 0, origRect.width, origRect.height);

AffineTransform oldTransform = g2d.getTransform();
g2d.scale(1,-1);
g2d.translate(0, -rect.getBounds().getSize().height);

// build out a basic representation of the panel
popupPaintablesPanel = new JPanel();
Color color = getParent().getColor();
popupPaintablesPanel.setBackground(color);
popupPaintablesPanel.setBounds(rect.getBounds());
popupPaintablesPanel.setSize(rect.getBounds().getSize());
popupPaintablesPanel.setPreferredSize(rect.getBounds().getSize());

MarkupAnnotation markupAnnotation = getParent();
// user
String title = markupAnnotation.getFormattedTitleText();
JLabel titleLabel = new JLabel(title);
popupPaintablesPanel.add(titleLabel);

// creation date
JLabel creationLabel = new JLabel();
creationLabel.setText(markupAnnotation.getFormattedCreationDate(FormatStyle.MEDIUM));
popupPaintablesPanel.add(creationLabel);

// text area
String contents = markupAnnotation.getFormattedTitleText();
JTextArea textArea = new JTextArea(contents);
textArea.setFont(new JLabel().getFont());
textArea.setWrapStyleWord(true);
textArea.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(color),
BorderFactory.createEmptyBorder(2, 2, 2, 2)));
popupPaintablesPanel.add(textArea);

Component[] components = popupPaintablesPanel.getComponents();
for (Component comp : components) {
Dimension size = comp.getPreferredSize();
comp.setSize(size);
comp.setPreferredSize(size);
}
popupPaintablesPanel.invalidate();
popupPaintablesPanel.revalidate();
popupPaintablesPanel.print(g2d);

g2d.setTransform(oldTransform);
}

private void buildPopupPaintables() {
resetPopupPaintables = false;
}

public void setContents(String content) {
super.setString(CONTENTS_KEY, content);
resetPopupPaintables = true;
}

@Override
public void resetAppearanceStream(double dx, double dy, AffineTransform pageTransform, boolean isNew) {

Expand All @@ -135,7 +215,7 @@ public void setOpen(boolean open) {
public MarkupAnnotation getParent() {
Object tmp = library.getObject(entries, PARENT_KEY);
// should normally be a text annotation type.
if (tmp != null && tmp instanceof MarkupAnnotation) {
if (tmp instanceof MarkupAnnotation) {
parent = (MarkupAnnotation) tmp;
}
return parent;
Expand Down
8 changes: 4 additions & 4 deletions logging.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
# Set the default logging level for the root logger
#.level = OFF
#.level = ALL
#.level=FINER
.level=INFO
.level=FINER
#.level=INFO
# Set the default logging level for new ConsoleHandler instances
#java.util.logging.ConsoleHandler.level=FINER
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.level=FINER
#java.util.logging.ConsoleHandler.level=INFO
# Set the default logging level for new FileHandler instances
java.util.logging.FileHandler.level=OFF
org.icepdf.core.util.parser.content.level=INFO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.List;
import java.util.*;
Expand Down Expand Up @@ -355,8 +353,7 @@ private void buildGUI() {
refreshCreationLabel();
// title, user name.
String title = selectedMarkupAnnotation != null ?
selectedMarkupAnnotation.getTitleText() != null ?
selectedMarkupAnnotation.getTitleText() : "" : "";
selectedMarkupAnnotation.getFormattedTitleText() : "";
titleLabel = new JLabel(title);

// Setup color appearance values.
Expand Down Expand Up @@ -771,13 +768,8 @@ public void refreshPopupState() {
}

private void refreshCreationLabel() {
if (selectedMarkupAnnotation != null &&
selectedMarkupAnnotation.getCreationDate() != null && creationLabel != null) {
LocalDateTime creationDate = selectedMarkupAnnotation.getCreationDate().asLocalDateTime();
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.MEDIUM)
.withLocale(Locale.getDefault());
creationLabel.setText(creationDate.format(formatter));
if (selectedMarkupAnnotation != null && creationLabel != null) {
creationLabel.setText(selectedMarkupAnnotation.getFormattedCreationDate(FormatStyle.MEDIUM));
}
}

Expand Down

0 comments on commit 5e487c0

Please sign in to comment.