KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > tree > AttributeRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.tree;
19
20 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
21 import org.apache.beehive.netui.tags.rendering.AnchorTag;
22 import org.apache.beehive.netui.tags.rendering.DivTag;
23 import org.apache.beehive.netui.tags.rendering.ImageTag;
24
25 import java.util.ArrayList JavaDoc;
26
27 class AttributeRenderer
28 {
29     private static final ArrayList JavaDoc empty = new ArrayList JavaDoc();
30     private static final RemoveInfo emptyRemoves = new RemoveInfo();
31     private ArrayList JavaDoc[] _lists;
32     private RemoveInfo _removes;
33
34     /**
35      * Create the attribute renderer. This will create Empty lists for each of the types of
36      * attributes supported. These empty lists will be replaced once an attribute is added to them.
37      */

38     public AttributeRenderer()
39     {
40         _lists = new ArrayList JavaDoc[TreeHtmlAttributeInfo.HTML_LOCATION_CNT];
41         for (int i = 0; i < _lists.length; i++) {
42             _lists[i] = empty;
43         }
44     }
45
46     /**
47      * Add all of the attributes associated with an element to the internal lists.
48      * @param elem
49      * @return
50      */

51     public RemoveInfo addElement(TreeElement elem)
52     {
53         // if the attributes are empty then there is nothing to add to lists
54
ArrayList JavaDoc attrs = elem.getAttributeList();
55         if (attrs == null || attrs.size() == 0)
56             return emptyRemoves;
57
58         // We will track all of the elements that we are removing from the current inheritence
59
// list set and return those back to the caller to stash away on the stack.
60
RemoveInfo removes = _removes;
61         if (removes == null)
62             removes = new RemoveInfo();
63
64         // walk all of the attributes
65
int cnt = attrs.size();
66         assert (cnt > 0);
67         for (int i = 0; i < cnt; i++) {
68             TreeHtmlAttributeInfo attr = (TreeHtmlAttributeInfo) attrs.get(i);
69             if (attr.isOnDiv()) {
70                 addAttribute(TreeHtmlAttributeInfo.HTML_LOCATION_DIV, attr, removes);
71             }
72             if (attr.isOnIcon()) {
73                 addAttribute(TreeHtmlAttributeInfo.HTML_LOCATION_ICON, attr, removes);
74             }
75             if (attr.isOnSelectionLink()) {
76                 addAttribute(TreeHtmlAttributeInfo.HTML_LOCATION_SELECTION_LINK, attr, removes);
77             }
78         }
79
80         // if we didn't remove anything then we should simply stash the array list away for next time
81
// and return null.
82
if (removes.removes.size() == 0) {
83             _removes = removes;
84             removes = null;
85         }
86         else {
87             _removes = null;
88         }
89
90         return removes;
91     }
92
93     /**
94      * This method will remove all of the elements scoped to the attribute.
95      * @param elem
96      */

97     public void removeElementScoped(TreeElement elem, RemoveInfo removes)
98     {
99         assert(elem != null);
100
101         // check to see if we can exist without processing this element. If the element
102
// didn't define any attribute then we can. This is inforced in the addElement method.
103
ArrayList JavaDoc attrs = elem.getAttributeList();
104         if (attrs == null || attrs.size() == 0)
105             return;
106
107         // walk all of the lists and each list looking for attributes that
108
// are scoped only to the element
109
for (int i = 0; i < _lists.length; i++) {
110             ArrayList JavaDoc al = _lists[i];
111             assert(al != null);
112
113             int cnt = al.size();
114             for (int j = 0; j < cnt; j++) {
115                 TreeHtmlAttributeInfo attr = (TreeHtmlAttributeInfo) al.get(j);
116                 assert(attr != null);
117                 // Remove all of the attributes not scoped to the element itself. We need to
118
// update the indexes because this removes the current item..
119
if (!attr.isApplyToDescendents()) {
120                     al.remove(j);
121                     cnt--;
122                     j--;
123                     if (removes != null && removes.scopeOverrides) {
124                         TreeHtmlAttributeInfo addBack = checkScopeRemoval(i, attr, removes);
125                         if (addBack != null) {
126                             if (!al.contains(addBack))
127                                 al.add(addBack);
128                         }
129                     }
130                 }
131             }
132         }
133     }
134
135     public void removeElement(TreeElement elem, RemoveInfo removes)
136     {
137         // walk all of the attributes associated with the element, for any that are passed
138
// on to their decendents we will remove. This will be done for each type of element
139
// being removed.
140
ArrayList JavaDoc attrs = elem.getAttributeList();
141         if (attrs != null) {
142             int cnt = attrs.size();
143             for (int i = 0; i < cnt; i++) {
144                 TreeHtmlAttributeInfo attr = (TreeHtmlAttributeInfo) attrs.get(i);
145
146                 // if the attribute is being passed on to the decendents then we need to output
147
// that attribute.
148
if (attr.isApplyToDescendents()) {
149                     if (attr.isOnDiv()) {
150                         removeAttribute(TreeHtmlAttributeInfo.HTML_LOCATION_DIV, attr, removes);
151                     }
152                     if (attr.isOnIcon()) {
153                         removeAttribute(TreeHtmlAttributeInfo.HTML_LOCATION_ICON, attr, removes);
154                     }
155                     if (attr.isOnSelectionLink()) {
156                         removeAttribute(TreeHtmlAttributeInfo.HTML_LOCATION_SELECTION_LINK, attr, removes);
157                     }
158                 }
159             }
160         }
161     }
162
163     /**
164      * This method will render the values assocated with the Icon Image.
165      * @param state
166      * @param elem
167      */

168     public void renderIconImage(ImageTag.State state, TreeElement elem)
169     {
170         ArrayList JavaDoc al = _lists[TreeHtmlAttributeInfo.HTML_LOCATION_ICON];
171
172         assert(al != null);
173         if (al.size() == 0)
174             return;
175
176         int cnt = al.size();
177         for (int i = 0; i < cnt; i++) {
178             TreeHtmlAttributeInfo attr = (TreeHtmlAttributeInfo) al.get(i);
179             state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, attr.getAttribute(), attr.getValue());
180         }
181     }
182
183     /**
184      * This method will render the values assocated with the selection link.
185      * @param state
186      * @param elem
187      */

188     public void renderSelectionLink(AnchorTag.State state, TreeElement elem)
189     {
190         ArrayList JavaDoc al = _lists[TreeHtmlAttributeInfo.HTML_LOCATION_SELECTION_LINK];
191
192         assert(al != null);
193         if (al.size() == 0)
194             return;
195
196         int cnt = al.size();
197         for (int i = 0; i < cnt; i++) {
198             TreeHtmlAttributeInfo attr = (TreeHtmlAttributeInfo) al.get(i);
199             state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, attr.getAttribute(), attr.getValue());
200         }
201     }
202
203     /**
204      * This method will render the values assocated with the div around a treeItem.
205      * @param state
206      * @param elem
207      */

208     public void renderDiv(DivTag.State state, TreeElement elem)
209     {
210         ArrayList JavaDoc al = _lists[TreeHtmlAttributeInfo.HTML_LOCATION_DIV];
211
212         assert(al != null);
213         if (al.size() == 0)
214             return;
215
216         int cnt = al.size();
217         for (int i = 0; i < cnt; i++) {
218             TreeHtmlAttributeInfo attr = (TreeHtmlAttributeInfo) al.get(i);
219             state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, attr.getAttribute(), attr.getValue());
220         }
221     }
222
223     public String JavaDoc toString()
224     {
225         return "AttributeRender: D:" + _lists[TreeHtmlAttributeInfo.HTML_LOCATION_DIV].size() +
226                 " I:" + _lists[TreeHtmlAttributeInfo.HTML_LOCATION_ICON].size() +
227                 " IL:" + _lists[TreeHtmlAttributeInfo.HTML_LOCATION_SELECTION_LINK].size();
228     }
229
230     private TreeHtmlAttributeInfo checkScopeRemoval(int list, TreeHtmlAttributeInfo attr, RemoveInfo removes)
231     {
232         int cnt = removes.removes.size();
233         assert(cnt > 0);
234
235         for (int i = 0; i < cnt; i++) {
236             TreeHtmlAttributeInfo a = (TreeHtmlAttributeInfo) removes.removes.get(i);
237             if (a.getAttribute().equals(attr.getAttribute())) {
238                 switch (list) {
239                     case TreeHtmlAttributeInfo.HTML_LOCATION_DIV:
240                         if (a.isOnDiv())
241                             return a;
242                         break;
243                     case TreeHtmlAttributeInfo.HTML_LOCATION_ICON:
244                         if (a.isOnIcon())
245                             return a;
246                         break;
247                     case TreeHtmlAttributeInfo.HTML_LOCATION_SELECTION_LINK:
248                         if (a.isOnSelectionLink())
249                             return a;
250                         break;
251                 }
252             }
253         }
254         return null;
255     }
256
257     /**
258      * This method will add an attribute to the list of inheritted attributes.
259      * @param list
260      * @param attr
261      * @param removes
262      */

263     private void addAttribute(int list, TreeHtmlAttributeInfo attr, RemoveInfo removes)
264     {
265         ArrayList JavaDoc al = _lists[list];
266         // if the array list is the empty list then we need to allocate a new array list
267
if (al == empty) {
268             al = new ArrayList JavaDoc();
269             _lists[list] = al;
270         }
271
272         // check to see if this attribute is already inside the tree.
273
int cnt = al.size();
274         for (int i = 0; i < cnt; i++) {
275             TreeHtmlAttributeInfo a = (TreeHtmlAttributeInfo) al.get(i);
276             assert(a != null);
277             if (a.getAttribute().equals(attr.getAttribute())) {
278                 removes.removes.add(a);
279                 if (!attr.isApplyToDescendents()) {
280                     removes.scopeOverrides = true;
281                 }
282                 al.remove(a);
283                 break;
284             }
285         }
286
287         // add this to the list
288
al.add(attr);
289     }
290
291     /**
292      * This method will add an attribute to the list of inheritted attributes.
293      * @param list
294      * @param attr
295      * @param removes
296      */

297     private void removeAttribute(int list, TreeHtmlAttributeInfo attr, RemoveInfo removes)
298     {
299         assert(attr != null);
300
301         // get the correct array list
302
ArrayList JavaDoc al = _lists[list];
303         assert(al != null);
304
305         // remove the attributes from the array list -- this should never fail
306
boolean removed = al.remove(attr);
307         assert(removed) : "Unable to remove attribute:" + attr.getAttribute() + "=" + attr.getValue();
308
309         // if there was a list of removed attributes we need to possibly add one back
310
// We do this by walking the removes list and when we find an attribute with the
311
// same name and supporting the same list type, we add it to the list.
312
if (removes != null) {
313
314             // walk the remove list looking for an attribute with the same name
315
int cnt = removes.removes.size();
316             for (int i = 0; i < cnt; i++) {
317                 TreeHtmlAttributeInfo a = (TreeHtmlAttributeInfo) removes.removes.get(i);
318                 if (attr.getAttribute().equals(a.getAttribute())) {
319
320                     // based upon the type we are removing, if the matching attribute supports the type
321
// then we add it back.
322
switch (list) {
323                         case TreeHtmlAttributeInfo.HTML_LOCATION_DIV:
324                             if (a.isOnDiv() && !al.contains(a))
325                                 al.add(a);
326                             break;
327                         case TreeHtmlAttributeInfo.HTML_LOCATION_ICON:
328                             if (a.isOnIcon() && !al.contains(a))
329                                 al.add(a);
330                             break;
331                         case TreeHtmlAttributeInfo.HTML_LOCATION_SELECTION_LINK:
332                             if (a.isOnSelectionLink() && !al.contains(a))
333                                 al.add(a);
334                             break;
335                     }
336                 }
337             }
338         }
339     }
340
341     public static class RemoveInfo
342     {
343         ArrayList JavaDoc removes;
344         boolean scopeOverrides;
345
346         public RemoveInfo()
347         {
348             removes = new ArrayList JavaDoc();
349             scopeOverrides = false;
350         }
351     }
352 }
353
Popular Tags