1 package com.ca.directory.jxplorer.viewer; 2 3 import javax.swing.*; 4 import java.lang.reflect.*; 5 6 import java.awt.*; 7 import java.awt.print.*; 8 import java.util.*; 9 import java.util.logging.Logger ; 10 import java.util.logging.Level ; 11 import java.io.*; 12 13 import com.ca.directory.jxplorer.*; 14 import com.ca.commons.cbutil.*; 15 import com.ca.commons.naming.*; 16 17 import javax.naming.directory.*; 18 import javax.naming.NamingException ; 19 import javax.swing.event.ChangeListener ; 20 import javax.swing.event.ChangeEvent ; 21 22 31 32 public class AttributeDisplay extends JTabbedPane 33 implements DataSink, Printable 34 { 35 38 39 public static final int HTMLRENDERER = 1; 40 41 44 45 public static final int EDITOR = 2; 46 47 50 51 protected JScrollPane viewHolder; 52 53 56 57 protected JPanel view; 58 59 60 62 65 66 protected DataSource dataSource; 67 68 71 72 protected DXEntry entry = null; 73 74 79 80 protected PluggableEditor currentEditor; 81 82 86 87 protected HTMLTemplateDisplay templateDisplay; 88 89 94 95 protected TableAttributeEditor tableDisplay; 96 97 100 101 103 106 107 protected Properties myProperties; 108 109 112 113 private Properties printprefs = new Properties(); 114 115 121 122 protected Hashtable editors = new Hashtable(); 123 124 127 128 protected Vector activeEditors = new Vector(8); 129 130 133 134 protected JFrame owner; 135 136 137 protected String oldOCSig = null; 139 140 141 private JMenuBar registerMenu = null; 142 143 144 145 private JToolBar registerButtons = null; 146 147 148 149 private JTree registerTree = null; 150 151 152 153 private JPopupMenu registerTreeMenu = null; 154 155 156 157 private JFrame registerJX = null; 158 159 163 164 protected ClassLoader myLoader = null; 166 170 171 protected CBResourceLoader resourceLoader = null; 173 177 178 public static String PACKAGEPREFIX = "com.ca.directory.jxplorer.viewer."; 179 180 public static void setPackagePrefix(String prefix) { PACKAGEPREFIX = prefix; } 181 182 public static String getPackagePrefix() { return PACKAGEPREFIX; } 183 184 187 192 193 protected static final String NONE = "none"; 194 195 199 200 private Component printComponent = null; 201 202 210 211 protected boolean ignoreChangeEvents = false; 212 213 private static Logger log = Logger.getLogger(AttributeDisplay.class.getName()); 214 215 224 225 public AttributeDisplay(Properties props, JFrame owner, CBResourceLoader resourceLoader) 226 { 227 super(); 228 229 230 myProperties = props; 231 232 if (myProperties.containsKey("plugins.package")) 233 { 234 setPackagePrefix(myProperties.getProperty("plugins.package")); 235 log.fine("SETTING PLUGIN PREFIX TO: " + PACKAGEPREFIX); 236 } 237 else 238 log.fine("PLUGIN PREFIX UNCHANGED: " + PACKAGEPREFIX); 239 240 this.resourceLoader = resourceLoader; 241 242 this.owner = owner; 243 244 initHTMLEditor(); 245 initTableEditor(); 246 addEditor(templateDisplay); 247 248 253 254 addChangeListener( new ChangeListener () 255 { 256 public void stateChanged(ChangeEvent e) 257 { 258 if (ignoreChangeEvents) return; 261 int index = getSelectedIndex(); 262 263 if (index >= 0 && activeEditors.size() > index && activeEditors.get(index) != null) 264 { 265 setCurrentEditor((PluggableEditor)activeEditors.get(index)); 266 } 267 else { 269 log.warning("internal error - unable to find editor # " + index + " in Attribute Display"); 270 } 271 } 272 }); 273 } 274 275 280 281 public void registerClassLoader(ClassLoader loader) 282 { 283 myLoader = loader; 284 if (tableDisplay == null) 285 initTableEditor(); 286 287 tableDisplay.registerClassLoader(loader); 288 } 289 290 294 295 protected synchronized void initHTMLEditor() 296 { 297 if (templateDisplay != null) return; 298 299 templateDisplay = new HTMLTemplateDisplay(this, myProperties, resourceLoader); 300 301 307 308 310 312 currentEditor = templateDisplay; 313 } 314 315 319 320 protected synchronized void initTableEditor() 321 { 322 if (tableDisplay != null) return; 323 324 tableDisplay = new TableAttributeEditor(owner); 325 326 332 333 } 335 336 341 343 public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException 344 { 345 347 Graphics2D g2 = (Graphics2D)g; 348 g2.setColor(Color.black); 350 353 Dimension d = printComponent.getSize(); double panelWidth = d.width; double panelHeight = d.height; double pageHeight = pf.getImageableHeight(); double pageWidth = pf.getImageableWidth(); double scale = pageWidth/panelWidth; 359 int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight); 360 361 if(pageIndex >= totalNumPages) 363 { 364 return Printable.NO_SUCH_PAGE; 365 } 366 367 g2.translate(pf.getImageableX(), pf.getImageableY()); 369 370 g2.translate(0f, -pageIndex*pageHeight); 372 373 g2.scale(scale, scale); 375 376 printComponent.paint(g2); 378 379 return Printable.PAGE_EXISTS; 380 } 381 382 386 387 public void print() 388 { 389 final PrinterJob job = PrinterJob.getPrinterJob(); 390 job.setPrintable(this); 391 392 395 final Component printMe = getPrintComponent(); 396 397 Thread worker = new Thread () 398 { 399 public void run() 400 { 401 if (job.printDialog()) 402 { 403 try 404 { 405 synchronized(this) 409 { 410 printComponent = printMe; 411 job.print(); 412 printComponent = null; 413 } 414 } 415 catch (Exception ex) 416 { log.warning("error printing: " + ex); } 417 } 418 } 419 }; 420 worker.start(); 421 422 } 423 424 425 433 434 public void openDocumentURL(String docURL) 435 { 436 if (templateDisplay == null) 437 initHTMLEditor(); 438 439 templateDisplay.openDocumentURL(docURL); 440 setCurrentEditor(templateDisplay); 441 } 442 443 444 452 453 public void displaySpecialEntry(DXEntry entry, DataSource ds, String editorName) 454 { 455 456 PluggableEditor ed = getEditor(editorName); 457 458 459 if (ed != null) 460 { 461 if (ed.isUnique() == false) 462 { 463 setCurrentEditor(ed); 464 } 465 else 466 { 467 if (currentEditor != ed) 468 addUniqueEditor(ed); 469 470 refreshEditors(entry, ds); 471 oldOCSig = null; 472 } 473 } 474 475 } 476 477 487 488 493 public void displayEntry(DXEntry dxentry, DataSource ds) 494 { 495 497 dataSource = ds; 498 entry = dxentry; 499 500 502 if (tableDisplay == null) 503 initTableEditor(); 504 if (templateDisplay == null ) 505 initHTMLEditor(); 506 507 510 if (entry == null) { 512 if (oldOCSig != null) 513 { 514 clearPluggableEditors(); 515 516 if (activeEditors.size() == 0) 517 { 518 addEditor(templateDisplay); 519 } 520 oldOCSig = null; 521 } 522 refreshEditors(null, ds); 523 } 524 else 525 { 526 dataSource = ds; Vector ocs = entry.getOrderedOCs(); 528 529 String newOCSig = new String (); 533 534 if (ocs != null) 535 for (int i=0; i<ocs.size(); i++) 536 { 537 Object ocSig = ocs.get(i); 538 if (ocSig != null) 539 newOCSig += ocSig.toString(); 540 } 541 542 if (newOCSig.equals(oldOCSig) == false) 546 { 547 setEditors(entry, ds, ocs); 548 oldOCSig = newOCSig; 549 } 550 551 if (entry.getStatus() == DXEntry.NEW) { 554 trimNonNewEntryEditors(); 556 suggestPluggableEditor(); 557 } 558 else 559 { 560 if (activeEditors.contains(templateDisplay) == false && !currentEditor.isUnique()) 568 { 569 add((PluggableEditor)templateDisplay, 0); 570 if (currentEditor != null) setCurrentEditor(currentEditor); 572 } 573 } 574 575 if (activeEditors.contains(currentEditor) == false) 576 { 577 suggestPluggableEditor(); 578 } 579 580 581 582 refreshEditors(entry, ds); 585 } 586 } 587 588 592 593 private void trimNonNewEntryEditors() 594 { 595 int size = activeEditors.size(); 596 for (int i=size-1; i>=0; i--) 597 { 598 if (((DataSink)activeEditors.get(i)).canCreateEntry() == false) 599 { 600 remove(i); 601 } 602 } 603 suggestTableEditor(); } 605 606 607 610 611 public boolean suggestPluggableEditor() 612 { 613 for (int i=activeEditors.size()-1; i>=0; i--) 614 { 615 PluggableEditor ed = (PluggableEditor)activeEditors.get(i); 618 if ( (ed != templateDisplay) && (ed != tableDisplay) ) 619 { 620 setCurrentEditor(ed); 621 return true; 622 } 623 } 624 return false; 625 } 626 627 631 632 public boolean suggestTableEditor() 633 { 634 636 if (activeEditors.contains(tableDisplay) == true) 637 { 638 setCurrentEditor(tableDisplay); 639 return true; 640 } 641 return false; 642 } 643 644 648 649 public boolean suggestHTMLEditor() 650 { 651 653 if (activeEditors.contains(templateDisplay) == true) 654 { 655 setCurrentEditor(templateDisplay); 656 return true; 657 } 658 return false; 659 } 660 661 662 669 670 protected void setCurrentEditor(PluggableEditor makeCurrent) 671 { 672 currentEditor = makeCurrent; 673 if (currentEditor != null && currentEditor.getDataSink()!= null) 674 currentEditor.getDataSink().displayEntry(entry, dataSource); 675 676 int index = activeEditors.indexOf(makeCurrent); 677 678 if (index == -1) 679 { 680 clearPluggableEditors(); 681 addEditor(makeCurrent); 682 683 setSelectedIndex(activeEditors.indexOf(makeCurrent)); 684 685 } 686 else if (index != getSelectedIndex()) 687 { 688 setSelectedIndex(index); 689 } 690 } 691 692 695 696 protected PluggableEditor getCurrentEditor() 697 { 698 return currentEditor; 699 } 700 701 708 709 protected void setEditors(DXEntry entry, DataSource ds, Vector ocs) 710 { 711 712 try 713 { 714 clearPluggableEditors(); 716 718 if ("false".equalsIgnoreCase(JXplorer.getProperty("plugins.ignoreUniqueness"))) 719 { 720 if(ocs==null) return; 722 723 int size = ocs.size(); 724 725 for (int i=0; i<size; i++) 726 { 727 Object objectClass = ocs.get(i); 728 if (objectClass != null) 729 { 730 PluggableEditor ed = getEditor(objectClass.toString()); 731 if (ed != null && ed.isUnique() == true) { addUniqueEditor(ed); refreshEditors(entry, ds); setCurrentEditor(ed); 736 return; } 738 } 739 } 740 } 741 else 742 log.warning("skipping uniqueness test for pluggable editors"); 743 744 boolean newEdSet = false; 745 746 for (int i=0; i<ocs.size(); i++) 748 { 749 Object objectClass = ocs.get(i); 750 if (objectClass != null) 751 { 752 PluggableEditor ed = getEditor(objectClass.toString()); 753 if (ed != null) 754 { 755 addEditor(ed); 756 757 if (newEdSet == false) 759 { 760 setCurrentEditor(ed); 761 newEdSet = true; 762 } 763 } 764 } 765 } 766 767 try 769 { 770 Attribute allOCs = entry.getAllObjectClasses(); 771 if (allOCs != null) 772 { 773 Enumeration vals = allOCs.getAll(); 774 while (vals.hasMoreElements()) 775 { 776 Object oc = vals.nextElement(); 777 if (oc != null) 778 { 779 String ocName = oc.toString(); 780 if (ocs.contains(ocName) == false) { 782 PluggableEditor ed = getEditor(ocName); 783 784 if (ed != null) 785 { 786 addEditor(ed); 787 788 if (ed.isUnique()) log.warning("WARNING: Illegal unique editor defined for oc: " + ocName + " not allowed - (oc not in primary structural inheritance chain)"); 790 } 791 } 792 } 793 } 794 } 795 } 796 catch (NamingException e) 797 { 798 log.log(Level.WARNING, "WARNING: non-fatal exception getting object classes for plugin editors. ", e); 799 } 800 801 addEditor(templateDisplay); if (entry.getStatus() != DXEntry.NEW) addEditor(tableDisplay); 805 } 806 catch (Exception e) 807 { 808 log.warning("Unexpected Exception in AttributeDisplay\n" + e); 809 e.printStackTrace(); 810 } 811 } 812 813 816 817 public void clearEditors() 819 { 820 removeAll(); 821 activeEditors.removeAllElements(); 822 templateDisplay.setToDefault(); 823 addEditor(templateDisplay); 824 tableDisplay.displayEntry(null, null); 825 addEditor(tableDisplay); 826 } 827 828 829 833 834 void clearPluggableEditors() 835 { 836 ignoreChangeEvents = true; 837 838 for (int i=activeEditors.size()-1; i>=0; i--) 839 { 840 PluggableEditor ed = (PluggableEditor)activeEditors.get(i); 841 if ( (ed != templateDisplay) && (ed != tableDisplay) ) 842 { 843 remove(i); 844 } 845 } 846 847 if (activeEditors.contains(templateDisplay)==false) 848 { 849 addEditor(templateDisplay); 850 } 851 852 if (activeEditors.contains(tableDisplay)==false) 853 { 854 addEditor(tableDisplay); 855 } 856 857 if (currentEditor != tableDisplay && currentEditor != templateDisplay) 858 { 859 suggestHTMLEditor(); 860 } 861 862 863 864 ignoreChangeEvents = false; 865 866 } 867 868 869 881 882 public PluggableEditor getUniqueEditor(Attribute oc) 883 { 884 try 885 { 886 Enumeration values = oc.getAll(); 887 while (values.hasMoreElements()) 888 { 889 String objectClassName = (String )values.nextElement(); 890 891 PluggableEditor editor = getEditor(objectClassName); 892 if (editor != null) 893 { 894 if (editor.isUnique()) 895 return editor; 896 } 897 } 898 return null; 899 } 900 catch (Exception e) 901 { 902 log.log(Level.FINE, "Unable to find unique pluggable editor: ", e); 903 return null; 904 } 905 } 906 907 912 913 PluggableEditor getEditor(String ocName) 914 { 915 ocName = ocName.toLowerCase(); 916 917 Object editorFromHash = editors.get(PACKAGEPREFIX + ocName); 918 919 if (editorFromHash != null) 920 return castToPluggableEditor(editorFromHash, ocName); 922 return loadEditorFromDisk(PACKAGEPREFIX + ocName ); } 924 925 930 931 private PluggableEditor castToPluggableEditor(Object rawEditor, String ocName) 932 { 933 if (rawEditor == NONE) return null; 935 936 if (rawEditor instanceof PluggableEditor) { 938 return (PluggableEditor)rawEditor; 939 } 940 else 941 { 942 log.warning("Unexpected Class Cast Error loading plugin editor '"+PACKAGEPREFIX + ocName + "' from hashtable"); 943 return null; 944 } 945 } 946 947 948 951 952 PluggableEditor loadEditorFromDisk(String ocName) 953 { 954 log.finer("looking for ocName: " + ocName); 956 try 957 { 958 Class c = myLoader.loadClass(ocName); 959 Constructor constructor = c.getConstructor(new Class [0]); 960 961 962 PluggableEditor editor = (PluggableEditor) constructor.newInstance(new Object [0]); 967 968 editors.put(ocName, editor); return editor; 970 } 971 catch (Exception e) { 973 if (e instanceof InvocationTargetException) { 975 log.warning("unable to load special editor for: '" + ocName + "' " + e); 976 if (JXplorer.debugLevel >= 1) 977 { 978 log.warning("Error loading plugin class: "); 979 ((InvocationTargetException)e).getTargetException().printStackTrace(); 980 } 981 } 982 983 log.log(Level.FINEST, "'Expected' Error loading " + ocName, e); 984 editors.put(ocName, NONE); } 987 return null; } 989 990 991 995 996 public void registerComponents(JMenuBar menu, JToolBar buttons, JTree tree, JPopupMenu treeMenu, JFrame jxplorer) 997 { 998 registerMenu = menu; 999 registerButtons = buttons; 1000 registerTree = tree; 1001 registerTreeMenu = treeMenu; 1002 registerJX = jxplorer; 1003 1004 1006 for (int i=0; i<activeEditors.size(); i++) 1007 { 1008 ((PluggableEditor)activeEditors.get(i)).registerComponents(menu, buttons, tree, treeMenu, jxplorer); 1009 } 1010 } 1011 1012 1013 1017 1018 void addEditor(PluggableEditor ed) 1019 { 1020 if (activeEditors.contains(ed) == false) { 1022 add(ed); 1023 ed.registerComponents(registerMenu, registerButtons, registerTree, registerTreeMenu, registerJX); 1024 } 1025 } 1026 1027 1031 1032 void addUniqueEditor(PluggableEditor ed) 1033 { 1034 ignoreChangeEvents = true; 1036 removeAll(); 1037 addEditor(ed); 1038 setCurrentEditor(ed); 1039 1040 ignoreChangeEvents = false; } 1042 1043 1044 1048 1049 public void refreshEditors() 1050 { 1051 1057 if(dataSource != null) 1058 refreshEditors(entry, dataSource); 1059 } 1060 1061 1064 1065 public void refreshEditors(DXEntry entry, DataSource ds) 1066 { 1067 if (currentEditor != null) 1068 { 1069 this.entry = entry; dataSource = ds; 1071 1072 currentEditor.getDataSink().displayEntry(entry, ds); 1073 1074 JComponent display = currentEditor.getDisplayComponent(); 1076 1077 1078 1082 if (indexOfComponent(display) == -1) { 1084 String title = currentEditor.getName(); 1085 ImageIcon icon = currentEditor.getIcon(); 1086 String toolTip = currentEditor.getToolTip(); 1087 1088 int index = getSelectedIndex(); 1090 super.remove(index); 1091 super.insertTab(title, icon, display, toolTip, index); 1092 super.setSelectedIndex(index); 1093 } 1094 } 1095 else 1096 log.warning("internal error - no editor available in AttributeDisplay"); 1097 1098 } 1099 1100 1103 public Component getPrintComponent() 1104 { 1105 return currentEditor.getPrintComponent(); 1106 } 1107 1108 public boolean canCreateEntry() { return true; } 1109 1110 public void add(PluggableEditor ed) 1111 { 1112 add(ed, getTabCount()); 1113 } 1114 1115 public void add(PluggableEditor ed, int index) 1116 { 1117 insertTab(ed.getName(), ed.getIcon(), ed.getDisplayComponent(), ed.getToolTip(), index); 1119 activeEditors.add(index, ed); 1120 } 1121 1122 public void remove(int index) 1123 { 1124 if (activeEditors.size() == 0) return; 1126 PluggableEditor ed = (PluggableEditor) activeEditors.remove(index); 1127 ed.unload(); 1128 super.remove(index); 1129 } 1130 1131 public void removeAll() 1132 { 1133 int size = activeEditors.size(); 1134 for (int i=size-1; i>=0; i--) 1135 remove(i); 1136 1137 super.removeAll(); } 1139} | Popular Tags |