📄 windowsdirectorychooserui.java
字号:
.equals(evt.getPropertyName())) {
findFile(chooser.getCurrentDirectory(), false, false);
}
if (JFileChooser
.ACCESSORY_CHANGED_PROPERTY
.equals(evt.getPropertyName())) {
Component oldValue = (Component)evt.getOldValue();
Component newValue = (Component)evt.getNewValue();
if (oldValue != null) {
chooser.remove(oldValue);
}
if (newValue != null) {
chooser.add("North", newValue);
}
chooser.revalidate();
chooser.repaint();
}
if (JFileChooser
.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY
.equals(evt.getPropertyName())) {
updateView(chooser);
}
}
}
private class SelectionListener implements TreeSelectionListener {
public void valueChanged(TreeSelectionEvent e) {
getApproveSelectionAction().setEnabled(tree.getSelectionCount() > 0);
setSelectedFiles();
}
}
public Action getApproveSelectionAction() {
return approveSelectionAction;
}
private void setSelectedFiles() {
TreePath[] selectedPaths = tree.getSelectionPaths();
if (selectedPaths == null || selectedPaths.length == 0) {
chooser.setSelectedFile(null);
return;
}
List files = new ArrayList();
for (int i = 0, c = selectedPaths.length; i < c; i++) {
File f =
((FileTreeNode)selectedPaths[i].getLastPathComponent()).getFile();
files.add(f);
}
chooser.setSelectedFiles((File[])files.toArray(new File[0]));
}
private class ApproveSelectionAction extends AbstractAction {
public ApproveSelectionAction() {
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
setSelectedFiles();
chooser.approveSelection();
}
}
/**
* Listens on nodes being opened and preload their children to get better UI
* experience (GUI should be more responsive and empty nodes discovered
* automatically).
*/
private class TreeExpansion implements TreeExpansionListener {
public void treeCollapsed(TreeExpansionEvent event) {
}
public void treeExpanded(TreeExpansionEvent event) {
// ensure children gets expanded later
if (event.getPath() != null) {
Object lastElement = event.getPath().getLastPathComponent();
if (lastElement instanceof FileTreeNode && useNodeQueue) {
if (((FileTreeNode)lastElement).isLoaded()) {
for (Enumeration e = ((FileTreeNode)lastElement).children();
e.hasMoreElements();
) {
//Object node = enum.nextElement();
addToQueue((FileTreeNode)e.nextElement(), tree);
}
}
}
}
}
}
private class FileSystemTreeRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, false,
// even "leaf" folders should look like other folders
row, hasFocus);
if (value instanceof FileTreeNode) {
FileTreeNode node = (FileTreeNode)value;
setText(getFileView(chooser).getName(node.getFile()));
if (OS.isMacOSX()
&& UIManager.getLookAndFeel().isNativeLookAndFeel()) {
// do not set icon for MacOSX when native look is used, it seems the
// Tree.icons set by the
// look and feel are not that good or Apple is doing something I
// can't figure.
// setIcon only if not running in MacOSX
} else {
setIcon(getFileView(chooser).getIcon(node.getFile()));
}
}
return this;
}
}
private class FileSystemTreeModel extends DefaultTreeModel {
public FileSystemTreeModel(FileSystemView fsv) {
super(new MyComputerTreeNode(fsv), false);
}
}
private class MyComputerTreeNode extends LazyMutableTreeNode {
public MyComputerTreeNode(FileSystemView fsv) {
super(fsv);
}
protected void loadChildren() {
FileSystemView fsv = (FileSystemView)getUserObject();
File[] roots = fsv.getRoots();
if (roots != null) {
Arrays.sort(roots);
for (int i = 0, c = roots.length; i < c; i++) {
add(new FileTreeNode(roots[i]));
}
}
}
public String toString() {
return "/";
}
}
private class FileTreeNode
extends LazyMutableTreeNode
implements Comparable {
public FileTreeNode(File file) {
super(file);
}
public boolean canEnqueue() {
return !isLoaded()
&& !chooser.getFileSystemView().isFloppyDrive(getFile())
&& !chooser.getFileSystemView().isFileSystemRoot(getFile());
}
public boolean isLeaf() {
if (!isLoaded()) {
return false;
} else {
return super.isLeaf();
}
}
protected void loadChildren() {
FileTreeNode[] nodes = getChildren();
for (int i = 0, c = nodes.length; i < c; i++) {
add(nodes[i]);
}
}
private FileTreeNode[] getChildren() {
File[] files =
chooser.getFileSystemView().getFiles(
getFile(),
chooser.isFileHidingEnabled());
ArrayList nodes = new ArrayList();
// keep only directories, no "file" in the tree.
if (files != null) {
for (int i = 0, c = files.length; i < c; i++) {
if (files[i].isDirectory()) {
nodes.add(new FileTreeNode(files[i]));
}
}
}
// sort directories, FileTreeNode implements Comparable
FileTreeNode[] result =
(FileTreeNode[])nodes.toArray(new FileTreeNode[0]);
Arrays.sort(result);
return result;
}
public File getFile() {
return (File)getUserObject();
}
public String toString() {
return chooser.getFileSystemView().getSystemDisplayName(
(File)getUserObject());
}
public int compareTo(Object o) {
if (!(o instanceof FileTreeNode)) {
return 1;
}
return getFile().compareTo(((FileTreeNode)o).getFile());
}
public void clear() {
super.clear();
((DefaultTreeModel)tree.getModel()).nodeStructureChanged(this);
}
}
/**
* From WindowsFileChooserUI
*/
protected class WindowsFileView extends BasicFileView {
public Icon getIcon(File f) {
Icon icon = getCachedIcon(f);
if (icon != null) {
return icon;
}
if (f != null) {
icon = getFileChooser().getFileSystemView().getSystemIcon(f);
}
if (icon == null) {
icon = super.getIcon(f);
}
cacheIcon(f, icon);
return icon;
}
}
private static synchronized void addToQueue(FileTreeNode node, JTree tree) {
if (nodeQueue == null || !nodeQueue.isAlive()) {
nodeQueue = new Queue();
nodeQueue.start();
}
if (node.canEnqueue()) {
nodeQueue.add(node, tree);
}
}
/**
* This queue takes care of loading nodes in the background.
*/
private static final class Queue extends Thread {
private volatile Stack nodes = new Stack();
private Object lock = new Object();
private volatile boolean running = true;
public Queue() {
super("DirectoryChooser-BackgroundLoader");
setDaemon(true);
}
public void add(WindowsDirectoryChooserUI.FileTreeNode node, JTree tree) {
if (!isAlive()) {
throw new IllegalArgumentException("Queue is no longer alive");
}
synchronized (lock) {
if (running) {
nodes.addElement(new QueueItem(node, tree));
lock.notifyAll();
}
}
}
public void run() {
while (running) {
while (nodes.size() > 0) {
final QueueItem item = (QueueItem)nodes.pop();
final WindowsDirectoryChooserUI.FileTreeNode node = item.node;
final JTree tree = item.tree;
// ask how many items we got
node.getChildCount();
Runnable runnable = new Runnable() {
public void run() {
((DefaultTreeModel)tree.getModel()).nodeChanged(node);
tree.repaint();
}
};
try {
SwingUtilities.invokeAndWait(runnable);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
// wait for 5 seconds for someone to use the queue, else just ends this
// queue
try {
synchronized (lock) {
lock.wait(5000);
}
if (nodes.size() == 0) {
running = false;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* An entry in the queue.
*/
private static final class QueueItem {
WindowsDirectoryChooserUI.FileTreeNode node;
JTree tree;
public QueueItem(WindowsDirectoryChooserUI.FileTreeNode node, JTree tree) {
this.node = node;
this.tree = tree;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -