Skip to content

Commit

Permalink
GH-233 code clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Jul 20, 2023
1 parent 131d5d5 commit d8e675c
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 43 deletions.
2 changes: 1 addition & 1 deletion core/core-awt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
implementation 'org.apache.pdfbox:fontbox:' + "${FONT_BOX_VERSION}"
implementation 'commons-logging:commons-logging:1.2'
runtimeOnly 'com.github.jai-imageio:jai-imageio-jpeg2000:' + "${JAI_VERSION}"
// test, yes we have some formal test now
// tests
testImplementation(platform("org.junit:junit-bom:${JUNIT_BOM_VERSION}"))
testImplementation('org.junit.jupiter:junit-jupiter')
}
Expand Down
21 changes: 14 additions & 7 deletions core/core-awt/src/main/java/org/icepdf/core/pobjects/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@ public void paint(Graphics g, int renderHintType, final int boundary, float user
* for search terms.
* @throws InterruptedException thread interrupted.
*/
public void paint(Graphics g, int renderHintType, final int boundary, float userRotation, float userZoom, boolean paintAnnotations, boolean paintSearchHighlight) throws InterruptedException {
public void paint(Graphics g, int renderHintType, final int boundary, float userRotation, float userZoom,
boolean paintAnnotations, boolean paintSearchHighlight) throws InterruptedException {
if (!inited) {
// make sure we don't do a page init on the awt thread in the viewer
// ri, let the
Expand Down Expand Up @@ -609,15 +610,18 @@ public void paint(Graphics g, int renderHintType, final int boundary, float user
* for search terms.
* @throws InterruptedException thread interrupted.
*/
public void paintPageContent(Graphics g, int renderHintType, float userRotation, float userZoom, boolean paintAnnotations, boolean paintSearchHighlight) throws InterruptedException {
public void paintPageContent(Graphics g, int renderHintType, float userRotation, float userZoom,
boolean paintAnnotations, boolean paintSearchHighlight) throws InterruptedException {
if (!inited) {
init();
}

paintPageContent(((Graphics2D) g), renderHintType, userRotation, userZoom, paintAnnotations, paintSearchHighlight);
paintPageContent(((Graphics2D) g), renderHintType, userRotation, userZoom, paintAnnotations,
paintSearchHighlight);
}

private void paintPageContent(Graphics2D g2, int renderHintType, float userRotation, float userZoom, boolean paintAnnotations, boolean paintSearchHighlight) throws InterruptedException {
private void paintPageContent(Graphics2D g2, int renderHintType, float userRotation, float userZoom,
boolean paintAnnotations, boolean paintSearchHighlight) throws InterruptedException {
// draw page content
if (shapes != null) {
pagePainted = false;
Expand Down Expand Up @@ -817,8 +821,10 @@ private void createPrintableMarkupAnnotationGlue(Annotation annotation) {
* the method @link{#createAnnotation} for creating new annotations.
*
* @param newAnnotation annotation object to add
* @param isNew annotation is new and should be added to stateManager, otherwise change will be part of the document
* but not yet added to the stateManager as the change was likely a missing content stream or popup.
* @param isNew annotation is new and should be added to stateManager, otherwise change will be part of
* the document
* but not yet added to the stateManager as the change was likely a missing content stream
* or popup.
* @return reference to annotation that was added.
*/
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -1829,7 +1835,8 @@ public static Rectangle2D convertTo(Rectangle2D rectangle, AffineTransform affin
* @param userZoom Zoom factor to be applied to the rendered page
* @return rectangle converted to page space.
*/
public Rectangle2D convertToPageSpace(Rectangle2D rectangle, final int boundary, float userRotation, float userZoom) {
public Rectangle2D convertToPageSpace(Rectangle2D rectangle, final int boundary, float userRotation,
float userZoom) {
AffineTransform affineTransform = getToPageSpaceTransform(boundary, userRotation, userZoom);
return Page.convertTo(rectangle, affineTransform);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
*
*/
public abstract class CrossReferenceBase<T extends Dictionary> implements CrossReference{
public abstract class CrossReferenceBase<T extends Dictionary> implements CrossReference {

public final T crossReference;

Expand All @@ -38,7 +38,8 @@ public PObject loadObject(ObjectLoader objectLoader, Reference reference, Name h
return null;
}

public CrossReferenceEntry getEntry(Reference reference) throws ObjectStateException, CrossReferenceStateException, IOException {
public CrossReferenceEntry getEntry(Reference reference) throws ObjectStateException,
CrossReferenceStateException, IOException {
CrossReferenceEntry crossReferenceEntry = indirectObjectReferences.get(reference);
DictionaryEntries entries = crossReference.getEntries();
Library library = crossReference.getLibrary();
Expand All @@ -62,7 +63,8 @@ public CrossReferenceEntry getEntry(Reference reference) throws ObjectStateExcep
}

public HashMap<Reference, CrossReferenceEntry> getEntries() {
HashMap<Reference, CrossReferenceEntry> completeIndirectReferences = new HashMap<>(indirectObjectReferences.size());
HashMap<Reference, CrossReferenceEntry> completeIndirectReferences =
new HashMap<>(indirectObjectReferences.size());
if (prevCrossReference != null) {
completeIndirectReferences.putAll(prevCrossReference.getEntries());
}
Expand All @@ -83,7 +85,7 @@ public void setXrefStartPos(int xrefStartPos) {
this.xrefStartPos = xrefStartPos;
}

public DictionaryEntries getDictionaryEntries(){
public DictionaryEntries getDictionaryEntries() {
return crossReference.getEntries();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public void initialize(ByteBuffer byteBuffer)
}

public PTrailer getTrailerDictionary() {
if (pTrailer == null ){
if (pTrailer == null) {
// find the trailer dictionary, this should only happen if the file needed to reindex the file.
for (CrossReference crossReference : crossReferences) {
if (crossReference.getDictionaryEntries() != null &&
crossReference.getDictionaryEntries().get(ROOT_KEY) != null){
crossReference.getDictionaryEntries().get(ROOT_KEY) != null) {
pTrailer = new PTrailer(library, crossReference.getDictionaryEntries());
return pTrailer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public void addEntry(CrossReferenceEntry crossReferenceEntry) {
int generation;
if (crossReferenceEntry instanceof CrossReferenceUsedEntry) {
generation = ((CrossReferenceUsedEntry) crossReferenceEntry).getGenerationNumber();
indirectObjectReferences.put(new Reference(crossReferenceEntry.objectNumber, generation), crossReferenceEntry);
indirectObjectReferences.put(new Reference(crossReferenceEntry.objectNumber, generation),
crossReferenceEntry);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ public PObject getPObject(ByteBuffer byteBuffer, int objectOffsetStart)
}
}
// update position to end of object
byteBuffer.position(byteBuffer.position() + streamLength);
boolean foundEndObjMarker = ByteBufferUtil.findString(byteBuffer, END_OBJ_MARKER);
if (!foundEndObjMarker) {
throw new IllegalStateException("couldn't find enobj");
}
// xxx removing for now as this is change will require quite a bit of regression testing.
// byteBuffer.position(byteBuffer.position() + streamLength);
// boolean foundEndObjMarker = ByteBufferUtil.findString(byteBuffer, END_OBJ_MARKER);
// if (!foundEndObjMarker) {
// throw new IllegalStateException("couldn't find enobj");
// }

} else {
streamByteBuffer = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.logging.Logger;

/**
* Simple builder to handle the multiple ways to write document changes to an output stream.
* Simple builder to handle the multiple ways of writing document changes to an output stream.
*/
public class DocumentBuilder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public BaseWriter() {

}

public BaseWriter(CrossReferenceRoot crossReferenceRoot, SecurityManager securityManager, CountingOutputStream output,
public BaseWriter(CrossReferenceRoot crossReferenceRoot, SecurityManager securityManager,
CountingOutputStream output,
long startingPosition) {
this.output = output;
this.crossReferenceRoot = crossReferenceRoot;
Expand Down Expand Up @@ -119,11 +120,13 @@ public void writeFullTrailer() throws IOException {
}

public void writeIncrementalCompressedXrefTable() throws IOException {
compressedXrefTableWriter.writeIncrementalCompressedXrefTable(crossReferenceRoot, securityManager, entries, startingPosition, output);
compressedXrefTableWriter.writeIncrementalCompressedXrefTable(crossReferenceRoot, securityManager, entries,
startingPosition, output);
}

public void writeFullCompressedXrefTable() throws IOException {
compressedXrefTableWriter.writeFullCompressedXrefTable(crossReferenceRoot, securityManager, entries, startingPosition, output);
compressedXrefTableWriter.writeFullCompressedXrefTable(crossReferenceRoot, securityManager, entries,
startingPosition, output);
}

public void writeNewLine() throws IOException {
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 @@ -2508,19 +2508,26 @@ public void openDocument(String pathname) {
saveFilePath = getTempSaveFileName(pathname);
File tmpFile = new File(saveFilePath);
if (tmpFile.exists() && new File(pathname).exists()) {
String[] options = {messageBundle.getString("viewer.button.yes.label"), messageBundle.getString("viewer.button.no.label")};
int ret = JOptionPane.showOptionDialog(viewer, MessageFormat.format(messageBundle.getString("viewer.dialog.restore.label"), new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(tmpFile.lastModified())), messageBundle.getString("viewer.dialog.restore.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
String[] options = {messageBundle.getString("viewer.button.yes.label"), messageBundle.getString(
"viewer.button.no.label")};
int ret = JOptionPane.showOptionDialog(viewer, MessageFormat.format(messageBundle.getString(
"viewer.dialog.restore.label"),
new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(tmpFile.lastModified())),
messageBundle.getString("viewer.dialog.restore.title"), JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (ret == JOptionPane.YES_OPTION) {
try {
Files.copy(tmpFile.toPath(), new File(pathname).toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.copy(tmpFile.toPath(), new File(pathname).toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
org.icepdf.ri.util.Resources.showMessageDialog(
viewer,
JOptionPane.INFORMATION_MESSAGE,
messageBundle,
"viewer.dialog.restore.exception.title",
"viewer.dialog.restore.exception.label",
e.getMessage() != null && !e.getMessage().isEmpty() ? e.getMessage() : e.toString());
e.getMessage() != null && !e.getMessage().isEmpty() ? e.getMessage() :
e.toString());
}
} else {
try {
Expand Down Expand Up @@ -3128,7 +3135,8 @@ public void commonNewDocumentHandling(String fileDescription) {
String filename = f.exists() ? f.getName() : fileDescription;
Object[] messageArguments = title == null ? new String[]{filename} : new String[]{title, filename};
String titleResource = title == null ? "notitle" : "default";
MessageFormat formatter = new MessageFormat(messageBundle.getString("viewer.window.title.open." + titleResource));
MessageFormat formatter =
new MessageFormat(messageBundle.getString("viewer.window.title.open." + titleResource));
viewer.setTitle(formatter.format(messageArguments));
}

Expand Down Expand Up @@ -3472,7 +3480,8 @@ public void exportDocument() {

protected void saveFileAs(SaveMode saveMode) {
String originalFileName = getOriginalFileName();
String newFileName = originalFileName == null || originalFileName.isEmpty() ? null : generateNewSaveName(originalFileName);
String newFileName = originalFileName == null || originalFileName.isEmpty() ? null :
generateNewSaveName(originalFileName);

// Create and display a file saving dialog
if (!USE_JFILECHOOSER) {
Expand Down Expand Up @@ -5138,12 +5147,14 @@ else if (source == selectToolButton) {
tool = DocumentViewModelImpl.DISPLAY_TOOL_LINE_ARROW_ANNOTATION;
setDocumentToolMode(DocumentViewModelImpl.DISPLAY_TOOL_LINE_ARROW_ANNOTATION);
}
} else if (checkAnnotationButton(source, squareAnnotationToolButton, squareAnnotationPropertiesToolButton)) {
} else if (checkAnnotationButton(source, squareAnnotationToolButton,
squareAnnotationPropertiesToolButton)) {
if (e.getStateChange() == ItemEvent.SELECTED) {
tool = DocumentViewModelImpl.DISPLAY_TOOL_SQUARE_ANNOTATION;
setDocumentToolMode(DocumentViewModelImpl.DISPLAY_TOOL_SQUARE_ANNOTATION);
}
} else if (checkAnnotationButton(source, circleAnnotationToolButton, circleAnnotationPropertiesToolButton)) {
} else if (checkAnnotationButton(source, circleAnnotationToolButton,
circleAnnotationPropertiesToolButton)) {
if (e.getStateChange() == ItemEvent.SELECTED) {
tool = DocumentViewModelImpl.DISPLAY_TOOL_CIRCLE_ANNOTATION;
setDocumentToolMode(DocumentViewModelImpl.DISPLAY_TOOL_CIRCLE_ANNOTATION);
Expand Down Expand Up @@ -5309,7 +5320,8 @@ public void windowClosing(WindowEvent e) {
try {
File tmpFile = new File(saveFilePath);
if (tmpFile.exists()) {
Files.move(tmpFile.toPath(), new File(origFilePath).toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.move(tmpFile.toPath(), new File(origFilePath).toPath(),
StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException ex) {
logger.log(Level.FINE, "IO Exception ", e);
Expand Down Expand Up @@ -5484,14 +5496,16 @@ protected void prepareKeyMap(JComponent component) {
new BaseAction(() -> showPage(getPageTree().getNumberOfPages() - 1)));
addKeyAction(component, KEY_CODE_SEARCH, MODIFIER_SEARCH, new BaseAction(this::showSearch));
addKeyAction(component, KEY_CODE_SEARCH, MODIFIER_ADVANCED_SEARCH, new BaseAction(this::showSearchPanel));
addKeyAction(component, KEY_CODE_SEARCH_PREVIOUS, MODIFIER_SEARCH_PREVIOUS, new BaseAction(this::previousSearchResult));
addKeyAction(component, KEY_CODE_SEARCH_PREVIOUS, MODIFIER_SEARCH_PREVIOUS,
new BaseAction(this::previousSearchResult));
addKeyAction(component, KEY_CODE_SEARCH_NEXT, MODIFIER_SEARCH_NEXT, new BaseAction(this::nextSearchResult));
addKeyAction(component, KEY_CODE_GOTO, MODIFIER_GOTO, new BaseAction(this::showPageSelectionDialog));
addKeyAction(component, KEY_CODE_PREFERENCES, MODIFIER_PREFERENCES,
new BaseAction(this::showViewerPreferences));
}

protected final void addKeyAction(final JComponent component, final int keyCode, final int modifier, final BaseAction action) {
protected final void addKeyAction(final JComponent component, final int keyCode, final int modifier,
final BaseAction action) {
final InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
final ActionMap actionMap = component.getActionMap();
final String key = keyCode + "-" + modifier;
Expand Down Expand Up @@ -5714,11 +5728,14 @@ public void propertyChange(PropertyChangeEvent evt) {
}

private Collection<AnnotationColorToggleButton> getColorButtons() {
return new HashSet<>(Arrays.asList(highlightAnnotationToolButton, strikeOutAnnotationToolButton, underlineAnnotationToolButton, lineAnnotationToolButton,
lineArrowAnnotationToolButton, squareAnnotationToolButton, circleAnnotationToolButton, inkAnnotationToolButton, textAnnotationToolButton));
return new HashSet<>(Arrays.asList(highlightAnnotationToolButton, strikeOutAnnotationToolButton,
underlineAnnotationToolButton, lineAnnotationToolButton,
lineArrowAnnotationToolButton, squareAnnotationToolButton, circleAnnotationToolButton,
inkAnnotationToolButton, textAnnotationToolButton));
}

public void changeAnnotationsVisibility(final AnnotationFilter filter, final boolean visible, final boolean execInvert) {
public void changeAnnotationsVisibility(final AnnotationFilter filter, final boolean visible,
final boolean execInvert) {
callOnFilteredAnnotations(a -> a instanceof MarkupAnnotation && filter.filter(a), a -> {
a.setFlag(Annotation.FLAG_HIDDEN, !visible);
a.setFlag(Annotation.FLAG_INVISIBLE, !visible);
Expand Down Expand Up @@ -5778,7 +5795,8 @@ private void callOnFilteredAnnotations(final AnnotationFilter filter, final Cons
for (int i = 0; i < pt.getNumberOfPages(); ++i) {
final Page p = pt.getPage(i);
if (p.getAnnotations() != null) {
final List<Annotation> annotations = p.getAnnotations().stream().filter(filter::filter).collect(Collectors.toList());
final List<Annotation> annotations =
p.getAnnotations().stream().filter(filter::filter).collect(Collectors.toList());
annotations.forEach(toExecute);
}
}
Expand Down

0 comments on commit d8e675c

Please sign in to comment.