package cologne.eck.all_peas.gui;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.accessibility.AccessibleContext;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class MultiFileSelectionDialog extends JDialog implements ActionListener {
private JPanel filePanel;
private static ArrayList <String> fileList;
public MultiFileSelectionDialog (Window parent, Point loc, String areaText,ArrayList <String> fileNames) {
super(parent);
this.setModalityType(JDialog.DEFAULT_MODALITY_TYPE);
JPanel contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel textPanel = new JPanel();
textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS));
JTextArea textArea = new JTextArea();
textArea.setText(areaText);
textPanel.add(textArea);
textPanel.add(Box.createHorizontalGlue());
textPanel.setMaximumSize(new Dimension(600,200));
textPanel.setMinimumSize(new Dimension(200,50));
contentPane.add(textPanel);
filePanel = new JPanel();
filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.Y_AXIS));
// alphabetical order:
Collections.sort(fileNames);
int len = fileNames.size();
for (int i = 0; i < len; i++) {
// don't show null file names
if (fileNames.get(i) == null) {
continue;
}
JCheckBox check = new JCheckBox(fileNames.get(i));
check.setSelected(true);
filePanel.add(check);
}
JScrollPane scrollPane = new JScrollPane(filePanel);
scrollPane.setPreferredSize(new Dimension(400, 200));
contentPane.add(scrollPane);
JPanel okPanel = new JPanel();
okPanel.setLayout(new BoxLayout(okPanel, BoxLayout.X_AXIS));
JButton okButton = new JButton("ok");
okButton.addActionListener(this);
okButton.setActionCommand("ok");
okPanel.add(Box.createHorizontalGlue());
okPanel.add(okButton);
contentPane.add(okPanel);
this.pack();
this.setLocation(loc);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ape) {
fileList = new ArrayList<String>();
String command = ape.getActionCommand();
if (command.equals("ok")) {
//System.out.println("ImageDisplay ok");
Component[] components = filePanel.getComponents();
int len = components.length;
for (int i = 0; i < len;i++) {
if (components[i] instanceof JCheckBox) {
if (( (JCheckBox)components[i]).isSelected() == true) {
if (command.equals("ok")) {
//System.out.println("ImageDisplay add: " + ((JCheckBox) components[i]).getText() );
fileList.add( ((JCheckBox) components[i]).getText() );
}
}
}
}
//System.out.println("dispose");
this.dispose();
}
}
/**
* Get a sorted list of selected file names.
*
* @return the list of selected file names
*/
public static ArrayList <String> getSelectedFiles() {
if (fileList.size() > 0) {
// return sorted file list
Collections.sort(fileList);
}
return fileList;
}
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleMultiFileSelectionDialog();
}
return accessibleContext;
}
protected class AccessibleMultiFileSelectionDialog extends AccessibleJDialog {
//Inherit everything, override nothing.
}
}