1 6 7 package SOFA.SOFAnet.Browser; 8 9 import javax.swing.*; 10 import java.awt.*; 11 import java.awt.event.*; 12 13 19 20 35 public class ListDialog extends JDialog 36 implements ActionListener { 37 private static ListDialog dialog; 38 private static Object value; 39 private JList list; 40 private boolean multiselect; 41 42 51 public static String showDialog(Component frameComp, 52 Component locationComp, 53 String labelText, 54 String title, 55 String [] possibleValues, 56 String initialValue, 57 String 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 [] vals = (Object [])value; 77 if (vals.length == 0) return ""; 78 else return (String )vals[0]; 79 } 80 else return (String )value; 81 } 82 } 83 84 public static Object showDialog(Component frameComp, 85 Component locationComp, 86 String labelText, 87 String title, 88 Object [] possibleValues, 89 Object initialValue, 90 String 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 newValue) { 109 value = newValue; 110 list.setSelectedValue(value, true); 111 } 112 113 private ListDialog(Frame frame, 114 Component locationComp, 115 String labelText, 116 String title, 117 Object [] data, 118 Object initialValue, 119 String longValue, 120 boolean multiselect, 121 boolean editable) { 122 super(frame, title, true); 123 124 JButton cancelButton = new JButton("Cancel"); 126 cancelButton.addActionListener(this); 127 final JButton setButton = new JButton(editable ? "Set" : "Close"); 129 setButton.setActionCommand("Set"); 130 setButton.addActionListener(this); 131 getRootPane().setDefaultButton(setButton); 132 133 list = new JList(data) { 135 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); } 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(); } 179 } 180 }); 181 } 182 JScrollPane listScroller = new JScrollPane(list); 183 listScroller.setPreferredSize(new Dimension(250, 80)); 184 listScroller.setAlignmentX(LEFT_ALIGNMENT); 185 186 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 (500, 300)); 199 200 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 Container contentPane = getContentPane(); 214 contentPane.add(listPane, BorderLayout.CENTER); 215 contentPane.add(buttonPane, BorderLayout.PAGE_END); 216 217 setValue(initialValue); 219 pack(); 220 setLocationRelativeTo(locationComp); 221 } 222 223 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 |