Skip to content

Commit 6857107

Browse files
committed
Replace BackgroundTask with SwingWorker
Since "move to Java 6" happened a while ago.
1 parent 4cf6d1a commit 6857107

File tree

6 files changed

+41
-239
lines changed

6 files changed

+41
-239
lines changed

src/main/java/com/itextpdf/rups/controller/RupsInstanceController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ private void startObjectLoader() {
331331
loader = new ObjectLoader(
332332
this, pdfFile, pdfFile.getOriginalFile().getName(), dialog
333333
);
334-
loader.start();
334+
loader.execute();
335335
}
336336

337337
// tree selection

src/main/java/com/itextpdf/rups/model/BackgroundTask.java

Lines changed: 0 additions & 126 deletions
This file was deleted.

src/main/java/com/itextpdf/rups/model/ObjectLoader.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.rups.view.Language;
4646

4747
import javax.swing.SwingUtilities;
48+
import javax.swing.SwingWorker;
4849

4950
/**
5051
* Loads the necessary iText PDF objects in Background.
5152
*/
52-
public class ObjectLoader extends BackgroundTask {
53+
public class ObjectLoader extends SwingWorker<Void, Void> {
5354
/**
5455
* This is the object that wait for task to complete.
5556
*/
@@ -125,11 +126,8 @@ public String getLoaderName() {
125126
return loaderName;
126127
}
127128

128-
/**
129-
* @see BackgroundTask#doTask()
130-
*/
131129
@Override
132-
public void doTask() {
130+
protected Void doInBackground() {
133131
objects = new IndirectObjectFactory(file.getPdfDocument());
134132
final int n = objects.getXRefMaximum();
135133
SwingUtilities.invokeLater(() -> {
@@ -142,10 +140,11 @@ public void doTask() {
142140
SwingUtilities.invokeLater(() -> progress.setTotal(0));
143141
nodes = new TreeNodeFactory(objects);
144142
SwingUtilities.invokeLater(() -> progress.setMessage(Language.GUI_UPDATING.getString()));
143+
return null;
145144
}
146145

147146
@Override
148-
public void finished() {
147+
protected void done() {
149148
try {
150149
eventListener.handleOpenDocument(this);
151150
} catch (Exception ex) {

src/test/java/com/itextpdf/rups/mock/MockedBackgroundTask.java renamed to src/test/java/com/itextpdf/rups/mock/NoopProgressDialog.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of the iText (R) project.
3-
Copyright (c) 1998-2024 Apryse Group NV
3+
Copyright (c) 1998-2025 Apryse Group NV
44
Authors: Apryse Software.
55
66
This program is free software; you can redistribute it and/or modify
@@ -42,24 +42,34 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.rups.mock;
4444

45-
import com.itextpdf.rups.model.BackgroundTask;
45+
import com.itextpdf.rups.model.IProgressDialog;
4646

47-
public class MockedBackgroundTask extends BackgroundTask {
48-
49-
private boolean taskExecuted;
47+
/**
48+
* {@link IProgressDialog} implementation, that does nothing.
49+
*/
50+
public final class NoopProgressDialog implements IProgressDialog {
51+
@Override
52+
public void setMessage(String msg) {
53+
// noop
54+
}
5055

51-
public MockedBackgroundTask() {
52-
this.taskExecuted = false;
56+
@Override
57+
public void setValue(int value) {
58+
// noop
5359
}
5460

55-
@Override public void doTask() {
56-
taskExecuted = true;
61+
@Override
62+
public void setTotal(int n) {
63+
// noop
5764
}
5865

59-
@Override public void finished() {
66+
@Override
67+
public void showErrorDialog(Exception ex) {
68+
// noop
6069
}
6170

62-
public boolean hasTaskExecuted() {
63-
return taskExecuted;
71+
@Override
72+
public void setVisible(boolean visible) {
73+
// noop
6474
}
6575
}

src/test/java/com/itextpdf/rups/model/BackgroundTaskTest.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/test/java/com/itextpdf/rups/view/itext/StructureTreeTest.java

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ This file is part of the iText (R) project.
4343
package com.itextpdf.rups.view.itext;
4444

4545
import com.itextpdf.rups.controller.PdfReaderController;
46-
import com.itextpdf.rups.model.IProgressDialog;
46+
import com.itextpdf.rups.mock.NoopProgressDialog;
47+
import com.itextpdf.rups.model.IRupsEventListener;
4748
import com.itextpdf.rups.model.ObjectLoader;
4849
import com.itextpdf.rups.model.PdfFile;
4950
import com.itextpdf.rups.view.itext.treenodes.StructureTreeNode;
@@ -52,6 +53,7 @@ This file is part of the iText (R) project.
5253
import java.io.File;
5354
import java.io.IOException;
5455

56+
import java.util.concurrent.ExecutionException;
5557
import org.junit.jupiter.api.Assertions;
5658
import org.junit.jupiter.api.Tag;
5759
import org.junit.jupiter.api.Test;
@@ -64,7 +66,8 @@ class StructureTreeTest extends ExtendedITextTest {
6466
private static final String sourceFolder = "./src/test/resources/com/itextpdf/rups/controller/";
6567

6668
@Test
67-
void extractMcidContentInStructureTreeTest() throws IOException {
69+
void extractMcidContentInStructureTreeTest()
70+
throws IOException, ExecutionException, InterruptedException {
6871
final PdfFile pdfFile = PdfFile.open(
6972
new File(sourceFolder + "hello_world_tagged.pdf")
7073
);
@@ -77,7 +80,8 @@ void extractMcidContentInStructureTreeTest() throws IOException {
7780
}
7881

7982
@Test
80-
void extractMcidContentInStructureTreeWithActualTextTest() throws IOException {
83+
void extractMcidContentInStructureTreeWithActualTextTest()
84+
throws IOException, ExecutionException, InterruptedException {
8185
final PdfFile pdfFile = PdfFile.open(
8286
new File(sourceFolder + "hello_world_tagged_actualtext.pdf")
8387
);
@@ -89,14 +93,17 @@ void extractMcidContentInStructureTreeWithActualTextTest() throws IOException {
8993
Assertions.assertEquals("0 [Olleh ]", nodeLabel);
9094
}
9195

92-
private static StructureTreeNode getStructureTreeRootNode(PdfFile pdfFile) {
96+
private static StructureTreeNode getStructureTreeRootNode(PdfFile pdfFile)
97+
throws ExecutionException, InterruptedException {
9398

9499
PdfReaderController controller = new PdfReaderController(null, null);
100+
// Using a noop listener here to prevent threading issues
95101
ObjectLoader loader = new ObjectLoader(
96-
controller, pdfFile, "Test loader", new DummyProgressDialog()
102+
new IRupsEventListener() {}, pdfFile, "Test loader", new NoopProgressDialog()
97103
);
98104
// preload everything
99-
loader.doTask();
105+
loader.execute();
106+
loader.get();
100107

101108
// initialise the main PDF object tree view
102109
controller.handleOpenDocument(loader);
@@ -107,32 +114,4 @@ private static StructureTreeNode getStructureTreeRootNode(PdfFile pdfFile) {
107114
tree.setModel(tree.recalculateTreeModel());
108115
return (StructureTreeNode) tree.getModel().getRoot();
109116
}
110-
111-
private static final class DummyProgressDialog implements IProgressDialog {
112-
113-
@Override
114-
public void setMessage(String msg) {
115-
116-
}
117-
118-
@Override
119-
public void setValue(int value) {
120-
121-
}
122-
123-
@Override
124-
public void setTotal(int n) {
125-
126-
}
127-
128-
@Override
129-
public void showErrorDialog(Exception ex) {
130-
131-
}
132-
133-
@Override
134-
public void setVisible(boolean visible) {
135-
136-
}
137-
}
138117
}

0 commit comments

Comments
 (0)