1 package com.ca.directory.jxplorer.viewer.tableviewer; 2 3 import java.util.*; 4 import javax.swing.*; 5 import javax.swing.table.*; 6 import javax.swing.event.*; 7 8 import javax.naming.*; 9 import javax.naming.directory.*; 10 11 import com.ca.commons.naming.*; 12 13 public class AttributeTableModel extends AbstractTableModel 14 { 15 16 protected boolean dataChanged = false; 18 int noRows = 0; 19 20 DXEntry oldEntry; 22 Vector attributeTypes = new Vector(); Vector attributeValues = new Vector(); 25 29 30 int numberNamingValues; 31 32 33 42 43 String [] namingTypes; 44 45 48 49 String [] namingRawValues; 50 51 54 55 public int getRDNSize() 56 { 57 return numberNamingValues; 58 } 59 60 64 65 public Class getColumnClass(int c) 66 { 67 return (c==1)?AttributeValue.class: AttributeType.class; 68 } 69 70 73 public int getColumnCount() { return 2; } 74 75 78 public int getRowCount() { return noRows;} 79 80 84 public void setValueAt(Object aValue,int row,int col) 85 { 86 dataChanged = true; 87 88 if (col == 1) { 90 if (aValue instanceof AttributeValue) { 92 attributeValues.set(row, aValue); 93 } 94 else { 96 ((AttributeValue)attributeValues.elementAt(row)).update(aValue); 97 } 98 fireTableCellUpdated(row, col); 99 } 100 } 101 102 107 108 public String getStringValueAt(int row, int col) 109 { 110 String s = getValueAt(row,col).toString(); 111 if ((s == null) || (s.length()==0)) 112 s = " "; 113 return s; 114 } 115 116 119 public Object getValueAt(int row, int col) 120 { 121 return (col==0)?attributeTypes.elementAt(row):attributeValues.elementAt(row); 122 } 123 124 127 public boolean isCellEditable(int row, int col) 128 { 129 if (col == 0) return false; if (col > 1) return false; if (attributeTypes.elementAt(row).toString().equalsIgnoreCase("objectclass")) 132 return false; 134 return true; 135 } 136 137 140 public String getColumnName(int columnIndex) 141 { 142 return (columnIndex == 0)?("attribute type"):("value"); 143 } 144 145 148 149 public void reset() 150 { 151 dataChanged = false; 152 153 156 157 for (int i=0; i<attributeValues.size(); i++) 158 ((AttributeValue)attributeValues.elementAt(i)).reset(); 159 160 164 165 RDN rdn = oldEntry.getRDN(); 166 167 if (rdn.equals(getRDN()) == false) 168 setRDN(rdn); 169 170 173 174 fireChange(); 175 } 176 177 178 181 public void clear() 182 { 183 dataChanged = false; 186 noRows = 0; 187 attributeTypes.clear(); 188 attributeValues.clear(); 189 fireChange(); 190 191 } 192 193 196 197 public void insertAttributes(DXEntry entry) 198 { 199 200 203 204 if (entry==null) return; 205 206 209 210 oldEntry = entry; 211 noRows = 0; 212 attributeTypes.clear(); 213 attributeValues.clear(); 214 215 219 220 try 221 { 222 227 228 RDN rdn = entry.getRDN(); 229 setRDN(rdn); 230 231 234 235 DXNamingEnumeration mandatory = (DXNamingEnumeration)entry.getMandatory(); 236 237 while (mandatory.hasMore()) 238 insertAttribute(((DXAttribute)mandatory.next()), AttributeType.MANDATORY); 239 240 243 244 DXNamingEnumeration active = (DXNamingEnumeration)entry.getAllNonNull(); 245 active.sort(); 246 247 while (active.hasMore()) 248 { 249 DXAttribute temp = (DXAttribute) active.next(); 250 251 if (mandatory.contains(temp)==false && (temp.size()>0) && (temp.get() != null)) 252 { 253 temp.sort(); 254 insertAttribute(temp, AttributeType.NORMAL); 255 } 256 } 257 258 261 DXNamingEnumeration possible = (DXNamingEnumeration)entry.getAll(); 262 possible.sort(); 263 264 while (possible.hasMore()) 265 { 266 DXAttribute temp = (DXAttribute) possible.next(); 267 if (mandatory.contains(temp)==false && ((temp.size()==0) || (temp.get() == null))) 268 { 269 insertAttribute(temp, AttributeType.NORMAL); 270 } 271 } 272 273 fireChange(); 274 275 dataChanged = false; 278 } 279 catch (NamingException e) 280 { 281 System.err.println("Naming Exception in AttributeTableModel: " + e); 282 } 283 catch (Exception e2) 284 { 285 System.err.println("Unexpected Exception in AttributeTableModel: " + e2); 286 e2.printStackTrace(); 287 } 288 289 } 290 291 297 298 protected void setRDN(RDN rdn) 299 { 300 numberNamingValues = rdn.size(); namingTypes = new String [numberNamingValues]; 302 namingRawValues = new String [numberNamingValues]; 303 304 for (int i=0; i<numberNamingValues; i++) 305 { 306 namingTypes[i] = rdn.getAtt(i); 307 namingRawValues[i] = rdn.getRawVal(i); 308 } 309 } 310 311 312 313 317 318 public void insertAttribute(DXAttribute att, int type) 319 throws NamingException 320 { 321 String namingValue = null; 324 String ID = att.getID(); 325 NamingEnumeration values = att.getAll(); 326 AttributeValue newAV = new AttributeValue(ID, ""); ; 328 329 332 333 if (att.size() == 0) { 335 newAV = new AttributeValue(ID, ""); 336 if (att.isString() == false) 337 { 338 newAV.setBinary(true); 339 } 340 addAttribute(ID, newAV, type); 341 } 342 else { 344 347 348 namingValue = getAnyNamingValue(ID); 349 if (namingValue != null && att.isString() == false) 352 throw new NamingException("Binary naming attributes not supported in JXplorer: can't use attribute " + ID + " to name an entry"); 353 354 while (values.hasMore()) 355 { 356 357 newAV = new AttributeValue(ID, values.next()); 358 359 362 363 if (att.isString()==false) 364 { 365 newAV.setBinary(true); 366 } 367 368 372 373 if (namingValue != null && newAV.getStringValue().equalsIgnoreCase(namingValue)) 374 newAV.setNamingStatus(true); 375 376 379 380 addAttribute(ID, newAV, type); 381 } 382 } 383 if (att instanceof DXAttribute) 385 { 386 if (((DXAttribute)att).hasOptions()) 387 newAV.setOptions(((DXAttribute)att).getOptions()); 388 newAV.setBinary(!((DXAttribute)att).isString()); 389 } 390 } 391 392 393 400 private String getAnyNamingValue(String ID) 401 { 402 for (int i=0; i<numberNamingValues; i++) 403 if (ID.equalsIgnoreCase(namingTypes[i])) 404 return namingRawValues[i]; 405 406 return null; } 408 409 410 411 public void addAttribute(String ID, AttributeValue val, int type) 412 { 413 attributeTypes.add(new AttributeType(ID, (type==AttributeType.MANDATORY))); 414 attributeValues.add(val); 415 noRows++; 416 } 417 418 public void addAttribute(String ID, AttributeValue val, int type, int indexPos) 419 { 420 attributeTypes.add(indexPos, new AttributeType(ID, (type==AttributeType.MANDATORY))); 421 attributeValues.add(indexPos, val); 422 noRows++; 423 } 424 425 public void deleteAttribute(String ID, int indexPos) 426 { 427 if (attributeTypes.elementAt(indexPos).toString().equals(ID)) 428 { 429 ((AttributeValue)attributeValues.elementAt(indexPos)).update(new String ("")); 430 } 431 else 432 System.err.println("Internal error: attempt to delete attribute with invalid ID in AttributeTableModel"+ 433 "\n att name = " + attributeTypes.elementAt(indexPos).toString() + " ID = " + ID); 434 } 435 436 public void fireChange() 437 { 438 dataChanged = true; 439 440 fireTableChanged(new TableModelEvent(this)); 441 } 442 443 public DXEntry getOldEntry() 444 { 445 return oldEntry; 446 } 447 448 455 456 public RDN getRDN() 457 { 458 String rdn = ""; 459 for (int i=0; i<attributeValues.size(); i++) 460 { 461 AttributeValue entryValue = (AttributeValue)attributeValues.get(i); 462 if (entryValue.isNaming()) 463 { 464 if (rdn.length()>0) 465 rdn += "+"; 466 rdn += attributeTypes.get(i).toString() + "=" + NameUtility.escape(entryValue.getStringValue()); 467 } 468 } 469 470 474 if ("".equals(rdn)) 475 rdn = oldEntry.getRDN().toString(); 476 477 478 return new RDN(rdn); 479 492 } 493 494 497 498 public DXEntry getNewEntry() 499 { 500 504 505 DN newDN = new DN(oldEntry.getDN()); 506 507 508 RDN newRDN = getRDN(); 509 510 511 512 newDN.setRDN(newRDN, newDN.size()-1); 513 514 517 518 DXEntry newEntry = new DXEntry(newDN); 519 520 524 525 AttributeValue test; 526 String id; 527 528 for (int i=0; i<attributeTypes.size(); i++) 529 { 530 test = (AttributeValue)attributeValues.elementAt(i); 531 if (!test.isEmpty()) 532 { 533 id = attributeTypes.elementAt(i).toString(); 534 BasicAttribute exists = (BasicAttribute)newEntry.get(id); 535 if (exists == null) newEntry.put(new BasicAttribute(id, test.value())); 537 else 538 exists.add(test.value()); 539 } 540 } 541 542 if (oldEntry.getStatus() == DXEntry.NEW) { 544 newEntry.setStatus(DXEntry.NEW); 545 } 546 547 return newEntry; 548 } 549 550 554 public Attribute getAttribute(String ID) 555 { 556 BasicAttribute returnAtt = new BasicAttribute(ID); 557 for (int i=0; i<attributeTypes.size(); i++) 558 if (ID.equals(attributeTypes.elementAt(i).toString())) 559 { 560 Object o = ((AttributeValue)attributeValues.elementAt(i)).value(); 561 562 if (o!= null && o instanceof String ) 564 { 565 if (((String )o).length() == 0) 566 o = null; 567 } 568 569 if (o != null) returnAtt.add(o); 571 572 } 573 574 return returnAtt; 575 } 576 577 582 583 public boolean isMandatory(String attributeTypeName) 584 { 585 for ( int i=0; i<attributeTypes.size(); i++) 586 { 587 if (((AttributeType)attributeTypes.elementAt(i)).toString().equals(attributeTypeName)) 588 { 589 return ((AttributeType)attributeTypes.elementAt(i)).isMandatory(); 590 } 591 } 592 System.err.println("unable to find type name " + attributeTypeName); 593 return false; } 595 596 599 600 608 public boolean checkMandatoryAttributesSet() 609 { 610 AttributeType type, testType; 611 AttributeValue value; 612 String ID = ""; 613 boolean inDoubt = false; 614 int i=0; 615 616 while (i<noRows) { 618 type = (AttributeType)attributeTypes.elementAt(i); 619 620 if (type.isMandatory()) { 622 ID = type.toString(); 623 inDoubt = true; 624 testType = type; 625 626 while (ID.equals(testType.toString()) && i<noRows) { if (inDoubt) { 630 value = (AttributeValue)attributeValues.elementAt(i); 631 if (value.isEmpty()==false) inDoubt = false; 633 } 634 i++; 635 if (i<noRows) testType = (AttributeType)attributeTypes.elementAt(i); 636 } 637 638 if (inDoubt) return false; } 641 else 642 { 643 i++; 644 } 645 } 646 647 return true; } 649 650 654 655 public void removeNamingComponent(AttributeType currentType, AttributeValue currentValue) 656 { 657 658 try 659 { 660 String type = currentType.getValue(); 661 String value = currentValue.getStringValue(); 662 663 if ("".equals(type) || "".equals(value)) return; 665 if (numberNamingValues == 1) return; 667 668 dataChanged = true; 669 670 for (int i=0; i<numberNamingValues; i++) 671 { 672 if (type.equals(namingTypes[i]) && value.equals(namingRawValues[i])) 673 { 674 int removeRow = i; 675 namingTypes = removeRowFromArray(namingTypes, removeRow); 676 namingRawValues = removeRowFromArray(namingRawValues, removeRow); 677 numberNamingValues--; 678 break; 679 } 680 } 681 682 for (int i=0; i<attributeValues.size(); i++) 683 { 684 AttributeValue attval = (AttributeValue)attributeValues.get(i); 685 if (attval.getID().equals(type)) 686 if (attval.getStringValue().equals(value)) 687 { 688 attval.setNamingStatus(false); 689 } 690 } 691 } 692 catch (Exception e) { 694 e.printStackTrace(); 695 return; 696 } 697 698 } 699 700 701 protected static String [] removeRowFromArray(String [] array, int removeRow) 702 { 703 int originalLength = array.length; 704 if (removeRow < 0 || removeRow >= originalLength) 705 return array; 706 707 String [] temp = new String [array.length-1]; 708 709 if (removeRow > 0) 710 System.arraycopy(array, 0, temp, 0, removeRow); 711 712 if (removeRow < originalLength-1) 713 System.arraycopy(array, removeRow+1, temp, removeRow, (originalLength-removeRow-1)); 714 715 return temp; 716 } 717 718 719 public void dumpNamingArrays() 720 { 721 System.out.println("dump naming array"); 722 for (int i=0; i<numberNamingValues; i++) 723 { 724 System.out.println(i + " type " + namingTypes[i]); 725 System.out.println(i + " value " + namingRawValues[i]); 726 } 727 } 728 729 730 public void addNamingComponent(AttributeType currentType, AttributeValue currentValue) 731 { 732 733 String type = currentType.getValue(); 734 String value = currentValue.getStringValue(); 735 736 if ("".equals(type) || "".equals(value)) return; 738 739 dataChanged = true; 740 741 String [] tempTypes = new String [numberNamingValues+1]; 742 String [] tempRawValues = new String [numberNamingValues+1]; 743 744 System.arraycopy(namingTypes, 0, tempTypes, 0, numberNamingValues); 745 System.arraycopy(namingRawValues, 0, tempRawValues, 0, numberNamingValues); 746 747 tempTypes[numberNamingValues] = type; 748 tempRawValues[numberNamingValues] = value; 749 750 numberNamingValues++; 751 752 namingTypes = tempTypes; 753 namingRawValues = tempRawValues; 754 755 currentValue.setNamingStatus(true); 756 } 757 758 759 760 764 765 public boolean changedByUser() 766 { 767 return dataChanged; 768 } 769 } | Popular Tags |