📄 multifilechooser.java
字号:
/* * File: MultiFileChooser.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.eudico.client.annotator.gui;import mpi.eudico.client.annotator.Constants;import mpi.eudico.client.annotator.ElanLocale;import mpi.eudico.client.annotator.Preferences;import mpi.eudico.client.annotator.util.*;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Frame;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.HeadlessException;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import javax.swing.ButtonGroup;import javax.swing.DefaultListModel;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.ListSelectionModel;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.WindowConstants;import javax.swing.border.CompoundBorder;import javax.swing.border.EmptyBorder;import javax.swing.border.LineBorder;import javax.swing.border.TitledBorder;import javax.swing.filechooser.FileFilter;/** * A custom Multiple File Chooser dialog that does not use the * <code>setAccessory</code> mechanism of JFileChooser. This mechanism is not * L&F independent, forcing the Metal L&F on all platforms. This chooser adds * a JFileChooser and other ui elements to a JComponent in what should be a * L&F independent way. * * @author Han Sloetjes * @version 1.0 july 2005 */public class MultiFileChooser extends JComponent implements ActionListener { /** generic multiple file chooser */ public static final int GENERIC = 0; /** * a multiple file chooser that let the user switch between media file * filtering and template file filtering */ public static final int MEDIA_TEMPLATE = 1; private JFileChooser chooser; private JList fileList; private DefaultListModel model; private JPanel selPanel; private JRadioButton mediaRB; private JRadioButton templateRB; private JButton remoteButton; private JButton okButton; private JButton cancelButton; private JButton copyButton; private JButton removeButton; private JButton upButton; private JButton downButton; private String dialogTitle; private JDialog dialog; private FileFilter mediaFilter; private FileFilter templateFilter; private FileFilter qtFilter; private FileFilter mp4Filter; private int returnValue = JFileChooser.ERROR_OPTION; private int mode = MEDIA_TEMPLATE; /** * Creates a new MultiFileChooser instance with the default selection * mode, <code>GENERIC</code> */ public MultiFileChooser() { this(GENERIC); } /** * Creates a new MultiFileChooser instance * * @param mode the mode, either <code>GENERIC</code> or * <code>MEDIA_TEMPLATE</code> */ public MultiFileChooser(int mode) { if ((mode >= GENERIC) && (mode <= MEDIA_TEMPLATE)) { this.mode = mode; } initComponents(); } private void initComponents() { mediaRB = new JRadioButton(); templateRB = new JRadioButton(); remoteButton = new JButton(); okButton = new JButton(); cancelButton = new JButton(); removeButton = new JButton(); upButton = new JButton(); downButton = new JButton(); copyButton = new JButton(); ImageIcon REMOVE_ICON = new ImageIcon(this.getClass().getResource("/mpi/eudico/client/annotator/resources/Remove.gif")); ImageIcon UP_ICON = new ImageIcon(this.getClass().getResource("/mpi/eudico/client/annotator/resources/Up.gif")); ImageIcon DOWN_ICON = new ImageIcon(this.getClass().getResource("/mpi/eudico/client/annotator/resources/Down.gif")); removeButton.setIcon(REMOVE_ICON); upButton.setIcon(UP_ICON); downButton.setIcon(DOWN_ICON); this.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); Insets insets = new Insets(2, 2, 2, 2); chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); chooser.setControlButtonsAreShown(false); chooser.setPreferredSize(new Dimension((int) chooser.getPreferredSize() .getWidth() - 80, (int) chooser.getPreferredSize().getHeight())); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.insets = insets; this.add(chooser, gbc); JPanel midPanel = new JPanel(new GridBagLayout()); copyButton.setText(" >> "); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.CENTER; gbc.insets = new Insets(4, 6, 4, 6); midPanel.add(copyButton, gbc); selPanel = new JPanel(new GridBagLayout()); selPanel.setBorder(new TitledBorder(ElanLocale.getString( "Frame.ElanFrame.NewDialog.RadioFileType"))); ButtonGroup bg = new ButtonGroup(); mediaRB.setSelected(true); mediaRB.setText(ElanLocale.getString( "Frame.ElanFrame.NewDialog.RadioButtonMedia")); templateRB.setText(ElanLocale.getString( "Frame.ElanFrame.NewDialog.RadioButtonTemplate")); bg.add(mediaRB); bg.add(templateRB); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.insets = insets; selPanel.add(mediaRB, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.insets = insets; selPanel.add(templateRB, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.anchor = GridBagConstraints.SOUTH; gbc.insets = new Insets(4, 6, 4, 6); midPanel.add(selPanel, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.anchor = GridBagConstraints.NORTH; //gbc.insets = insets; gbc.fill = GridBagConstraints.VERTICAL; gbc.weighty = 1.0; this.add(midPanel, gbc); JPanel rightPanel = new JPanel(new GridBagLayout()); JPanel infoPanel = new JPanel(new GridBagLayout()); Dimension dim = new Dimension(70, 40); infoPanel.setPreferredSize(dim); infoPanel.setMinimumSize(dim); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.CENTER; gbc.insets = insets; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.weighty = 1.0; infoPanel.add(new JLabel(ElanLocale.getString( "Frame.ElanFrame.NewDialog.Selected"))); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.insets = insets; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.weightx = 1.0; rightPanel.add(infoPanel, gbc); model = new DefaultListModel(); fileList = new JList(model); fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane jsp = new JScrollPane(fileList); jsp.setPreferredSize(new Dimension(jsp.getPreferredSize().getSize().width - 30, jsp.getPreferredSize().getSize().height)); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.insets = insets; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1.0; gbc.weighty = 1.0; rightPanel.add(jsp, gbc); JPanel controlPanel = new JPanel(new GridLayout(1, 3, 6, 6)); controlPanel.add(removeButton); controlPanel.add(upButton); controlPanel.add(downButton); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 2; gbc.insets = insets; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.weightx = 1.0; rightPanel.add(controlPanel, gbc); remoteButton.setText(ElanLocale.getString( "Frame.ElanFrame.NewDialog.RemoteMedia")); remoteButton.addActionListener(this); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 3; gbc.anchor = GridBagConstraints.NORTH; gbc.insets = new Insets(7, 2, 3, 2); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.weightx = 1.0; rightPanel.add(remoteButton, gbc); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 0; gbc.anchor = GridBagConstraints.NORTH; gbc.insets = insets; gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1.0; gbc.weighty = 1.0; this.add(rightPanel, gbc); JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 6, 6)); okButton.setText(ElanLocale.getString("Button.OK")); cancelButton.setText(ElanLocale.getString("Button.Cancel")); buttonPanel.add(okButton); buttonPanel.add(cancelButton); JPanel butBorderPanel = new JPanel(new GridBagLayout()); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.weightx = 1.0; butBorderPanel.add(new JPanel(), gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.anchor = GridBagConstraints.EAST; gbc.insets = insets; butBorderPanel.add(buttonPanel, gbc); butBorderPanel.setBorder(new CompoundBorder( new LineBorder(Color.GRAY, 1), new EmptyBorder(6, 6, 6, 6))); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 3; gbc.anchor = GridBagConstraints.CENTER; gbc.insets = insets; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.weightx = 1.0; this.add(butBorderPanel, gbc); chooser.addActionListener(this); copyButton.addActionListener(this); removeButton.addActionListener(this); upButton.addActionListener(this); downButton.addActionListener(this); okButton.addActionListener(this); cancelButton.addActionListener(this); mediaRB.addActionListener(this); templateRB.addActionListener(this); if (mode == GENERIC) { selPanel.setVisible(false); remoteButton.setVisible(false); } else { //start with media file filter mediaFilter = ElanFileFilter.createFileFilter(ElanFileFilter.MEDIA_TYPE); templateFilter = ElanFileFilter.createFileFilter(ElanFileFilter.TEMPLATE_TYPE); qtFilter = ElanFileFilter.createFileFilter(ElanFileFilter.QT_TYPE); mp4Filter = ElanFileFilter.createFileFilter(ElanFileFilter.MP4_TYPE); 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)); } } /** * Returns an array of file objects that the user has selected. * * @return an array of file objects */ public Object[] getFiles() { Object[] obj = new Object[model.getSize()]; for (int i = 0; i < obj.length; i++) { obj[i] = model.getElementAt(i); } return obj; } /** * Sets a list of files and/or directories in the multiple file box. * * @param files the files that should be added to the selected files list */ public void setFiles(File[] files) { model.clear(); for (int i = 0; i < files.length; i++) { model.insertElementAt(files[i], i); } fileList.setSelectedIndex(0); fileList.ensureIndexIsVisible(0); } /** * Clears the list of files. */ public void clearList() { model.clear(); } /** * Shows an input pane where the user can enter the url of a (rtsp) streaming file. * The file is added to the list. */ private void addRemoteFile() { Object rf = JOptionPane.showInputDialog(this, ElanLocale.getString("Frame.ElanFrame.NewDialog.RemoteLabel"), ElanLocale.getString("Frame.ElanFrame.NewDialog.RemoteMedia"), JOptionPane.PLAIN_MESSAGE, null, null, "rtsp://"); if (rf == null) { return; } String url = (String) rf; url.replace('\\', '/'); // try some simple repairs boolean valid = true; if (!url.startsWith("rtsp")) { int ds = url.indexOf("//"); if (ds > -1) { url = "rtsp:" + url.substring(ds); } else { url = "rtsp://" + url; } } if (url.indexOf("://") != 4) { valid = false; // do not even try to repair } int dot = url.lastIndexOf('.'); int slash = url.lastIndexOf('/'); if ((dot < 0) || (dot < slash) || ((dot > slash) && (slash <= 7))) { valid = false; } // no use trying to check the string as an URL (doesn't know the rtsp protocol) // or URI (accepts almost any string) if (!valid) { JOptionPane.showMessageDialog(this, ElanLocale.getString("Frame.ElanFrame.NewDialog.RemoteMessage") + url, ElanLocale.getString("Message.Error"), JOptionPane.ERROR_MESSAGE); addRemoteFile(); } else { int curIndex = fileList.getSelectedIndex(); model.add(curIndex + 1, url); fileList.setSelectedIndex(curIndex + 1); } } /** * Copies the selected files from the File chooser to the user list. */ private void copyFile() { String strCurDir = null; if (chooser.isMultiSelectionEnabled()) { File[] files = chooser.getSelectedFiles(); int curIndex; for (int i = 0; i < files.length; i++) { if (!model.contains(files[i])) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -