1 19 24 25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common; 26 27 import java.util.List ; 28 import java.util.ResourceBundle ; 29 30 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean; 31 import org.netbeans.modules.j2ee.sun.share.configbean.ASDDVersion; 32 33 34 57 public class GenericTableModel extends BeanTableModel { 58 59 private String parentPropertyName; 60 private ParentPropertyFactory parentPropertyFactory; 61 private boolean indexedChildren; 62 private int keyIndex; 63 64 private List properties; 65 private String [] columnNames; 66 67 private ASDDVersion appServerVersion; 68 69 78 public GenericTableModel(ParentPropertyFactory ppf, List p) { 79 this(null, ppf, p, false, 0); 80 } 81 82 94 public GenericTableModel(String ppn, ParentPropertyFactory ppf, List p) { 95 this(ppn, ppf, p, false, 0); 96 } 97 98 105 public GenericTableModel(String ppn, List p) { 106 this(ppn, null, p, true, 0); 107 } 108 109 122 public GenericTableModel(String ppn, ParentPropertyFactory ppf, List p, boolean indexed, int key) { 123 parentPropertyName = ppn; 124 parentPropertyFactory = ppf; 125 properties = p; 126 indexedChildren = indexed; 127 keyIndex = key; 128 129 columnNames = new String [p.size()]; 130 for(int i = 0; i < properties.size(); i++) { 131 columnNames[i] = ((TableEntry) properties.get(i)).getColumnName(); 132 } 133 } 134 135 144 public void setData(CommonDDBean parent, ASDDVersion asVersion) { 145 assert asVersion != null : "Application Server version cannot be null!!!"; 146 appServerVersion = asVersion; 147 148 if(parentPropertyName != null) { 149 CommonDDBean [] children = (CommonDDBean []) parent.getValues(parentPropertyName); 150 setData(parent, children); 151 } 152 } 153 154 163 public void setData(List rows, ASDDVersion asVersion) { 164 assert asVersion != null : "Application Server version cannot be null!!!"; 165 appServerVersion = asVersion; 166 167 CommonDDBean [] children = null; 168 169 if(rows != null) { 170 children = (CommonDDBean []) rows.toArray(new CommonDDBean[0]); 171 } 172 173 setData(null, children); 174 } 175 176 185 public void setData(CommonDDBean [] rows, ASDDVersion asVersion) { 186 assert asVersion != null : "Application Server version cannot be null!!!"; 187 appServerVersion = asVersion; 188 189 CommonDDBean [] children = null; 190 191 if(rows != null && rows.length > 0) { 192 children = new CommonDDBean [rows.length]; 193 System.arraycopy(rows, 0, children, 0, rows.length); 194 } 195 196 setData(null, children); 197 } 198 199 206 public void setDataBaseBean(CommonDDBean parent, ASDDVersion asVersion) { 207 assert asVersion != null : "Application Server version cannot be null!!!"; 208 appServerVersion = asVersion; 209 210 setData(parent, new CommonDDBean [0]); 211 fireTableDataChanged(); 212 } 213 214 public List getData() { 215 return getChildren(); 216 } 217 218 public CommonDDBean getDataBaseBean() { 219 CommonDDBean bbParent = null; 220 221 Object parent = getParent(); 222 if(parent instanceof CommonDDBean) { 223 bbParent = (CommonDDBean) parent; 224 } 225 226 return bbParent; 227 } 228 229 public ASDDVersion getAppServerVersion() { 230 return appServerVersion; 231 } 232 233 public int getRowCount() { 234 int result = super.getRowCount(); 235 236 if(indexedChildren) { 239 CommonDDBean parent = (CommonDDBean) getParent(); 240 if(parent != null) { 243 result = parent.size(parentPropertyName); 244 } 245 } 246 247 return result; 248 } 249 250 protected String [] getColumnNames() { 251 return columnNames; 252 } 253 254 protected List getPropertyDefinitions() { 255 return properties; 256 } 257 258 public Object getValueAt(int row, int column){ 259 Object result = null; 260 TableEntry columnEntry = (TableEntry) properties.get(column); 261 262 if(indexedChildren) { 263 CommonDDBean parent = (CommonDDBean) getParent(); 264 result = columnEntry.getEntry(parent, row); 266 } else { 267 CommonDDBean param = (CommonDDBean) getChildren().get(row); 268 if(param != null) { 269 result = columnEntry.getEntry(param); 270 } 271 } 272 273 return result; 274 } 275 276 278 public Object addRow(Object [] values) { 279 CommonDDBean param = null; 280 281 if(parentPropertyFactory != null) { 282 param = parentPropertyFactory.newParentProperty(appServerVersion); 283 for(int i = 0, max = properties.size(); i < max; i++) { 284 ((TableEntry) properties.get(i)).setEntry(param, values[i]); 285 } 286 if(parentPropertyName != null) { 287 ((CommonDDBean) getParent()).addValue(parentPropertyName, param); 288 } 289 290 getChildren().add(param); 291 } else if(indexedChildren) { 292 CommonDDBean parent = (CommonDDBean) getParent(); 293 int newRow = parent.addValue(parentPropertyName, Boolean.TRUE); 294 for(int i = 0, max = properties.size(); i < max; i++) { 295 TableEntry columnEntry = (TableEntry) properties.get(i); 296 columnEntry.setEntry(parent, newRow, values[i]); 298 } 299 } 300 301 int rowChanged = getRowCount() - 1; 302 fireTableRowsInserted(rowChanged, rowChanged); 303 304 return param; 305 } 306 307 public void editRow(int row, Object [] values) { 308 if(indexedChildren) { 309 CommonDDBean parent = (CommonDDBean) getParent(); 310 for(int i = 0, max = properties.size(); i < max; i++) { 311 TableEntry columnEntry = (TableEntry) properties.get(i); 312 columnEntry.setEntry(parent, row, values[i]); 314 } 315 } else { 316 CommonDDBean param = (CommonDDBean) getChildren().get(row); 317 318 if(param != null) { 319 for(int i = 0; i < properties.size(); i++) { 320 ((TableEntry) properties.get(i)).setEntry(param, values[i]); 321 } 322 } 323 } 324 325 fireTableRowsUpdated(row, row); 326 } 327 328 public void removeRow(int row) { 329 CommonDDBean parent = (CommonDDBean) getParent(); 330 331 if(indexedChildren) { 332 parent.removeValue(parentPropertyName, row); 333 } else { 334 List children = getChildren(); 335 336 if(parentPropertyName != null) { 337 parent.removeValue(parentPropertyName, children.get(row)); 338 } 339 340 children.remove(row); 341 } 342 343 fireTableRowsDeleted(row, row); 344 } 345 346 347 public Object [] getValues(int row) { 348 int numColumns = properties.size(); 349 Object [] values = new Object [numColumns]; 350 351 if(indexedChildren) { 352 CommonDDBean parent = (CommonDDBean) getParent(); 353 354 for(int i = 0; i < numColumns; i++) { 355 TableEntry columnEntry = (TableEntry) properties.get(i); 356 values[i] = columnEntry.getEntry(parent, row); 358 } 359 } else { 360 CommonDDBean param = (CommonDDBean) getChildren().get(row); 361 362 if(param != null) { 363 for(int i = 0; i < numColumns; i++) { 364 values[i] = ((TableEntry) properties.get(i)).getEntry(param); 365 } 366 } 367 } 368 369 return values; 370 } 371 372 public boolean alreadyExists(Object [] values) { 373 boolean exists = false; 374 375 if(keyIndex != -1 && values != null && values[keyIndex] != null) { 376 return alreadyExistsImpl(values[keyIndex].toString()); 377 } 378 379 return exists; 380 } 381 382 public boolean alreadyExists(String keyPropertyValue) { 383 boolean exists = false; 384 385 if(keyIndex != -1) { 386 exists = alreadyExistsImpl(keyPropertyValue); 387 } 388 389 return exists; 390 } 391 392 private boolean alreadyExistsImpl(String keyPropertyValue) { 393 boolean exists = false; 394 395 if(keyPropertyValue != null) { 396 TableEntry entry = (TableEntry) properties.get(keyIndex); 397 if(indexedChildren) { 398 CommonDDBean parent = (CommonDDBean) getParent(); 399 401 for(int i = 0, count = getRowCount(); i < count; i++) { 402 if(keyPropertyValue.equals((String ) entry.getEntry(parent, i))) { 404 exists = true; 405 break; 406 } 407 } 408 } else { 409 for(int i = 0, count = getRowCount(); i < count; i++) { 410 CommonDDBean rowBean = (CommonDDBean) getChildren().get(i); 411 if(rowBean != null) { 412 if(keyPropertyValue.equals(entry.getEntry(rowBean))) { 413 exists = true; 414 break; 415 } 416 } 417 } 418 } 419 } 420 421 return exists; 422 } 423 424 427 public static abstract class TableEntry { 428 protected final ResourceBundle bundle; 429 protected final String resourceBase; 430 protected final String parentPropertyName; 431 protected final String propertyName; 432 protected final String columnName; 433 protected final boolean requiredFieldFlag; 434 protected final boolean nameFieldFlag; 435 436 public TableEntry(String pn, String c) { 437 this(pn, c, false); 438 } 439 440 public TableEntry(String pn, String c, boolean required) { 441 this(null, pn, c, required); 442 } 443 444 public TableEntry(String ppn, String pn, String c, boolean required) { 445 this(ppn, pn, c, required, false); 446 } 447 448 public TableEntry(String ppn, String pn, String c, boolean required, boolean isName) { 449 parentPropertyName = ppn; 450 bundle = null; 451 resourceBase = null; 452 propertyName = pn; 453 columnName = c; 454 requiredFieldFlag = required; 455 nameFieldFlag = isName; 456 } 457 458 public TableEntry(String ppn, String pn, ResourceBundle resBundle, 459 String base, boolean required, boolean isName) { 460 parentPropertyName = ppn; 461 propertyName = pn; 462 bundle = resBundle; 463 resourceBase = base; 464 columnName = bundle.getString("LBL_" + resourceBase); requiredFieldFlag = required; 466 nameFieldFlag = isName; 467 } 468 469 public String getPropertyName() { 470 return propertyName; 471 } 472 473 public String getColumnName() { 474 return columnName; 475 } 476 477 public boolean isRequiredField() { 478 return requiredFieldFlag; 479 } 480 481 public boolean isNameField() { 482 return nameFieldFlag; 483 } 484 485 public String getLabelName() { 486 return columnName + " :"; } 488 489 public char getLabelMnemonic() { 490 assert bundle != null : "Coding error: incorrect column definition for " + columnName; return bundle.getString("MNE_" + resourceBase).charAt(0); } 493 494 public String getAccessibleName() { 495 assert bundle != null : "Coding error: incorrect column definition for " + columnName; return bundle.getString("ACSN_" + resourceBase); } 498 499 public String getAccessibleDescription() { 500 assert bundle != null : "Coding error: incorrect column definition for " + columnName; return bundle.getString("ACSD_" + resourceBase); } 503 504 public abstract Object getEntry(CommonDDBean parent); 505 public abstract void setEntry(CommonDDBean parent, Object value); 506 507 public abstract Object getEntry(CommonDDBean parent, int row); 508 public abstract void setEntry(CommonDDBean parent, int row, Object value); 509 510 } 511 512 515 public static class ValueEntry extends TableEntry { 516 public ValueEntry(String pn, String c) { 517 super(pn, c, false); 518 } 519 520 public ValueEntry(String pn, String c, boolean required) { 521 super(pn, c, required); 522 } 523 524 public ValueEntry(String ppn, String pn, String c, boolean required) { 525 super(ppn, pn, c, required); 526 } 527 528 public ValueEntry(String ppn, String pn, String c, boolean required, boolean isName) { 529 super(ppn, pn, c, required, isName); 530 } 531 532 public ValueEntry(String ppn, String pn, ResourceBundle resBundle, 533 String resourceBase, boolean required, boolean isName) { 534 super(ppn, pn, resBundle, resourceBase, required, isName); 535 } 536 537 public Object getEntry(CommonDDBean parent) { 538 return parent.getValue(propertyName); 539 } 540 541 public void setEntry(CommonDDBean parent, Object value) { 542 if(value instanceof String && ((String ) value).length() == 0) { 543 value = null; 544 } 545 parent.setValue(propertyName, value); 546 } 547 548 public Object getEntry(CommonDDBean parent, int row) { 549 return parent.getValue(propertyName, row); 550 } 551 552 public void setEntry(CommonDDBean parent, int row, Object value) { 553 parent.setValue(propertyName, row, value); 554 } 555 556 } 557 558 561 public static class AttributeEntry extends TableEntry { 562 public AttributeEntry(String pn, String c) { 563 super(pn, c); 564 } 565 566 public AttributeEntry(String pn, String c, boolean required) { 567 super(pn, c, required); 568 } 569 570 public AttributeEntry(String ppn, String pn, String c, boolean required) { 571 super(ppn, pn, c, required); 572 } 573 574 public AttributeEntry(String ppn, String pn, String c, boolean required, boolean isName) { 575 super(ppn, pn, c, required, isName); 576 } 577 578 public AttributeEntry(String ppn, String pn, ResourceBundle resBundle, 579 String resourceBase, boolean required, boolean isName) { 580 super(ppn, pn, resBundle, resourceBase, required, isName); 581 } 582 583 public Object getEntry(CommonDDBean parent) { 584 return parent.getAttributeValue(propertyName); 585 } 586 587 public void setEntry(CommonDDBean parent, Object value) { 588 String attrValue = null; 589 if(value != null) { 590 attrValue = value.toString(); 591 } 592 parent.setAttributeValue(propertyName, attrValue); 593 } 594 595 public Object getEntry(CommonDDBean parent, int row) { 596 return parent.getAttributeValue(parentPropertyName, row, propertyName); 597 } 598 599 public void setEntry(CommonDDBean parent, int row, Object value) { 600 String attrValue = null; 601 if(value != null) { 602 attrValue = value.toString(); 603 } 604 605 parent.setAttributeValue(parentPropertyName, row, propertyName, attrValue); 606 } 616 } 617 618 619 624 public interface ParentPropertyFactory { 625 626 629 public CommonDDBean newParentProperty(ASDDVersion asVersion); 630 631 } 632 } 633 | Popular Tags |