KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.table;
14
15 import java.util.Iterator JavaDoc;
16
17 import org.apache.log4j.Logger;
18 import org.w3c.dom.Element JavaDoc;
19
20 import com.tonbeller.jpivot.olap.model.Dimension;
21 import com.tonbeller.jpivot.olap.model.Displayable;
22 import com.tonbeller.jpivot.olap.model.EmptyMember;
23 import com.tonbeller.jpivot.olap.model.Hierarchy;
24 import com.tonbeller.jpivot.olap.model.Level;
25 import com.tonbeller.jpivot.olap.model.Member;
26 import com.tonbeller.jpivot.olap.model.Property;
27 import com.tonbeller.jpivot.olap.model.PropertyHolder;
28 import com.tonbeller.jpivot.olap.model.Visitable;
29 import com.tonbeller.jpivot.olap.model.VisitorSupportStrict;
30 import com.tonbeller.jpivot.table.span.PropertyHeading;
31 import com.tonbeller.jpivot.table.span.PropertyUtils;
32 import com.tonbeller.jpivot.table.span.Span;
33 import com.tonbeller.jpivot.table.span.SpanVisitor;
34
35 /**
36  * renders a row or column heading. Creates a DOM Element from a Member, Hierarchy,
37  * Dimension or Level.
38  *
39  * @author av
40  */

41 public class SpanBuilderImpl extends PartBuilderSupport implements SpanBuilder {
42   String JavaDoc memberName;
43   String JavaDoc headingName;
44   private static final Logger logger = Logger.getLogger(SpanBuilderImpl.class);
45
46   /**
47    * creates an instance
48    * @param memberName either "row-heading" or "column-heading"
49    * @param headingName element name for the heading of a row- or column-heading (i.e. "heading-heading").
50    */

51   public SpanBuilderImpl(String JavaDoc memberName, String JavaDoc headingName) {
52     this.memberName = memberName;
53     this.headingName = headingName;
54   }
55
56   class RenderSwitch extends VisitorSupportStrict implements SpanVisitor {
57
58     private static final String JavaDoc CAPTION = "caption";
59     Element JavaDoc elem;
60     private SBContext sbctx;
61
62     public RenderSwitch(SBContext sbctx) {
63       this.sbctx = sbctx;
64     }
65
66     void renderHeading(Displayable d) {
67       elem = table.elem(headingName);
68       Element JavaDoc caption = table.append(CAPTION, elem);
69       String JavaDoc label = d.getLabel();
70       caption.setAttribute(CAPTION, label);
71       sbctx.setCaption(caption, label);
72     }
73
74     private void renderMember(Displayable d, String JavaDoc label) {
75       elem = table.elem(memberName);
76       Element JavaDoc caption = table.append(CAPTION, elem);
77       //String label = d.getLabel();
78
caption.setAttribute(CAPTION, label);
79       sbctx.setCaption(caption, label);
80
81       if (d instanceof PropertyHolder) {
82         Property[] props = ((PropertyHolder) d).getProperties();
83         PropertyUtils.addInlineProperties(caption, props);
84         Property style = PropertyUtils.getInlineProperty(props, PropertyUtils.STYLE_PROPERTY);
85         if (style != null) {
86           String JavaDoc value = style.getValue().trim();
87           if (value.length() > 0)
88             elem.setAttribute(PropertyUtils.STYLE_PROPERTY, value.toLowerCase());
89         }
90       }
91
92     }
93
94     public void visitPropertyHeading(PropertyHeading v) {
95       renderHeading(v);
96     }
97
98     public void visitDimension(Dimension v) {
99       renderHeading(v);
100     }
101
102     public void visitHierarchy(Hierarchy v) {
103       renderHeading(v);
104     }
105
106     public void visitLevel(Level v) {
107       renderHeading(v);
108     }
109
110     public void visitMember(Member v) {
111       renderMember(v, v.getLabel());
112     }
113
114     public void visitProperty(Property v) {
115       renderMember(v, v.getValue());
116     }
117
118     public void visitEmptyMember(EmptyMember v) {
119       renderMember(v, v.getLabel());
120     }
121
122     public Element JavaDoc getElem() {
123       return elem;
124     }
125
126     public void setElem(Element JavaDoc elem) {
127       this.elem = elem;
128     }
129   }
130
131   /**
132    * renders a row- or column heading
133    */

134   public Element JavaDoc build(SBContext sbctx, Span span, boolean even) {
135     if (logger.isDebugEnabled())
136       logger.debug("build " + span);
137     
138     RenderSwitch renderSwitch = new RenderSwitch(sbctx);
139     
140     Visitable v = span.getObject();
141     
142     v.accept(renderSwitch);
143     Element JavaDoc elem = renderSwitch.getElem();
144     for (Iterator JavaDoc it = table.clickableIterator(); it.hasNext();)
145       ((ClickableMember) it.next()).decorate(sbctx, span.getObject());
146     return elem;
147   }
148
149 }
150
Popular Tags