Skip to content

Commit

Permalink
Update all operations on pathsListModel to run on the EDT
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkryst committed Nov 10, 2023
1 parent 1fa1c0b commit 7d99d4d
Show file tree
Hide file tree
Showing 2 changed files with 335 additions and 213 deletions.
73 changes: 45 additions & 28 deletions src/main/java/com/valkryst/JPathList/JPathList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -90,12 +92,6 @@ public void dragOver(final DropTargetDragEvent event) {}
public void addPath(final Path path) throws IOException {
Objects.requireNonNull(path);

synchronized (pathsListModel) {
if (pathsListModel.contains(path)) {
return;
}
}

if (Files.notExists(path)) {
throw new FileNotFoundException("The file '%s' does not exist".formatted(path));
}
Expand All @@ -110,9 +106,13 @@ public void addPath(final Path path) throws IOException {
return;
}

synchronized (pathsListModel) {
pathsListModel.addElement(path);
}
SwingUtilities.invokeLater(() -> {
synchronized (pathsListModel) {
if (!pathsListModel.contains(path)) {
pathsListModel.addElement(path);
}
}
});
return;
}

Expand All @@ -121,9 +121,13 @@ public void addPath(final Path path) throws IOException {
}

if (recursionMode < JFileChooser.FILES_ONLY || recursionMode > JFileChooser.FILES_AND_DIRECTORIES) {
synchronized (pathsListModel) {
pathsListModel.addElement(path);
}
SwingUtilities.invokeLater(() -> {
synchronized (pathsListModel) {
if (!pathsListModel.contains(path)) {
pathsListModel.addElement(path);
}
}
});
return;
}

Expand All @@ -141,9 +145,13 @@ public void addPath(final Path path) throws IOException {

// In these cases, we want to add the directory itself to the list.
if (recursionMode == JFileChooser.DIRECTORIES_ONLY || recursionMode == JFileChooser.FILES_AND_DIRECTORIES) {
synchronized (pathsListModel) {
pathsListModel.addElement(path);
}
SwingUtilities.invokeLater(() -> {
synchronized (pathsListModel) {
if (!pathsListModel.contains(path)) {
pathsListModel.addElement(path);
}
}
});
}

this.addPaths(pathsList);
Expand All @@ -154,7 +162,7 @@ public void addPath(final Path path) throws IOException {
*
* @param paths Paths to be added.
*
* @throws IOException If an I/O error occurs.
* @throws IOException If an I/O error occurs when recursing directories.
* @throws NullPointerException If {@code paths} is {@code null}.
*/
public void addPaths(final Path... paths) throws IOException {
Expand All @@ -170,7 +178,7 @@ public void addPaths(final Path... paths) throws IOException {
*
* @param paths Paths to be added.
*
* @throws IOException If an I/O error occurs.
* @throws IOException If an I/O error occurs when recursing directories.
* @throws NullPointerException If {@code paths} is {@code null}.
*/
public void addPaths(final List<Path> paths) throws IOException {
Expand All @@ -183,9 +191,11 @@ public void addPaths(final List<Path> paths) throws IOException {

/** Removes all paths from the list. */
public void removeAllPaths() {
synchronized (pathsListModel) {
pathsListModel.clear();
}
SwingUtilities.invokeLater(() -> {
synchronized (pathsListModel) {
pathsListModel.clear();
}
});
}

/**
Expand All @@ -197,9 +207,11 @@ public void removeAllPaths() {
public void removePath(final Path path) {
Objects.requireNonNull(path);

synchronized (pathsListModel) {
pathsListModel.removeElement(path);
}
SwingUtilities.invokeLater(() -> {
synchronized (pathsListModel) {
pathsListModel.removeElement(path);
}
});
}

/**
Expand Down Expand Up @@ -236,12 +248,17 @@ public void removePaths(final List<Path> paths) {
* <p>The list itself is a copy, but the paths are not.</p>
*
* @return The list of paths.
*
* @throws InterruptedException If the current thread is interrupted while waiting for the EDT.
* @throws InvocationTargetException If an exception is thrown while executing the runnable on the EDT.
*/
public List<Path> getPaths() {
List<Path> list;
synchronized (pathsListModel) {
list = Collections.list(pathsListModel.elements());
}
public List<Path> getPaths() throws InterruptedException, InvocationTargetException {
final var list = new ArrayList<Path>();
SwingUtilities.invokeAndWait(() -> {
synchronized (pathsListModel) {
list.addAll(Collections.list(pathsListModel.elements()));
}
});

return list;
}
Expand Down
Loading

0 comments on commit 7d99d4d

Please sign in to comment.