BrightSide Workbench Full Report + Source Code
WebMap.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.navigation;
19 
20 import java.io.IOException;
21 import java.util.SortedSet;
22 import org.turro.elephant.context.IConstructor;
23 import org.turro.elephant.impl.context.ContextAdapter;
24 import org.turro.elephant.web.ElContext;
25 import org.turro.elephant.web.ElContextMap;
26 import org.turro.html.HTMLHelper;
27 
32 public class WebMap {
33 
35  protected int maxLevels;
36 
38  this.constructor = constructor;
39  }
40 
41  public void draw() throws IOException {
42  draw(3);
43  }
44 
45  //TODO: intelligent draw: stack items depending on children count
46  public void draw(int maxLevels) throws IOException {
47  this.maxLevels = maxLevels;
50  hp.startTable("cellspacing='5px' width='100%' class='wmap'");
51  hp.startTableRow("class='wmap'");
52  SortedSet<ElContext> ctxs = new ContextAdapter(root.getChildren()).getVisible();
53  int level = 0;
54  boolean hasSingles = false;
55  int ctxCount = 0;
56  for(ElContext ctx : ctxs) {
57  if(new ContextAdapter(ctx.getChildren()).getVisible().size() == 0) {
58  hasSingles = true;
59  } else {
60  ctxCount++;
61  }
62  }
63  if(hasSingles) {
64  ctxCount++;
65  }
66  for(ElContext ctx : ctxs) {
67  if(new ContextAdapter(ctx.getChildren()).getVisible().size() > 0) {
68  hp.startTableCol("style='width:" + ((int)Math.ceil(100 / ctxCount)) + "%'")
69  .startTag("ul", "class='wmap" + level + "'")
70  .startTag("li", "class='wmap" + level + "'")
71  .startAnchor(ctx.getWebPath(), ctx.getName(), "wmap" + level)
72  .write(ctx.getName())
73  .endTag();
74  doChildren(hp, ctx, level + 1);
75  hp.endTag("ul");
76  }
77  }
78  if(hasSingles) {
79  hp.startTableCol("style='width:" + ((int)Math.ceil(100 / ctxCount)) + "%'");
80  for(ElContext ctx : ctxs) {
81  if(new ContextAdapter(ctx.getChildren()).getVisible().size() == 0) {
82  hp.startTag("ul", "class='wmap" + level + "'")
83  .startTag("li", "class='wmap" + level + "'")
84  .startAnchor(ctx.getWebPath(), ctx.getName(), "wmap" + level)
85  .write(ctx.getName())
86  .endTag();
87  doChildren(hp, ctx, level + 1);
88  hp.endTag("ul");
89  }
90  }
91  }
92  hp.endAllTags();
93  }
94 
95  private void doChildren(HTMLHelper hp, ElContext current, int level) {
96  if(level < maxLevels) {
97  SortedSet<ElContext> ctxs= new ContextAdapter(current.getChildren()).getVisible();
98  if (ctxs.size() > 0) {
99  for(ElContext ctx : ctxs) {
100  hp.startTag("ul", "class='wmap" + level + "'")
101  .startTag("li", "class='wmap" + level + "'")
102  .startAnchor(ctx.getWebPath(), ctx.getName(), "wmap" + level)
103  .write(ctx.getName())
104  .endTag();
105  doChildren(hp, ctx, level + 1);
106  hp.endTag("ul");
107  }
108  }
109  }
110  }
111 
112 }
WebMap(IConstructor constructor)
Definition: WebMap.java:37
TreeSet< ElContext > getChildren()
Definition: ElContext.java:196
HTMLGenerator write(String value)
HTMLGenerator startTag(String tag)
HTMLGenerator startTableRow(String attributes)
HTMLGenerator startAnchor(String url, String hint)
HTMLGenerator startTableCol(String attributes)
HTMLGenerator startTable(String attributes)