KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > flow > ViewRegistry


1 /*
2  * $Id: ViewRegistry.java,v 1.9 2004/06/07 20:39:00 eelco12 Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/flow/ViewRegistry.java,v $
4  */

5
6 package org.infohazard.maverick.flow;
7
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.infohazard.maverick.util.XML;
16 import org.jdom.Element;
17
18 /**
19  * Factory for defining global view objects which can then be referenced
20  * for the creation of individual commands.
21  */

22 abstract class ViewRegistry
23 {
24     /**
25      * The name assigned to anonymous views. No risk of conflicts because
26      * anonymous views are only allowed when there are no other views
27      * specified.
28      */

29     public static final String JavaDoc ANONYMOUS_VIEW_NAME = "anonymous view";
30
31     /**
32      */

33     protected static final String JavaDoc TAG_VIEW = "view";
34     protected static final String JavaDoc ATTR_VIEW_ID = "id";
35     protected static final String JavaDoc ATTR_VIEW_MODE = "mode";
36     protected static final String JavaDoc ATTR_VIEW_NAME = "name";
37     protected static final String JavaDoc ATTR_VIEW_REF = "ref";
38
39     /**
40      * Logger.
41      */

42     private static Log log = LogFactory.getLog(ViewRegistry.class);
43
44     /**
45      */

46     protected MasterFactory masterFact;
47
48     /**
49      */

50     public ViewRegistry(MasterFactory masterFact)
51     {
52         this.masterFact = masterFact;
53     }
54
55     /**
56      */

57     public void defineGlobalViews(Element viewsNode) throws ConfigException
58     {
59         String JavaDoc defaultMode = viewsNode.getAttributeValue(ATTR_VIEW_MODE);
60
61         Iterator JavaDoc it = viewsNode.getChildren(TAG_VIEW).iterator();
62         while (it.hasNext())
63         {
64             Element viewNode = (Element)it.next();
65
66             String JavaDoc id = viewNode.getAttributeValue(ATTR_VIEW_ID);
67             String JavaDoc mode = viewNode.getAttributeValue(ATTR_VIEW_MODE);
68             if (mode == null)
69                 mode = defaultMode;
70
71             View v = this.masterFact.createView(viewNode);
72
73             this.defineGlobalView(id, mode, v);
74         }
75     }
76
77     /**
78      * Creates a mapping from view name to View object. Nameless
79      * views are given the name ANONYMOUS_VIEW_NAME, in which case
80      * it will be the only view available.
81      */

82     public Map JavaDoc createViewsMap(List JavaDoc viewNodes) throws ConfigException
83     {
84         Map JavaDoc result = new HashMap JavaDoc();
85
86         Iterator JavaDoc it = viewNodes.iterator();
87         while (it.hasNext())
88         {
89             Element viewNode = (Element)it.next();
90
91             String JavaDoc viewName = viewNode.getAttributeValue(ATTR_VIEW_NAME);
92             String JavaDoc ref = viewNode.getAttributeValue(ATTR_VIEW_REF);
93             
94             // viewName can default to the ref name
95
if (viewName == null)
96                 viewName = ref;
97             
98             // Maybe no name was specified
99
if (viewName == null)
100             {
101                 if (viewNodes.size() > 1)
102                     throw new ConfigException("You cannot have views without names if there are more than one: " + XML.toString(viewNode));
103                 else
104                     viewName = ANONYMOUS_VIEW_NAME;
105
106                 log.info("Has single unnamed view");
107             }
108             else
109             {
110                 log.info("Has view named: " + viewName);
111             }
112
113             // Hook up any references
114
if (ref != null)
115             {
116                 this.addView(result, viewName, ref);
117             }
118             else // not a reference, thus view is defined in-place
119
{
120                 String JavaDoc mode = viewNode.getAttributeValue(ATTR_VIEW_MODE);
121
122                 View v = this.masterFact.createView(viewNode);
123
124                 this.addView(result, viewName, mode, v);
125             }
126         }
127
128         if (result.isEmpty())
129             throw new ConfigException("No views defined.");
130
131         return result;
132     }
133
134     /**
135      * Defines a global view which can later be used by calling addView()
136      * with the ref parameter.
137      *
138      * @param mode can be null
139      */

140     abstract protected void defineGlobalView(String JavaDoc id, String JavaDoc mode, View v) throws ConfigException;
141
142     /**
143      * Adds any views to the target Map which are associated with the viewName.
144      */

145     abstract protected void addView(Map JavaDoc target, String JavaDoc viewName, String JavaDoc ref) throws ConfigException;
146
147     /**
148      * Adds one view to the target Map, using the specified mode.
149      *
150      * @param mode can be null
151      */

152     abstract protected void addView(Map JavaDoc target, String JavaDoc viewName, String JavaDoc mode, View v) throws ConfigException;
153 }
Popular Tags