1 99 100 101 package swingwtx.swing; 102 103 import swingwtx.swing.event.*; 104 import swingwt.awt.event.*; 105 106 import org.eclipse.swt.events.*; 107 import org.eclipse.swt.widgets.*; 108 import org.eclipse.swt.*; 109 110 import java.util.*; 111 112 public class JComboBox extends swingwtx.swing.JComponent { 113 114 protected Combo ppeer = null; 115 protected String pText = ""; 116 protected Vector litems = null; 117 protected int pSelectedIndex = -1; 118 protected int pMaxRows = 8; 119 protected Vector popupListeners = new Vector(); 120 protected Vector itemListeners = new Vector(); 121 protected Vector changeListeners = new Vector(); 122 protected ComboBoxModel model = null; 123 124 125 private Object retValue = null; 126 127 128 protected boolean isEditable = false; 129 130 public JComboBox() { litems = new Vector(); } 131 132 public JComboBox(Object [] items) { 133 litems = new Vector(); 134 for (int i = 0; i < items.length; i++) { 135 litems.add(items[i]); 136 } 137 if (!isEditable && litems.size() > 0) 138 pSelectedIndex = 0; 139 } 140 141 public JComboBox(Vector items) { 142 litems = items; 143 if (!isEditable && litems != null && litems.size() > 0) 144 pSelectedIndex = 0; 145 } 146 147 public JComboBox(ComboBoxModel model) { 148 this.model = model; 149 copyModel(); 150 if (!isEditable && litems != null && litems.size() > 0) 151 pSelectedIndex = 0; 152 } 153 154 protected final static int CANCELED = 0; 155 protected final static int INVISIBLE = 1; 156 protected final static int VISIBLE = 2; 157 158 public void addChangeListener(ChangeListener l) { 159 changeListeners.add(l); 160 } 161 162 public void removeChangeListener(ChangeListener l) { 163 changeListeners.remove(l); 164 } 165 166 public void addPopupMenuListener(PopupMenuListener l) { 167 popupListeners.add(l); 168 } 169 170 public void removePopupMenuListener(PopupMenuListener l) { 171 popupListeners.remove(l); 172 } 173 174 public void addItemListener(ItemListener l) { 175 itemListeners.add(l); 176 } 177 178 public void removeItemListener(ItemListener l) { 179 itemListeners.remove(l); 180 } 181 182 public void processPopupMenuEvent(int id) { 183 Iterator i = popupListeners.iterator(); 184 PopupMenuEvent e = new PopupMenuEvent(this); 185 while (i.hasNext()) { 186 PopupMenuListener l = (PopupMenuListener) i.next(); 187 switch(id) { 188 case CANCELED: l.popupMenuCanceled(e); break; 189 case INVISIBLE: l.popupMenuWillBecomeInvisible(e); break; 190 case VISIBLE: l.popupMenuWillBecomeVisible(e); break; 191 } 192 } 193 } 194 195 public void processItemEvent(ItemEvent e) { 196 197 if (model != null) 199 model.setSelectedItem(e.getItem()); 200 201 for (int i = 0; i < itemListeners.size(); i++) { 202 ItemListener l = (ItemListener) itemListeners.get(i); 203 l.itemStateChanged(e); 204 } 205 } 206 207 public void processChangeEvent(ChangeEvent e) { 208 for (int i = 0; i < changeListeners.size(); i++) { 209 ChangeListener l = (ChangeListener) changeListeners.get(i); 210 l.stateChanged(e); 211 } 212 } 213 214 215 218 protected void copyModel() { 219 if (model == null) return; 220 litems = new Vector(model.getSize()); 221 for (int i = 0; i < model.getSize(); i++) { 222 litems.add(model.getElementAt(i)); 223 } 224 } 225 226 public Object getSelectedItem() { 227 retValue = null; 228 SwingUtilities.invokeSync(new Runnable () { public void run() { 229 if (isEditable) { 230 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 231 retValue = pText; 232 else 233 retValue = ppeer.getText(); 234 } 235 else { 236 if ((!SwingWTUtils.isSWTControlAvailable(ppeer)) && pSelectedIndex > -1) 237 retValue = litems.get(pSelectedIndex); 238 else if (SwingWTUtils.isSWTControlAvailable(ppeer)) 239 if (ppeer.getSelectionIndex() != -1) 240 retValue = litems.get(ppeer.getSelectionIndex()); 241 } 242 }}); 243 return retValue; 244 } 245 246 public void setSelectedItem(final Object text) { 247 SwingUtilities.invokeSync(new Runnable () { 248 public void run() { 249 if (isEditable) { 250 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 251 pText = text.toString(); 252 else 253 ppeer.setText(text.toString()); 254 } 255 else { 256 if (litems != null) { 258 if (text instanceof String ) { 259 Object [] itm = litems.toArray(); 260 for (int i = 0; i < itm.length; i++) 261 if (itm[i].toString().equals(text.toString())) { 262 setSelectedIndex(i); 263 break; 264 } 265 } 266 else { 267 int index = litems.indexOf(text); 269 if (index != -1) setSelectedIndex(index); 270 } 271 } 272 } 273 } 274 }); 275 } 276 277 public ComboBoxModel getModel() { return model; } 278 public void setModel(ComboBoxModel model) { 279 this.model = model; 280 copyModel(); 281 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 282 String [] items = new String [litems.size()]; 283 for (int i=0; i < litems.size(); i++) { 284 Object o = litems.get(i); 285 items[i] = o == null ? "" : o.toString(); 286 } 287 ppeer.setItems(items); 288 } 289 } 290 291 public void addItem(final Object item) { 292 if (litems == null) 293 litems = new Vector(); 294 litems.add(item); 295 296 SwingUtilities.invokeSync(new Runnable () { 297 public void run() { 298 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 299 ppeer.add(item.toString()); 300 } 301 }); 302 303 } 304 305 public void insertItemAt(final Object item, final int index) { 306 if (litems == null) 307 litems = new Vector(); 308 litems.add(index, item); 309 SwingUtilities.invokeSync(new Runnable () { 310 public void run() { 311 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 312 ppeer.add(item.toString(), index); 313 } 314 }); 315 } 316 317 public int getSelectedIndex() { 318 retValue = null; 319 SwingUtilities.invokeSync(new Runnable () { 320 public void run() { 321 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 322 retValue = new Integer (pSelectedIndex); 323 else 324 retValue = new Integer (ppeer.getSelectionIndex()); 325 } 326 }); 327 return ((Integer ) retValue).intValue(); 328 } 329 330 public Object getItemAt(int index) { 331 if (index == -1) 332 return null; 333 return litems.get(index); 334 } 335 336 public swingwt.awt.Component getComponent(int n) { 337 return this; 338 } 339 340 public int getItemCount() { 341 return litems.size(); 342 } 343 344 public void setSelectedIndex(final int index) { 345 final JComboBox me = this; 346 if (model!=null) { 347 if (index!=-1) 348 model.setSelectedItem(model.getElementAt(index)); 349 else 350 model.setSelectedItem(null); 351 } 352 SwingUtilities.invokeSync(new Runnable () { 353 public void run() { 354 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 355 pSelectedIndex = index; 356 else { 357 ppeer.select(index); 358 processActionEvent(0); 359 ItemEvent ie = new ItemEvent(me, 0, getSelectedItem(), ItemEvent.SELECTED); 360 processItemEvent(ie); 361 } 362 } 363 }); 364 } 365 366 public void removeAllItems() { 367 if (litems != null) 368 litems.removeAllElements(); 369 SwingUtilities.invokeSync(new Runnable () { 370 public void run() { 371 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 372 ppeer.removeAll(); 373 } 374 }); 375 } 376 377 public void removeItem(final Object item) { 378 litems.remove(item); 379 SwingUtilities.invokeSync(new Runnable () { 380 public void run() { 381 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 382 ppeer.remove(item.toString()); 383 } 384 }); 385 } 386 387 public void removeItemAt(final int index) { 388 litems.remove(index); 389 SwingUtilities.invokeSync(new Runnable () { 390 public void run() { 391 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 392 ppeer.remove(index); 393 } 394 }); 395 } 396 397 public void setEditable(boolean b) { 398 isEditable = b; 399 } 400 401 public boolean getEditable() { 402 return isEditable; 403 } 404 405 public int getMaximumRowCount() { 406 return pMaxRows; 407 } 408 public void setMaximumRowCount(int max) { 409 pMaxRows = max; 410 } 411 412 417 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 418 descendantHasPeer = true; 419 ppeer = new Combo(parent.getComposite(), SWT.BORDER | SWT.DROP_DOWN ); 420 421 if (litems != null) { 422 String [] items = new String [litems.size()]; 423 for (int i=0; i < litems.size(); i++) { 424 Object o = litems.get(i); 425 items[i] = o == null ? "" : o.toString(); 426 } 427 ppeer.setItems(items); 428 } 429 430 if (pSelectedIndex != -1) 431 ppeer.select(pSelectedIndex); 432 433 else if (litems != null && litems.size() > 0) { 435 pSelectedIndex = 0; 436 ppeer.select(0); 437 } 438 if (isEditable) 440 if (!pText.equals("")) 441 ppeer.setText(pText); 442 443 peer = ppeer; 444 this.parent = parent; 445 446 registerComboEvents(); 447 } 448 449 protected void registerComboEvents() { 450 451 final JComboBox me = this; 452 453 ppeer.addModifyListener(new ModifyListener() { 455 public void modifyText(ModifyEvent e) { 456 if (ppeer.getSelectionIndex() != -1) { 458 processPopupMenuEvent(INVISIBLE); 459 processPopupMenuEvent(VISIBLE); 460 processPopupMenuEvent(CANCELED); 461 } 462 } 463 }); 464 465 ppeer.addSelectionListener(new SelectionAdapter() { 467 public void widgetSelected(SelectionEvent e) { 468 ItemEvent ie = new ItemEvent(me, 0, getSelectedItem(), ItemEvent.SELECTED); 469 processItemEvent(ie); 470 ChangeEvent ce = new ChangeEvent(me); 471 processChangeEvent(ce); 472 processActionEvent(0); } 474 }); 475 476 handleComboKeyStrokes(); 478 479 } 480 481 492 protected void handleComboKeyStrokes() { 493 494 498 if (isEditable) return; 499 500 ppeer.addKeyListener(new org.eclipse.swt.events.KeyAdapter() { 501 public void keyPressed(org.eclipse.swt.events.KeyEvent e) { 502 503 e.doit = false; 505 506 if ( ((e.stateMask & SWT.ALT) > 0) || 508 ((e.stateMask & SWT.CTRL) > 0) ) return; 509 510 int currentlySelected = getSelectedIndex(); 513 514 String curChar = ""; 516 String selected = ""; 517 518 if (currentlySelected != -1) 519 selected = getSelectedItem().toString(); 520 521 if (currentlySelected != -1 && selected.length() > 0) 522 curChar = selected.substring(0, 1).toLowerCase(); 523 524 boolean foundMatch = false; 525 526 if (litems!=null) { 527 for (int i = 0; i < litems.size(); i++) { 528 String s = litems.get(i).toString(); 529 if ( s.length() > 0 && 531 s.substring(0, 1).toLowerCase().equals(new String (new char[] { e.character }).toLowerCase()) ) 532 { 533 534 if (curChar.equals(new String (new char[] { e.character }).toLowerCase())) 536 i = currentlySelected + 1; 537 538 if (i >= litems.size()) 540 break; 541 542 if (!litems.get(i).toString().substring(0, 1).toLowerCase().equals(new String (new char[] { e.character }).toLowerCase())) 546 break; 547 548 foundMatch = true; 549 550 final int matchedIndex = i; 553 SwingUtilities.invokeIn(new Runnable () { 554 public void run() { 555 setSelectedItem(litems.get(matchedIndex)); 556 } 557 }, 1); 558 break; 559 560 } 561 } 562 } 563 564 if (!foundMatch) { 567 final Object selItem = getSelectedItem(); 568 SwingUtilities.invokeIn(new Runnable () { 569 public void run() { 570 setSelectedItem(selItem); 571 } 572 }, 1); 573 } 574 575 } 576 }); 577 578 } 579 580 } 581 | Popular Tags |