gtkfilechooserui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 882 行 · 第 1/2 页

JAVA
882
字号
                          SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS, 0, 0));	leftPanel.add(createDirectoryList(), new GridBagConstraints(                          0, 1, 1, 1, 1, 1, GridBagConstraints.EAST,                          GridBagConstraints.BOTH,                          SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS, 0, 0));	// create files list	rightPanel = new JPanel(new GridBagLayout());	rightPanel.setName("GTKFileChooser.fileListPanel");	headerRenderer = new JTableHeader().getDefaultRenderer();	JComponent fileListLabel =	    (JComponent)headerRenderer.getTableCellRendererComponent(null, filesLabelText,								     false, false, 0, 0);	fileListLabel.setName("GTKFileChooser.fileListLabel");	rightPanel.add(fileListLabel, new GridBagConstraints(                          0, 0, 1, 1, 1, 0, GridBagConstraints.WEST,                          GridBagConstraints.HORIZONTAL,                          SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS,                          0, 0));	rightPanel.add(createFilesList(), new GridBagConstraints(                          0, 1, 1, 1, 1, 1, GridBagConstraints.EAST,                          GridBagConstraints.BOTH,                          SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS, 0, 0));	splitPanel.add(leftPanel,  leftToRight ? JSplitPane.LEFT : JSplitPane.RIGHT);	splitPanel.add(rightPanel, leftToRight ? JSplitPane.RIGHT : JSplitPane.LEFT);	centerPanel.add(splitPanel, BorderLayout.CENTER);	JComponent accessoryPanel = getAccessoryPanel();	JComponent accessory = fc.getAccessory();	if (accessoryPanel != null) {	    if (accessory == null) {		accessoryPanel.setPreferredSize(ZERO_ACC_SIZE);		accessoryPanel.setMaximumSize(ZERO_ACC_SIZE);	    } else {		getAccessoryPanel().add(accessory, BorderLayout.CENTER);		accessoryPanel.setPreferredSize(accessory.getPreferredSize());		accessoryPanel.setMaximumSize(MAX_SIZE);	    }	    align(accessoryPanel);	    centerPanel.add(accessoryPanel, BorderLayout.AFTER_LINE_ENDS);	}	interior.add(centerPanel);	interior.add(Box.createRigidArea(vstrut10));	JPanel pathFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,                                                          0, 0));        pathFieldPanel.setBorder(new EmptyBorder(0, 0, 4, 0));	JLabel pathFieldLabel = new JLabel(pathLabelText);	pathFieldLabel.setName("GTKFileChooser.pathFieldLabel");	pathFieldLabel.setDisplayedMnemonic(pathLabelMnemonic);	align(pathFieldLabel);	pathFieldPanel.add(pathFieldLabel);	File currentDirectory = fc.getCurrentDirectory();	String curDirName = null;	if (currentDirectory != null) {	    curDirName = currentDirectory.getPath();	}	pathField = new JLabel(curDirName) {	    public Dimension getMaximumSize() {		Dimension d = super.getMaximumSize();		d.height = getPreferredSize().height;		return d;	    }	};	pathField.setName("GTKFileChooser.pathField");	pathFieldLabel.setLabelFor(pathField);	align(pathField);	pathFieldPanel.add(pathField);	interior.add(pathFieldPanel);	// add the fileName field	fileNameTextField = new JTextField() {	    public boolean isManagingFocus() {		return true;	    }	    public Dimension getMaximumSize() {		Dimension d = super.getMaximumSize();		d.height = getPreferredSize().height;		return d;	    }	};	fileNameTextField.setName("GTKFileChooser.fileNameTextField");	fileNameTextField.getActionMap().put("fileNameCompletionAction", getFileNameCompletionAction());	fileNameTextField.getInputMap().put(KeyStroke.getKeyStroke('\t'), "fileNameCompletionAction");	interior.add(fileNameTextField);	// Add buttons	bottomButtonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));	bottomButtonPanel.setName("GTKFileChooser.bottomButtonPanel");	align(bottomButtonPanel);	JButton cancelButton = getCancelButton(fc);	align(cancelButton);	cancelButton.setMargin(buttonMargin);	bottomButtonPanel.add(cancelButton);	JButton approveButton = getApproveButton(fc);;	align(approveButton);	approveButton.setMargin(buttonMargin);	bottomButtonPanel.add(approveButton);	fc.add(bottomButtonPanel, BorderLayout.SOUTH);    }    protected void installStrings(JFileChooser fc) {	super.installStrings(fc);        Locale l = fc.getLocale();	newFolderButtonText = UIManager.getString("FileChooser.newFolderButtonText", l);	deleteFileButtonText = UIManager.getString("FileChooser.deleteFileButtonText", l);	renameFileButtonText = UIManager.getString("FileChooser.renameFileButtonText", l);	newFolderButtonMnemonic = UIManager.getInt("FileChooser.newFolderButtonMnemonic", l);	deleteFileButtonMnemonic = UIManager.getInt("FileChooser.deleteFileButtonMnemonic", l);	renameFileButtonMnemonic = UIManager.getInt("FileChooser.renameFileButtonMnemonic", l);	newFolderButtonToolTipText = UIManager.getString("FileChooser.newFolderButtonToolTipText", l);	deleteFileButtonToolTipText = UIManager.getString("FileChooser.deleteFileButtonToolTipText", l);	renameFileButtonToolTipText = UIManager.getString("FileChooser.renameFileButtonToolTipText", l);	foldersLabelText = UIManager.getString("FileChooser.foldersLabelText",l);	filesLabelText = UIManager.getString("FileChooser.filesLabelText",l);		pathLabelText = UIManager.getString("FileChooser.pathLabelText",l);	pathLabelMnemonic = UIManager.getInt("FileChooser.pathLabelMnemonic");     }    protected void uninstallStrings(JFileChooser fc) {	super.uninstallStrings(fc);	newFolderButtonText = null;	deleteFileButtonText = null;	renameFileButtonText = null;	newFolderButtonToolTipText = null;	deleteFileButtonToolTipText = null;	renameFileButtonToolTipText = null;	foldersLabelText = null;	filesLabelText = null;		pathLabelText = null;    }    protected JScrollPane createFilesList() {	fileList = new JList();	fileList.setName("GTKFileChooser.fileList");	if (getFileChooser().isMultiSelectionEnabled()) {	    fileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);	} else {	    fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);	}		fileList.setModel(new GTKFileListModel());	fileList.setCellRenderer(new FileCellRenderer());	fileList.addListSelectionListener(createListSelectionListener(getFileChooser()));	fileList.addMouseListener(createDoubleClickListener(getFileChooser(), fileList));	align(fileList);	JScrollPane scrollpane = new JScrollPane(fileList);	scrollpane.setName("GTKFileChooser.fileListScrollPane");	scrollpane.setPreferredSize(prefListSize);	scrollpane.setMaximumSize(MAX_SIZE);	align(scrollpane);	return scrollpane;    }    protected JScrollPane createDirectoryList() {	directoryList = new JList();	directoryList.setName("GTKFileChooser.directoryList");	align(directoryList);	directoryList.setCellRenderer(new DirectoryCellRenderer());	directoryList.setModel(new GTKDirectoryListModel());	directoryList.addMouseListener(createDoubleClickListener(getFileChooser(), directoryList));	directoryList.addListSelectionListener(createListSelectionListener(getFileChooser()));	JScrollPane scrollpane = new JScrollPane(directoryList);	scrollpane.setName("GTKFileChooser.directoryListScrollPane");	scrollpane.setMaximumSize(MAX_SIZE);	scrollpane.setPreferredSize(prefListSize);	align(scrollpane);	return scrollpane;    }    protected class GTKDirectoryListModel extends AbstractListModel implements ListDataListener {	public GTKDirectoryListModel() {	    getModel().addListDataListener(this);	}	public int getSize() {	    return getModel().getDirectories().size();	}	public Object getElementAt(int index) {	    return getModel().getDirectories().elementAt(index);	}	public void intervalAdded(ListDataEvent e) {	}	// PENDING - implement	public void intervalRemoved(ListDataEvent e) {	}	// PENDING - this is inefficient - should sent out	// incremental adjustment values instead of saying that the	// whole list has changed.	public void fireContentsChanged() {	    fireContentsChanged(this, 0, getModel().getDirectories().size()-1);	}	// PENDING - fire the correct interval changed - currently sending	// out that everything has changed	public void contentsChanged(ListDataEvent e) {	    fireContentsChanged();	}    }    protected class GTKFileListModel extends AbstractListModel implements ListDataListener {	public GTKFileListModel() {	    getModel().addListDataListener(this);	}	public int getSize() {	    return getModel().getFiles().size();	}	public boolean contains(Object o) {	    return getModel().getFiles().contains(o);	}	public int indexOf(Object o) {	    return getModel().getFiles().indexOf(o);	}	public Object getElementAt(int index) {	    return getModel().getFiles().elementAt(index);	}	public void intervalAdded(ListDataEvent e) {	}	// PENDING - implement	public void intervalRemoved(ListDataEvent e) {	}	// PENDING - this is inefficient - should sent out	// incremental adjustment values instead of saying that the	// whole list has changed.	public void fireContentsChanged() {	    fireContentsChanged(this, 0, getModel().getFiles().size()-1);	}	// PENDING - fire the interval changed	public void contentsChanged(ListDataEvent e) {	    fireContentsChanged();	}    }    protected class FileCellRenderer extends DefaultListCellRenderer  {	public Component getListCellRendererComponent(JList list, Object value, int index,						      boolean isSelected, boolean cellHasFocus) {	    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);	    setText(getFileChooser().getName((File) value));	    return this;	}    }    protected class DirectoryCellRenderer extends DefaultListCellRenderer  {	public Component getListCellRendererComponent(JList list, Object value, int index,						      boolean isSelected, boolean cellHasFocus) {	    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);	    setText(getFileChooser().getName((File) value) + "/");	    return this;	}    }    public Dimension getPreferredSize(JComponent c) {	Dimension prefSize = new Dimension(PREF_SIZE);	JComponent accessory = getFileChooser().getAccessory();	if (accessory != null) {	    prefSize.width += accessory.getPreferredSize().width + 20;	}	Dimension d = c.getLayout().preferredLayoutSize(c);	if (d != null) {	    return new Dimension(d.width < prefSize.width ? prefSize.width : d.width,				 d.height < prefSize.height ? prefSize.height : d.height);	} else {	    return prefSize;	}    }    public Dimension getMinimumSize(JComponent x)  {	return new Dimension(MIN_SIZE);    }    public Dimension getMaximumSize(JComponent x) {	return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);    }    protected void align(JComponent c) {	c.setAlignmentX(JComponent.LEFT_ALIGNMENT);	c.setAlignmentY(JComponent.TOP_ALIGNMENT);    }    protected DirectoryComboBoxRenderer createDirectoryComboBoxRenderer(JFileChooser fc) {	return new DirectoryComboBoxRenderer();    }    //    // Renderer for DirectoryComboBox    //    class DirectoryComboBoxRenderer extends SynthComboBoxUI.SynthComboBoxRenderer  {	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;	    }	    setText(((File)value).getAbsolutePath());	    return this;	}    }    //    // 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();	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;	    }            int oldSize = directories.size();	    directories.clear();            if (oldSize > 0) {                fireIntervalRemoved(this, 0, oldSize);            }	    // 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 = fsv.createFileObject(directory.getCanonicalPath());	    } 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	    File f = canonical;	    do {		directories.add(f);	    } while ((f = f.getParentFile()) != null);            int newSize = directories.size();            if (newSize > 0) {                fireIntervalAdded(this, 0, newSize);            }	    setSelectedItem(canonical);	}	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);	}    }    /**     * 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);	}    }}

⌨️ 快捷键说明

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