1 31 package swingwtx.custom; 32 33 import swingwt.awt.*; 34 import swingwt.awt.event.*; 35 import swingwtx.swing.*; 36 import swingwtx.swing.event.*; 37 38 import java.util.*; 39 40 54 public class JLookupPopup extends JPanel { 55 56 57 protected Vector items = new Vector(); 58 59 protected Vector popupListeners = new Vector(); 60 61 protected Vector itemListeners = new Vector(); 62 63 protected JTextField text = new JTextField(); 64 protected JButton button = new JButton("..."); 65 protected int selectedIndex = -1; 66 67 68 public JLookupPopup() { 69 super(); 70 setLayout(new BorderLayout()); 71 72 text.addKeyListener(new KeyAdapter() { 74 public void keyPressed(KeyEvent e) { 75 if (e.getKeyCode() == 16777218) { 76 nextItem(); e.consume(); 78 } 79 else if (e.getKeyCode() == 16777217) { 80 previousItem(); e.consume(); 82 } 83 else if (e.getKeyChar() == ' ') { 84 buttonPressed(); e.consume(); 86 } 87 else { 88 alphaSearchItem(e.getKeyChar()); e.consume(); 90 } 91 } 92 }); 93 94 button.addActionListener(new ActionListener() { 96 public void actionPerformed(ActionEvent e) { 97 buttonPressed(); 98 } 99 }); 100 101 add(text, BorderLayout.CENTER); 103 add(button, BorderLayout.EAST); 104 } 105 106 protected final static int CANCELED = 0; 107 protected final static int INVISIBLE = 1; 108 protected final static int VISIBLE = 2; 109 110 public void addFocusListener(FocusListener l) { 111 text.addFocusListener(l); 112 } 113 public void removeFocusListener(FocusListener l) { 114 text.removeFocusListener(l); 115 } 116 public void addKeyListener(KeyListener l) { 117 text.addKeyListener(l); 118 } 119 public void removeKeyListener(KeyListener l) { 120 text.removeKeyListener(l); 121 } 122 public void addPopupMenuListener(PopupMenuListener l) { 123 popupListeners.add(l); 124 } 125 126 public void removePopupMenuListener(PopupMenuListener l) { 127 popupListeners.remove(l); 128 } 129 130 public void addItemListener(ItemListener l) { 131 itemListeners.add(l); 132 } 133 134 public void removeItemListener(ItemListener l) { 135 itemListeners.remove(l); 136 } 137 138 public void processPopupMenuEvent(int id) { 139 Iterator i = popupListeners.iterator(); 140 PopupMenuEvent e = new PopupMenuEvent(this); 141 while (i.hasNext()) { 142 PopupMenuListener l = (PopupMenuListener) i.next(); 143 switch(id) { 144 case CANCELED: l.popupMenuCanceled(e); break; 145 case INVISIBLE: l.popupMenuWillBecomeInvisible(e); break; 146 case VISIBLE: l.popupMenuWillBecomeVisible(e); break; 147 } 148 } 149 } 150 151 public void processItemEvent(ItemEvent e) { 152 Iterator i = itemListeners.iterator(); 153 while (i.hasNext()) { 154 ItemListener l = (ItemListener) i.next(); 155 l.itemStateChanged(e); 156 } 157 } 158 159 160 public void addItem(Object item) { 161 items.add(item); 162 if (items.size() == 1) 164 setSelectedIndex(0); 165 } 166 167 168 public void removeItem(Object item) { 169 items.remove(item); 170 } 171 172 173 public int getSelectedIndex() { 174 return selectedIndex; 175 } 176 177 public void setSelectedIndex(int i) { 178 selectedIndex = i; 179 text.setText( items.get(i).toString() ); 180 } 181 184 public void setSelectedItem(Object o) { 185 text.setText(o.toString()); 186 selectedIndex = items.indexOf(o); 187 } 188 191 public Object getSelectedItem() { 192 return items.get(selectedIndex); 193 } 194 195 public int getItemCount() { 196 return items.size(); 197 } 198 199 public Object getItemAt(int index) { 200 return items.get(index); 201 } 202 203 public void removeAllItems() { 204 items.removeAllElements(); 205 text.setText(""); 206 selectedIndex = -1; 207 } 208 211 public void setEnabled(boolean b) { 212 text.setEnabled(b); 213 button.setEnabled(b); 214 } 215 218 public boolean isEnabled() { 219 return button.isEnabled(); 220 } 221 222 public String getToolTipText() { 223 return text.getToolTipText(); 224 } 225 226 public void setToolTipText(String tip) { 227 text.setToolTipText(tip); 228 } 229 230 231 public void setMaximumRowCount(int i) {} 232 233 238 protected void buttonPressed() { 239 new LookupPopup(this); 240 } 241 242 protected void previousItem() { 243 if (items.size() == 0) return; 245 if (getSelectedIndex() == -1) 247 setSelectedIndex(0); 248 if (getSelectedIndex() > 0) 249 setSelectedIndex(getSelectedIndex() - 1); 250 } 251 protected void nextItem() { 252 if (items.size() == 0) return; 254 if (getSelectedIndex() == -1) 256 setSelectedIndex(0); 257 if (getSelectedIndex() < (items.size() - 1)) 258 setSelectedIndex(getSelectedIndex() + 1); 259 } 260 261 265 protected void alphaSearchItem(char cs) { 266 267 int currentlySelected = getSelectedIndex(); 270 271 String curChar = ""; 273 if (currentlySelected != -1) 274 curChar = getSelectedItem().toString().substring(0, 1).toLowerCase(); 275 276 boolean foundMatch = false; 277 278 for (int i = 0; i < items.size(); i++) { 279 String s = items.get(i).toString(); 280 if (s.substring(0, 1).toLowerCase().equals(new String (new char[] { cs }).toLowerCase())) { 282 283 if (curChar.equals(new String (new char[] { cs }).toLowerCase())) 285 i = currentlySelected + 1; 286 287 if (i >= items.size()) 289 break; 290 291 if (!items.get(i).toString().substring(0, 1).toLowerCase().equals(new String (new char[] { cs }).toLowerCase())) 295 break; 296 297 setSelectedIndex(i); 299 break; 300 301 } 302 } 303 } 304 305 public void requestFocus() { 306 text.requestFocus(); 307 } 308 309 public void grabFocus() { 310 text.grabFocus(); 311 } 312 313 public JTextField getJTextField() { 314 return text; 315 } 316 317 330 private class LookupPopup extends JFrame { 331 332 private JLookupPopup popup = null; 333 private JList lst = null; 334 335 public LookupPopup(JLookupPopup selector) { 336 337 popup = selector; 338 339 341 SwingUtilities.invokeSync( new Runnable () { 342 public void run() { 343 344 lst = new JList(popup.items); 345 lst.addMouseListener( new MouseAdapter() { 346 public void mouseClicked(MouseEvent e) { 347 if (e.getClickCount() == 2) 348 btnOkPressed(); 349 } 350 }); 351 352 JButton btnOk = new JButton("Ok"); 354 btnOk.setMnemonic('o'); 355 btnOk.addActionListener(new ActionListener() { 356 public void actionPerformed(ActionEvent e) { 357 btnOkPressed(); 358 } 359 }); 360 361 JButton btnCancel = new JButton("Cancel"); 362 btnCancel.setMnemonic('c'); 363 btnCancel.addActionListener(new ActionListener() { 364 public void actionPerformed(ActionEvent e) { 365 btnCancelPressed(); 366 } 367 }); 368 369 JPanel pnlButtons = new JPanel(); 370 pnlButtons.setLayout(new FlowLayout()); 371 pnlButtons.add(btnOk); 372 pnlButtons.add(btnCancel); 373 374 getContentPane().setLayout(new BorderLayout()); 375 getContentPane().add(lst, BorderLayout.CENTER); 376 getContentPane().add(pnlButtons, BorderLayout.SOUTH); 377 } 378 }); 379 380 int mouseX = SwingWTUtils.getDisplay().getCursorLocation().x; 382 int mouseY = SwingWTUtils.getDisplay().getCursorLocation().y; 383 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 384 385 if (mouseX > (screen.width - 400)) 388 mouseX = screen.width - 400; 389 if (mouseY > (screen.height - 400)) 390 mouseY = screen.height- 400; 391 392 setSize(400, 400); 393 setLocation(mouseX, mouseY); 394 setTitle("Select"); 395 396 show(); 397 398 SwingUtilities.invokeIn(new Runnable () { 399 public void run() { 400 401 lst.grabFocus(); 403 404 if (popup.getSelectedIndex() != -1) 406 lst.setSelectedIndex(popup.getSelectedIndex()); 407 408 ((org.eclipse.swt.widgets.Table) lst.getPeer()).showSelection(); 410 411 popup.processPopupMenuEvent(JLookupPopup.VISIBLE); 413 } 414 }, 100); 415 416 } 417 418 protected void btnOkPressed() { 419 popup.processItemEvent(new ItemEvent(popup, ItemEvent.DESELECTED, popup.getSelectedItem(), 0)); 420 popup.setSelectedIndex(lst.getSelectedIndex()); 421 dispose(); 422 popup.processItemEvent(new ItemEvent(popup, ItemEvent.SELECTED, popup.getSelectedItem(), 0)); 423 popup.processPopupMenuEvent(JLookupPopup.INVISIBLE); 424 } 425 protected void btnCancelPressed() { 426 popup.processPopupMenuEvent(JLookupPopup.CANCELED); 427 dispose(); 428 popup.processPopupMenuEvent(JLookupPopup.INVISIBLE); 429 } 430 431 } 432 433 } 434 | Popular Tags |