KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Browser > ListDialog


1 /*
2  * ListDialog.java
3  *
4  * Created on 11. květen 2004, 14:20
5  */

6
7 package SOFA.SOFAnet.Browser;
8
9 import javax.swing.*;
10 import java.awt.*;
11 import java.awt.event.*;
12
13 /*
14  * ListDialog.java is a 1.4 class meant to be used by programs such as
15  * ListDialogRunner. It requires no additional files.
16  *
17  * Modified by Ladislav Sobr.
18  */

19
20 /**
21  * Use this modal dialog to let the user choose one string from a long
22  * list. See ListDialogRunner.java for an example of using ListDialog.
23  * The basics:
24  * <pre>
25     String[] choices = {"A", "long", "array", "of", "strings"};
26     String selectedName = ListDialog.showDialog(
27                                 componentInControllingFrame,
28                                 locatorComponent,
29                                 "A description of the list:",
30                                 "Dialog Title",
31                                 choices,
32                                 choices[0]);
33  * </pre>
34  */

35 public class ListDialog extends JDialog
36                         implements ActionListener {
37     private static ListDialog dialog;
38     private static Object JavaDoc value;
39     private JList list;
40     private boolean multiselect;
41
42     /**
43      * Set up and show the dialog. The first Component argument
44      * determines which frame the dialog depends on; it should be
45      * a component in the dialog's controlling frame. The second
46      * Component argument should be null if you want the dialog
47      * to come up with its left corner in the center of the screen;
48      * otherwise, it should be the component on top of which the
49      * dialog should appear.
50      */

51     public static String JavaDoc showDialog(Component frameComp,
52                                     Component locationComp,
53                                     String JavaDoc labelText,
54                                     String JavaDoc title,
55                                     String JavaDoc[] possibleValues,
56                                     String JavaDoc initialValue,
57                                     String JavaDoc longValue,
58                                     boolean multiselect,
59                                     boolean editable) {
60         Frame frame = JOptionPane.getFrameForComponent(frameComp);
61         dialog = new ListDialog(frame,
62                                 locationComp,
63                                 labelText,
64                                 title,
65                                 possibleValues,
66                                 initialValue,
67                                 longValue,
68                                 multiselect,
69                                 editable);
70         dialog.setVisible(true);
71         if (value == null) return "";
72         else
73         {
74           if (multiselect)
75           {
76             Object JavaDoc[] vals = (Object JavaDoc[])value;
77             if (vals.length == 0) return "";
78             else return (String JavaDoc)vals[0];
79           }
80           else return (String JavaDoc)value;
81         }
82     }
83
84     public static Object JavaDoc showDialog(Component frameComp,
85                                     Component locationComp,
86                                     String JavaDoc labelText,
87                                     String JavaDoc title,
88                                     Object JavaDoc[] possibleValues,
89                                     Object JavaDoc initialValue,
90                                     String JavaDoc longValue,
91                                     boolean multiselect,
92                                     boolean editable) {
93         Frame frame = JOptionPane.getFrameForComponent(frameComp);
94         dialog = new ListDialog(frame,
95                                 locationComp,
96                                 labelText,
97                                 title,
98                                 possibleValues,
99                                 initialValue,
100                                 longValue,
101                                 multiselect,
102                                 editable);
103         dialog.setVisible(true);
104         return value;
105     }
106
107     
108     private void setValue(Object JavaDoc newValue) {
109         value = newValue;
110         list.setSelectedValue(value, true);
111     }
112
113     private ListDialog(Frame frame,
114                        Component locationComp,
115                        String JavaDoc labelText,
116                        String JavaDoc title,
117                        Object JavaDoc[] data,
118                        Object JavaDoc initialValue,
119                        String JavaDoc longValue,
120                        boolean multiselect,
121                        boolean editable) {
122         super(frame, title, true);
123
124         //Create and initialize the buttons.
125
JButton cancelButton = new JButton("Cancel");
126         cancelButton.addActionListener(this);
127         //
128
final JButton setButton = new JButton(editable ? "Set" : "Close");
129         setButton.setActionCommand("Set");
130         setButton.addActionListener(this);
131         getRootPane().setDefaultButton(setButton);
132
133         //main part of the dialog
134
list = new JList(data) {
135             //Subclass JList to workaround bug 4832765, which can cause the
136
//scroll pane to not let the user easily scroll up to the beginning
137
//of the list. An alternative would be to set the unitIncrement
138
//of the JScrollBar to a fixed value. You wouldn't get the nice
139
//aligned scrolling, but it should work.
140
public int getScrollableUnitIncrement(Rectangle visibleRect,
141                                                   int orientation,
142                                                   int direction) {
143                 int row;
144                 if (orientation == SwingConstants.VERTICAL &&
145                       direction < 0 && (row = getFirstVisibleIndex()) != -1) {
146                     Rectangle r = getCellBounds(row, row);
147                     if ((r.y == visibleRect.y) && (row != 0)) {
148                         Point loc = r.getLocation();
149                         loc.y--;
150                         int prevIndex = locationToIndex(loc);
151                         Rectangle prevR = getCellBounds(prevIndex, prevIndex);
152
153                         if (prevR == null || prevR.y >= r.y) {
154                             return 0;
155                         }
156                         return prevR.height;
157                     }
158                 }
159                 return super.getScrollableUnitIncrement(
160                                 visibleRect, orientation, direction);
161             }
162         };
163
164         this.multiselect = multiselect;
165         if (multiselect) list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
166         else list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
167         if (longValue != null) {
168             list.setPrototypeCellValue(longValue); //get extra space
169
}
170         list.setLayoutOrientation(JList.VERTICAL);
171         list.setVisibleRowCount(-1);
172         if (editable)
173         {
174           list.addMouseListener(new MouseAdapter() {
175               public void mouseClicked(MouseEvent e) {
176                   if (e.getClickCount() == 2) {
177                       setButton.doClick(); //emulate button click
178
}
179               }
180           });
181         }
182         JScrollPane listScroller = new JScrollPane(list);
183         listScroller.setPreferredSize(new Dimension(250, 80));
184         listScroller.setAlignmentX(LEFT_ALIGNMENT);
185
186         //Create a container so that we can add a title around
187
//the scroll pane. Can't add a title directly to the
188
//scroll pane because its background would be white.
189
//Lay out the label and scroll pane from top to bottom.
190
JPanel listPane = new JPanel();
191         listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
192         JLabel label = new JLabel(labelText);
193         label.setLabelFor(list);
194         listPane.add(label);
195         listPane.add(Box.createRigidArea(new Dimension(0,5)));
196         listPane.add(listScroller);
197         listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
198         listPane.setPreferredSize(new java.awt.Dimension JavaDoc(500, 300));
199
200         //Lay out the buttons from left to right.
201
JPanel buttonPane = new JPanel();
202         buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
203         buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
204         buttonPane.add(Box.createHorizontalGlue());
205         if (editable)
206         {
207           buttonPane.add(cancelButton);
208           buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
209         }
210         buttonPane.add(setButton);
211
212         //Put everything together, using the content pane's BorderLayout.
213
Container contentPane = getContentPane();
214         contentPane.add(listPane, BorderLayout.CENTER);
215         contentPane.add(buttonPane, BorderLayout.PAGE_END);
216
217         //Initialize values.
218
setValue(initialValue);
219         pack();
220         setLocationRelativeTo(locationComp);
221     }
222
223     //Handle clicks on the Set and Cancel buttons.
224
public void actionPerformed(ActionEvent e) {
225         if ("Set".equals(e.getActionCommand())) {
226             if (multiselect) ListDialog.value = list.getSelectedValues();
227             else ListDialog.value = list.getSelectedValue();
228         }
229         ListDialog.dialog.setVisible(false);
230     }
231 }
232
Popular Tags