📄 windowsdirectorychooserui.java
字号:
/**
* $ $ License.
*
* Copyright $ L2FProd.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.l2fprod.common.swing.plaf.windows;
import com.l2fprod.common.swing.JDirectoryChooser;
import com.l2fprod.common.swing.LookAndFeelTweaks;
import com.l2fprod.common.swing.plaf.DirectoryChooserUI;
import com.l2fprod.common.swing.tree.LazyMutableTreeNode;
import com.l2fprod.common.util.OS;
import com.l2fprod.common.util.ResourceManager;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Stack;
import javax.swing.*;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.filechooser.FileView;
import javax.swing.plaf.basic.BasicFileChooserUI;
import javax.swing.text.JTextComponent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
/**
* WindowsDirectoryChooserUI. <br>
*
*/
public class WindowsDirectoryChooserUI
extends BasicFileChooserUI
implements DirectoryChooserUI {
private static Queue nodeQueue;
private JDirectoryChooser chooser;
private JTextComponent message;
private JTree tree;
private JScrollPane treeScroll;
private JButton approveButton;
private JButton cancelButton;
private JPanel buttonPanel;
private BasicFileView fileView = new WindowsFileView();
private Action approveSelectionAction = new ApproveSelectionAction();
private boolean useNodeQueue;
public WindowsDirectoryChooserUI(JDirectoryChooser chooser) {
super(chooser);
}
public void rescanCurrentDirectory(JFileChooser fc) {
super.rescanCurrentDirectory(fc);
findFile(
chooser.getSelectedFile() == null
? chooser.getCurrentDirectory()
: chooser.getSelectedFile(),
true,
true);
}
public void ensureFileIsVisible(JFileChooser fc, File f) {
super.ensureFileIsVisible(fc, f);
findFile(f, false, false);
}
protected String getToolTipText(MouseEvent event) {
TreePath path = tree.getPathForLocation(event.getX(), event.getY());
if (path != null && path.getLastPathComponent() instanceof FileTreeNode) {
FileTreeNode node = (FileTreeNode)path.getLastPathComponent();
String typeDescription =
getFileView(chooser).getTypeDescription(node.getFile());
if (typeDescription == null || typeDescription.length() == 0) {
return node.toString();
} else {
return node.toString() + " - " + typeDescription;
}
} else {
return null;
}
}
public void installComponents(JFileChooser chooser) {
this.chooser = (JDirectoryChooser)chooser;
chooser.setLayout(LookAndFeelTweaks.createBorderLayout());
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Component accessory = chooser.getAccessory();
if (accessory != null) {
chooser.add("North", chooser.getAccessory());
}
tree = new JTree() {
public String getToolTipText(MouseEvent event) {
String tip = WindowsDirectoryChooserUI.this.getToolTipText(event);
if (tip == null) {
return super.getToolTipText(event);
} else {
return tip;
}
}
};
tree.addTreeExpansionListener(new TreeExpansion());
tree.setModel(new FileSystemTreeModel(chooser.getFileSystemView()));
tree.setRootVisible(false);
tree.setShowsRootHandles(false);
tree.setCellRenderer(new FileSystemTreeRenderer());
tree.setToolTipText("");
chooser.add("Center", treeScroll = new JScrollPane(tree));
treeScroll.setPreferredSize(new Dimension(300, 300));
approveButton = new JButton();
approveButton.setAction(getApproveSelectionAction());
cancelButton = new JButton();
cancelButton.addActionListener(getCancelSelectionAction());
buttonPanel = new JPanel(LookAndFeelTweaks.createButtonAreaLayout());
buttonPanel.add(approveButton);
buttonPanel.add(cancelButton);
chooser.add("South", buttonPanel);
updateView(chooser);
}
protected void installStrings(JFileChooser fc) {
super.installStrings(fc);
saveButtonToolTipText =
ResourceManager.get(DirectoryChooserUI.class).getString(
"DirectoryChooser.saveButtonToolTipText");
openButtonToolTipText =
ResourceManager.get(DirectoryChooserUI.class).getString(
"DirectoryChooser.openButtonToolTipText");
cancelButtonToolTipText =
ResourceManager.get(DirectoryChooserUI.class).getString(
"DirectoryChooser.cancelButtonToolTipText");
}
public void uninstallComponents(JFileChooser chooser) {
chooser.remove(message);
chooser.remove(treeScroll);
chooser.remove(buttonPanel);
}
public FileView getFileView(JFileChooser fc) {
return fileView;
}
protected void installListeners(JFileChooser fc) {
super.installListeners(fc);
tree.addTreeSelectionListener(new SelectionListener());
}
protected void uninstallListeners(JFileChooser fc) {
super.uninstallListeners(fc);
}
public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) {
return new ChangeListener();
}
private void updateView(JFileChooser chooser) {
if (chooser.getApproveButtonText() != null) {
approveButton.setText(chooser.getApproveButtonText());
approveButton.setMnemonic(chooser.getApproveButtonMnemonic());
} else {
if (JFileChooser.OPEN_DIALOG == chooser.getDialogType()) {
approveButton.setText(openButtonText);
approveButton.setToolTipText(openButtonToolTipText);
approveButton.setMnemonic(openButtonMnemonic);
} else {
approveButton.setText(saveButtonText);
approveButton.setToolTipText(saveButtonToolTipText);
approveButton.setMnemonic(saveButtonMnemonic);
}
}
cancelButton.setText(cancelButtonText);
cancelButton.setMnemonic(cancelButtonMnemonic);
buttonPanel.setVisible(chooser.getControlButtonsAreShown());
}
/**
* Ensures the file is visible, tree expanded and optionally selected
*
* @param fileToLocate
* @param selectFile
*/
private void findFile(
File fileToLocate,
boolean selectFile,
boolean reload) {
if (fileToLocate == null || !fileToLocate.isDirectory()) {
return;
}
// build the canonical path so we can navigate the tree model to find the
// node
File file = null;
try {
file = fileToLocate.getCanonicalFile();
} catch (Exception e) {
return;
}
// temporarly disable loading nodes in the background
useNodeQueue = false;
try {
// split the full path into individual files to locate them in the tree
// model
List files = new ArrayList();
files.add(file);
while ((file = chooser.getFileSystemView().getParentDirectory(file))
!= null) {
files.add(0, file);
}
List path = new ArrayList();
// start from the root
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)tree.getModel().getRoot();
path.add(node);
DefaultMutableTreeNode current;
boolean found = true;
// ...and go through the tree model to find the files. Stop as soon as
// path is completely found or if one of the files in the path is not
// found.
while (files.size() > 0 && found) {
found = false;
for (int i = 0, c = node.getChildCount(); i < c; i++) {
current = (DefaultMutableTreeNode)node.getChildAt(i);
File f = ((FileTreeNode)current).getFile();
if (files.get(0).equals(f)) {
path.add(current);
files.remove(0);
node = current;
found = true;
break;
}
}
}
// select the path we found, it may be the file we were looking for or a
// subpath only
TreePath pathToSelect = new TreePath(path.toArray());
if (pathToSelect.getLastPathComponent() instanceof FileTreeNode
&& reload) {
((FileTreeNode) (pathToSelect.getLastPathComponent())).clear();
}
if (selectFile) {
tree.expandPath(pathToSelect);
tree.setSelectionPath(pathToSelect);
}
tree.scrollPathToVisible(pathToSelect);
} finally {
// re-enable background loading
useNodeQueue = true;
}
}
private class ChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser
.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY
.equals(evt.getPropertyName())) {
updateView(chooser);
}
if (JFileChooser
.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY
.equals(evt.getPropertyName())) {
if (chooser.isMultiSelectionEnabled()) {
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
} else {
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
}
}
if (JFileChooser
.DIRECTORY_CHANGED_PROPERTY
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -