metalfilechooserui.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 2,081 行 · 第 1/4 页

JAVA
2,081
字号
     * existing list, and repopulates it with the new selected directory     * and all its parent directories.     *      * @param selectedDirectory  the selected directory.     */    public void setSelectedItem(Object selectedDirectory)    {      items.clear();      FileSystemView fsv = getFileChooser().getFileSystemView();      File parent = (File) selectedDirectory;      while (parent != null)        {          items.add(0, parent);          parent = fsv.getParentDirectory(parent);        }      selectedIndex = items.indexOf(selectedDirectory);      fireContentsChanged(this, 0, items.size() - 1);    }      }  /**   * Handles changes to the selection in the directory combo box.   */  protected class DirectoryComboBoxAction    extends AbstractAction  {    /**     * Creates a new action.     */    protected DirectoryComboBoxAction()    {      // Nothing to do here.    }        /**     * Handles the action event.     *      * @param e  the event.     */    public void actionPerformed(ActionEvent e)    {      JFileChooser fc = getFileChooser();      fc.setCurrentDirectory((File) directoryModel.getSelectedItem());    }  }  /**   * A renderer for the items in the directory combo box.   */  class DirectoryComboBoxRenderer    extends DefaultListCellRenderer  {    /**     * Creates a new renderer.     */    public DirectoryComboBoxRenderer(JFileChooser fc)    {     }        /**     * Returns a component that can be used to paint the given value within      * the list.     *      * @param list  the list.     * @param value  the value (a {@link File}).     * @param index  the item index.     * @param isSelected  is the item selected?     * @param cellHasFocus  does the list cell have focus?     *      * @return The list cell renderer.     */    public Component getListCellRendererComponent(JList list, Object value,        int index, boolean isSelected, boolean cellHasFocus)    {      FileView fileView = getFileView(getFileChooser());      File file = (File) value;      setIcon(fileView.getIcon(file));      setText(fileView.getName(file));            if (isSelected)        {          setBackground(list.getSelectionBackground());          setForeground(list.getSelectionForeground());        }      else        {          setBackground(list.getBackground());          setForeground(list.getForeground());        }      setEnabled(list.isEnabled());      setFont(list.getFont());      return this;    }  }  /**   * A renderer for the files and directories in the file chooser.   */  protected class FileRenderer    extends DefaultListCellRenderer  {        /**     * Creates a new renderer.     */    protected FileRenderer()    {      // Nothing to do here.    }        /**     * Returns a component that can render the specified value.     *      * @param list  the list.     * @param value  the value (a {@link File}).     * @param index  the index.     * @param isSelected  is the item selected?     * @param cellHasFocus  does the item have the focus?     *      * @return The renderer.     */    public Component getListCellRendererComponent(JList list, Object value,        int index, boolean isSelected, boolean cellHasFocus)    {      FileView v = getFileView(getFileChooser());      File f = (File) value;      if (f != null)	{	  setText(v.getName(f));	  setIcon(v.getIcon(f));	}      else	{	  setText("");	  setIcon(null);	}      setOpaque(true);      if (isSelected)        {          setBackground(list.getSelectionBackground());          setForeground(list.getSelectionForeground());        }      else        {          setBackground(list.getBackground());          setForeground(list.getForeground());        }      setEnabled(list.isEnabled());      setFont(list.getFont());      if (cellHasFocus)        setBorder(UIManager.getBorder("List.focusCellHighlightBorder"));      else        setBorder(noFocusBorder);      return this;    }  }  /**   * A combo box model for the file selection filters.   */  protected class FilterComboBoxModel    extends AbstractListModel    implements ComboBoxModel, PropertyChangeListener  {    /** Storage for the filters in the model. */    protected FileFilter[] filters;    /** The index of the selected file filter. */    private Object selected;        /**     * Creates a new model.     */    protected FilterComboBoxModel()    {      filters = new FileFilter[1];      filters[0] = getAcceptAllFileFilter(getFileChooser());      selected = filters[0];    }        /**     * Handles property changes.     *      * @param e  the property change event.     */    public void propertyChange(PropertyChangeEvent e)    {      if (e.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY))        {          JFileChooser fc = getFileChooser();          FileFilter[] choosableFilters = fc.getChoosableFileFilters();          filters = choosableFilters;          fireContentsChanged(this, 0, filters.length);          selected = e.getNewValue();          fireContentsChanged(this, -1, -1);        }      else if (e.getPropertyName().equals(              JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY))        {          // repopulate list          JFileChooser fc = getFileChooser();          FileFilter[] choosableFilters = fc.getChoosableFileFilters();          filters = choosableFilters;          fireContentsChanged(this, 0, filters.length);        }    }        /**     * Sets the selected filter.     *      * @param filter  the filter (<code>null</code> ignored).     */    public void setSelectedItem(Object filter)    {      if (filter != null)      {          selected = filter;          fireContentsChanged(this, -1, -1);      }    }        /**     * Returns the selected file filter.     *      * @return The selected file filter.     */    public Object getSelectedItem()    {      return selected;    }        /**     * Returns the number of items in the model.     *      * @return The number of items in the model.     */    public int getSize()    {      return filters.length;    }        /**     * Returns the item at the specified index.     *      * @param index  the item index.     *      * @return The item at the specified index.     */    public Object getElementAt(int index)    {      return filters[index];    }      }  /**   * A renderer for the items in the file filter combo box.   */  public class FilterComboBoxRenderer    extends DefaultListCellRenderer  {    /**     * Creates a new renderer.     */    public FilterComboBoxRenderer()    {      // Nothing to do here.    }        /**     * Returns a component that can be used to paint the given value within      * the list.     *      * @param list  the list.     * @param value  the value (a {@link FileFilter}).     * @param index  the item index.     * @param isSelected  is the item selected?     * @param cellHasFocus  does the list cell have focus?     *      * @return This component as the renderer.     */    public Component getListCellRendererComponent(JList list, Object value,        int index, boolean isSelected, boolean cellHasFocus)    {      super.getListCellRendererComponent(list, value, index, isSelected,                                          cellHasFocus);      FileFilter filter = (FileFilter) value;      setText(filter.getDescription());      return this;    }  }  /**   * A listener for selection events in the file list.   *    * @see #createListSelectionListener(JFileChooser)   */  class MetalFileChooserSelectionListener     implements ListSelectionListener  {    /**     * Creates a new <code>SelectionListener</code> object.     */    protected MetalFileChooserSelectionListener()    {      // Do nothing here.    }    /**     * Makes changes to different properties when     * a value has changed in the filechooser's selection.     *     * @param e - the list selection event that occured.     */    public void valueChanged(ListSelectionEvent e)    {      File f = (File) fileList.getSelectedValue();      if (f == null)        return;      JFileChooser filechooser = getFileChooser();      if (! filechooser.isTraversable(f))        filechooser.setSelectedFile(f);      else        filechooser.setSelectedFile(null);    }  }  /**   * A mouse listener for the {@link JFileChooser}.   * This listener is used for editing filenames.   */  protected class SingleClickListener    extends MouseAdapter  {        /** Stores instance of the list */    JList list;        /**      * Stores the current file that is being edited.     * It is null if nothing is currently being edited.     */    File editFile;        /** The current file chooser. */    JFileChooser fc;        /** The last file selected. */    Object lastSelected;        /** The textfield used for editing. */    JTextField editField;        /**     * Creates a new listener.     *      * @param list  the directory/file list.     */    public SingleClickListener(JList list)    {      this.list = list;      editFile = null;      fc = getFileChooser();      lastSelected = null;      startEditing = false;    }        /**     * Receives notification of a mouse click event.     *      * @param e  the event.     */    public void mouseClicked(MouseEvent e)    {      if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1)        {          int index = list.locationToIndex(e.getPoint());          File[] sf = fc.getSelectedFiles();          if ((!fc.isMultiSelectionEnabled() || (sf != null && sf.length <= 1))              && index >= 0 && !startEditing && list.isSelectedIndex(index))            {              Object tmp = list.getModel().getElementAt(index);              if (lastSelected != null && lastSelected.equals(tmp))                editFile(index);              lastSelected = tmp;            }          else              completeEditing();        }      else        completeEditing();    }        /**     * Sets up the text editor for the current file.     *      * @param index -     *          the current index of the item in the list to be edited.     */    void editFile(int index)    {      Rectangle bounds = list.getCellBounds(index, index);      list.scrollRectToVisible(bounds);      editFile = (File) list.getModel().getElementAt(index);      if (editFile.canWrite())        {          startEditing = true;          editField = new JTextField(editFile.getName());          editField.addActionListener(new EditingActionListener());                    Icon icon = getFileView(fc).getIcon(editFile);          if (icon != null)            {              int padding = icon.getIconWidth() + 4;              bounds.x += padding;              bounds.width -= padding;            }          editField.setBounds(bounds);                    list.add(editField);                    editField.requestFocus();          editField.selectAll();        }      else        completeEditing();      list.repaint();    }        /**      * Completes the editing.     */    void completeEditing()    {      if (editField != null && editFile != null)        {          String text = editField.getText();          if (text != null && text != "" && !text.equals(fc.getName(editFile)))              if (editFile.renameTo                  (fc.getFileSystemView().createFileObject                   (fc.getCurrentDirectory(), text)))                  rescanCurrentDirectory(fc);          list.remove(editField);        }      startEditing = false;      editFile = null;      lastSelected = null;      editField = null;      list.repaint();    }        /**     * ActionListener for the editing text field.     */    class EditingActionListener implements ActionListener    {            /**       * This method is invoked when an action occurs.       *        * @param e -       *          the <code>ActionEvent</code> that occurred       */      public void actionPerformed(ActionEvent e)      {        if (e.getActionCommand().equals("notify-field-accept"))          completeEditing();        else if (editField != null)          {            list.remove(editField);            startEditing = false;            editFile = null;            lastSelected = null;            editField = null;            list.repaint();          }      }    }  }  /**   * A mouse listener for the {@link JFileChooser}.   * This listener is used for the table   */  private class TableClickListener extends MouseAdapter  {    /** Stores instance of the table */    JTable table;    /** Stores instance of the file chooser */    JFileChooser fc;    /** The last selected file. */    Object lastSelected = null;        /**      * Stores the current file that is being edited.     * It is null if nothing is currently being edited.     */    File editFile;        /** The textfield used for editing. */    JTextField editField;    /**     * Creates a new listener.     *      * @param table     *          the directory/file table     * @param fc     *          the JFileChooser     */    public TableClickListener(JTable table, JFileChooser fc)    {      this.table = table;

⌨️ 快捷键说明

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