Download this file

MultiFileSelectionDialog.java    121 lines (109 with data), 3.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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.
}
}