1 101 102 103 package swingwtx.swing; 104 105 import swingwtx.swing.event.*; 106 import swingwt.awt.Dimension; 107 108 import org.eclipse.swt.widgets.*; 109 import org.eclipse.swt.*; 110 111 import java.util.*; 112 113 public class JList extends swingwtx.swing.JComponent implements JSWTScrollable, ListDataListener { 114 115 protected org.eclipse.swt.widgets.Table ppeer = null; 116 protected String pText = ""; 117 protected int pSelectedIndex = -1; 118 protected ListCellRenderer cellRenderer = new DefaultListCellRenderer(); 119 protected ListModel listModel = null; 120 protected ListSelectionModel listSelectionModel = new DefaultListSelectionModel(this); 121 122 123 private Object [] retVals = null; 124 private int iRetVal = 0; 125 private int[] iRetVals = null; 126 127 128 public JList() { listModel = new DefaultListModel(); listModel.addListDataListener(this); } 129 130 135 public JList(Object [] items) { 136 loadData(items); 137 } 138 139 144 public JList(Vector items) { 145 loadData(items); 146 } 147 148 public JList(ListModel model) { 149 listModel = model; 150 listModel.addListDataListener(this); 151 } 152 153 public void addListSelectionListener(ListSelectionListener l) { 154 listSelectionModel.addListSelectionListener(l); 155 } 156 157 public void removeListSelectionListener(ListSelectionListener l) { 158 listSelectionModel.removeListSelectionListener(l); 159 } 160 161 public void clearSelection() { 162 ppeer.deselectAll(); 163 } 164 165 public Object getSelectedValue() { 166 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 167 if (getModel().getSize() > 0) 170 return getModel().getElementAt(0); 171 else 172 return null; 173 else 174 return listModel.getElementAt(ppeer.getSelectionIndex()); 175 } 176 177 public void setSelectedValue(final int index) { 178 SwingUtilities.invokeSync(new Runnable () { 179 public void run() { 180 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 181 pSelectedIndex = index; 182 else 183 ppeer.select(index); 184 } 185 }); 186 } 187 188 public void setSelectedValue(final Object value, boolean shouldscroll) { 189 if (!checkIsDefault()) return; 190 SwingUtilities.invokeSync(new Runnable () { 191 public void run() { 192 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 193 pSelectedIndex = ((DefaultListModel) listModel).indexOf(value); 194 else 195 ppeer.select(((DefaultListModel) listModel).indexOf(value)); 196 } 197 }); 198 } 199 200 public ListSelectionModel getSelectionModel() { return listSelectionModel; } 201 public void setSelectionModel(ListSelectionModel l) { listSelectionModel = l; } 202 203 public void setSelectionMode(int mode) { 204 listSelectionModel.setSelectionMode(mode); 205 } 206 207 public Object [] getSelectedValues() { 208 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return null; 209 retVals = null; 210 SwingUtilities.invokeSync(new Runnable () { 211 public void run() { 212 retVals = new Object [ppeer.getSelectionCount()]; 213 for (int i = 0; i < ppeer.getSelectionIndices().length; i++) { 214 retVals[i] = listModel.getElementAt(ppeer.getSelectionIndices()[i]); 215 } 216 } 217 }); 218 return retVals; 219 } 220 221 public void addItem(final Object item) { 222 if (!checkIsDefault()) return; 223 ((DefaultListModel) listModel).addElement(item); 224 SwingUtilities.invokeSync(new Runnable () { 225 public void run() { 226 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 227 updateList(); 228 } 229 }); 230 } 231 232 public void insertItemAt(final Object item, final int index) { 233 if (!checkIsDefault()) return; 234 ((DefaultListModel) listModel).add(index, item); 235 SwingUtilities.invokeSync(new Runnable () { 236 public void run() { 237 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 238 updateList(); 239 } 240 }); 241 } 242 243 public int getSelectedIndex() { 244 iRetVal = 0; 245 SwingUtilities.invokeSync(new Runnable () { 246 public void run() { 247 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 248 iRetVal = -1; 249 else 250 iRetVal = ppeer.getSelectionIndex(); 251 } 252 }); 253 return iRetVal; 254 } 255 256 public int[] getSelectedIndices() { 257 iRetVals = null; 258 SwingUtilities.invokeSync(new Runnable () { 259 public void run() { 260 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 261 iRetVals = ppeer.getSelectionIndices(); 262 else 263 iRetVals = new int[0]; 264 } 265 }); 266 return iRetVals; 267 } 268 269 public Object getItemAt(int index) { 270 return listModel.getElementAt(index); 271 } 272 273 public int getItemCount() { 274 return listModel.getSize(); 275 } 276 277 278 public void showSelection() { 279 SwingUtilities.invokeSync(new Runnable () { 280 public void run() { 281 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 282 ppeer.showSelection(); 283 } 284 }); 285 } 286 287 public void setSelectedIndex(final int index) { 288 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) 289 pSelectedIndex = index; 290 else 291 SwingUtilities.invokeSync(new Runnable () { 292 public void run() { 293 ppeer.select(index); 294 } 295 }); 296 } 297 298 public void setSelectedIndices(final int[] selection) { 299 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return; 300 SwingUtilities.invokeSync(new Runnable () { 301 public void run() { 302 ppeer.select(selection); 303 } 304 }); 305 } 306 307 public void removeAllItems() { 308 setModel(new DefaultListModel()); 309 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 310 SwingUtilities.invokeSync(new Runnable () { 311 public void run() { 312 ppeer.removeAll(); 313 updateList(); 314 } 315 }); 316 } 317 318 public void removeItem(final Object item) { 319 if (!checkIsDefault()) return; 320 final int index = ((DefaultListModel) listModel).indexOf(item); 321 ((DefaultListModel) listModel).removeElement(item); 322 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 323 SwingUtilities.invokeSync(new Runnable () { 324 public void run() { 325 ppeer.remove(index); 326 updateList(); 327 } 328 }); 329 } 330 331 public void removeItemAt(final int index) { 332 if (!checkIsDefault()) return; 333 ((DefaultListModel) listModel).remove(index); 334 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 335 SwingUtilities.invokeSync(new Runnable () { 336 public void run() { 337 ppeer.remove(index); 338 } 339 }); 340 } 341 342 346 public void replaceItemAt(final Object replacement, final int index) { 347 if (!checkIsDefault()) return; 348 ((DefaultListModel) listModel).set(index, replacement); 349 updateList(); 350 } 351 352 353 public void setVisibleRowCount(int rows) { 354 Dimension d = getPreferredSize(); 355 d.height = rows * SwingWTUtils.getRenderStringHeight("W"); 356 if (d.width == 0) 357 d.width = SwingWTUtils.getRenderStringWidth("WWWWWWWWWWWWWWWWWWW"); 358 setPreferredSize(d); 359 } 360 361 public int locationToIndex(final swingwt.awt.Point point) { 362 return ( point.y / SwingWTUtils.getRenderStringHeight("W") ); 365 } 366 367 368 public void setSelectionBackground(swingwt.awt.Color color) { } 369 370 public void setSelectionForeground(swingwt.awt.Color color) { } 371 372 373 protected void updateList() { 374 final JList list = this; 375 SwingUtilities.invokeSync(new Runnable () { 376 public void run() { 377 378 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return; 380 381 ppeer.removeAll(); 383 384 org.eclipse.swt.widgets.TableColumn tc = ppeer.getColumn(0); 386 387 if (listModel != null) { 389 int widestListItem = 0; 390 for (int i = 0; i < listModel.getSize(); i++) { 391 TableItem ti = new TableItem(ppeer, 0); 392 JLabel renderer = 394 (JLabel) cellRenderer.getListCellRendererComponent(list, listModel.getElementAt(i), i, false, false); 395 396 ti.setText(0, renderer.getText() ); 398 399 int needed = SwingWTUtils.getRenderStringWidth(renderer.getText()); 402 if (needed > widestListItem) { 403 widestListItem = needed; 404 tc.setWidth(needed); 405 } 406 407 if (renderer.getIcon() != null) 409 ti.setImage(0, SwingWTUtils.getSWTImageFromSwingIcon(list, renderer.getIcon())); 410 411 if (renderer.getBackground() != null) 413 if (renderer.getBackground().getSWTColor() != null) 414 ti.setBackground(renderer.getBackground().getSWTColor()); 415 if (renderer.getForeground() != null) 416 if (renderer.getForeground().getSWTColor() != null) 417 ti.setForeground(renderer.getForeground().getSWTColor()); 418 419 ppeer.setCursor(renderer.getCursor().getSWTCursor()); 421 } 422 } 423 } 424 }); 425 } 426 427 430 public void setListData(Object [] listData) { 431 loadData(listData); 432 updateList(); 433 } 434 435 438 public void setListData(Vector listData) { 439 loadData(listData); 440 updateList(); 441 } 442 443 protected void loadData(Vector data) { 444 loadData(data.toArray()); 445 } 446 447 protected void loadData(Object [] data) { 448 listModel = new DefaultListModel(); 449 for (int i = 0; i < data.length; i++) { 450 ((DefaultListModel) listModel).addElement(data[i]); 451 } 452 listModel.addListDataListener(this); 453 } 454 455 public void setCellRenderer(ListCellRenderer r) { cellRenderer = r; } 456 public ListCellRenderer getCellRenderer() { return cellRenderer; } 457 458 public ListModel getModel() { 459 return listModel; 460 } 461 462 public void setModel(ListModel l) { 463 listModel = l; 464 listModel.addListDataListener(this); 465 updateList(); 466 } 467 468 471 protected boolean checkIsDefault() { 472 return listModel instanceof DefaultListModel; 473 } 474 475 480 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 481 descendantHasPeer = true; 482 483 ppeer = new Table(parent.getComposite(), SWT.BORDER | 485 (listSelectionModel.getSelectionMode() == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ? SWT.MULTI : SWT.SINGLE) 486 | SWT.FULL_SELECTION ); 487 488 org.eclipse.swt.widgets.TableColumn tc = 490 new org.eclipse.swt.widgets.TableColumn(ppeer, 0); 491 492 peer = ppeer; 493 this.parent = parent; 494 495 ppeer.setLinesVisible(false); 497 registerSelectionEvents(); 498 499 updateList(); 501 502 if (pSelectedIndex != -1) ppeer.select(pSelectedIndex); 503 504 } 505 506 public void contentsChanged(swingwtx.swing.event.ListDataEvent e) { 507 updateList(); 508 } 509 510 public void intervalAdded(swingwtx.swing.event.ListDataEvent e) { 511 updateList(); 512 } 513 514 public void intervalRemoved(swingwtx.swing.event.ListDataEvent e) { 515 updateList(); 516 } 517 518 522 protected void registerSelectionEvents() { 523 ppeer.addSelectionListener( new org.eclipse.swt.events.SelectionAdapter() { 524 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { 525 if (listSelectionModel instanceof DefaultListSelectionModel) { 526 if (listSelectionModel.getSelectionMode() == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) { 527 int lowest = ppeer.getSelectionIndex(); int highest = ppeer.getSelectionIndex(); 528 int sel[] = ppeer.getSelectionIndices(); 529 for (int i = 0; i < sel.length; i++) { 530 if (lowest > sel[i]) lowest = sel[i]; 531 if (highest < sel[i]) highest = sel[i]; 532 } 533 ((DefaultListSelectionModel) listSelectionModel).fireListSelectionEvent(JList.this, lowest, highest); 534 } 535 else { 536 ((DefaultListSelectionModel) listSelectionModel).fireListSelectionEvent(JList.this, ppeer.getSelectionIndex(), ppeer.getSelectionIndex()); 537 } 538 } 539 } 540 }); 541 } 542 543 } 544 | Popular Tags |