📄 rtextfilechooser.java
字号:
/*****************************************************************************/
/**
* Returns the color used to paint hidden files if hidden files are being
* shown.
*
* @return The color used to paint hidden file names.
* @see #setHiddenFileColor
*/
public Color getHiddenFileColor() {
return hiddenFileColor;
}
/*****************************************************************************/
/**
* Creates the icons used by this file chooser. This method can be
* called when the user changes the Look and Feel, and icons used in this
* file chooser will change to reflect the new Look and Feel.
*/
public void getIcons() {
// Icons for buttons at the top. Use pretty, homemade defaults
// if any of these aren't found.
newFolderIcon = UIManager.getIcon("FileChooser.newFolderIcon");
upFolderIcon = UIManager.getIcon("FileChooser.upFolderIcon");
//homeFolderIcon = UIManager.getIcon("FileChooser.homeFolderIcon");
detailsViewIcon = UIManager.getIcon("FileChooser.detailsViewIcon");
listViewIcon = UIManager.getIcon("FileChooser.listViewIcon");
String path = "org/fife/ui/rtextfilechooser/images/";
if (newFolderIcon==null)
newFolderIcon = new ImageIcon(cl.findResource(path + "createnewdirectory.gif"));
if (upFolderIcon==null)
upFolderIcon = new ImageIcon(cl.findResource(path + "uponelevel.gif"));
if (detailsViewIcon==null)
detailsViewIcon = new ImageIcon(cl.findResource(path + "detailsview.gif"));
if (listViewIcon==null)
listViewIcon = new ImageIcon(cl.findResource(path + "listview.gif"));
iconsViewIcon = new ImageIcon(cl.findResource(path + "listview.gif"));
}
/*****************************************************************************/
/**
* Returns whether or not the current OS is case-insensitive (e.g.,
* is Windows or OS X).
*
* @return Whether or not the underlying OS is case-insensitive.
*/
private static final boolean getIgnoreCase() {
String os = System.getProperty("os.name");
boolean ignoreCase = false;
if (os!=null) {
os = os.toLowerCase();
ignoreCase = os.indexOf("windows")>-1 ||
os.indexOf("mac os x")>-1;
}
return ignoreCase;
}
/*****************************************************************************/
/**
* Returns the mnemonic for the given key.
*
* @param key The key.
* @return The mnemonic.
*/
private int getMnemonic(String key) {
Object value = UIManager.get(key);
if (value == null)
return 0;
else if (value instanceof Integer) {
return ((Integer)value).intValue();
}
else if (value instanceof String) {
try {
return Integer.parseInt((String)value);
} catch (NumberFormatException nfe) { }
}
return 0;
}
/*****************************************************************************/
/**
* Returns the name of the file.
*
* @param f The file for which you want its name.
* @return The file's name.
*/
public String getName(File f) {
String name = fileSystemView.getSystemDisplayName(f);
if (name==null || name.equals(""))
name = f.getAbsolutePath();
return name;
}
/*****************************************************************************/
/**
* Returns the file selected by the user. Note that if the user
* selected more than one file, only the first file selected is
* returned.
*
* @return The file selected by the user.
* @see #getSelectedFiles
* @see #setSelectedFile
* @see #setSelectedFiles
*/
public File getSelectedFile() {
return selectedFiles[0];
}
/*****************************************************************************/
/**
* Returns the files selected by the user.
*
* @return The files selected by the user.
* @see #getSelectedFile
* @see #setSelectedFile
* @see #setSelectedFiles
*/
public File[] getSelectedFiles() {
return (File[]) selectedFiles.clone();
}
/*****************************************************************************/
/**
* Returns whether hidden files and directories are shown by the file
* chooser.
*
* @return Whether hidden files are displayed.
* @see #setShowHiddenFiles
*/
public boolean getShowHiddenFiles() {
return showHiddenFiles;
}
/*****************************************************************************/
/**
* Returns a string to use as the tool tip for a file in a view. This
* tool tip will be localized according to the current locale.
*
* @param file The file for which to get the tool tip.
* @return The tool tip.
*/
final String getToolTipFor(File file) {
return "<html><body> " + nameString + file.getName() +
"<br> " + lastModifiedString +
Utilities.getLastModifiedString(file.lastModified()) +
"<br> " + sizeString + getFileSizeStringFor(file) +
"</body></html>";
}
/*****************************************************************************/
/**
* Attempts to delete the files selected in the view.
*/
void handleDelete() {
// Get the selected files. If there are no selected files (i.e.,
// they pressed "delete" when no files were selected), beep.
File[] files = view.getSelectedFiles();
if (files==null || files.length==0) {
UIManager.getLookAndFeel().provideErrorFeedback(this);
return;
}
// Prompt to confirm the file deletion.
int count = files.length;
int choice;
if (count==1) {
String fileName = files[0].getName();
choice = JOptionPane.showConfirmDialog(this,
deleteConfirmPrompt + fileName + "?");
}
else { // count>1
choice = JOptionPane.showConfirmDialog(this,
deleteMultipleConfirmPrompt);
}
// If they chose "yes," delete the files.
if (choice==JOptionPane.YES_OPTION) {
for (int i=0; i<count; i++) {
if (!files[i].delete()) {
Object[] arguments = { files[i].getName() };
String msg = MessageFormat.format(deleteFailText,
arguments);
JOptionPane.showMessageDialog(this,
msg, errorDialogTitle,
JOptionPane.ERROR_MESSAGE);
}
}
refreshView();
}
}
/*****************************************************************************/
/**
* Gets the strings used on the various file chooser widgets.
*/
protected void installStrings() {
ResourceBundle msg = ResourceBundle.getBundle(
"org.fife.ui.rtextfilechooser.FileChooser");
saveButtonText = UIManager.getString("FileChooser.saveButtonText");
openButtonText = UIManager.getString("FileChooser.openButtonText");
saveDialogTitleText = UIManager.getString("FileChooser.saveDialogTitleText");
openDialogTitleText = UIManager.getString("FileChooser.openDialogTitleText");
cancelButtonText = UIManager.getString("FileChooser.cancelButtonText");
saveButtonMnemonic = getMnemonic("FileChooser.saveButtonMnemonic");
openButtonMnemonic = getMnemonic("FileChooser.openButtonMnemonic");
cancelButtonMnemonic = getMnemonic("FileChooser.cancelButtonMnemonic");
saveButtonToolTipText = UIManager.getString("FileChooser.saveButtonToolTipText");
openButtonToolTipText = UIManager.getString("FileChooser.openButtonToolTipText");
cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText");
lookInLabel.setText(msg.getString("LookInLabel"));
upOneLevelButton.setToolTipText(msg.getString("UpOneLevelTTT"));
newFolderButton.setToolTipText(msg.getString("NewFolderTTT"));
listMenuItem.setText(msg.getString("ListViewTTT"));
detailsMenuItem.setText(msg.getString("DetailsViewTTT"));
iconsMenuItem.setText(msg.getString("IconsViewTTT"));
fileNameLabel.setText(msg.getString("FileNameLabel"));
filterLabel.setText(msg.getString("FilterLabel"));
encodingLabel.setText(msg.getString("EncodingLabel"));
// Populate the combo box with all available encodings.
Map availcs = Charset.availableCharsets();
Set keys = availcs.keySet();
for (Iterator i=keys.iterator(); i.hasNext(); )
encodingComboBox.addItem((String)i.next());
newFolderPrompt = msg.getString("NewFolderPrompt");
errorNewDirPrompt = msg.getString("ErrorNewDirPrompt");
errorDialogTitle = msg.getString("Error");
newNamePrompt = msg.getString("NewNamePrompt");
renameFailText = msg.getString("RenameFailText");
renameErrorMessage = msg.getString("RenameErrorMessage");
deleteConfirmPrompt = msg.getString("DeleteConfirmPrompt");
deleteMultipleConfirmPrompt = msg.getString("DeleteMultipleConfirmPrompt");
deleteFailText = msg.getString("DeleteFailText");
invalidFileNameMessage = msg.getString("InvalidFileName");
directoryText = msg.getString("Directory"); // "Directory"
fileText = msg.getString("File"); // "File"
nameString = msg.getString("Name");
sizeString = msg.getString("Size");
typeString = msg.getString("Type");
statusString = msg.getString("Status");
lastModifiedString = msg.getString("LastModified");
readString = msg.getString("Read");
writeString = msg.getString("Write");
readWriteString = msg.getString("ReadWrite");
}
/*****************************************************************************/
/**
* Returns whether or not this filename contains wildcards, depending on
* the OS on which we're running.
*
* @param filename The filename to check for wildcards.
* @return Whether or not the file name contained wildcards.
*/
private static boolean isGlobPattern(String filename) {
return ((File.separatorChar == '\\' && filename.indexOf('*') >= 0)
|| (File.separatorChar == '/' && (filename.indexOf('*') >= 0
|| filename.indexOf('?') >= 0
|| filename.indexOf('[') >= 0)));
}
/*****************************************************************************/
/**
* Returns whether or not multi-selection is enabled.
*
* @return Whether or not multi-selection is enabled.
* @see #setMultiSelectionEnabled
*/
public final boolean isMultiSelectionEnabled() {
return multiSelectionEnabled;
}
/*****************************************************************************/
/**
* Returns whether or not a file should be underlined when displayed by
* a view of this file chooser.
*
* @param file A file.
* @return Whether or not <code>file</code> should be underlined by the
* view.
*/
public boolean isUnderlinedFile(File file) {
int num = underlinedFiles!=null ? underlinedFiles.length : 0;
for (int i=0; i<num; i++)
if (file.equals(underlinedFiles[i]))
return true;
return false;
}
/*****************************************************************************/
/**
* Ensures that the "File Filter" combo box contains all file filters
* known by this file dialog.
*/
private void populatefilterComboBox() {
filterComboBox.removeAllItems();
int max = fileFilters.size();
for (int i=0; i<max; i++)
filterComboBox.addItem(fileFilters.get(i));
}
/*****************************************************************************/
/**
* Populates the "Look in" combo box with proper values for the current
* directory.
*/
private void populateLookInComboBox() {
// Add all of the roots.
lookInComboBox.removeAllItems();
RootManager rm = RootManager.getInstance();
int j = 1;
for (Iterator i=rm.iterator(); i.hasNext(); ) {
File root = (File)i.next();
lookInComboBox.addItem(root);
// If the "current directory" is under this root, add all
// dirs in the path leading up to it.
File currentDirectoryRoot = rm.getRootForFile(currentDirectory);
if (currentDirectoryRoot.equals(root)) {
File temp = currentDirectory;
// We check for null also because, for example, in Windows,
// the "Desktop" folder is considered a root (and has no
// parent), but our loop finds the "root" to be C:\.
while (temp!=null && !temp.equals(currentDirectoryRoot)) {
lookInComboBox.insertItemAt(temp, j);
temp = temp.getParentFile();
}
}
j++;
}
lookInComboBox.setSelectedItem(currentDirectory);
lookInRenderer.recalculateDepths();
}
/*****************************************************************************/
/**
* If the GUI has been initialized, this sets the encoding combo box to
* display the proper value.
*/
protected void refreshEncodingComboBox() {
if (!guiInitialized)
return;
Charset cs1 = Charset.forName(encoding);
int count = encodingComboBox.getItemCount();
for (int i=0; i<count; i++) {
String item = (String)encodingComboBox.getItemAt(i);
Charset cs2 = Charset.forName(item);
if (cs1.equals(cs2)) {
encodingComboBox.setSelectedIndex(i);
return;
}
}
// Encoding not found: select default.
String defaultEncoding = getDefaultEncoding();
cs1 = Charset.forName(defaultEncoding);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -