1 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 ; 26 27 class AttributeRenderer 28 { 29 private static final ArrayList empty = new ArrayList (); 30 private static final RemoveInfo emptyRemoves = new RemoveInfo(); 31 private ArrayList [] _lists; 32 private RemoveInfo _removes; 33 34 38 public AttributeRenderer() 39 { 40 _lists = new ArrayList [TreeHtmlAttributeInfo.HTML_LOCATION_CNT]; 41 for (int i = 0; i < _lists.length; i++) { 42 _lists[i] = empty; 43 } 44 } 45 46 51 public RemoveInfo addElement(TreeElement elem) 52 { 53 ArrayList attrs = elem.getAttributeList(); 55 if (attrs == null || attrs.size() == 0) 56 return emptyRemoves; 57 58 RemoveInfo removes = _removes; 61 if (removes == null) 62 removes = new RemoveInfo(); 63 64 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 (removes.removes.size() == 0) { 83 _removes = removes; 84 removes = null; 85 } 86 else { 87 _removes = null; 88 } 89 90 return removes; 91 } 92 93 97 public void removeElementScoped(TreeElement elem, RemoveInfo removes) 98 { 99 assert(elem != null); 100 101 ArrayList attrs = elem.getAttributeList(); 104 if (attrs == null || attrs.size() == 0) 105 return; 106 107 for (int i = 0; i < _lists.length; i++) { 110 ArrayList 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 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 ArrayList 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 (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 168 public void renderIconImage(ImageTag.State state, TreeElement elem) 169 { 170 ArrayList 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 188 public void renderSelectionLink(AnchorTag.State state, TreeElement elem) 189 { 190 ArrayList 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 208 public void renderDiv(DivTag.State state, TreeElement elem) 209 { 210 ArrayList 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 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 263 private void addAttribute(int list, TreeHtmlAttributeInfo attr, RemoveInfo removes) 264 { 265 ArrayList al = _lists[list]; 266 if (al == empty) { 268 al = new ArrayList (); 269 _lists[list] = al; 270 } 271 272 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 al.add(attr); 289 } 290 291 297 private void removeAttribute(int list, TreeHtmlAttributeInfo attr, RemoveInfo removes) 298 { 299 assert(attr != null); 300 301 ArrayList al = _lists[list]; 303 assert(al != null); 304 305 boolean removed = al.remove(attr); 307 assert(removed) : "Unable to remove attribute:" + attr.getAttribute() + "=" + attr.getValue(); 308 309 if (removes != null) { 313 314 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 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 removes; 344 boolean scopeOverrides; 345 346 public RemoveInfo() 347 { 348 removes = new ArrayList (); 349 scopeOverrides = false; 350 } 351 } 352 } 353 | Popular Tags |