1 22 package org.enhydra.kelp.common.xmlc; 23 24 import org.enhydra.kelp.common.event.SwingTableSelectionEvent; 26 import org.enhydra.kelp.common.event.SwingTableSelectionListener; 27 import org.enhydra.kelp.common.swing.CellRendererWithToolTip; 28 29 import javax.swing.*; 31 import javax.swing.table.*; 32 import javax.swing.event.*; 33 import java.awt.*; 34 import java.beans.*; 35 import java.util.Vector ; 36 import java.util.ResourceBundle ; 37 38 44 public class MapPanel extends JPanel implements ListSelectionListener { 45 46 static ResourceBundle res = 48 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); 50 protected static final int NO_SELECTION = -1; 52 private JTable table; 53 private JScrollPane scrollTable; 54 private BorderLayout layoutMain; 55 private MapTableModel tableModel = new MapTableModel(); 56 private int currentSelectionIndex = NO_SELECTION; 57 private Vector swingTableSelectionListeners = new Vector (); 58 private String [][] map = new String [0][2]; 59 60 64 public MapPanel() { 65 try { 66 jbInit(); 67 pmInit(); 68 } catch (Exception ex) { 69 ex.printStackTrace(); 70 } 71 } 72 73 public synchronized void addSwingTableSelectionListener(SwingTableSelectionListener l) { 74 if (!swingTableSelectionListeners.contains(l)) { 75 swingTableSelectionListeners.addElement(l); 76 } 77 } 78 79 public synchronized void removeSwingTableSelectionListener(SwingTableSelectionListener l) { 80 if (swingTableSelectionListeners.contains(l)) { 81 swingTableSelectionListeners.removeElement(l); 82 } 83 } 84 85 88 public void valueChanged(ListSelectionEvent e) { 89 ListSelectionModel lsm = (ListSelectionModel) e.getSource(); 90 91 if (lsm.isSelectionEmpty()) { 92 currentSelectionIndex = NO_SELECTION; 93 } else { 94 currentSelectionIndex = lsm.getMinSelectionIndex(); 95 } 96 SwingTableSelectionListener listener = null; 97 98 for (int i = 0; i < swingTableSelectionListeners.size(); i++) { 99 listener = 100 (SwingTableSelectionListener) swingTableSelectionListeners.elementAt(i); 101 listener.onSwingTableSelection(new SwingTableSelectionEvent(this, 102 currentSelectionIndex)); 103 } 104 } 105 106 112 protected int getCurrentSelectionIndex() { 113 return currentSelectionIndex; 114 } 115 116 protected String getSelectedFolder() { 117 String folder = null; 118 119 if (currentSelectionIndex > -1) { 120 folder = tableModel.getRow(currentSelectionIndex).getFolder(); 121 } 122 return folder; 123 } 124 125 protected void setSelectedFolder(String folder) { 126 if (currentSelectionIndex > -1) { 127 tableModel.getRow(currentSelectionIndex).setFolder(folder); 128 tableModel.fireTableDataChanged(); 129 } 130 } 131 132 protected String getSelectedPackageName() { 133 String pack = null; 134 135 if (currentSelectionIndex > -1) { 136 pack = tableModel.getRow(currentSelectionIndex).getPackageName(); 137 } 138 return pack; 139 } 140 141 protected void setSelectedPackageName(String pack) { 142 if (currentSelectionIndex > -1) { 143 tableModel.getRow(currentSelectionIndex).setPackageName(pack); 144 tableModel.fireTableDataChanged(); 145 } 146 } 147 148 protected void updateSelectedRow(String folder, String pack) { 149 if (currentSelectionIndex > -1) { 150 tableModel.getRow(currentSelectionIndex).setFolder(folder); 151 tableModel.getRow(currentSelectionIndex).setPackageName(pack); 152 tableModel.fireTableDataChanged(); 153 } 154 } 155 156 163 protected void addRow(String f, String p) { 164 tableModel.addRow(f, p); 165 } 166 167 173 protected void removeRow(int index) { 174 tableModel.removeRow(index); 175 } 176 177 183 private void jbInit() throws Exception { 184 layoutMain = 185 (BorderLayout) Beans.instantiate(getClass().getClassLoader(), 186 BorderLayout.class.getName()); 187 table = (JTable) Beans.instantiate(getClass().getClassLoader(), 188 JTable.class.getName()); 189 scrollTable = new JScrollPane(table); 190 table.setToolTipText(new String ()); 191 table.sizeColumnsToFit(JTable.AUTO_RESIZE_ALL_COLUMNS); 192 table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); 193 table.setColumnSelectionAllowed(false); 194 scrollTable.getViewport().add(table, BorderLayout.CENTER); 195 ListSelectionModel rowSM = table.getSelectionModel(); 196 197 rowSM.addListSelectionListener(this); 198 this.setPreferredSize(new Dimension(620, 200)); 199 this.setLayout(layoutMain); 200 this.add(scrollTable, BorderLayout.CENTER); 201 } 202 203 209 public String [][] getMap() { 210 tableModel.readMap(); 211 return map; 212 } 213 214 220 public void setMap(String [][] m) { 221 map = m; 222 tableModel.populateModel(); 223 table.setModel(tableModel); 224 } 225 226 230 private void pmInit() { 231 tableModel = new MapTableModel(); 232 table.setModel(tableModel); 233 table.setDefaultRenderer(String .class, new CellRendererWithToolTip()); 234 tableModel.addTableModelListener(table); 235 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 236 table.getTableHeader().setUpdateTableInRealTime(false); 237 table.getTableHeader().setReorderingAllowed(false); 238 } 239 240 245 class MapTableModel extends AbstractTableModel { 246 private Vector rowVector = new Vector (); 247 248 public MapTableModel() {} 249 250 public int getRowCount() { 251 int count = 0; 252 253 if (rowVector != null) { 254 count = rowVector.size(); 255 } 256 return count; 257 } 258 259 public int getColumnCount() { 260 return 2; 261 } 262 263 271 public String getColumnName(int columnIndex) { 272 String name = new String (); 273 274 switch (columnIndex) { 275 case 0: 276 name = res.getString("Source_Directory"); 277 break; 278 case 1: 279 name = res.getString("Package"); 280 break; 281 } 282 return name; 283 } 284 285 293 public Class getColumnClass(int columnIndex) { 294 Class columnClass = null; 295 Object value = getValueAt(0, columnIndex); 296 297 if (value != null) { 298 columnClass = value.getClass(); 299 } 300 return columnClass; 301 } 302 303 312 public boolean isCellEditable(int rowIndex, int columnIndex) { 313 return false; 314 } 315 316 325 public Object getValueAt(int rowIndex, int columnIndex) { 326 Object value = null; 327 328 if (!isTableEmpty()) { 329 MapRow row = (MapRow) rowVector.elementAt(rowIndex); 330 MapCell cell = new MapCell(columnIndex, row); 331 332 value = cell.getValue(); 333 } 334 return value; 335 } 336 337 345 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 346 if (!isTableEmpty()) { 347 MapRow row = (MapRow) rowVector.elementAt(rowIndex); 348 MapCell cell = new MapCell(columnIndex, row); 349 350 cell.setValue(aValue); 351 fireTableCellUpdated(columnIndex, rowIndex); 352 } 353 } 354 355 363 protected MapRow getRow(int rowIndex) { 364 MapRow row = null; 365 366 if (!isTableEmpty()) { 367 if (rowVector.size() > rowIndex) { 368 row = (MapRow) rowVector.elementAt(rowIndex); 369 } 370 } 371 return row; 372 } 373 374 protected void readMap() { 375 int rowCount = rowVector.size(); 376 377 map = new String [rowCount][2]; 378 MapRow row = null; 379 380 for (int i = 0; i < rowCount; i++) { 381 row = (MapRow) rowVector.elementAt(i); 382 map[i][0] = row.getFolder(); 383 map[i][1] = row.getPackageName(); 384 } 385 } 386 387 protected void populateModel() { 388 while (getRowCount() > 0) { 389 removeRow(0); 390 } 391 if (map != null) { 392 int rowCount = map.length; 393 394 for (int i = 0; i < rowCount; i++) { 395 addRow(map[i][0], map[i][1]); 396 } 397 } 398 } 399 400 private boolean isTableEmpty() { 403 boolean empty = true; 404 405 if (rowVector != null) { 406 if (rowVector.size() > 0) { 407 empty = false; 408 } 409 } 410 return empty; 411 } 412 413 private void addRow(String f, String p) { 414 MapRow newRow = null; 415 416 newRow = new MapRow(f, p); 417 rowVector.addElement(newRow); 418 int size = rowVector.size(); 419 420 fireTableDataChanged(); 421 } 422 423 private void removeRow(int index) { 424 rowVector.removeElementAt(index); 425 fireTableDataChanged(); 426 } 427 428 } 429 430 437 class MapRow { 438 private String fromFolder = new String (); 439 private String toPackage = new String (); 440 441 448 public MapRow(String f, String p) { 449 fromFolder = f; 450 toPackage = p; 451 } 452 453 459 public String getFolder() { 460 return fromFolder; 461 } 462 463 469 public void setFolder(String f) { 470 fromFolder = f; 471 } 472 473 479 public String getPackageName() { 480 return toPackage; 481 } 482 483 489 public void setPackageName(String p) { 490 toPackage = p; 491 } 492 493 } 494 495 502 class MapCell { 503 private MapRow row; 504 private int column; 505 506 513 public MapCell(int c, MapRow r) { 514 column = c; 515 row = r; 516 } 517 518 526 public boolean setValue(Object value) { 527 boolean set = true; 528 529 switch (column) { 530 case 0: 531 row.setFolder((String ) value); 532 break; 533 case 1: 534 row.setPackageName((String ) value); 535 break; 536 default: 537 set = false; 538 break; 539 } 540 return set; 541 } 542 543 549 public Object getValue() { 550 Object value = null; 551 552 switch (column) { 553 case 0: 554 value = row.getFolder(); 555 break; 556 case 1: 557 value = row.getPackageName(); 558 break; 559 } 560 return value; 561 } 562 563 protected MapRow getRow() { 567 return row; 568 } 569 570 } 571 } 572 | Popular Tags |