1 53 package org.swixml; 54 55 import org.jdom.Document; 56 import org.jdom.input.SAXBuilder; 57 58 import javax.swing.*; 59 import java.awt.*; 60 import java.awt.event.ActionListener ; 61 import java.awt.event.WindowAdapter ; 62 import java.awt.event.WindowEvent ; 63 import java.awt.event.WindowListener ; 64 import java.io.*; 65 import java.lang.reflect.Field ; 66 import java.lang.reflect.Modifier ; 67 import java.net.URL ; 68 import java.security.AccessControlException ; 69 import java.util.*; 70 import java.util.List ; 71 72 80 public class SwingEngine { 81 87 public static final String MAC_OSX_OS_NAME = "mac os x"; 88 89 92 public static final String MAC_OSX_LOCALE_VARIANT = "mac"; 93 94 97 public static final boolean DEBUG_MODE = false; 98 101 private static final String XML_ERROR_MSG = "Invalid SwiXML Descriptor."; 102 105 private static final String IO_ERROR_MSG = "Resource could not be found "; 106 109 private static final String MAPPING_ERROR_MSG = 110 " could not be mapped to any Object and remained un-initialized."; 111 112 118 private static Frame appFrame; 119 122 private static String default_resource_bundle_name = null; 123 126 private static Locale default_locale = Locale.getDefault(); 127 130 private static boolean MAC_OSX = false; 131 134 private static boolean MAC_OSX_SUPPORTED = true; 135 136 140 static { 141 System.out.println("SwixML @version@"); 142 try { 143 MAC_OSX = System.getProperty("os.name").toLowerCase().startsWith(SwingEngine.MAC_OSX_OS_NAME); 144 } catch (Exception e) { 145 MAC_OSX = false; 146 } 147 } 148 149 155 private Parser parser = new Parser(this); 156 159 private Object client; 160 163 private Container root; 164 167 private Map idmap = new HashMap(); 168 171 private Collection components = null; 172 175 private Localizer localizer = new Localizer(); 176 182 private final TagLibrary taglib = SwingTagLibrary.getInstance(); 183 186 protected ClassLoader cl = this.getClass().getClassLoader(); 187 188 191 public SwingEngine() { 192 this.client = this; 193 this.setLocale(SwingEngine.default_locale); 194 this.getLocalizer().setResourceBundle(SwingEngine.default_resource_bundle_name); 195 196 try { 197 if (SwingEngine.isMacOSXSupported() && SwingEngine.isMacOSX()) { 198 System.setProperty("com.apple.macos.useScreenMenuBar", "true"); 201 System.setProperty("apple.laf.useScreenMenuBar", "true"); 202 203 System.setProperty("apple.awt.showGrowBox", "true"); 205 System.setProperty("com.apple.mrj.application.growbox.intrudes", "false"); 206 } 207 } catch (AccessControlException e) { 208 } 210 } 211 212 217 public SwingEngine(Object client) { 218 this(); 219 this.client = client; 220 } 221 222 227 public SwingEngine(final String resource) { 228 this(SwingEngine.class.getClassLoader(), resource); 229 } 230 231 237 public SwingEngine(ClassLoader cl, final String resource) { 238 this(); 239 this.setClassLoader(cl); 240 Reader reader = null; 241 try { 242 InputStream in = cl.getResourceAsStream(resource); 243 if (in == null) { 244 throw new IOException(IO_ERROR_MSG + resource); 245 } 246 reader = new InputStreamReader(in); 247 render(reader); 248 } catch (Exception e) { 249 if (SwingEngine.DEBUG_MODE) 250 System.err.println(e); 251 } finally { 252 try { 253 reader.close(); 254 } catch (Exception e) { 255 } 257 } 258 } 259 260 267 public Container render(final URL url) throws Exception { 268 Reader reader = null; 269 Container obj = null; 270 try { 271 InputStream in = url.openStream(); 272 if (in == null) { 273 throw new IOException(IO_ERROR_MSG + url.toString()); 274 } 275 reader = new InputStreamReader(in); 276 obj = render(reader); 277 } finally { 278 try { 279 reader.close(); 280 } catch (Exception ex) { 281 } 283 } 284 return obj; 285 } 286 287 293 public Container render(final String resource) throws Exception { 294 Reader reader = null; 295 Container obj = null; 296 try { 297 InputStream in = cl.getResourceAsStream(resource); 298 if (in == null) { 299 throw new IOException(IO_ERROR_MSG + resource); 300 } 301 reader = new InputStreamReader(in); 302 obj = render(reader); 303 } finally { 304 try { 305 reader.close(); 306 } catch (Exception ex) { 307 } 309 } 310 return obj; 311 } 312 313 319 public Container render(final File xml_file) throws Exception { 320 if (xml_file == null) { 321 throw new IOException(); 322 } 323 return render(new FileReader(xml_file)); 324 } 325 326 332 public Container render(final Reader xml_reader) throws Exception { 333 if (xml_reader == null) { 334 throw new IOException(); 335 } 336 try { 337 return render(new SAXBuilder().build(xml_reader)); 338 } catch (org.xml.sax.SAXParseException e) { 339 System.err.println(e); 340 } catch (org.jdom.input.JDOMParseException e) { 341 System.err.println(e); 342 } 343 throw new Exception (SwingEngine.XML_ERROR_MSG); 344 } 345 346 352 public Container render(final Document jdoc) throws Exception { 353 idmap.clear(); 354 try { 355 root = (Container) parser.parse(jdoc); 356 } catch (Exception e) { 357 if (SwingEngine.DEBUG_MODE) 358 System.err.println(e); 359 throw (e); 360 } 361 components = null; 363 mapMembers(client); 365 if (Frame.class.isAssignableFrom(root.getClass())) { 366 SwingEngine.setAppFrame((Frame) root); 367 } 368 return root; 369 } 370 371 393 public void insert(final URL url, final Container container) 394 throws Exception { 395 Reader reader = null; 396 try { 397 InputStream in = url.openStream(); 398 if (in == null) { 399 throw new IOException(IO_ERROR_MSG + url.toString()); 400 } 401 reader = new InputStreamReader(in); 402 insert(reader, container); 403 } finally { 404 try { 405 reader.close(); 406 } catch (Exception ex) { 407 } 409 } 410 } 411 412 434 public void insert(final Reader reader, final Container container) 435 throws Exception { 436 if (reader == null) { 437 throw new IOException(); 438 } 439 insert(new SAXBuilder().build(reader), container); 440 } 441 442 464 public void insert(final String resource, final Container container) 465 throws Exception { 466 Reader reader = null; 467 try { 468 InputStream in = cl.getResourceAsStream(resource); 469 if (in == null) { 470 throw new IOException(IO_ERROR_MSG + resource); 471 } 472 reader = new InputStreamReader(in); 473 insert(reader, container); 474 } finally { 475 try { 476 reader.close(); 477 } catch (Exception ex) { 478 } 480 } 481 } 482 483 505 public void insert(final Document jdoc, final Container container) 506 throws Exception { 507 root = container; 508 try { 509 parser.parse(jdoc, container); 510 } catch (Exception e) { 511 if (SwingEngine.DEBUG_MODE) 512 System.err.println(e); 513 throw (e); 514 } 515 components = null; 517 mapMembers(client); 519 } 520 521 528 public static void setResourceBundleName(String bundlename) { 529 SwingEngine.default_resource_bundle_name = bundlename; 530 } 531 532 538 public static void setDefaultLocale(Locale locale) { 539 SwingEngine.default_locale = locale; 540 } 541 542 547 public static void setAppFrame(Frame frame) { 548 if (frame != null) { 549 if (SwingEngine.appFrame == null) { 550 SwingEngine.appFrame = frame; 551 } 552 } 553 } 554 555 558 public static Frame getAppFrame() { 559 return SwingEngine.appFrame; 560 } 561 562 571 public Object getClient() { 572 return client; 573 } 574 575 580 public Container getRootComponent() { 581 return root; 582 } 583 584 589 public Iterator getAllComponentItertor() { 590 if (components == null) { 591 traverse(root, components = new ArrayList()); 592 } 593 return components.iterator(); 594 } 595 596 601 public Iterator getIdComponentItertor() { 602 return idmap.values().iterator(); 603 } 604 605 610 public Map getIdMap() { 611 return idmap; 612 } 613 614 627 public int cleanup() { 628 List zombies = new ArrayList(); 629 Iterator it = idmap.keySet().iterator(); 630 while (it != null && it.hasNext()) { 631 Object key = it.next(); 632 Object obj = idmap.get(key); 633 if (obj instanceof Component && !((Component) obj).isDisplayable()) { 634 zombies.add(key); 635 } 636 } 637 for (int i = 0; i < zombies.size(); i++) { 638 idmap.remove(zombies.get(i)); 639 } 640 components = null; 641 return zombies.size(); 642 } 643 644 649 public void forget(final String id) { 650 idmap.remove(id); 651 } 652 653 659 public Component find(final String id) { 660 Object obj = idmap.get(id); 661 if (obj != null && !Component.class.isAssignableFrom(obj.getClass())) { 662 obj = null; 663 } 664 return (Component) obj; 665 } 666 667 672 public void setLocale(Locale l) { 673 if (SwingEngine.isMacOSXSupported() && SwingEngine.isMacOSX()) { 674 l = new Locale(l.getLanguage(), 675 l.getCountry(), 676 SwingEngine.MAC_OSX_LOCALE_VARIANT); 677 } 678 this.localizer.setLocale(l); 679 } 680 681 686 public void setResourceBundle(String bundlename) { 687 this.localizer.setResourceBundle(bundlename); 688 } 689 690 693 public Localizer getLocalizer() { 694 return localizer; 695 } 696 697 703 public TagLibrary getTaglib() { 704 return taglib; 705 } 706 707 715 public void setClassLoader(ClassLoader cl) { 716 this.cl = cl; 717 this.localizer.setClassLoader(cl); 718 } 719 720 724 public ClassLoader getClassLoader() { 725 return cl; 726 } 727 728 738 public boolean setActionListener(final Component c, final ActionListener al) { 739 boolean b = false; 740 if (c != null) { 741 if (Container.class.isAssignableFrom(c.getClass())) { 742 final Component[] s = ((Container) c).getComponents(); 743 for (int i = 0; i < s.length; i++) { 744 b = b | setActionListener(s[i], al); 745 } 746 } 747 if (!b) { 748 if (JMenu.class.isAssignableFrom(c.getClass())) { 749 final JMenu m = (JMenu) c; 750 final int k = m.getItemCount(); 751 for (int i = 0; i < k; i++) { 752 b = b | setActionListener(m.getItem(i), al); 753 } 754 } else if (AbstractButton.class.isAssignableFrom(c.getClass())) { 755 ((AbstractButton) c).addActionListener(al); 756 b = true; 757 } 758 } 759 760 } 761 return b; 762 } 763 764 773 public Iterator getDescendants(final Component c) { 774 List list = new ArrayList(12); 775 SwingEngine.traverse(c, list); 776 return list.iterator(); 777 } 778 779 787 protected void mapMembers(Object obj) { 788 if (obj != null) { 789 mapMembers(obj, obj.getClass()); 790 } 791 } 792 793 private void mapMembers(Object obj, Class cls) { 794 795 if (obj != null && cls != null && !Object .class.equals(cls)) { 796 Field [] flds = cls.getDeclaredFields(); 797 for (int i = 0; i < flds.length; i++) { 801 Object widget = idmap.get(flds[i].getName()); 802 if (widget != null) { 803 if (flds[i].getType().isAssignableFrom(widget.getClass()) && !Modifier.isTransient(flds[i].getModifiers())) { 805 try { 806 boolean accessible = flds[i].isAccessible(); 807 flds[i].setAccessible(true); 808 flds[i].set(obj, widget); 809 flds[i].setAccessible(accessible); 810 } catch (IllegalArgumentException e) { 811 } catch (IllegalAccessException e) { 813 } 815 } 816 } 817 818 if (flds[i] == null) { 823 if (!SwingEngine.DEBUG_MODE) { 824 try { 825 flds[i].set(obj, flds[i].getType().newInstance()); 826 } catch (IllegalArgumentException e) { 827 } catch (IllegalAccessException e) { 829 } catch (InstantiationException e) { 831 } 833 } else { System.err.println(flds[i].getType() 835 + " : " 836 + flds[i].getName() 837 + SwingEngine.MAPPING_ERROR_MSG); 838 } 839 } 840 } 841 842 mapMembers(obj, cls.getSuperclass()); 845 } 846 } 847 848 857 protected static void traverse(final Component c, Collection collection) { 858 if (c != null) { 859 collection.add(c); 860 if (c instanceof JMenu) { 861 final JMenu m = (JMenu) c; 862 final int k = m.getItemCount(); 863 for (int i = 0; i < k; i++) { 864 traverse(m.getItem(i), collection); 865 } 866 } else if (c instanceof Container) { 867 final Component[] s = ((Container) c).getComponents(); 868 for (int i = 0; i < s.length; i++) { 869 traverse(s[i], collection); 870 } 871 } 872 } 873 } 874 875 880 public static void setMacOSXSuport(boolean osx) { 881 SwingEngine.MAC_OSX_SUPPORTED = osx; 882 } 883 884 889 public static boolean isMacOSXSupported() { 890 return SwingEngine.MAC_OSX_SUPPORTED; 891 } 892 893 898 public static boolean isMacOSX() { 899 return SwingEngine.MAC_OSX; 900 } 901 902 906 public void test() { 907 WindowListener wl = new WindowAdapter () { 908 public void windowClosing(WindowEvent e) { 909 super.windowClosing(e); 910 System.exit(0); 911 } 912 }; 913 if (root != null) { 914 if (JFrame.class.isAssignableFrom(root.getClass()) 915 || JDialog.class.isAssignableFrom(root.getClass())) { 916 ((Window) root).addWindowListener(wl); 917 root.setVisible(true); 918 } else { 919 JFrame jf = new JFrame("SwiXml Test"); 920 jf.getContentPane().add(root); 921 jf.pack(); 922 jf.addWindowListener(wl); 923 jf.setVisible(true); 924 } 925 } 926 } 927 } 928 | Popular Tags |