001: // (c) copyright 2006 by eXXcellent solutions, Ulm. Author: bschmid
002:
003: package wingset;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007: import org.wings.plaf.WingSetExample;
008: import org.wings.session.Session;
009: import org.wings.session.SessionManager;
010: import org.wings.util.PropertyDiscovery;
011:
012: import javax.swing.tree.DefaultMutableTreeNode;
013: import javax.swing.tree.DefaultTreeModel;
014: import javax.swing.tree.TreeNode;
015: import java.io.File;
016: import java.util.ArrayList;
017: import java.util.Arrays;
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.LinkedHashSet;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Set;
024:
025: /**
026: * @author Benjamin Schmid <B.Schmid@exxcellent.de>
027: */
028: class WingSetTreeModel extends DefaultTreeModel {
029: private static final Log LOG = LogFactory
030: .getLog(WingSetTreeModel.class);
031:
032: public WingSetTreeModel() {
033: super (buildNodes());
034: }
035:
036: private static TreeNode buildNodes() {
037: final DefaultMutableTreeNode root = new DefaultMutableTreeNode(
038: "Demo");
039:
040: // Collect all classes which are in question of being a wingset example
041: final List<String> wingsClassNames = new ArrayList<String>();
042: wingsClassNames.addAll(collectExampleViaWebappDir());
043: wingsClassNames.addAll(collectExampleViaDefaultProperties());
044: Collections.sort(wingsClassNames);
045:
046: final Map<String, Set<WingSetExample>> exampleRegistry = new HashMap<String, Set<WingSetExample>>();
047: // Test them for being an instancable wingsetexample
048: for (String wingsClassFileName : wingsClassNames) {
049: try {
050: Class clazz = Thread.currentThread()
051: .getContextClassLoader().loadClass(
052: wingsClassFileName);
053: if (WingSetExample.class.isAssignableFrom(clazz)) {
054: final WingSetExample exampleInstance = (WingSetExample) clazz
055: .newInstance();
056: final String sectionName = exampleInstance
057: .getExampleGroup();
058: Set<WingSetExample> sectionMap = exampleRegistry
059: .get(sectionName);
060: if (sectionMap == null) {
061: sectionMap = new LinkedHashSet<WingSetExample>();
062: exampleRegistry.put(sectionName, sectionMap);
063: }
064: sectionMap.add(exampleInstance);
065: }
066: } catch (Exception e) {
067: LOG.debug("Skipping class " + wingsClassFileName
068: + " due to " + e);
069: }
070: }
071:
072: // build the nodes
073: buildNodes(root, exampleRegistry);
074:
075: return root;
076: }
077:
078: private static List<String> collectExampleViaWebappDir() {
079: final String dirName = SessionManager.getSession()
080: .getServletContext().getRealPath(
081: "/WEB-INF/classes/wingset");
082: final File dir = new File(dirName);
083: final String[] dirContent = dir.list();
084: for (int i = 0; i < dirContent.length; i++) {
085: String fileName = dirContent[i];
086: if (fileName.endsWith(".class")) {
087: dirContent[i] = "wingset."
088: + fileName.substring(0, fileName.length() - 6);
089: }
090: }
091: return Arrays.asList(dirContent);
092: }
093:
094: private static List<String> collectExampleViaDefaultProperties() {
095: final ArrayList<String> exampleClassNames = new ArrayList<String>();
096: Map properties = PropertyDiscovery
097: .loadOptionalProperties("org/wings/plaf/css/default.properties");
098: for (Object o : properties.keySet()) {
099: if (o instanceof String
100: && ((String) o).endsWith(".wingset-example")) {
101: exampleClassNames.add((String) properties.get(o));
102: }
103: }
104: return exampleClassNames;
105: }
106:
107: private static void buildNodes(DefaultMutableTreeNode rootNode,
108: Map<String, Set<WingSetExample>> registry) {
109: final Session session = SessionManager.getSession();
110: final boolean includeTests = "TRUE"
111: .equalsIgnoreCase((String) session
112: .getProperty("wingset.include.tests"));
113: final boolean includeExperiments = "TRUE"
114: .equalsIgnoreCase((String) session
115: .getProperty("wingset.include.experiments"));
116:
117: Map<String, DefaultMutableTreeNode> sectionNodeMap = new HashMap<String, DefaultMutableTreeNode>();
118:
119: for (String sectionName : registry.keySet()) {
120: if (!includeTests && "Test".equalsIgnoreCase(sectionName))
121: continue;
122: if (!includeExperiments
123: && "Experiment".equalsIgnoreCase(sectionName))
124: continue;
125:
126: DefaultMutableTreeNode sectionNode = sectionNodeMap
127: .get(sectionName);
128: if (sectionNode == null) {
129: sectionNode = new DefaultMutableTreeNode(sectionName);
130: sectionNodeMap.put(sectionName, sectionNode);
131: rootNode.add(sectionNode);
132: }
133: Set<WingSetExample> examples = registry.get(sectionName);
134: for (WingSetExample example : examples) {
135: sectionNode.add(new DefaultMutableTreeNode(example));
136: }
137: }
138: }
139: }
|