1 package com.ca.directory.jxplorer.viewer; 2 3 import com.ca.commons.cbutil.*; 4 import com.ca.commons.naming.*; 5 import com.ca.directory.jxplorer.*; 6 import com.ca.directory.jxplorer.tree.NewEntryWin; 7 import com.ca.directory.jxplorer.viewer.tableviewer.*; 8 import com.ca.directory.jxplorer.viewer.tableviewer.AttributeValue; 9 10 import javax.naming.NamingException ; 11 import javax.naming.directory.Attribute ; 12 import javax.swing.*; 13 import java.awt.*; 14 import java.awt.event.*; 15 import java.io.File ; 16 import java.util.logging.Logger ; 17 import java.util.logging.Level ; 18 19 23 24 25 31 public class TableAttributeEditor extends JPanel 32 implements DataSink, PluggableEditor { 34 private static Logger log = Logger.getLogger(TableAttributeEditor.class.getName()); 35 36 JTable attributeTable; 37 AttributeTableModel tableData; 38 JScrollPane tableScroller; 39 CBButton submit, reset, changeClass, opAttrs; JFrame owner; 41 JDialog virtualEntryDialog = null; 42 43 46 boolean virtualEntry = false; 47 48 51 DXEntry currentEntry = null; 52 53 56 DN currentDN = null; 57 58 61 public DataSource dataSource; 62 63 67 DXEntry classChangedOriginalEntry = null; 68 69 SmartPopupTableTool popupTableTool; 70 71 ClassLoader myLoader; 72 73 final AttributeValueCellEditor myEditor; 74 75 79 public TableAttributeEditor(JFrame MyOwner) 80 { 81 88 super(); 89 90 owner = MyOwner; 91 92 94 tableData = new AttributeTableModel(); 95 96 attributeTable = new JTable(tableData); 97 99 popupTableTool = new SmartPopupTableTool(attributeTable, tableData, (JXplorer) owner); 100 101 final AttributeTypeCellRenderer typeRenderer = new AttributeTypeCellRenderer(); 103 104 attributeTable.setDefaultRenderer(AttributeType.class, typeRenderer); 105 106 final AttributeValueCellRenderer valueRenderer = new AttributeValueCellRenderer(); 108 109 attributeTable.setDefaultRenderer(AttributeValue.class, valueRenderer); 110 111 myEditor = new AttributeValueCellEditor(owner); 113 114 attributeTable.setDefaultEditor(AttributeValue.class, myEditor); 115 116 attributeTable.getTableHeader().setReorderingAllowed(false); 117 118 currentDN = null; 119 120 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 121 buttonPanel.add(submit = new CBButton(CBIntText.get("Submit"), CBIntText.get("Submit your changes to the Directory."))); 122 buttonPanel.add(reset = new CBButton(CBIntText.get("Reset"), CBIntText.get("Reset this entry i.e. cancels any changes."))); 123 buttonPanel.add(changeClass = new CBButton(CBIntText.get("Change Classes"), CBIntText.get("Change the Object Class of this entry."))); 124 buttonPanel.add(opAttrs = new CBButton(CBIntText.get("Properties"), CBIntText.get("View the Operational Attributes of this entry."))); 125 126 opAttrs.setDefaultCapable(false); 131 submit.setDefaultCapable(false); 132 reset.setDefaultCapable(false); 133 changeClass.setDefaultCapable(false); 134 135 setLayout(new BorderLayout(10, 10)); 136 137 tableScroller = new JScrollPane(); 138 attributeTable.setBackground(Color.white); 139 tableScroller.setPreferredSize(new Dimension(300, 285)); 140 tableScroller.setViewportView(attributeTable); 141 add(tableScroller, BorderLayout.CENTER); 142 add(buttonPanel, BorderLayout.SOUTH); 143 144 setVisible(true); 145 146 opAttrs.addActionListener(new ActionListener() 148 { 149 public void actionPerformed(ActionEvent e) 150 { 151 displayOperationalAttributes(); 152 } 153 }); 154 155 reset.addActionListener(new ActionListener() 156 { 157 public void actionPerformed(ActionEvent e) 158 { 159 myEditor.stopCellEditing(); 160 tableData.reset(); 162 } 163 }); 164 165 submit.addActionListener(new ActionListener() 166 { 167 public void actionPerformed(ActionEvent e) 168 { 169 doSubmit(); 170 } 171 }); 172 173 changeClass.addActionListener(new ActionListener() 176 { 177 public void actionPerformed(ActionEvent e) 178 { 179 changeClass(); 180 } 181 }); 182 183 attributeTable.addMouseListener(new MouseAdapter() 184 { 185 public void mousePressed(MouseEvent e) 186 { 187 if (!doPopupStuff(e)) super.mousePressed(e); 188 } 189 190 public void mouseReleased(MouseEvent e) 191 { 192 if (!doPopupStuff(e)) super.mouseReleased(e); 193 } 194 195 public boolean doPopupStuff(MouseEvent e) 197 { 198 if (e.isPopupTrigger() == false) return false; 199 200 int row = attributeTable.rowAtPoint(new Point(e.getX(), e.getY())); 201 202 attributeTable.clearSelection(); 203 attributeTable.addRowSelectionInterval(row, row); 204 attributeTable.repaint(); 205 206 popupTableTool.registerCurrentRow((AttributeType) attributeTable.getValueAt(row, 0), (AttributeValue) attributeTable.getValueAt(row, 1), row, tableData.getRDN()); popupTableTool.show(attributeTable, e.getX(), e.getY()); 208 popupTableTool.registerCellEditor(myEditor); return true; 210 } 211 }); 212 } 213 214 217 public void changeClass() 218 { 227 myEditor.stopCellEditing(); 228 229 if (virtualEntry) 230 { 231 doVirtualEntryDisplay(); 232 return; 233 } 234 235 242 if (classChangedOriginalEntry == null) 243 classChangedOriginalEntry = tableData.getOldEntry(); 244 245 DXEntry newEntry = tableData.getNewEntry(); 246 DN newDN = newEntry.getDN(); 247 248 252 if (newDN.equals(classChangedOriginalEntry.getDN()) == false) 253 { 254 if (promptForSave(false) == false) { 257 tableData.reset(); 259 newEntry = tableData.getNewEntry(); 260 newDN = newEntry.getDN(); 261 } 262 else { classChangedOriginalEntry = tableData.getNewEntry(); 266 } 267 } 268 269 272 273 280 if (dataSource.getSchemaOps() == null) 281 { 282 JOptionPane.showMessageDialog(owner, CBIntText.get("Because there is no schema currently published by the\ndirectory, changing an entry's object class is unavailable."), CBIntText.get("No Schema"), JOptionPane.INFORMATION_MESSAGE); 283 return; 284 } 285 else 286 { 287 NewEntryWin userData = new NewEntryWin(dataSource, newDN, newEntry.getAsNonNullAttributes(), 288 this, CBUtility.getParentFrame(this)); 289 userData.setSize(400, 250); 290 CBUtility.center(userData, owner); userData.setVisible(true); 292 } 293 } 294 295 298 public void doSubmit() 299 { 300 if (dataSource == null) 301 { 302 CBUtility.error("No dataSource available to write changes to in Table Attribute Editor"); 303 return; 304 } 305 306 myEditor.stopCellEditing(); 307 308 if ("false".equalsIgnoreCase(JXplorer.getProperty("option.ignoreSchemaOnSubmission")) 310 && (tableData.checkMandatoryAttributesSet() == false)) 311 { 312 CBUtility.error(TableAttributeEditor.this, CBIntText.get("All Mandatory Attributes must have values!"), null); 313 return; 314 } 315 316 writeTableData(); 317 } 318 319 322 public void displayOperationalAttributes() 323 { 324 JXplorer jx = null; 325 326 if (owner instanceof JXplorer) 327 jx = (JXplorer) owner; 328 else 329 return; 330 331 String [] opAttrs = new String []{"createTimestamp", "modifyTimestamp", "creatorsName", "modifiersName", "subschemaSubentry"}; 332 int size = opAttrs.length; 333 DXEntry entry = null; 334 try 335 { 336 entry = (jx.getSearchBroker()).unthreadedReadEntry(currentDN, opAttrs); 337 } 338 catch (NamingException e) 339 { 340 CBUtility.error(TableAttributeEditor.this, CBIntText.get("Unable to read entry " + currentDN), e); 341 } 342 StringBuffer buffy = new StringBuffer ("DN: " + currentDN.toString() + "\n\n"); 343 344 for (int i = 0; i < size; i++) 346 { 347 DXAttribute att = (DXAttribute) entry.get(opAttrs[i]); 348 349 try 350 { 351 if (att != null) 352 { 353 buffy.append(opAttrs[i] + ": " + att.get().toString() + "\n\n"); 354 } 355 } 356 catch (NamingException ee) 357 { 358 log.log(Level.WARNING, "Problem accessing Operational Attributes via Table Editor\n", ee); 359 } 360 } 361 362 JTextArea area = new JTextArea(buffy.toString()); 364 area.setFont(new Font("SansSerif", Font.PLAIN, 11)); 365 area.setLineWrap(true); 366 area.setWrapStyleWord(true); 367 JScrollPane scrollPane = new JScrollPane(area); 368 scrollPane.setPreferredSize(new Dimension(300, 125)); 369 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 370 JOptionPane.showMessageDialog(jx, scrollPane, CBIntText.get("Properties (Operational Attributes)"), JOptionPane.INFORMATION_MESSAGE); 371 } 372 373 380 public boolean promptForSave(boolean reset) 381 { 382 return false; 383 389 394 424 } 425 426 430 public void doVirtualEntryDisplay() 431 { 432 virtualEntryDialog = new JDialog(owner, CBIntText.get("Virtual Entry"), true); 433 434 CBButton btnYes = new CBButton(CBIntText.get("Yes"), CBIntText.get("Click yes to make a Virtual Entry.")); 435 CBButton btnNo = new CBButton(CBIntText.get("No"), CBIntText.get("Click no to cancel without making a Virtual Entry.")); 436 437 Container pane = virtualEntryDialog.getContentPane(); 439 pane.setLayout(new BorderLayout()); 440 CBPanel panel1 = new CBPanel(); 441 CBPanel panel2 = new CBPanel(); 442 CBPanel panel3 = new CBPanel(); 443 444 panel1.add(new JLabel(CBIntText.get("This entry is a Virtual Entry. Are you sure you want to give this entry an object class?"))); 445 panel2.add(btnYes); 446 panel2.add(btnNo); 447 448 panel3.makeWide(); 449 panel3.addln(panel1); 450 panel3.addln(panel2); 451 452 pane.add(panel3); 453 454 btnYes.addActionListener(new ActionListener() 455 { 456 public void actionPerformed(ActionEvent e) 457 { 458 processVirtualEntry(); 459 } 460 }); 461 462 btnNo.addActionListener(new ActionListener() 463 { 464 public void actionPerformed(ActionEvent e) 465 { 466 shutVirtualEntryDialog(); 467 } 468 }); 469 virtualEntryDialog.setSize(475, 125); 470 CBUtility.center(virtualEntryDialog, owner); 471 virtualEntryDialog.setVisible(true); 472 } 473 474 479 public void processVirtualEntry() 480 { 481 482 NewEntryWin userData = null; 483 if (dataSource.getSchemaOps() == null) 484 { 485 JOptionPane.showMessageDialog(owner, CBIntText.get("Because there is no schema currently published by the\ndirectory, changing an entry's object class is unavailable."), CBIntText.get("No Schema"), JOptionPane.INFORMATION_MESSAGE); 486 return; 487 } 488 else 489 { 490 shutVirtualEntryDialog(); userData = new NewEntryWin(dataSource, currentEntry.getDN(), this, owner, true); 492 userData.setSize(400, 250); 493 CBUtility.center(userData, owner); userData.setVisible(true); 495 496 while (userData.isVisible()) { 498 try 499 { 500 wait(); 501 } 502 catch (Exception e) 503 { 504 userData.dispose(); 505 } 506 } 507 } 508 509 if (userData.newObjectClasses != null) { 511 try 512 { 513 DXOps dxOps = new DXOps(dataSource.getDirContext()); 514 dxOps.addAttribute(currentEntry.getDN(), userData.newObjectClasses); 515 dataSource.getEntry(currentEntry.getDN()); } 517 catch (NamingException e) 518 { 519 CBUtility.error(TableAttributeEditor.this, CBIntText.get("Unable to add new object classes to {0}.", new String []{currentEntry.getDN().toString()}), e); 520 } 521 } 522 } 523 524 527 public void shutVirtualEntryDialog() 528 { 529 if (virtualEntryDialog != null) 530 { 531 virtualEntryDialog.setVisible(false); 532 virtualEntryDialog.dispose(); 533 } 534 } 535 536 539 544 public void displayEntry(DXEntry entry, DataSource ds) 545 { 546 548 currentEntry = entry; 550 dataSource = ds; 551 552 if (entry != null && entry.size() == 0) 553 { 554 561 tableData.clear(); 563 564 submit.setEnabled(false); 566 reset.setEnabled(false); 567 changeClass.setText(CBIntText.get("Add Class")); 568 changeClass.setEnabled(true); 569 opAttrs.setEnabled(false); 570 571 virtualEntry = true; 572 573 return; 574 } 575 576 virtualEntry = false; 577 578 if (entry != null) 580 currentDN = entry.getDN(); 581 582 changeClass.setText(CBIntText.get("Change Class")); 584 585 if (classChangedOriginalEntry != null) 588 { 589 if (entry == null || entry.getDN().equals(classChangedOriginalEntry.getDN()) == false) 591 classChangedOriginalEntry = null; 592 } 593 594 605 if (tableData.changedByUser()) { boolean prompt = false; 608 609 DXEntry oldEntry = tableData.getOldEntry(); 610 611 if (oldEntry != null) 612 { 613 618 if (entry == null) 619 { 620 prompt = true; 621 } 622 else if (!oldEntry.getDN().isEmpty() && entry.getDN().equals(oldEntry.getDN()) == false) 624 { 625 DN oldParent = oldEntry.getDN().parentDN(); 626 627 DN newParent = entry.getDN().parentDN(); 628 629 if (oldParent.equals(newParent) == false) 630 { 631 prompt = true; 632 } 633 else 634 { 635 if (entry.getDN().getLowestRDN().equals(tableData.getRDN()) == false) 636 { 637 prompt = true; 638 } 639 } 640 } 641 642 if (prompt) { 644 if (promptForSave(false)) { 646 return; } 649 } 650 } 651 } 652 653 myEditor.setDataSource(ds); 655 658 if (dataSource == null || entry == null) 659 { 660 submit.setEnabled(false); 661 reset.setEnabled(false); 662 changeClass.setEnabled(false); 663 opAttrs.setEnabled(false); 664 } 665 else 666 { 667 submit.setEnabled(true); 668 reset.setEnabled(true); 669 opAttrs.setEnabled(true); 670 671 if (entry.get("objectclass") != null) changeClass.setEnabled(true); } 674 675 myEditor.stopCellEditing(); 676 677 if (entry != null) 678 { 679 entry.expandAllAttributes(); 680 currentDN = entry.getDN(); 681 682 tableData.insertAttributes(entry); 683 popupTableTool.setDN(currentDN); myEditor.setDN(currentDN); } 686 else 687 { 688 tableData.clear(); } 690 691 tableScroller.getVerticalScrollBar().setValue(0); } 693 694 public JComponent getDisplayComponent() 695 { 696 validate(); 697 repaint(); 698 return this; 699 } 700 701 public String [] getAttributeValuesAsStringArray(Attribute a) 702 throws NamingException 703 { 704 if (a == null) return new String [0]; 705 DXNamingEnumeration e = new DXNamingEnumeration(a.getAll()); 706 if (e == null) return new String [0]; 707 return e.toStringArray(); 708 } 709 710 713 public boolean objectClassesChanged(DXAttributes a, DXAttributes b) 714 { 715 boolean result = false; 716 try 717 { 718 String [] A = getAttributeValuesAsStringArray(a.getAllObjectClasses()); 719 String [] B = getAttributeValuesAsStringArray(b.getAllObjectClasses()); 720 721 Object [] test = CBArray.difference(A, B); 722 if (test.length > 0) result = true; 723 test = CBArray.difference(B, A); 724 if (test.length > 0) result = true; 725 726 727 return result; 728 } 729 catch (NamingException e) 730 { 731 log.log(Level.WARNING, "Error in TableAttributeEditor:objectClassesChanged ", e); 732 return true; 733 } 734 } 735 736 739 public void writeTableData() 740 { 741 742 myEditor.stopCellEditing(); 743 744 if (dataSource == null) { 746 CBUtility.error("no datasource to write data to in writeTableData()"); 747 return; 748 } 750 DXEntry oldEntry = tableData.getOldEntry(); 751 752 DXEntry newEntry = tableData.getNewEntry(); 753 754 756 757 if (classChangedOriginalEntry != null) 758 { 759 760 oldEntry = classChangedOriginalEntry; 763 classChangedOriginalEntry = null; 767 if (objectClassesChanged(oldEntry, newEntry)) 768 { 769 oldEntry.removeEmptyAttributes(); 770 771 newEntry.setStatus(oldEntry.getStatus()); 772 773 Object [] delSet = CBArray.difference(oldEntry.toIDStringArray(), newEntry.toIDStringArray()); 774 775 777 778 if ((delSet != null) && (delSet.length > 0)) 779 { 780 for (int i = 0; i < delSet.length; i++) 781 { 782 newEntry.put(new DXAttribute(delSet[i].toString())); } 784 } 785 } 786 } 787 788 dataSource.modifyEntry(oldEntry, newEntry); 789 } 790 791 794 public Component getPrintComponent() 795 { 796 return attributeTable; 797 } 798 799 802 public boolean isUnique() 803 { 804 return false; 805 } 806 807 public String getName() 808 { 809 return CBIntText.get("Table Editor"); 810 } 811 812 public ImageIcon getIcon() 813 { 814 return new ImageIcon("images" + File.separator + "table.gif"); 815 } 817 public String getToolTip() 818 { 819 return CBIntText.get("The table editor is generally used for editing data, it also functions perfectly well as a simple, but robust, entry viewer."); 820 } 822 public DataSink getDataSink() 823 { 824 return this; 825 } 826 827 public boolean canCreateEntry() 828 { 829 return true; 830 } 831 832 public void registerComponents(JMenuBar menu, JToolBar buttons, JTree tree, JPopupMenu treeMenu, JFrame jx) 833 { 834 } 835 836 public void unload() 837 { 838 } 839 840 843 public ImageIcon getTreeIcon(String rdn) 844 { 845 return null; 846 } 847 848 851 public JPopupMenu getPopupMenu(String rdn) 852 { 853 return null; 854 } 855 856 859 public boolean hideSubEntries(String rdn) 860 { 861 return false; 862 } 863 864 867 public void registerClassLoader(ClassLoader loader) 868 { 869 myLoader = loader; 870 myEditor.registerClassLoader(loader); 871 } 872 873 public void setVisible(boolean state) 874 { 875 super.setVisible(state); 876 877 if (state == false && tableData.changedByUser()) { 880 884 promptForSave(false); 885 } 886 } 887 } | Popular Tags |