KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > tree > TreeRenderer


1 package org.apache.slide.projector.processor.tree;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9 import org.apache.slide.projector.ConfigurationException;
10 import org.apache.slide.projector.Context;
11 import org.apache.slide.projector.Result;
12 import org.apache.slide.projector.URI;
13 import org.apache.slide.projector.descriptor.MapValueDescriptor;
14 import org.apache.slide.projector.descriptor.ParameterDescriptor;
15 import org.apache.slide.projector.descriptor.StringValueDescriptor;
16 import org.apache.slide.projector.descriptor.TreeValueDescriptor;
17 import org.apache.slide.projector.i18n.ParameterMessage;
18 import org.apache.slide.projector.processor.SimpleProcessor;
19 import org.apache.slide.projector.processor.TemplateRenderer;
20 import org.apache.slide.projector.value.ArrayValue;
21 import org.apache.slide.projector.value.MapValue;
22 import org.apache.slide.projector.value.StreamableValue;
23 import org.apache.slide.projector.value.StringValue;
24 import org.apache.slide.projector.value.Value;
25
26 public class TreeRenderer extends TemplateRenderer {
27     final private static String JavaDoc HEADER_FRAGMENT = "header ";
28     final private static String JavaDoc FOOTER_FRAGMENT = "footer ";
29     final private static String JavaDoc FIRST_FRAGMENT = "first ";
30     final private static String JavaDoc LAST_FRAGMENT = "last ";
31     final private static String JavaDoc EVEN_FRAGMENT = "even ";
32     final private static String JavaDoc DEFAULT_FRAGMENT = "default ";
33     final private static String JavaDoc EMPTY_FRAGMENT = "empty ";
34     final private static String JavaDoc OPEN_FOLDER_FRAGMENT = "open ";
35     final private static String JavaDoc CLOSED_FOLDER_FRAGMENT = "closed ";
36     final private static String JavaDoc ACTIVE_FRAGMENT = "active ";
37
38     final public static String JavaDoc ID = "id";
39     final public static String JavaDoc ACTIVE_ID = "activeId";
40
41     private ParameterDescriptor []parameterDescriptors;
42
43     public TreeRenderer() {
44         setRequiredFragments(new String JavaDoc[] { DEFAULT_FRAGMENT });
45         setOptionalFragments(new String JavaDoc[] { HEADER_FRAGMENT, FOOTER_FRAGMENT, FIRST_FRAGMENT, LAST_FRAGMENT,
46                                             EVEN_FRAGMENT, EMPTY_FRAGMENT, OPEN_FOLDER_FRAGMENT, CLOSED_FOLDER_FRAGMENT, ACTIVE_FRAGMENT });
47     }
48
49     public void configure(StreamableValue config) throws ConfigurationException {
50         super.configure(config);
51         List JavaDoc validMapEntries = new ArrayList JavaDoc();
52         for ( Iterator JavaDoc i = parameterDescriptions.iterator(); i.hasNext(); ) {
53             ParameterDescriptor parentParameter = (ParameterDescriptor)i.next();
54             if ( !parentParameter.getName().equals(FRAGMENT) ) {
55                 validMapEntries.add(parentParameter);
56             }
57         }
58         parameterDescriptors = new ParameterDescriptor[] {
59             new ParameterDescriptor(ID, new ParameterMessage("treeRenderer/id"), new TreeValueDescriptor(new StringValueDescriptor())),
60             new ParameterDescriptor(SimpleProcessor.INPUT, new ParameterMessage("treeRenderer/input"), new TreeValueDescriptor(new MapValueDescriptor(validMapEntries)))
61         };
62     }
63
64     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
65         Value[] idArray = ((ArrayValue)parameter.get(ID)).getArray();
66         Value[] inputArray = ((ArrayValue)parameter.get(SimpleProcessor.INPUT)).getArray();
67         URI activeIdResource = (URI)context.getBookmark();
68         String JavaDoc activeId = null;
69         int[] active = new int[0];
70         if ( activeIdResource != null ) {
71             activeId = activeIdResource.toString();
72             String JavaDoc path = buildPathToActiveNode(null, activeId, idArray);
73             if ( path != null ) {
74                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(path, ",");
75                 active = new int[tokenizer.countTokens()];
76                 int counter = 0;
77                 while ( tokenizer.hasMoreTokens() ) {
78                     active[counter] = Integer.parseInt(tokenizer.nextToken());
79                     counter++;
80                 }
81             }
82         }
83         return new Result(OK, OUTPUT, new StringValue(process(parameter, idArray, inputArray, active, 0, context), "text/html", false ));
84     }
85
86     private String JavaDoc buildPathToActiveNode(String JavaDoc path, String JavaDoc activeId, Value[] idArray) {
87         for ( int i = 0; i < idArray.length; i++ ) {
88             if ( idArray[i] instanceof StringValue && ((StringValue)idArray[i]).toString().equals(activeId) ) {
89                 if ( path != null) {
90                     return path+","+i;
91                 } else {
92                     return String.valueOf(i);
93                 }
94             } else if ( idArray[i] instanceof ArrayValue ) {
95                 String JavaDoc newPath;
96                 if ( path != null) {
97                     newPath = path+","+i;
98                 } else {
99                      newPath = String.valueOf(i);
100                 }
101                 String JavaDoc nestedPath = buildPathToActiveNode(newPath, activeId, ((ArrayValue)idArray[i]).getArray());
102                 if ( nestedPath != null ) {
103                     return nestedPath;
104                 }
105             }
106         }
107         return null;
108     }
109
110     public String JavaDoc process(Map JavaDoc parameter, Value[] idArray, Value[] inputArray, int[] active, int level, Context context) throws Exception JavaDoc {
111         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(1024);
112         Template defaultTemplate = getRequiredFragment(DEFAULT_FRAGMENT+level);
113         Template headerTemplate = getOptionalFragment(HEADER_FRAGMENT+level);
114         Template footerTemplate = getOptionalFragment(FOOTER_FRAGMENT+level);
115         Template firstTemplate = getOptionalFragment(FIRST_FRAGMENT+level);
116         Template lastTemplate = getOptionalFragment(LAST_FRAGMENT+level);
117         Template evenTemplate = getOptionalFragment(EVEN_FRAGMENT+level);
118         Template activeTemplate = getOptionalFragment(ACTIVE_FRAGMENT+level);
119         Template emptyTemplate = getOptionalFragment(EMPTY_FRAGMENT+level);
120         Template openTemplate = getOptionalFragment(OPEN_FOLDER_FRAGMENT+level);
121         Template closedTemplate = getOptionalFragment(CLOSED_FOLDER_FRAGMENT+level);
122         if ( headerTemplate != null ) headerTemplate.evaluate(buffer, parameter);
123         for ( int i = 0; i < idArray.length; i++ ) {
124             if ( inputArray[i] instanceof MapValue ) {
125                 parameter.putAll(((MapValue)inputArray[i]).getMap());
126             }
127             if ( inputArray[i] instanceof ArrayValue ) {
128                 // Array resources can only follow StringResources that indicate the folders name
129
if ( ( active.length <= level || active[level] == i)) {
130                     buffer.append(process(parameter, ((ArrayValue)idArray[i]).getArray(), ((ArrayValue)inputArray[i]).getArray(), active, level+1, context));
131                 }
132             } else if ( i < idArray.length-1 && idArray[i+1] instanceof ArrayValue && openTemplate != null && (active.length <= level || active[level] == i)) {
133                 openTemplate.evaluate(buffer, parameter);
134             } else if ( i < idArray.length-1 && idArray[i+1] instanceof ArrayValue && closedTemplate != null ) {
135                 closedTemplate.evaluate(buffer, parameter);
136             } else if ( level == active.length-1 && i == active[level] && activeTemplate != null ) {
137                 activeTemplate.evaluate(buffer, parameter);
138             } else if ( i == 0 && firstTemplate != null ) {
139                 firstTemplate.evaluate(buffer, parameter);
140             } else if ( i == idArray.length-1 && lastTemplate != null ) {
141                 lastTemplate.evaluate(buffer, parameter);
142             } else if ( evenTemplate != null && i%2 == 0 ) {
143                 evenTemplate.evaluate(buffer, parameter);
144             } else if ( defaultTemplate != null ) {
145                 defaultTemplate.evaluate(buffer, parameter);
146             }
147         }
148         if ( footerTemplate != null ) footerTemplate.evaluate(buffer, parameter);
149         return buffer.toString();
150     }
151
152     public ParameterDescriptor[] getParameterDescriptors() {
153         return parameterDescriptors;
154     }
155 }
Popular Tags