📄 multifilechooser.java
字号:
curIndex = fileList.getSelectedIndex(); model.add(curIndex + 1, files[i]); fileList.setSelectedIndex(curIndex + 1); } if (i == 0) { strCurDir = files[i].getAbsolutePath(); strCurDir = new File(strCurDir).getParent(); } } } else { File f = chooser.getSelectedFile(); if (f != null) { int curIndex = fileList.getSelectedIndex(); model.add(curIndex + 1, f); fileList.setSelectedIndex(curIndex + 1); strCurDir = f.getAbsolutePath(); strCurDir = new File(strCurDir).getParent(); } } // store dir boolean isMedia = false; boolean isTemplate = false; FileFilter[] filters = chooser.getChoosableFileFilters(); for (int i = 0; i < filters.length; i++) { if (filters[i] == mediaFilter) { isMedia = true; break; } if (filters[i] == templateFilter) { isTemplate = true; break; } } if (isTemplate) { if (strCurDir != null) { Preferences.set("TemplateDir", strCurDir, null); } } else if (isMedia) { if (strCurDir != null) { Preferences.set("MediaDir", strCurDir, null); } } } /** * Removes the selected file from the selected files list. */ private void removeFile() { int index = fileList.getSelectedIndex(); if (index != -1) { model.removeElementAt(index); if (index >= model.getSize()) { index--; } fileList.setSelectedIndex(index); fileList.ensureIndexIsVisible(index); } } /** * Moves the selected file up in the list. */ private void moveUp() { int index = fileList.getSelectedIndex(); if (index > 0) { model.insertElementAt(model.remove(index), index - 1); fileList.setSelectedIndex(index - 1); fileList.ensureIndexIsVisible(index - 1); } } /** * Moves the selected file down in the list. */ private void moveDown() { int index = fileList.getSelectedIndex(); if ((index == -1) || (index == (model.getSize() - 1))) { return; } model.insertElementAt(model.remove(index), index + 1); fileList.setSelectedIndex(index + 1); fileList.ensureIndexIsVisible(index + 1); } /** * Sets the return value to <code>JFileChooser.APPROVE_OPTION</code> and * closes the dialog. Action after hitting the "OK" button. */ private void apply() { returnValue = JFileChooser.APPROVE_OPTION; if (dialog != null) { dialog.dispose(); } } /** * Sets the return value to <code>JFileChooser.CANCEL_OPTION</code> and * closes the dialog. Action after hitting the "Cancel" button. */ private void cancel() { returnValue = JFileChooser.CANCEL_OPTION; if (dialog != null) { dialog.dispose(); } } /** * Changes the active file filter to the media file filter (.mpg + .wav). */ private void setMediaFilter() { boolean isMedia = false; FileFilter[] filters = chooser.getChoosableFileFilters(); for (int i = 0; i < filters.length; i++) { if (filters[i] == mediaFilter) { isMedia = true; break; } } if (!isMedia) { File selectedFile = chooser.getSelectedFile(); if (selectedFile != null) { String strTemplateDir = selectedFile.getAbsolutePath(); strTemplateDir = (new File(strTemplateDir)).getParent(); Preferences.set("TemplateDir", strTemplateDir, null); } chooser.removeChoosableFileFilter(templateFilter); chooser.addChoosableFileFilter(mediaFilter); chooser.addChoosableFileFilter(mp4Filter); chooser.addChoosableFileFilter(qtFilter); chooser.setFileFilter(mediaFilter); String mediaDir = (String) Preferences.get("MediaDir", null); chooser.setCurrentDirectory(new File((mediaDir == null) ? Constants.USERHOME : mediaDir)); } } /** * Changes the active file filter to the template file filter (.etf). */ private void setTemplateFilter() { boolean isTemplate = false; FileFilter[] filters = chooser.getChoosableFileFilters(); for (int i = 0; i < filters.length; i++) { if (filters[i] == templateFilter) { isTemplate = true; break; } } if (!isTemplate) { File selectedFile = chooser.getSelectedFile(); if (selectedFile != null) { String strMediaDir = selectedFile.getAbsolutePath(); strMediaDir = (new File(strMediaDir)).getParent(); Preferences.set("MediaDir", strMediaDir, null); } chooser.removeChoosableFileFilter(mediaFilter); chooser.removeChoosableFileFilter(mp4Filter); chooser.removeChoosableFileFilter(qtFilter); chooser.addChoosableFileFilter(templateFilter); String templateDir = (String) Preferences.get("TemplateDir", null); chooser.setCurrentDirectory(new File((templateDir == null) ? Constants.USERHOME : templateDir)); } } /** * Action Listener implementation. * * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if (src == copyButton) { copyFile(); } else if (src == removeButton) { removeFile(); } else if (src == upButton) { moveUp(); } else if (src == downButton) { moveDown(); } else if (src == okButton) { apply(); } else if (src == cancelButton) { cancel(); } else if (src == chooser) { copyFile(); } else if (src == mediaRB) { setMediaFilter(); } else if (src == templateRB) { setTemplateFilter(); } else if (src == remoteButton) { addRemoteFile(); } } /** * Main method for testing purposes. * * @param args app arguments */ public static void main(String[] args) { /* try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception ex) { ex.printStackTrace(); } */ /* int v = new MultiFileChooser(MEDIA_TEMPLATE).showDialog(null, null); //System.out.println("Return: " + v); System.exit(0); */ } /** * Copied from JFileChooser. * * @param parent the parent component, usually a frame * @param approveButtonText the text for the "OK" or approve button * * @return JFileChooser.ERROR_OPTION, APPROVE_OPTION or CANCEL_OPTION * * @throws HeadlessException * * @see JFileChooser.showDialog(Component, String) */ public int showDialog(Component parent, String approveButtonText) throws HeadlessException { if (approveButtonText != null) { setApproveButtonText(approveButtonText); } dialog = createDialog(parent); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { returnValue = JFileChooser.CANCEL_OPTION; } }); returnValue = JFileChooser.ERROR_OPTION; dialog.show(); // blocks... dialog.dispose(); dialog = null; return returnValue; } /** * Copied from JFileChooser. Creates a configured dialog. * * @param parent the parent * * @return a dialog object * * @throws HeadlessException */ protected JDialog createDialog(Component parent) throws HeadlessException { Frame frame = (parent instanceof Frame) ? (Frame) parent : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent); JDialog dia = new ClosableDialog(frame, dialogTitle, true); Container contentPane = dia.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(this, BorderLayout.CENTER); dia.pack(); dia.setLocationRelativeTo(parent); return dia; } /** * Returns the current mode. * * @return the current mode, either <code>GENERIC</code> or * <code>MEDIA_TEMPLATE</code> */ public int getMode() { return mode; } /** * Sets the mode for the chooser dialog. * * @param mode the new mode, either <code>GENERIC</code> or * <code>MEDIA_TEMPLATE</code> */ public void setMode(int mode) { if (this.mode != mode) { this.mode = mode; if (mode == GENERIC) { selPanel.setVisible(false); } else { selPanel.setVisible(true); } } } /** * Returns the current directory. * * @return the current directory */ public File getCurrentDirectory() { return chooser.getCurrentDirectory(); } /** * Sets the directory for the chooser. * * @param dir the directory for the chooser */ public void setCurrentDirectory(File dir) { chooser.setCurrentDirectory(dir); } /** * Sets the text for the approve button. * * @param approveButtonText the text for the approve button */ public void setApproveButtonText(String approveButtonText) { okButton.setText(approveButtonText); } /** * Returns the current text of the approve button. * * @return the current approve button text */ public String getApproveButtonText() { return okButton.getText(); } /** * Sets the file filter. * * @param filter the file filter for the chooser */ public void setFileFilter(FileFilter filter) { chooser.setFileFilter(filter); } /** * Returns the current file filter * * @return the current file filter */ public FileFilter getFileFilter() { return chooser.getFileFilter(); } /** * Returns the set of file filters. * * @return the set of file filters */ public FileFilter[] getChoosableFileFilters() { return chooser.getChoosableFileFilters(); } /** * Adds a filter to the file filter. * * @param filter a file filter */ public void addChoosableFileFilter(FileFilter filter) { chooser.addChoosableFileFilter(filter); } /** * Removes the specified filter from the set of filters * * @param f the filter to remove * * @return true if the filter was part of the set and successfully removed */ public boolean removeChoosableFileFilter(FileFilter f) { return chooser.removeChoosableFileFilter(f); } /** * Removes all (choosable) file filters. */ public void resetChoosableFileFilters() { chooser.resetChoosableFileFilters(); } /** * Sets the file selection mode for the chooser! * * @param mode the file selection mode either JFileChooser.FILES_ONLY, * JFileChooser.DIRECTORIES_ONLY or * JFileChooser.FILES_AND_DIRECTORIES */ public void setFileSelectionMode(int mode) { chooser.setFileSelectionMode(mode); } /** * Returns the file selection mode of the chooser * * @return the file selection mode * * @see #setFileSelectionMode(int) */ public int getFileSelectionMode() { return chooser.getFileSelectionMode(); } /** * Returns whether files can be selected. * * @return true if files can be selected in the chooser * * @see #setFileSelectionMode * @see #getFileSelectionMode */ public boolean isFileSelectionEnabled() { return chooser.isFileSelectionEnabled(); } /** * Sets the title for the dialog. * * @param title the title */ public void setDialogTitle(String title) { dialogTitle = title; if (dialog != null) { dialog.setTitle(title); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -