📄 filechooserui.java
字号:
/** Returns the file name of the selected file, or null if
* the user has not entered or selected a file.
*/
public String getFileName()
{
if (fileNameTextField != null)
{
return fileNameTextField.getText();
}
else
{
return null;
}
}
/** Sets the file name in the file name text field */
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));
}
}
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;
}
File directory= (File) value;
setText(getFileChooser().getName(directory));
Icon icon= FileChooserUI.getIcon(directory, fileIcon, getFileChooser().getFileSystemView());
ii.icon= icon;
ii.depth= directoryComboBoxModel.getDepth(index);
setIcon(ii);
return this;
}
}
final static int space= 10;
class IndentIcon implements Icon
{
Icon icon= null;
int depth= 0;
public void paintIcon(Component c, Graphics g, int x, int y)
{
if (c.getComponentOrientation().isLeftToRight())
{
icon.paintIcon(c, g, x + depth * space, y);
}
else
{
icon.paintIcon(c, g, x, y);
}
}
public int getIconWidth()
{
return icon.getIconWidth() + depth * space;
}
public int getIconHeight()
{
return icon.getIconHeight();
}
}
//
// DataModel for DirectoryComboxbox
//
protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc)
{
return new DirectoryComboBoxModel();
}
/**
* Data model for a type-face selection combo-box.
*/
protected class DirectoryComboBoxModel
extends AbstractListModel
implements ComboBoxModel
{
Vector directories= new Vector();
int[] depths= null;
File selectedDirectory= null;
JFileChooser chooser= getFileChooser();
FileSystemView fsv= chooser.getFileSystemView();
public DirectoryComboBoxModel()
{
// Add the current directory to the model, and make it the
// selectedDirectory
File dir= getFileChooser().getCurrentDirectory();
if (dir != null)
{
addItem(dir);
}
}
/**
* Adds the directory to the model and sets it to be selected,
* additionally clears out the previous selected directory and
* the paths leading up to it, if any.
*/
private void addItem(File directory)
{
if (directory == null)
{
return;
}
directories.clear();
File[] baseFolders;
if (useShellFolder)
{
baseFolders= (File[]) ShellFolder.get("fileChooserComboBoxFolders");
}
else
{
baseFolders= fsv.getRoots();
}
directories.addAll(Arrays.asList(baseFolders));
// Get the canonical (full) path. This has the side
// benefit of removing extraneous chars from the path,
// for example /foo/bar/ becomes /foo/bar
File canonical= null;
try
{
canonical= directory.getCanonicalFile();
}
catch (IOException e)
{
// Maybe drive is not ready. Can't abort here.
canonical= directory;
}
// create File instances of each directory leading up to the top
try
{
File sf= ShellFolder.getShellFolder(canonical);
File f= sf;
Vector path= new Vector(10);
do
{
path.addElement(f);
}
while ((f= f.getParentFile()) != null);
int pathCount= path.size();
// Insert chain at appropriate place in vector
for (int i= 0; i < pathCount; i++)
{
f= (File) path.get(i);
if (directories.contains(f))
{
int topIndex= directories.indexOf(f);
for (int j= i - 1; j >= 0; j--)
{
directories.insertElementAt(path.get(j), topIndex + i - j);
}
break;
}
}
calculateDepths();
setSelectedItem(sf);
}
catch (FileNotFoundException ex)
{
calculateDepths();
}
}
private void calculateDepths()
{
depths= new int[directories.size()];
for (int i= 0; i < depths.length; i++)
{
File dir= (File) directories.get(i);
File parent= dir.getParentFile();
depths[i]= 0;
if (parent != null)
{
for (int j= i - 1; j >= 0; j--)
{
if (parent.equals((File) directories.get(j)))
{
depths[i]= depths[j] + 1;
break;
}
}
}
}
}
public int getDepth(int i)
{
return (depths != null && i >= 0 && i < depths.length) ? depths[i] : 0;
}
public void setSelectedItem(Object selectedDirectory)
{
this.selectedDirectory= (File) selectedDirectory;
fireContentsChanged(this, -1, -1);
}
public Object getSelectedItem()
{
return selectedDirectory;
}
public int getSize()
{
return directories.size();
}
public Object getElementAt(int index)
{
return directories.elementAt(index);
}
}
//
// Renderer for Types ComboBox
//
protected FilterComboBoxRenderer createFilterComboBoxRenderer()
{
return new FilterComboBoxRenderer();
}
/**
* Render different type sizes and styles.
*/
public class FilterComboBoxRenderer extends DefaultListCellRenderer
{
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
super.getListCellRendererComponent(
list,
value,
index,
isSelected,
cellHasFocus);
if (value != null && value instanceof FileFilter)
{
setText(((FileFilter) value).getDescription());
}
return this;
}
}
//
// DataModel for Types Comboxbox
//
protected FilterComboBoxModel createFilterComboBoxModel()
{
return new FilterComboBoxModel();
}
/**
* Data model for a type-face selection combo-box.
*/
protected class FilterComboBoxModel
extends AbstractListModel
implements ComboBoxModel, PropertyChangeListener
{
protected FileFilter[] filters;
protected FilterComboBoxModel()
{
super();
filters= getFileChooser().getChoosableFileFilters();
}
public void propertyChange(PropertyChangeEvent e)
{
String prop= e.getPropertyName();
if (prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY)
{
filters= (FileFilter[]) e.getNewValue();
fireContentsChanged(this, -1, -1);
}
else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY)
{
fireContentsChanged(this, -1, -1);
}
}
public void setSelectedItem(Object filter)
{
if (filter != null)
{
getFileChooser().setFileFilter((FileFilter) filter);
setFileName(null);
fireContentsChanged(this, -1, -1);
}
}
public Object getSelectedItem()
{
// Ensure that the current filter is in the list.
// NOTE: we shouldnt' have to do this, since JFileChooser adds
// the filter to the choosable filters list when the filter
// is set. Lets be paranoid just in case someone overrides
// setFileFilter in JFileChooser.
FileFilter currentFilter= getFileChooser().getFileFilter();
boolean found= false;
if (currentFilter != null)
{
for (int i= 0; i < filters.length; i++)
{
if (filters[i] == currentFilter)
{
found= true;
}
}
if (found == false)
{
getFileChooser().addChoosableFileFilter(currentFilter);
}
}
return getFileChooser().getFileFilter();
}
public int getSize()
{
if (filters != null)
{
return filters.length;
}
else
{
return 0;
}
}
public Object getElementAt(int index)
{
if (index > getSize() - 1)
{
// This shouldn't happen. Try to recover gracefully.
return getFileChooser().getFileFilter();
}
if (filters != null)
{
return filters[index];
}
else
{
return null;
}
}
}
/** Informs this instance that the selection has changed. */
public void valueChanged(ListSelectionEvent e)
{
JFileChooser fc= getFileChooser();
File f= fc.getSelectedFile();
if (!e.getValueIsAdjusting()
&& f != null
&& !getFileChooser().isTraversable(f))
{
setFileName(fileNameString(f));
}
}
/**
* Acts when DirectoryComboBox has changed the selected item.
*/
protected class DirectoryComboBoxAction extends AbstractAction
{
protected DirectoryComboBoxAction()
{
super("DirectoryComboBoxAction");
}
public void actionPerformed(ActionEvent e)
{
File f= (File) directoryComboBox.getSelectedItem();
getFileChooser().setCurrentDirectory(f);
}
}
protected JButton getApproveButton(JFileChooser fc)
{
return approveButton;
}
/**
* <code>ButtonAreaLayout</code> behaves in a similar manner to
* <code>FlowLayout</code>. It lays out all components from left to
* right, flushed right. The widths of all components will be set
* to the largest preferred size width.
*/
private static class ButtonAreaLayout implements LayoutManager
{
private int hGap= 5;
private int topMargin= 17;
public void addLayoutComponent(String string, Component comp)
{
}
public void layoutContainer(Container container)
{
Component[] children= container.getComponents();
if (children != null && children.length > 0)
{
int numChildren= children.length;
Dimension[] sizes= new Dimension[numChildren];
Insets insets= container.getInsets();
int yLocation= insets.top + topMargin;
int maxWidth= 0;
for (int counter= 0; counter < numChildren; counter++)
{
sizes[counter]= children[counter].getPreferredSize();
maxWidth= Math.max(maxWidth, sizes[counter].width);
}
int xLocation, xOffset;
if (container.getComponentOrientation().isLeftToRight())
{
xLocation= container.getSize().width - insets.left - maxWidth;
xOffset= hGap + maxWidth;
}
else
{
xLocation= insets.left;
xOffset= - (hGap + maxWidth);
}
for (int counter= numChildren - 1; counter >= 0; counter--)
{
children[counter].setBounds(
xLocation,
yLocation,
maxWidth,
sizes[counter].height);
xLocation -= xOffset;
}
}
}
public Dimension minimumLayoutSize(Container c)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -