KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > menusystem > MenuFactory


1 package com.genimen.djeneric.tools.specifier.menusystem;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.BufferedReader JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.util.ArrayList JavaDoc;
9
10 import javax.swing.JMenu JavaDoc;
11 import javax.swing.JMenuItem JavaDoc;
12 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
13 import javax.xml.parsers.ParserConfigurationException JavaDoc;
14 import javax.xml.transform.TransformerException JavaDoc;
15
16 import org.apache.xpath.XPathAPI;
17 import org.w3c.dom.Document JavaDoc;
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.traversal.NodeIterator;
20 import org.xml.sax.InputSource JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc[] createMenus(InputStream JavaDoc stream) throws SAXException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc,
37       TransformerException JavaDoc, DjenericException, ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
38   {
39     ArrayList JavaDoc result = new ArrayList JavaDoc();
40     InputSource JavaDoc is = new InputSource JavaDoc(new BufferedInputStream JavaDoc(stream));
41     DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance();
42
43     Document JavaDoc xmlDoc = dfactory.newDocumentBuilder().parse(is);
44
45     NodeIterator menus = XPathAPI.selectNodeIterator(xmlDoc, "/menus/menu");
46
47     Element JavaDoc menu;
48     while ((menu = (Element JavaDoc) menus.nextNode()) != null)
49     {
50       String JavaDoc menuTitle = menu.getAttribute("title");
51
52       JMenu JavaDoc jMenu = new JMenu JavaDoc(menuTitle);
53       result.add(jMenu);
54
55       NodeIterator items = XPathAPI.selectNodeIterator(menu, "menuitem");
56       Element JavaDoc item;
57       while ((item = (Element JavaDoc) items.nextNode()) != null)
58       {
59         String JavaDoc itemTitle = item.getAttribute("title");
60         String JavaDoc extentName = item.getAttribute("somextent");
61         String JavaDoc classname = item.getAttribute("class");
62
63         if (extentName != null && extentName.trim().length() == 0) extentName = null;
64
65         JMenuItem JavaDoc jMenuItem = new JMenuItem JavaDoc(itemTitle);
66         jMenu.add(jMenuItem);
67
68         Class JavaDoc 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 JavaDoc[]) result.toArray(new JMenu JavaDoc[result.size()]);
75   }
76
77   public ArrayList JavaDoc getExtensions(String JavaDoc typeName) throws IOException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc,
78       ClassNotFoundException JavaDoc, DjenericException
79   {
80     ArrayList JavaDoc result = new ArrayList JavaDoc();
81
82     InputStream JavaDoc is = _environment.getResourceAsStream(typeName + ".ext");
83     if (is == null) return result;
84
85     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
86     String JavaDoc 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 JavaDoc title = ln.substring(0, idx).trim();
96           String JavaDoc className = ln.substring(idx + 1).trim();
97
98           Class JavaDoc 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