windowsfilechooserui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,088 行 · 第 1/5 页
JAVA
2,088 行
fnl.setLabelFor(filenameTextField); filenameTextField.addFocusListener( new FocusAdapter() { public void focusGained(FocusEvent e) { if (!getFileChooser().isMultiSelectionEnabled()) { listSelectionModel.clearSelection(); } } } ); if (fc.isMultiSelectionEnabled()) { setFileName(fileNameString(fc.getSelectedFiles())); } else { setFileName(fileNameString(fc.getSelectedFile())); } fileAndFilterPanel.add(filenameTextField); fileAndFilterPanel.add(Box.createRigidArea(vstrut8)); filterComboBoxModel = createFilterComboBoxModel(); fc.addPropertyChangeListener(filterComboBoxModel); filterComboBox = new JComboBox(filterComboBoxModel); ftl.setLabelFor(filterComboBox); filterComboBox.setRenderer(createFilterComboBoxRenderer()); fileAndFilterPanel.add(filterComboBox); getBottomPanel().add(fileAndFilterPanel); getBottomPanel().add(Box.createRigidArea(hstrut10)); // buttons getButtonPanel().setLayout(new BoxLayout(getButtonPanel(), BoxLayout.Y_AXIS)); approveButton = new JButton(getApproveButtonText(fc)) { public Dimension getMaximumSize() { return approveButton.getPreferredSize().width > cancelButton.getPreferredSize().width ? approveButton.getPreferredSize() : cancelButton.getPreferredSize(); } }; approveButton.setMnemonic(getApproveButtonMnemonic(fc)); approveButton.addActionListener(getApproveSelectionAction()); approveButton.setToolTipText(getApproveButtonToolTipText(fc)); getButtonPanel().add(Box.createRigidArea(vstrut4)); getButtonPanel().add(approveButton); getButtonPanel().add(Box.createRigidArea(vstrut6)); cancelButton = new JButton(cancelButtonText) { public Dimension getMaximumSize() { return approveButton.getPreferredSize().width > cancelButton.getPreferredSize().width ? approveButton.getPreferredSize() : cancelButton.getPreferredSize(); } }; cancelButton.setMnemonic(cancelButtonMnemonic); cancelButton.setToolTipText(cancelButtonToolTipText); cancelButton.addActionListener(getCancelSelectionAction()); getButtonPanel().add(cancelButton); if(fc.getControlButtonsAreShown()) { addControlButtons(); } } protected JPanel getButtonPanel() { if(buttonPanel == null) { buttonPanel = new JPanel(); } return buttonPanel; } protected JPanel getBottomPanel() { if(bottomPanel == null) { bottomPanel = new JPanel(); } return bottomPanel; } protected void installStrings(JFileChooser fc) { super.installStrings(fc); Locale l = fc.getLocale(); lookInLabelMnemonic = UIManager.getInt("FileChooser.lookInLabelMnemonic"); lookInLabelText = UIManager.getString("FileChooser.lookInLabelText",l); saveInLabelText = UIManager.getString("FileChooser.saveInLabelText",l); fileNameLabelMnemonic = UIManager.getInt("FileChooser.fileNameLabelMnemonic"); fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText",l); filesOfTypeLabelMnemonic = UIManager.getInt("FileChooser.filesOfTypeLabelMnemonic"); filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText",l); upFolderToolTipText = UIManager.getString("FileChooser.upFolderToolTipText",l); upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName",l); homeFolderToolTipText = UIManager.getString("FileChooser.homeFolderToolTipText",l); homeFolderAccessibleName = UIManager.getString("FileChooser.homeFolderAccessibleName",l); newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText",l); newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName",l); listViewButtonToolTipText = UIManager.getString("FileChooser.listViewButtonToolTipText",l); listViewButtonAccessibleName = UIManager.getString("FileChooser.listViewButtonAccessibleName",l); detailsViewButtonToolTipText = UIManager.getString("FileChooser.detailsViewButtonToolTipText",l); detailsViewButtonAccessibleName = UIManager.getString("FileChooser.detailsViewButtonAccessibleName",l); fileNameHeaderText = UIManager.getString("FileChooser.fileNameHeaderText",l); fileSizeHeaderText = UIManager.getString("FileChooser.fileSizeHeaderText",l); fileTypeHeaderText = UIManager.getString("FileChooser.fileTypeHeaderText",l); fileDateHeaderText = UIManager.getString("FileChooser.fileDateHeaderText",l); fileAttrHeaderText = UIManager.getString("FileChooser.fileAttrHeaderText",l); } protected void installListeners(JFileChooser fc) { super.installListeners(fc); ActionMap actionMap = getActionMap(); SwingUtilities.replaceUIActionMap(fc, actionMap); } protected ActionMap getActionMap() { return createActionMap(); } protected ActionMap createActionMap() { AbstractAction escAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { if (editFile != null) { cancelEdit(); } else { getFileChooser().cancelSelection(); } } public boolean isEnabled(){ return getFileChooser().isEnabled(); } }; ActionMap map = new ActionMapUIResource(); map.put("approveSelection", getApproveSelectionAction()); map.put("cancelSelection", escAction); map.put("Go Up", getChangeToParentDirectoryAction()); return map; } class ShortCutPanel extends JToolBar implements ActionListener { JToggleButton[] buttons; File[] files; XPStyle xp = XPStyle.getXP(); final Dimension buttonSize = new Dimension(83, (xp != null) ? 69 : 54); ShortCutPanel() { super(JToolBar.VERTICAL); setFloatable(false); putClientProperty("JToolBar.isRollover", Boolean.TRUE); if (xp != null) { putClientProperty("XPStyle.subClass", "placesbar"); setBorder(new EmptyBorder(1, 1, 1, 1)); } else { setBorder(listViewBorder); } Color bgColor = new Color(UIManager.getColor("ToolBar.shadow").getRGB()); setBackground(bgColor); JFileChooser chooser = getFileChooser(); FileSystemView fsv = chooser.getFileSystemView(); files = (File[])ShellFolder.get("fileChooserShortcutPanelFolders"); buttons = new JToggleButton[files.length]; ButtonGroup buttonGroup = new ButtonGroup(); for (int i = 0; i < files.length; i++) { if (fsv.isFileSystemRoot(files[i])) { // Create special File wrapper for drive path files[i] = fsv.createFileObject(files[i].getAbsolutePath()); } String folderName = fsv.getSystemDisplayName(files[i]); int index = folderName.lastIndexOf(File.separatorChar); if (index >= 0 && index < folderName.length() - 1) { folderName = folderName.substring(index + 1); } Icon icon = null; if (files[i] instanceof ShellFolder) { // We want a large icon, fsv only gives us a small. ShellFolder sf = (ShellFolder)files[i]; icon = new ImageIcon(sf.getIcon(true), sf.getFolderType()); } else { icon = fsv.getSystemIcon(files[i]); } buttons[i] = new JToggleButton(folderName, icon); if (xp != null) { buttons[i].setIconTextGap(2); buttons[i].setMargin(new Insets(2, 2, 2, 2)); buttons[i].setText("<html><center>"+folderName+"</center></html>"); } else { Color fgColor = new Color(UIManager.getColor("List.selectionForeground").getRGB()); buttons[i].setBackground(bgColor); buttons[i].setForeground(fgColor); } buttons[i].setHorizontalTextPosition(JToggleButton.CENTER); buttons[i].setVerticalTextPosition(JToggleButton.BOTTOM); buttons[i].setAlignmentX(JComponent.CENTER_ALIGNMENT); buttons[i].setPreferredSize(buttonSize); buttons[i].setMaximumSize(buttonSize); buttons[i].addActionListener(this); add(buttons[i]); if (i < files.length-1 && xp != null) { add(Box.createRigidArea(vstrut1)); } buttonGroup.add(buttons[i]); } doDirectoryChanged(chooser.getCurrentDirectory()); } void doDirectoryChanged(File f) { for (int i=0; i<buttons.length; i++) { buttons[i].setSelected(files[i].equals(f)); } } public void actionPerformed(ActionEvent e) { JToggleButton b = (JToggleButton)e.getSource(); for (int i=0; i<buttons.length; i++) { if (b == buttons[i]) { getFileChooser().setCurrentDirectory(files[i]); break; } } } public Dimension getPreferredSize() { Dimension min = super.getMinimumSize(); Dimension pref = super.getPreferredSize(); if (min.height > pref.height) { pref = new Dimension(pref.width, min.height); } return pref; } } // class ShortCutPanel private void updateListRowCount() { if (smallIconsView) { list.setVisibleRowCount(getModel().getSize() / 3); } else { list.setVisibleRowCount(-1); } } protected JPanel createList(JFileChooser fc) { JPanel p = new JPanel(new BorderLayout()); final JFileChooser fileChooser = fc; list = new JList() { public int getNextMatch(String prefix, int startIndex, Position.Bias bias) { ListModel model = getModel(); int max = model.getSize(); if (prefix == null || startIndex < 0 || startIndex >= max) { throw new IllegalArgumentException(); } // start search from the next element before/after the selected element boolean backwards = (bias == Position.Bias.Backward); for (int i = startIndex; backwards ? i >= 0 : i < max; i += (backwards ? -1 : 1)) { String filename = fileChooser.getName((File)model.getElementAt(i)); if (filename.regionMatches(true, 0, prefix, 0, prefix.length())) { return i; } } return -1; } }; list.setCellRenderer(new FileRenderer()); list.setLayoutOrientation(JList.VERTICAL_WRAP); updateListRowCount(); getModel().addListDataListener(new ListDataListener() { public void intervalAdded(ListDataEvent e) { updateListRowCount(); } public void intervalRemoved(ListDataEvent e) { updateListRowCount(); } public void contentsChanged(ListDataEvent e) { updateListRowCount(); } }); if (fc.isMultiSelectionEnabled()) { list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } else { list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } list.setModel(getModel()); list.addListSelectionListener(createListSelectionListener(fc)); list.addMouseListener(createDoubleClickListener(fc, list)); list.addMouseListener(createSingleClickListener(fc, list)); getModel().addListDataListener(new ListDataListener() { public void contentsChanged(ListDataEvent e) { // Update the selection after JList has been updated new DelayedSelectionUpdater(); } public void intervalAdded(ListDataEvent e) { int i0 = e.getIndex0(); int i1 = e.getIndex1(); if (i0 == i1) { File file = (File)getModel().getElementAt(i0); if (file.equals(newFolderFile)) { new DelayedSelectionUpdater(file); newFolderFile = null; } } } public void intervalRemoved(ListDataEvent e) { } }); JScrollPane scrollpane = new JScrollPane(list); XPStyle xp = XPStyle.getXP(); if (xp != null) { Color bg = xp.getColor("listview.fillcolor", null); if (bg != null) { list.setBackground(bg); } } if (listViewBorder != null) { scrollpane.setBorder(listViewBorder); } p.add(scrollpane, BorderLayout.CENTER); return p; } class DetailsTableModel extends AbstractTableModel implements ListDataListener { String[] columnNames = { fileNameHeaderText, fileSizeHeaderText, fileTypeHeaderText, fileDateHeaderText, fileAttrHeaderText }; JFileChooser chooser; ListModel listModel; DetailsTableModel(JFileChooser fc) { this.chooser = fc; listModel = getModel(); listModel.addListDataListener(this); } public int getRowCount() { return listModel.getSize(); } public int getColumnCount() { return COLUMN_COLCOUNT; } public String getColumnName(int column) { return columnNames[column]; } public Class getColumnClass(int column) { switch (column) { case COLUMN_FILENAME: return File.class; case COLUMN_FILEDATE: return Date.class; default: return super.getColumnClass(column); } } public Object getValueAt(int row, int col) { // Note: It is very important to avoid getting info on drives, as // this will trigger "No disk in A:" and similar dialogs. // // Use (f.exists() && !chooser.getFileSystemView().isFileSystemRoot(f)) to // determine if it is safe to call methods directly on f. File f = (File)listModel.getElementAt(row); switch (col) { case COLUMN_FILENAME: return f; case COLUMN_FILESIZE: if (!f.exists() || f.isDirectory()) { return null; } return (f.length() / 1024 + 1) + "KB"; case COLUMN_FILETYPE: if (!f.exists()) { return null; } return chooser.getFileSystemView().getSystemTypeDescription(f); case COLUMN_FILEDATE: if (!f.exists() || chooser.getFileSystemView().isFileSystemRoot(f)) { return null; } long time = f.lastModified(); return (time == 0L) ? null : new Date(time); case COLUMN_FILEATTR: if (!f.exists() || chooser.getFileSystemView().isFileSystemRoot(f)) { return null; } String attributes = ""; if (!f.canWrite()) { attributes += "R"; } if (f.isHidden()) { attributes += "H"; } return attributes; } return null; } public void setValueAt(Object value, int row, int col) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?