|
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 |
| package edu.rice.cs.util.swing; |
|
38 |
| |
|
39 |
| import javax.swing.*; |
|
40 |
| import java.awt.BorderLayout; |
|
41 |
| import java.awt.Dimension; |
|
42 |
| import java.awt.EventQueue; |
|
43 |
| import java.awt.FlowLayout; |
|
44 |
| import java.awt.Frame; |
|
45 |
| import java.awt.Toolkit; |
|
46 |
| import java.awt.event.*; |
|
47 |
| import java.util.ArrayList; |
|
48 |
| import java.util.List; |
|
49 |
| import java.util.Vector; |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| public class ScrollableListDialog<T> extends JDialog { |
|
65 |
| |
|
66 |
| private static final int DEFAULT_WIDTH = 400; |
|
67 |
| |
|
68 |
| private static final int DEFAULT_HEIGHT = 450; |
|
69 |
| |
|
70 |
| |
|
71 |
| private static final double WIDTH_RATIO = .75; |
|
72 |
| |
|
73 |
| private static final double HEIGHT_RATIO = .50; |
|
74 |
| |
|
75 |
| |
|
76 |
| protected final JList list; |
|
77 |
| |
|
78 |
| |
|
79 |
| protected int _buttonPressed = -1; |
|
80 |
| |
|
81 |
| |
|
82 |
| protected List<T> listItems; |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| public static class Builder<T> { |
|
87 |
| protected Frame _owner; |
|
88 |
| protected String _dialogTitle; |
|
89 |
| protected String _leaderText; |
|
90 |
| protected List<T> _listItems = new ArrayList<T>(); |
|
91 |
| protected List<T> _selectedItems = new ArrayList<T>(); |
|
92 |
| protected int _messageType = JOptionPane.PLAIN_MESSAGE; |
|
93 |
| protected int _width = DEFAULT_WIDTH; |
|
94 |
| protected int _height = DEFAULT_HEIGHT; |
|
95 |
| protected Icon _icon = null; |
|
96 |
| protected boolean _fitToScreen = true; |
|
97 |
| protected List<JButton> _buttons = new ArrayList<JButton>(); |
|
98 |
| protected List<JComponent> _additional = new ArrayList<JComponent>(); |
|
99 |
| protected boolean _selectable = false; |
|
100 |
9
| public Builder() { addOkButton(); }
|
|
101 |
9
| public Builder<T> setOwner(Frame owner) { _owner = owner; return this; }
|
|
102 |
9
| public Builder<T> setTitle(String dialogTitle) { _dialogTitle = dialogTitle; return this; }
|
|
103 |
9
| public Builder<T> setText(String leaderText) { _leaderText = leaderText; return this; }
|
|
104 |
9
| public Builder<T> setItems(List<T> listItems) { _listItems = listItems; return this; }
|
|
105 |
0
| public Builder<T> setSelectedItems(List<T> selItems) { _selectedItems = selItems; return this; }
|
|
106 |
7
| public Builder<T> setMessageType(int messageType) { _messageType = messageType; return this; }
|
|
107 |
0
| public Builder<T> setWidth(int width) { _width = width; return this; }
|
|
108 |
0
| public Builder<T> setHeight(int height) { _height = height; return this; }
|
|
109 |
0
| public Builder<T> setIcon(Icon icon) { _icon = icon; return this; }
|
|
110 |
0
| public Builder<T> setFitToScreen(boolean fts) { _fitToScreen = fts; return this; }
|
|
111 |
0
| public Builder<T> clearButtons() { _buttons.clear(); return this; }
|
|
112 |
9
| public Builder<T> addOkButton() { _buttons.add(new JButton("OK")); return this; }
|
|
113 |
0
| public Builder<T> addButton(JButton b) { _buttons.add(b); return this; }
|
|
114 |
0
| public Builder<T> addAdditionalComponent(JComponent c) { _additional.add(c); return this; }
|
|
115 |
0
| public Builder<T> setSelectable(boolean b) { _selectable = b; return this; }
|
|
116 |
9
| public ScrollableListDialog<T> build() {
|
|
117 |
9
| return new ScrollableListDialog<T>(_owner, _dialogTitle, _leaderText, _listItems, _selectedItems, _messageType,
|
|
118 |
| _width, _height, _icon, _fitToScreen, _buttons, _additional, _selectable); |
|
119 |
| } |
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
9
| private ScrollableListDialog(Frame owner, String dialogTitle, String leaderText, List<T> listItems, List<T> selItems,
|
|
153 |
| int messageType, int width, int height, Icon icon, boolean fitToScreen, |
|
154 |
| List<JButton> buttons, List<JComponent> additional, boolean selectable) { |
|
155 |
9
| super(owner, dialogTitle, true);
|
|
156 |
9
| this.listItems = listItems;
|
|
157 |
| |
|
158 |
9
| if (!_isknownMessageType(messageType)) {
|
|
159 |
2
| throw new IllegalArgumentException("The message type \"" + messageType + "\" is unknown");
|
|
160 |
| } |
|
161 |
| |
|
162 |
1
| if (listItems == null) throw new IllegalArgumentException("listItems cannot be null");
|
|
163 |
| |
|
164 |
| |
|
165 |
6
| JLabel dialogIconLabel = null;
|
|
166 |
6
| if (icon != null) {
|
|
167 |
0
| dialogIconLabel = new JLabel(icon);
|
|
168 |
| } |
|
169 |
| else { |
|
170 |
6
| Icon messageIcon = _getIcon(messageType);
|
|
171 |
4
| if (messageIcon != null) dialogIconLabel = new JLabel(messageIcon);
|
|
172 |
| } |
|
173 |
| |
|
174 |
6
| final JLabel leaderLabel = new JLabel(leaderText);
|
|
175 |
6
| final JPanel leaderPanel = new JPanel();
|
|
176 |
6
| leaderPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
|
|
177 |
4
| if (dialogIconLabel != null) leaderPanel.add(dialogIconLabel);
|
|
178 |
6
| leaderPanel.add(leaderLabel);
|
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
6
| final Vector<String> dataAsStrings = new Vector<String>(listItems.size());
|
|
183 |
| |
|
184 |
6
| String longestString = "";
|
|
185 |
6
| for (T obj : listItems) {
|
|
186 |
12
| if (obj != null) {
|
|
187 |
12
| final String objAsString = obj.toString();
|
|
188 |
| |
|
189 |
12
| if (objAsString.length() > longestString.length()) {
|
|
190 |
6
| longestString = objAsString;
|
|
191 |
| } |
|
192 |
12
| dataAsStrings.add(objAsString);
|
|
193 |
| } |
|
194 |
| } |
|
195 |
| |
|
196 |
6
| if (selectable) {
|
|
197 |
0
| final Vector<String> selAsStrings = new Vector<String>(selItems.size());
|
|
198 |
0
| for (T obj : selItems) {
|
|
199 |
0
| if (obj != null) {
|
|
200 |
0
| final String objAsString = obj.toString();
|
|
201 |
0
| selAsStrings.add(objAsString);
|
|
202 |
| } |
|
203 |
| } |
|
204 |
0
| list = new CheckBoxJList(dataAsStrings, selAsStrings);
|
|
205 |
| |
|
206 |
0
| list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
|
207 |
| } |
|
208 |
| else { |
|
209 |
6
| list = new JList(dataAsStrings);
|
|
210 |
| |
|
211 |
6
| list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
212 |
| } |
|
213 |
| |
|
214 |
6
| list.setPrototypeCellValue(longestString);
|
|
215 |
| |
|
216 |
| |
|
217 |
6
| final JScrollPane scrollPane = new JScrollPane(list);
|
|
218 |
| |
|
219 |
| |
|
220 |
6
| final JPanel buttonPanel = new JPanel();
|
|
221 |
6
| buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
|
|
222 |
| |
|
223 |
6
| _addButtons(buttonPanel, buttons);
|
|
224 |
6
| _addAdditionalComponents(buttonPanel, additional);
|
|
225 |
| |
|
226 |
| |
|
227 |
6
| final JPanel contentPanel = new JPanel();
|
|
228 |
6
| contentPanel.setLayout(new BorderLayout(10, 5));
|
|
229 |
6
| contentPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 0, 10));
|
|
230 |
| |
|
231 |
6
| contentPanel.add(leaderPanel, BorderLayout.NORTH);
|
|
232 |
6
| contentPanel.add(scrollPane, BorderLayout.CENTER);
|
|
233 |
6
| contentPanel.add(buttonPanel, BorderLayout.SOUTH);
|
|
234 |
| |
|
235 |
6
| getContentPane().add(contentPanel);
|
|
236 |
| |
|
237 |
| |
|
238 |
6
| Dimension dialogSize = new Dimension();
|
|
239 |
| |
|
240 |
6
| if (fitToScreen) {
|
|
241 |
| |
|
242 |
6
| Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
|
243 |
6
| int screenBasedWidth = (int) (WIDTH_RATIO * screenSize.getWidth());
|
|
244 |
6
| int screenBasedHeight = (int) (HEIGHT_RATIO * screenSize.getHeight());
|
|
245 |
| |
|
246 |
6
| dialogSize.setSize(Math.max(DEFAULT_WIDTH, screenBasedWidth),
|
|
247 |
| Math.max(DEFAULT_HEIGHT, screenBasedHeight)); |
|
248 |
| } else { |
|
249 |
| |
|
250 |
0
| dialogSize.setSize(width, height);
|
|
251 |
| } |
|
252 |
| |
|
253 |
6
| setSize(dialogSize);
|
|
254 |
| } |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
15
| private boolean _isknownMessageType(int messageType) {
|
|
264 |
15
| return messageType == JOptionPane.ERROR_MESSAGE ||
|
|
265 |
| messageType == JOptionPane.INFORMATION_MESSAGE || |
|
266 |
| messageType == JOptionPane.WARNING_MESSAGE || |
|
267 |
| messageType == JOptionPane.QUESTION_MESSAGE || |
|
268 |
| messageType == JOptionPane.PLAIN_MESSAGE; |
|
269 |
| } |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
6
| private Icon _getIcon(int messageType) {
|
|
280 |
6
| assert _isknownMessageType(messageType);
|
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
6
| if (messageType == JOptionPane.ERROR_MESSAGE) {
|
|
287 |
1
| return UIManager.getIcon("OptionPane.errorIcon");
|
|
288 |
5
| } else if (messageType == JOptionPane.INFORMATION_MESSAGE) {
|
|
289 |
1
| return UIManager.getIcon("OptionPane.informationIcon");
|
|
290 |
4
| } else if (messageType == JOptionPane.WARNING_MESSAGE) {
|
|
291 |
1
| return UIManager.getIcon("OptionPane.warningIcon");
|
|
292 |
3
| } else if (messageType == JOptionPane.QUESTION_MESSAGE) {
|
|
293 |
1
| return UIManager.getIcon("OptionPane.questionIcon");
|
|
294 |
2
| } else if (messageType == JOptionPane.PLAIN_MESSAGE) {
|
|
295 |
2
| return null;
|
|
296 |
| } else { |
|
297 |
| |
|
298 |
| assert false; |
|
299 |
| } |
|
300 |
| |
|
301 |
0
| return null;
|
|
302 |
| } |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
6
| protected void _addButtons(JPanel buttonPanel, List<JButton> buttons) {
|
|
317 |
6
| int i = 0;
|
|
318 |
6
| for (JButton b: buttons) {
|
|
319 |
6
| final int j = i++;
|
|
320 |
6
| b.addActionListener(new ActionListener() {
|
|
321 |
0
| public void actionPerformed(ActionEvent notUsed) {
|
|
322 |
0
| _buttonPressed = j;
|
|
323 |
0
| closeDialog();
|
|
324 |
| } |
|
325 |
| }); |
|
326 |
6
| buttonPanel.add(b);
|
|
327 |
| } |
|
328 |
| |
|
329 |
6
| getRootPane().setDefaultButton(buttons.get(0));
|
|
330 |
| } |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
6
| protected void _addAdditionalComponents(JPanel buttonPanel, List<JComponent> additional) {
|
|
338 |
6
| int i = 0;
|
|
339 |
6
| for (JComponent c: additional) {
|
|
340 |
0
| buttonPanel.add(c);
|
|
341 |
| } |
|
342 |
| } |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
0
| public void showDialog() {
|
|
348 |
0
| pack();
|
|
349 |
0
| Utilities.setPopupLoc(this, getOwner());
|
|
350 |
0
| setVisible(true);
|
|
351 |
| } |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
0
| protected void closeDialog() {
|
|
358 |
0
| setVisible(false);
|
|
359 |
| } |
|
360 |
| |
|
361 |
| |
|
362 |
0
| public int getButtonPressed() { return _buttonPressed; }
|
|
363 |
| |
|
364 |
| |
|
365 |
0
| public List<T> getSelectedItems() {
|
|
366 |
0
| ArrayList<T> l = new ArrayList<T>();
|
|
367 |
0
| for (int i: list.getSelectedIndices()) l.add(listItems.get(i));
|
|
368 |
| |
|
369 |
0
| return l;
|
|
370 |
| } |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
0
| public static void main(String args[]) {
|
|
376 |
0
| final List<String> data = new java.util.ArrayList<String>();
|
|
377 |
0
| data.add("how");
|
|
378 |
0
| data.add("now");
|
|
379 |
0
| data.add("brown");
|
|
380 |
0
| data.add("cow");
|
|
381 |
| |
|
382 |
0
| EventQueue.invokeLater(new Runnable() {
|
|
383 |
0
| public void run() {
|
|
384 |
0
| ScrollableListDialog<String> ld = new ScrollableListDialog.Builder<String>()
|
|
385 |
| .setOwner(null) |
|
386 |
| .setTitle("TITLE") |
|
387 |
| .setText("LEADER") |
|
388 |
| .setItems(data) |
|
389 |
| .setMessageType(JOptionPane.ERROR_MESSAGE) |
|
390 |
| .setSelectable(true) |
|
391 |
| .setSelectedItems(data.subList(0,2)) |
|
392 |
| .build(); |
|
393 |
0
| ld.pack();
|
|
394 |
0
| ld.setVisible(true);
|
|
395 |
| } |
|
396 |
| }); |
|
397 |
| } |
|
398 |
| } |
|
399 |
| |