/Users/lyon/j4p/src/gui/dialogs/SelectorDialog.java
|
1 package gui.dialogs;
2
3 import gui.run.RunButton;
4 import gui.run.RunCheckBox;
5
6 import javax.swing.JDialog;
7 import javax.swing.JPanel;
8 import javax.swing.JScrollPane;
9 import javax.swing.BorderFactory;
10 import java.util.Vector;
11 import java.awt.Container;
12 import java.awt.GridLayout;
13 import java.awt.FlowLayout;
14 import java.awt.BorderLayout;
15
16 /**
17 * DocJava, Inc.
18 * http://www.docjava.com
19 * Programmer: dlyon
20 * Date: Sep 15, 2004
21 * Time: 5:14:32 PM
22 */
23 public class SelectorDialog {
24 private static RunCheckBox getRunCheckBox(String s) {
25 return new RunCheckBox(s) {
26 public void run() {
27
28 }
29 };
30 }
31
32 public static void markAll(RunCheckBox cb[]) {
33 for (int i = 0; i < cb.length; i++)
34 cb[i].setSelected(true);
35 }
36
37 public static void unmarkAll(RunCheckBox cb[]) {
38 for (int i = 0; i < cb.length; i++)
39 cb[i].setSelected(false);
40 }
41
42 private static RunCheckBox[] getRunCheckBoxes(String s[]) {
43 Vector v = new Vector();
44 for (int i = 0; i < s.length; i++)
45 v.addElement(getRunCheckBox(s[i]));
46 RunCheckBox checkBoxes[] = new RunCheckBox[v.size()];
47 v.copyInto(checkBoxes);
48 return checkBoxes;
49 }
50
51 public static String[] getStringSelectorDialog(Object oa[]){
52 String s[] = new String[oa.length];
53 for (int i=0; i < oa.length; i++)
54 s[i] = oa[i].toString();
55 return getStringSelectorDialog(s);
56 }
57
58 /**
59 * A string selector dialog presents a list of checkboxes
60 * for the user to select. This works atomically, blocking
61 * the invokers thread.
62 * @param selectStrings an array from which to choose
63 * @return a list of selected strings
64 */
65 public static String[] getStringSelectorDialog(String selectStrings[]) {
66 JDialog jd = new JDialog();
67 Container c = jd.getContentPane();
68 jd.setModal(true);
69 final RunCheckBox checkBoxes[] = getRunCheckBoxes(selectStrings);
70 JPanel checkBoxPanel = new JPanel();
71 for (int i = 0; i < checkBoxes.length; i++)
72 checkBoxPanel.add(checkBoxes[i]);
73 checkBoxPanel.setLayout(new GridLayout(0, 1));
74 JScrollPane jsp = new JScrollPane(checkBoxPanel);
75
76 JPanel buttonPanel = new JPanel();
77 buttonPanel.add(new RunButton("[Mark All") {
78 public void run() {
79 markAll(checkBoxes);
80 }
81 });
82 buttonPanel.add(new RunButton("[Unmark All") {
83 public void run() {
84 unmarkAll(checkBoxes);
85 }
86 });
87 buttonPanel.setLayout(new FlowLayout());
88 c.setLayout(new BorderLayout());
89 c.add(jsp, BorderLayout.CENTER);
90 c.add(buttonPanel, BorderLayout.SOUTH);
91 jsp.setBorder(BorderFactory.createEtchedBorder());
92 jd.setSize(200, 200);
93 jd.setVisible(true);
94 return getMarkedCheckBoxLabels(checkBoxes);
95 }
96
97 private static String[] getMarkedCheckBoxLabels(RunCheckBox rcb[]) {
98 Vector v = new Vector();
99 for (int i = 0; i < rcb.length; i++)
100 if (rcb[i].isSelected())
101 v.addElement(rcb[i].getText());
102 String s[] = new String[v.size()];
103 v.copyInto(s);
104 return s;
105 }
106
107 private static String[] getTestStrings() {
108 Vector v = new Vector();
109 for (int i = 0; i < 90; i++)
110 v.addElement("checkbox#" + i);
111 final String checkBoxes[] = new String[v.size()];
112 v.copyInto(checkBoxes);
113 return checkBoxes;
114 }
115
116 public static void main(String args[]) {
117 testStringSelectorDialog();
118 }
119
120 private static void testStringSelectorDialog() {
121 String[] testStrings = getTestStrings();
122 String[] s = getStringSelectorDialog(testStrings);
123
124 utils.Print.print(s);
125 }
126 }
127