1 package com.genimen.djeneric.tools.specifier.menusystem; 2 3 import java.io.BufferedInputStream ; 4 import java.io.BufferedReader ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.io.InputStreamReader ; 8 import java.util.ArrayList ; 9 10 import javax.swing.JMenu ; 11 import javax.swing.JMenuItem ; 12 import javax.xml.parsers.DocumentBuilderFactory ; 13 import javax.xml.parsers.ParserConfigurationException ; 14 import javax.xml.transform.TransformerException ; 15 16 import org.apache.xpath.XPathAPI; 17 import org.w3c.dom.Document ; 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.traversal.NodeIterator; 20 import org.xml.sax.InputSource ; 21 import org.xml.sax.SAXException ; 22 23 import com.genimen.djeneric.repository.exceptions.DjenericException; 24 import com.genimen.djeneric.tools.specifier.interfaces.DjenericEditorEnvironment; 25 import com.genimen.djeneric.tools.specifier.interfaces.MenuExtension; 26 27 public class MenuFactory 28 { 29 DjenericEditorEnvironment _environment; 30 31 public MenuFactory(DjenericEditorEnvironment environment) 32 { 33 _environment = environment; 34 } 35 36 public JMenu [] createMenus(InputStream stream) throws SAXException , IOException , ParserConfigurationException , 37 TransformerException , DjenericException, ClassNotFoundException , InstantiationException , IllegalAccessException 38 { 39 ArrayList result = new ArrayList (); 40 InputSource is = new InputSource (new BufferedInputStream (stream)); 41 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 42 43 Document xmlDoc = dfactory.newDocumentBuilder().parse(is); 44 45 NodeIterator menus = XPathAPI.selectNodeIterator(xmlDoc, "/menus/menu"); 46 47 Element menu; 48 while ((menu = (Element ) menus.nextNode()) != null) 49 { 50 String menuTitle = menu.getAttribute("title"); 51 52 JMenu jMenu = new JMenu (menuTitle); 53 result.add(jMenu); 54 55 NodeIterator items = XPathAPI.selectNodeIterator(menu, "menuitem"); 56 Element item; 57 while ((item = (Element ) items.nextNode()) != null) 58 { 59 String itemTitle = item.getAttribute("title"); 60 String extentName = item.getAttribute("somextent"); 61 String classname = item.getAttribute("class"); 62 63 if (extentName != null && extentName.trim().length() == 0) extentName = null; 64 65 JMenuItem jMenuItem = new JMenuItem (itemTitle); 66 jMenu.add(jMenuItem); 67 68 Class clazz = _environment.loadClass(classname); 69 MenuExtension ext = (MenuExtension) clazz.newInstance(); 70 MenuExtensionDescr descr = new MenuExtensionDescr(itemTitle, ext, extentName); 71 jMenuItem.addActionListener(new MenuExtensionAction(ext, _environment, extentName)); 72 } 73 } 74 return (JMenu []) result.toArray(new JMenu [result.size()]); 75 } 76 77 public ArrayList getExtensions(String typeName) throws IOException , InstantiationException , IllegalAccessException , 78 ClassNotFoundException , DjenericException 79 { 80 ArrayList result = new ArrayList (); 81 82 InputStream is = _environment.getResourceAsStream(typeName + ".ext"); 83 if (is == null) return result; 84 85 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 86 String ln; 87 while ((ln = br.readLine()) != null) 88 { 89 ln = ln.trim(); 90 if (ln.length() > 0) 91 { 92 int idx = ln.indexOf("="); 93 if (idx != -1) 94 { 95 String title = ln.substring(0, idx).trim(); 96 String className = ln.substring(idx + 1).trim(); 97 98 Class clazz = _environment.loadClass(className); 99 100 MenuExtension menuExtension = (MenuExtension) clazz.newInstance(); 101 menuExtension.init(_environment); 102 MenuExtensionDescr extension = new MenuExtensionDescr(title, menuExtension, typeName); 103 result.add(extension); 104 } 105 } 106 } 107 br.close(); 108 return result; 109 } 110 111 } 112 | Popular Tags |