KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > table > AxisHeaderBuilderSupport


1 package com.tonbeller.jpivot.table;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.w3c.dom.Element JavaDoc;
8
9 import com.tonbeller.jpivot.olap.model.Alignable;
10 import com.tonbeller.jpivot.table.SpanBuilder.SBContext;
11 import com.tonbeller.jpivot.table.span.Span;
12 import com.tonbeller.wcf.popup.MenuItem;
13 import com.tonbeller.wcf.popup.MenuItemSupport;
14 import com.tonbeller.wcf.popup.PopUp;
15
16 public class AxisHeaderBuilderSupport implements AxisHeaderBuilder {
17
18   private SpanBuilder spanBuilder;
19
20   public AxisHeaderBuilderSupport(SpanBuilder spanBuilder) {
21     this.spanBuilder = spanBuilder;
22   }
23   
24   class MySBContext implements SBContext {
25     Element JavaDoc captionElem;
26     String JavaDoc captionLabel;
27     List JavaDoc clickables = new ArrayList JavaDoc();
28
29     public void setCaption(Element JavaDoc elem, String JavaDoc label) {
30       this.captionElem = elem;
31       this.captionLabel = label;
32     }
33     public void addClickable(String JavaDoc href, String JavaDoc label) {
34       MenuItemSupport mis = new MenuItemSupport(href);
35       mis.setLabel(label);
36       clickables.add(mis);
37     }
38
39     void render() {
40       if (clickables.isEmpty())
41         return;
42       
43       if (clickables.size() == 1) {
44         MenuItem mi = (MenuItem) clickables.get(0);
45         captionElem.setAttribute("href", mi.getHref());
46         return;
47       }
48       
49       PopUp pu = new PopUp();
50       pu.setLabel(captionLabel);
51       for (Iterator JavaDoc it = clickables.iterator(); it.hasNext();) {
52         MenuItem mi = (MenuItem) it.next();
53         pu.addItem(mi);
54       }
55       Element JavaDoc e = pu.render(captionElem.getOwnerDocument());
56       captionElem.appendChild(e);
57     }
58     
59   }
60
61   public void build(Element JavaDoc row, Span span, int rowspan, int colspan, boolean even, boolean memberIndent) {
62     MySBContext sbctx = new MySBContext();
63     Element JavaDoc elem = spanBuilder.build(sbctx, span, even);
64     sbctx.render();
65     elem.setAttribute("rowspan", Integer.toString(rowspan));
66     elem.setAttribute("colspan", Integer.toString(colspan));
67
68     // no special formatting present?
69
if (elem.getAttribute("style").length() == 0) {
70       // valid styles are { span, even, odd, span-right, even-right, odd-right}
71
String JavaDoc style;
72       if (colspan > 1 || rowspan > 1)
73         style = "span";
74       else if (even)
75         style = "even";
76       else
77         style = "odd";
78       if (isRightAligned(span))
79         style = style + "-right";
80       elem.setAttribute("style", style);
81     }
82
83     // indent level for hierarchy style view
84
if (memberIndent && span.isMember()) {
85       elem.setAttribute("indent", Integer.toString(span.getIndent()));
86     }
87
88     row.appendChild(elem);
89   }
90
91   private boolean isRightAligned(Span span) {
92     Object JavaDoc obj = span.getObject();
93     if (obj instanceof Alignable)
94       return ((Alignable) obj).getAlignment() == Alignable.Alignment.RIGHT;
95     return false;
96   }
97
98 }
99
Popular Tags