1 28 package com.genimen.djeneric.tools.generator.dialogs; 29 30 import java.awt.BorderLayout ; 31 import java.awt.Dialog ; 32 import java.awt.Dimension ; 33 import java.awt.Frame ; 34 import java.awt.Toolkit ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.KeyEvent ; 37 import java.awt.event.MouseEvent ; 38 import java.awt.event.WindowEvent ; 39 import java.util.Vector ; 40 41 import javax.swing.DefaultComboBoxModel ; 42 import javax.swing.JButton ; 43 import javax.swing.JDialog ; 44 import javax.swing.JList ; 45 import javax.swing.JPanel ; 46 import javax.swing.JScrollPane ; 47 48 import com.genimen.djeneric.util.DjLogger; 49 50 public class ListSelectionDlg extends JDialog 51 { 52 private static final long serialVersionUID = 1L; 53 JPanel panel1 = new JPanel (); 54 BorderLayout borderLayout1 = new BorderLayout (); 55 JPanel jPanel3 = new JPanel (); 56 BorderLayout borderLayout2 = new BorderLayout (); 57 JPanel jPanel4 = new JPanel (); 58 JButton _butOk = new JButton (); 59 JButton _butCancel = new JButton (); 60 boolean _wasCancelled = true; 61 JScrollPane jScrollPane1 = new JScrollPane (); 62 JList _lstList = new JList (); 63 64 public ListSelectionDlg(Dialog dialog, String title, Vector lst) 65 { 66 super(dialog, title, true); 67 init(lst); 68 } 69 70 public ListSelectionDlg(Frame frame, String title, Vector lst) 71 { 72 super(frame, title, true); 73 init(lst); 74 } 75 76 protected void init(Vector lst) 77 { 78 try 79 { 80 jbInit(); 81 DefaultComboBoxModel mdl = new DefaultComboBoxModel (lst); 82 _lstList.setModel(mdl); 83 pack(); 84 setSize(300, 300); 85 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 86 Dimension frameSize = getSize(); 87 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 88 89 if (lst.size() > 0) _lstList.setSelectedIndex(0); 90 91 setVisible(true); 92 } 93 catch (Exception ex) 94 { 95 DjLogger.log(ex); 96 } 97 } 98 99 public Object [] getSelection() 100 { 101 if (_lstList.getSelectedIndex() == -1) return new Object [0]; 102 return _lstList.getSelectedValues(); 103 } 104 105 void jbInit() throws Exception 106 { 107 panel1.setLayout(borderLayout1); 108 jPanel3.setLayout(borderLayout2); 109 _butOk.setPreferredSize(new Dimension (73, 27)); 110 _butOk.setText("OK"); 111 _butOk.addKeyListener(new java.awt.event.KeyAdapter () 112 { 113 public void keyPressed(KeyEvent e) 114 { 115 handleKeys(e); 116 } 117 }); 118 _butOk.addActionListener(new java.awt.event.ActionListener () 119 { 120 public void actionPerformed(ActionEvent e) 121 { 122 _butOk_actionPerformed(e); 123 } 124 }); 125 _butCancel.setText("Cancel"); 126 _butCancel.addKeyListener(new java.awt.event.KeyAdapter () 127 { 128 public void keyPressed(KeyEvent e) 129 { 130 handleKeys(e); 131 } 132 }); 133 _butCancel.addActionListener(new java.awt.event.ActionListener () 134 { 135 public void actionPerformed(ActionEvent e) 136 { 137 _butCancel_actionPerformed(e); 138 } 139 }); 140 _lstList.addKeyListener(new java.awt.event.KeyAdapter () 141 { 142 public void keyPressed(KeyEvent e) 143 { 144 handleKeys(e); 145 } 146 }); 147 _lstList.addMouseListener(new java.awt.event.MouseAdapter () 148 { 149 public void mouseClicked(MouseEvent e) 150 { 151 _lstList_mouseClicked(e); 152 } 153 }); 154 this.addWindowListener(new java.awt.event.WindowAdapter () 155 { 156 public void windowOpened(WindowEvent e) 157 { 158 this_windowOpened(e); 159 } 160 }); 161 getContentPane().add(panel1); 162 panel1.add(jPanel3, BorderLayout.SOUTH); 163 jPanel3.add(jPanel4, BorderLayout.EAST); 164 jPanel4.add(_butCancel, null); 165 jPanel4.add(_butOk, null); 166 panel1.add(jScrollPane1, BorderLayout.CENTER); 167 jScrollPane1.getViewport().add(_lstList, null); 168 } 169 170 void _butOk_actionPerformed(ActionEvent e) 171 { 172 _wasCancelled = false; 173 setVisible(false); 174 } 175 176 void _butCancel_actionPerformed(ActionEvent e) 177 { 178 setVisible(false); 179 } 180 181 void handleKeys(KeyEvent e) 182 { 183 if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null); 184 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null); 185 } 186 187 void _lstList_mouseClicked(MouseEvent e) 188 { 189 if (e.getClickCount() > 1) _butOk_actionPerformed(null); 190 } 191 192 void this_windowOpened(WindowEvent e) 193 { 194 _lstList.requestFocus(); 195 196 } 197 198 public boolean wasCancelled() 199 { 200 return _wasCancelled; 201 } 202 } | Popular Tags |