📄 xpfilechooserui.java
字号:
c.removePropertyChangeListener(filterComboBoxModel);
cancelButton.removeActionListener(getCancelSelectionAction());
approveButton.removeActionListener(getApproveSelectionAction());
fileNameTextField.removeActionListener(getApproveSelectionAction());
super.uninstallUI(c);
}
/**
* Returns the preferred size of the specified
* <code>JFileChooser</code>.
* The preferred size is at least as large,
* in both height and width,
* as the preferred size recommended
* by the file chooser's layout manager.
*
* @param c a <code>JFileChooser</code>
* @return a <code>Dimension</code> specifying the preferred
* width and height of the file chooser
*/
public Dimension getPreferredSize(JComponent c) {
int prefWidth = PREF_SIZE.width;
Dimension d = c.getLayout().preferredLayoutSize(c);
if (d != null) {
return new Dimension(d.width < prefWidth ? prefWidth : d.width, d.height < PREF_SIZE.height ? PREF_SIZE.height : d.height);
} else {
return new Dimension(prefWidth, PREF_SIZE.height);
}
}
/**
* Returns the minimum size of the <code>JFileChooser</code>.
*
* @param c a <code>JFileChooser</code>
* @return a <code>Dimension</code> specifying the minimum
* width and height of the file chooser
*/
public Dimension getMinimumSize(JComponent c) {
return MIN_SIZE;
}
/**
* Returns the maximum size of the <code>JFileChooser</code>.
*
* @param c a <code>JFileChooser</code>
* @return a <code>Dimension</code> specifying the maximum
* width and height of the file chooser
*/
public Dimension getMaximumSize(JComponent c) {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
void setFileSelected() {
if (getFileChooser().isMultiSelectionEnabled() && !isDirectorySelected()) {
File[] files = getFileChooser().getSelectedFiles(); // Should be selected
Object[] selectedObjects = list.getSelectedValues(); // Are actually selected
// Remove files that shouldn't be selected
for (int j = 0; j < selectedObjects.length; j++) {
boolean found = false;
for (int i = 0; i < files.length; i++) {
if (files[i].equals(selectedObjects[j])) {
found = true;
break;
}
}
if (!found) {
int index = getModel().indexOf(selectedObjects[j]);
if (index >= 0) {
listSelectionModel.removeSelectionInterval(index, index);
}
}
}
// Add files that should be selected
for (int i = 0; i < files.length; i++) {
boolean found = false;
for (int j = 0; j < selectedObjects.length; j++) {
if (files[i].equals(selectedObjects[j])) {
found = true;
break;
}
}
if (!found) {
int index = getModel().indexOf(files[i]);
if (index >= 0) {
listSelectionModel.addSelectionInterval(index, index);
}
}
}
} else {
JFileChooser chooser = getFileChooser();
File f = null;
if (isDirectorySelected()) {
f = getDirectory();
} else {
f = chooser.getSelectedFile();
}
int i;
if (f != null && (i = getModel().indexOf(f)) >= 0) {
listSelectionModel.setSelectionInterval(i, i);
ensureIndexIsVisible(i);
} else {
listSelectionModel.clearSelection();
}
}
}
private String fileNameString(File file) {
if (file == null) {
return null;
} else {
JFileChooser fc = getFileChooser();
if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) {
return file.getPath();
} else {
return file.getName();
}
}
}
private String fileNameString(File[] files) {
StringBuffer buf = new StringBuffer();
for (int i = 0; files != null && i < files.length; i++) {
if (i > 0) {
buf.append(" ");
}
if (files.length > 1) {
buf.append("\"");
}
buf.append(fileNameString(files[i]));
if (files.length > 1) {
buf.append("\"");
}
}
return buf.toString();
}
/* The following methods are used by the PropertyChange Listener */
private void doSelectedFileChanged(PropertyChangeEvent e) {
applyEdit();
File f = (File) e.getNewValue();
JFileChooser fc = getFileChooser();
if (f != null && ((fc.isFileSelectionEnabled() && !f.isDirectory()) || (f.isDirectory() && fc.isDirectorySelectionEnabled()))) {
setFileName(fileNameString(f));
setFileSelected();
}
}
private void doSelectedFilesChanged(PropertyChangeEvent e) {
applyEdit();
File[] files = (File[]) e.getNewValue();
JFileChooser fc = getFileChooser();
if (files != null && files.length > 0 && (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) {
setFileName(fileNameString(files));
setFileSelected();
}
}
private void doDirectoryChanged(PropertyChangeEvent e) {
JFileChooser fc = getFileChooser();
FileSystemView fsv = fc.getFileSystemView();
applyEdit();
resetEditIndex();
clearIconCache();
listSelectionModel.clearSelection();
ensureIndexIsVisible(0);
File currentDirectory = fc.getCurrentDirectory();
if (currentDirectory != null) {
directoryComboBoxModel.addItem(currentDirectory);
getNewFolderAction().setEnabled(currentDirectory.canWrite());
getChangeToParentDirectoryAction().setEnabled(!fsv.isRoot(currentDirectory));
if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) {
if (fsv.isFileSystem(currentDirectory)) {
setFileName(currentDirectory.getPath());
} else {
setFileName(null);
}
}
}
}
private void doFilterChanged(PropertyChangeEvent e) {
applyEdit();
resetEditIndex();
clearIconCache();
listSelectionModel.clearSelection();
}
private void doFileSelectionModeChanged(PropertyChangeEvent e) {
applyEdit();
resetEditIndex();
clearIconCache();
listSelectionModel.clearSelection();
JFileChooser fc = getFileChooser();
File currentDirectory = fc.getCurrentDirectory();
if (currentDirectory != null
&& fc.isDirectorySelectionEnabled()
&& !fc.isFileSelectionEnabled()
&& fc.getFileSystemView().isFileSystem(currentDirectory)) {
setFileName(currentDirectory.getPath());
} else {
setFileName(null);
}
}
private void doMultiSelectionChanged(PropertyChangeEvent e) {
if (getFileChooser().isMultiSelectionEnabled()) {
listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
} else {
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listSelectionModel.clearSelection();
getFileChooser().setSelectedFiles(null);
}
}
private void doAccessoryChanged(PropertyChangeEvent e) {
if (getAccessoryPanel() != null) {
if (e.getOldValue() != null) {
getAccessoryPanel().remove((JComponent) e.getOldValue());
}
JComponent accessory = (JComponent) e.getNewValue();
if (accessory != null) {
getAccessoryPanel().add(accessory, BorderLayout.CENTER);
}
}
}
private void doApproveButtonTextChanged(PropertyChangeEvent e) {
JFileChooser chooser = getFileChooser();
approveButton.setText(getApproveButtonText(chooser));
approveButton.setToolTipText(getApproveButtonToolTipText(chooser));
}
private void doDialogTypeChanged(PropertyChangeEvent e) {
JFileChooser chooser = getFileChooser();
approveButton.setText(getApproveButtonText(chooser));
approveButton.setToolTipText(getApproveButtonToolTipText(chooser));
if (chooser.getDialogType() == JFileChooser.SAVE_DIALOG) {
lookInLabel.setText(saveInLabelText);
} else {
lookInLabel.setText(lookInLabelText);
}
}
private void doApproveButtonMnemonicChanged(PropertyChangeEvent e) {
// Note: Metal does not use mnemonics for approve and cancel
}
private void doControlButtonsChanged(PropertyChangeEvent e) {
if (getFileChooser().getControlButtonsAreShown()) {
addControlButtons();
} else {
removeControlButtons();
}
}
/*
* Listen for filechooser property changes, such as
* the selected file changing, or the type of the dialog changing.
*/
public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) {
return new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String s = e.getPropertyName();
if (s.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
doSelectedFileChanged(e);
} else if (s.equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY)) {
doSelectedFilesChanged(e);
} else if (s.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) {
doDirectoryChanged(e);
} else if (s.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) {
doFilterChanged(e);
} else if (s.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) {
doFileSelectionModeChanged(e);
} else if (s.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY)) {
doMultiSelectionChanged(e);
} else if (s.equals(JFileChooser.ACCESSORY_CHANGED_PROPERTY)) {
doAccessoryChanged(e);
} else if (
s.equals(JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY)
|| s.equals(JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY)) {
doApproveButtonTextChanged(e);
} else if (s.equals(JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY)) {
doDialogTypeChanged(e);
} else if (s.equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY)) {
doApproveButtonMnemonicChanged(e);
} else if (s.equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY)) {
doControlButtonsChanged(e);
} else if (s.equals(JFileChooser.CANCEL_SELECTION)) {
applyEdit();
} else if (s.equals("componentOrientation")) {
ComponentOrientation o = (ComponentOrientation) e.getNewValue();
JFileChooser cc = (JFileChooser) e.getSource();
if (o != (ComponentOrientation) e.getOldValue()) {
cc.applyComponentOrientation(o);
}
if (detailsTable != null) {
detailsTable.setComponentOrientation(o);
detailsTable.getParent().getParent().setComponentOrientation(o);
}
} else if (s.equals("ancestor")) {
if (e.getOldValue() == null && e.getNewValue() != null) {
// Ancestor was added, set initial focus
fileNameTextField.selectAll();
fileNameTextField.requestFocus();
}
}
}
};
}
protected void removeControlButtons() {
getBottomPanel().remove(getButtonPanel());
}
protected void addControlButtons() {
getBottomPanel().add(getButtonPanel());
}
private void ensureIndexIsVisible(int i) {
if (i >= 0) {
list.ensureIndexIsVisible(i);
if (detailsTable != null) {
detailsTable.scrollRectToVisible(detailsTable.getCellRect(i, COLUMN_FILENAME, true));
}
}
}
public void ensureFileIsVisible(JFileChooser fc, File f) {
ensureIndexIsVisible(getModel().indexOf(f));
}
public void rescanCurrentDirectory(JFileChooser fc) {
getModel().validateFileCache();
}
public String getFileName() {
if (fileNameTextField != null) {
return fileNameTextField.getText();
} else {
return null;
}
}
public void setFileName(String filename) {
if (fileNameTextField != null) {
fileNameTextField.setText(filename);
}
}
/**
* Property to remember whether a directory is currently selected in the UI.
* This is normally called by the UI on a selection event.
*
* @param directorySelected if a directory is currently selected.
* @since 1.4
*/
protected void setDirectorySelected(boolean directorySelected) {
super.setDirectorySelected(directorySelected);
JFileChooser chooser = getFileChooser();
if (directorySelected) {
approveButton.setText(directoryOpenButtonText);
approveButton.setToolTipText(directoryOpenButtonToolTipText);
} else {
approveButton.setText(getApproveButtonText(chooser));
approveButton.setToolTipText(getApproveButtonToolTipText(chooser));
}
}
public String getDirectoryName() {
// PENDING(jeff) - get the name from the directory combobox
return null;
}
public void setDirectoryName(String dirname) {
// PENDING(jeff) - set the name in the directory combobox
}
protected DirectoryComboBoxRenderer createDirectoryComboBoxRenderer(JFileChooser fc) {
return new DirectoryComboBoxRenderer();
}
//
// Renderer for DirectoryComboBox
//
class DirectoryComboBoxRenderer extends DefaultListCellRenderer {
IndentIcon ii = new IndentIcon();
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value == null) {
setText("");
return this;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -