|
|
@ -24,9 +24,9 @@ package com.github.weisj.darklaf.components.filetree; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.file.*; |
|
|
|
import java.nio.file.*; |
|
|
|
import java.util.*; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
import java.util.function.Consumer; |
|
|
|
import java.util.function.Consumer; |
|
|
|
import java.util.function.Function; |
|
|
|
|
|
|
|
import java.util.stream.Stream; |
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.*; |
|
|
|
import javax.swing.*; |
|
|
@ -37,6 +37,7 @@ public class FileTreeNode implements TreeNode, Comparable<FileTreeNode> { |
|
|
|
protected final FileTreeNode parent; |
|
|
|
protected final FileTreeNode parent; |
|
|
|
protected final FileTreeModel model; |
|
|
|
protected final FileTreeModel model; |
|
|
|
protected final Path file; |
|
|
|
protected final Path file; |
|
|
|
|
|
|
|
protected AtomicInteger taskCount = new AtomicInteger(); |
|
|
|
protected AtomicReference<List<FileTreeNode>> children; |
|
|
|
protected AtomicReference<List<FileTreeNode>> children; |
|
|
|
protected WatchKey watchKey; |
|
|
|
protected WatchKey watchKey; |
|
|
|
|
|
|
|
|
|
|
@ -75,18 +76,13 @@ public class FileTreeNode implements TreeNode, Comparable<FileTreeNode> { |
|
|
|
return index; |
|
|
|
return index; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void add(final List<FileTreeNode> nodes, final FileTreeNode node) { |
|
|
|
private int remove(final List<FileTreeNode> nodes, final FileTreeNode node) { |
|
|
|
int index = addSorted(nodes, node); |
|
|
|
|
|
|
|
model.nodesWereInserted(FileTreeNode.this, new int[] {index}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void remove(final List<FileTreeNode> nodes, final FileTreeNode node) { |
|
|
|
|
|
|
|
int index = nodes.indexOf(node); |
|
|
|
int index = nodes.indexOf(node); |
|
|
|
if (index < 0) |
|
|
|
if (index >= 0) { |
|
|
|
return; |
|
|
|
|
|
|
|
nodes.remove(node); |
|
|
|
nodes.remove(node); |
|
|
|
model.unregister(node); |
|
|
|
model.unregister(node); |
|
|
|
model.nodesWereRemoved(FileTreeNode.this, new int[] {index}, new Object[] {node}); |
|
|
|
} |
|
|
|
|
|
|
|
return index; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void reload() { |
|
|
|
public void reload() { |
|
|
@ -99,30 +95,45 @@ public class FileTreeNode implements TreeNode, Comparable<FileTreeNode> { |
|
|
|
if (children.get() == null) |
|
|
|
if (children.get() == null) |
|
|
|
return; |
|
|
|
return; |
|
|
|
List<FileTreeNode> fileList = children.get(); |
|
|
|
List<FileTreeNode> fileList = children.get(); |
|
|
|
doInBackground((Consumer<FileTreeNode> proc) -> { |
|
|
|
this.<ReloadOp>doInBackground(pub -> { |
|
|
|
traverseChildren(this::toNodes).accept(proc); |
|
|
|
taskCount.getAndIncrement(); |
|
|
|
fileList.stream().filter(n -> Files.notExists(n.file)).forEach(proc); |
|
|
|
this.traverseChildren(s -> { |
|
|
|
}, chunks -> { |
|
|
|
Stream.concat(s.map(this::toNode), fileList.stream()).map(n -> { |
|
|
|
for (FileTreeNode node : chunks) { |
|
|
|
if (Files.notExists(n.file)) { |
|
|
|
if (Files.notExists(node.file)) { |
|
|
|
return ReloadOp.remove(n, remove(fileList, n)); |
|
|
|
remove(fileList, node); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (model.showHiddenFiles) { |
|
|
|
if (model.showHiddenFiles) { |
|
|
|
if (!fileList.contains(node)) { |
|
|
|
if (!fileList.contains(n)) { |
|
|
|
add(fileList, node); |
|
|
|
return ReloadOp.add(n, addSorted(fileList, n)); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (isHidden(node.file)) { |
|
|
|
if (isHidden(n.file)) { |
|
|
|
remove(fileList, node); |
|
|
|
return ReloadOp.remove(n, remove(fileList, n)); |
|
|
|
} else if (!fileList.contains(node)) { |
|
|
|
} else if (!fileList.contains(n)) { |
|
|
|
add(fileList, node); |
|
|
|
return ReloadOp.add(n, addSorted(fileList, n)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
}).forEach(pub); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}, chunk -> { |
|
|
|
|
|
|
|
for (ReloadOp op : chunk) { |
|
|
|
|
|
|
|
if (op != null) { |
|
|
|
|
|
|
|
switch (op.type) { |
|
|
|
|
|
|
|
case ADD: |
|
|
|
|
|
|
|
model.nodesWereInserted(FileTreeNode.this, new int[] {op.index}); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case REMOVE: |
|
|
|
|
|
|
|
model.nodesWereRemoved(FileTreeNode.this, new int[] {op.index}, new Object[] {op.node}); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}, () -> { |
|
|
|
}, () -> { |
|
|
|
if (depth > 0) |
|
|
|
if (depth > 0) |
|
|
|
fileList.forEach(n -> n.reload(depth - 1)); |
|
|
|
fileList.forEach(n -> n.reload(depth - 1)); |
|
|
|
|
|
|
|
taskCount.getAndDecrement(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -132,17 +143,13 @@ public class FileTreeNode implements TreeNode, Comparable<FileTreeNode> { |
|
|
|
return list; |
|
|
|
return list; |
|
|
|
} |
|
|
|
} |
|
|
|
List<FileTreeNode> fileList = Collections.synchronizedList(new ArrayList<>()); |
|
|
|
List<FileTreeNode> fileList = Collections.synchronizedList(new ArrayList<>()); |
|
|
|
doInBackground( |
|
|
|
this.<Integer>doInBackground(pub -> { |
|
|
|
traverseChildren(asNodes(stream -> stream.filter(p -> model.showHiddenFiles || !isHidden(p)))), |
|
|
|
traverseChildren(s -> { |
|
|
|
chunks -> { |
|
|
|
s.filter(p -> model.showHiddenFiles || !isHidden(p)).map(this::toNode) |
|
|
|
Collections.sort(fileList); |
|
|
|
.map(n -> addSorted(fileList, n)).forEach(pub); |
|
|
|
int[] indices = new int[chunks.size()]; |
|
|
|
}); |
|
|
|
int i = 0; |
|
|
|
}, chunks -> { |
|
|
|
for (FileTreeNode node : chunks) { |
|
|
|
model.nodesWereInserted(FileTreeNode.this, chunks.stream().mapToInt(Integer::intValue).toArray()); |
|
|
|
indices[i] = addSorted(fileList, node); |
|
|
|
|
|
|
|
i++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
model.nodesWereInserted(FileTreeNode.this, indices); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
return fileList; |
|
|
|
return fileList; |
|
|
|
}); |
|
|
|
}); |
|
|
@ -156,24 +163,17 @@ public class FileTreeNode implements TreeNode, Comparable<FileTreeNode> { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Function<Stream<Path>, Stream<FileTreeNode>> asNodes( |
|
|
|
private FileTreeNode toNode(final Path path) { |
|
|
|
final Function<Stream<Path>, Stream<Path>> transformer) { |
|
|
|
return model.createNode(FileTreeNode.this, path); |
|
|
|
return stream -> toNodes(transformer.apply(stream)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Stream<FileTreeNode> toNodes(final Stream<Path> stream) { |
|
|
|
|
|
|
|
return stream.map(p -> model.createNode(FileTreeNode.this, p)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private <T> Consumer<Consumer<T>> traverseChildren(final Function<Stream<Path>, Stream<T>> transformer) { |
|
|
|
private void traverseChildren(final Consumer<Stream<Path>> consumer) { |
|
|
|
return publish -> { |
|
|
|
|
|
|
|
if (Files.isDirectory(file)) { |
|
|
|
if (Files.isDirectory(file)) { |
|
|
|
try (Stream<Path> files = Files.walk(file, 1, FileVisitOption.FOLLOW_LINKS)) { |
|
|
|
try (Stream<Path> files = Files.walk(file, 1, FileVisitOption.FOLLOW_LINKS)) { |
|
|
|
transformer.apply(files.filter(p -> !file.equals(p)).filter(Files::isReadable)).forEach(publish); |
|
|
|
consumer.accept(files.filter(p -> !file.equals(p)).filter(Files::isReadable)); |
|
|
|
} catch (IOException ignored) { |
|
|
|
} catch (IOException ignored) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private <T> void doInBackground(final Consumer<Consumer<T>> task, final Consumer<List<T>> processor) { |
|
|
|
private <T> void doInBackground(final Consumer<Consumer<T>> task, final Consumer<List<T>> processor) { |
|
|
@ -263,12 +263,19 @@ public class FileTreeNode implements TreeNode, Comparable<FileTreeNode> { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isBusy() { |
|
|
|
|
|
|
|
return taskCount.get() > 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static class RootNode extends FileTreeNode { |
|
|
|
public static class RootNode extends FileTreeNode { |
|
|
|
|
|
|
|
|
|
|
|
public RootNode(final FileTreeModel model) { |
|
|
|
private final List<Path> roots; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public RootNode(final FileTreeModel model, final List<Path> roots) { |
|
|
|
super(null, null, model); |
|
|
|
super(null, null, model); |
|
|
|
|
|
|
|
this.roots = roots; |
|
|
|
List<FileTreeNode> nodes = new ArrayList<>(); |
|
|
|
List<FileTreeNode> nodes = new ArrayList<>(); |
|
|
|
FileSystems.getDefault().getRootDirectories().forEach(p -> { |
|
|
|
createInitialDirectories().forEach(p -> { |
|
|
|
FileTreeNode node = model.createNode(RootNode.this, p); |
|
|
|
FileTreeNode node = model.createNode(RootNode.this, p); |
|
|
|
model.register(node); |
|
|
|
model.register(node); |
|
|
|
nodes.add(node); |
|
|
|
nodes.add(node); |
|
|
@ -276,12 +283,16 @@ public class FileTreeNode implements TreeNode, Comparable<FileTreeNode> { |
|
|
|
children.set(nodes); |
|
|
|
children.set(nodes); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected Iterable<Path> createInitialDirectories() { |
|
|
|
|
|
|
|
return roots.size() > 0 ? roots : FileSystems.getDefault().getRootDirectories(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void reload(final int depth) { |
|
|
|
protected void reload(final int depth) { |
|
|
|
if (depth < 0) |
|
|
|
if (depth < 0) |
|
|
|
return; |
|
|
|
return; |
|
|
|
List<FileTreeNode> nodes = children.get(); |
|
|
|
List<FileTreeNode> nodes = children.get(); |
|
|
|
FileSystems.getDefault().getRootDirectories().forEach(p -> { |
|
|
|
createInitialDirectories().forEach(p -> { |
|
|
|
FileTreeNode node = model.createNode(this, p); |
|
|
|
FileTreeNode node = model.createNode(this, p); |
|
|
|
if (!nodes.contains(node)) { |
|
|
|
if (!nodes.contains(node)) { |
|
|
|
model.register(node); |
|
|
|
model.register(node); |
|
|
@ -310,4 +321,30 @@ public class FileTreeNode implements TreeNode, Comparable<FileTreeNode> { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class ReloadOp { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final Type type; |
|
|
|
|
|
|
|
private final FileTreeNode node; |
|
|
|
|
|
|
|
private final int index; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ReloadOp(final Type type, final FileTreeNode node, final int index) { |
|
|
|
|
|
|
|
this.type = type; |
|
|
|
|
|
|
|
this.node = node; |
|
|
|
|
|
|
|
this.index = index; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ReloadOp add(final FileTreeNode n, final int index) { |
|
|
|
|
|
|
|
return new ReloadOp(Type.ADD, n, index); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ReloadOp remove(final FileTreeNode n, final int index) { |
|
|
|
|
|
|
|
return new ReloadOp(Type.REMOVE, n, index); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private enum Type { |
|
|
|
|
|
|
|
ADD, REMOVE |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|