⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metalfilechooserui.java

📁 Mobile 应用程序使用 Java Micro Edition (Java ME) 平台
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    // 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 = getFileChooser().getIcon(directory);	    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 = ShellFolder.getNormalizedFile(directory);	    } 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 = useShellFolder ? ShellFolder.getShellFolder(canonical)					 : 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);		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;	    }	}    }    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) {            directoryComboBox.hidePopup();	    File f = (File)directoryComboBox.getSelectedItem();            if (!getFileChooser().getCurrentDirectory().equals(f)) {	        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) {	    if (c != null) {		Component[] children = c.getComponents();		if (children != null && children.length > 0) {		    int       numChildren = children.length;		    int       height = 0;		    Insets    cInsets = c.getInsets();		    int       extraHeight = topMargin + cInsets.top + cInsets.bottom;		    int       extraWidth = cInsets.left + cInsets.right;		    int       maxWidth = 0;		    for (int counter = 0; counter < numChildren; counter++) {			Dimension aSize = children[counter].getPreferredSize();			height = Math.max(height, aSize.height);			maxWidth = Math.max(maxWidth, aSize.width);		    }		    return new Dimension(extraWidth + numChildren * maxWidth + 					 (numChildren - 1) * hGap,					 extraHeight + height);		}	    }	    return new Dimension(0, 0);	}	public Dimension preferredLayoutSize(Container c) {	    return minimumLayoutSize(c);	}	public void removeLayoutComponent(Component c) { }    }    private static void groupLabels(AlignedLabel[] group) {	for (int i = 0; i < group.length; i++) {	    group[i].group = group;	}    }    private class AlignedLabel extends JLabel {	private AlignedLabel[] group;	private int maxWidth = 0;	AlignedLabel(String text) {	    super(text);	    setAlignmentX(JComponent.LEFT_ALIGNMENT);	}	public Dimension getPreferredSize() {	    Dimension d = super.getPreferredSize();	    // Align the width with all other labels in group.	    return new Dimension(getMaxWidth() + 11, d.height);	}	private int getMaxWidth() {	    if (maxWidth == 0 && group != null) {		int max = 0;		for (int i = 0; i < group.length; i++) {		    max = Math.max(group[i].getSuperPreferredWidth(), max);		}		for (int i = 0; i < group.length; i++) {		    group[i].maxWidth = max;		}	    }	    return maxWidth;	}	private int getSuperPreferredWidth() {	    return super.getPreferredSize().width;	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -