1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.Frame ; 35 import java.awt.Toolkit ; 36 import java.awt.event.ActionEvent ; 37 import java.awt.event.KeyEvent ; 38 import java.util.ArrayList ; 39 40 import javax.swing.DefaultComboBoxModel ; 41 import javax.swing.JButton ; 42 import javax.swing.JDialog ; 43 import javax.swing.JList ; 44 import javax.swing.JPanel ; 45 import javax.swing.JScrollPane ; 46 import javax.swing.ListSelectionModel ; 47 48 import com.genimen.djeneric.language.Messages; 49 50 public class DjListSelectionDialog 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 _theList = new JList (); 63 64 public DjListSelectionDialog(Frame mainFrame, ArrayList theList, String title) 65 { 66 super(mainFrame, title, true); 67 jbInit(); 68 DefaultComboBoxModel mdl = new DefaultComboBoxModel (theList.toArray()); 69 _theList.setModel(mdl); 70 pack(); 71 setSize(500, 300); 72 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 73 Dimension frameSize = getSize(); 74 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 75 76 _theList.setSelectedIndex(0); 77 78 if (theList.size() > 0) setVisible(true); 79 } 80 81 public Object getSelectedObject() 82 { 83 if (_theList.getSelectedIndex() == -1) return null; 84 return _theList.getSelectedValue(); 85 } 86 87 void jbInit() 88 { 89 panel1.setLayout(borderLayout1); 90 jPanel3.setLayout(borderLayout2); 91 _butOk.setText(Messages.getString("global.Ok")); 92 _butOk.addKeyListener(new java.awt.event.KeyAdapter () 93 { 94 public void keyPressed(KeyEvent e) 95 { 96 _butOk_keyPressed(e); 97 } 98 }); 99 _butOk.addActionListener(new java.awt.event.ActionListener () 100 { 101 public void actionPerformed(ActionEvent e) 102 { 103 _butOk_actionPerformed(e); 104 } 105 }); 106 _butCancel.setText(Messages.getString("global.Cancel")); 107 _butCancel.addKeyListener(new java.awt.event.KeyAdapter () 108 { 109 public void keyPressed(KeyEvent e) 110 { 111 _butCancel_keyPressed(e); 112 } 113 }); 114 _butCancel.addActionListener(new java.awt.event.ActionListener () 115 { 116 public void actionPerformed(ActionEvent e) 117 { 118 _butCancel_actionPerformed(e); 119 } 120 }); 121 _theList.addKeyListener(new java.awt.event.KeyAdapter () 122 { 123 public void keyPressed(KeyEvent e) 124 { 125 _lstContext_keyPressed(e); 126 } 127 }); 128 _theList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 129 getContentPane().add(panel1); 130 panel1.add(jPanel3, BorderLayout.SOUTH); 131 jPanel3.add(jPanel4, BorderLayout.EAST); 132 jPanel4.add(_butCancel, null); 133 jPanel4.add(_butOk, null); 134 panel1.add(jScrollPane1, BorderLayout.CENTER); 135 jScrollPane1.getViewport().add(_theList, null); 136 Util.sizeButtons(jPanel4); 137 } 138 139 void _butOk_actionPerformed(ActionEvent e) 140 { 141 _wasCancelled = false; 142 setVisible(false); 143 } 144 145 public boolean isCanceled() 146 { 147 return _wasCancelled; 148 } 149 150 void _butCancel_actionPerformed(ActionEvent e) 151 { 152 setVisible(false); 153 } 154 155 void _lstContext_keyPressed(KeyEvent e) 156 { 157 if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null); 158 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null); 159 } 160 161 void _butCancel_keyPressed(KeyEvent e) 162 { 163 _lstContext_keyPressed(e); 164 } 165 166 void _butOk_keyPressed(KeyEvent e) 167 { 168 _lstContext_keyPressed(e); 169 } 170 } | Popular Tags |