KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > table > navi > DrillExpandUI


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.navi;
14
15 import org.w3c.dom.Element JavaDoc;
16
17 import com.tonbeller.jpivot.core.ModelChangeEvent;
18 import com.tonbeller.jpivot.core.ModelChangeListener;
19 import com.tonbeller.jpivot.olap.model.Member;
20 import com.tonbeller.jpivot.olap.model.Position;
21 import com.tonbeller.jpivot.table.AxisBuilder;
22 import com.tonbeller.jpivot.table.SpanBuilder;
23 import com.tonbeller.jpivot.table.SpanBuilderDecorator;
24 import com.tonbeller.jpivot.table.TableComponent;
25 import com.tonbeller.jpivot.table.TableComponentExtensionSupport;
26 import com.tonbeller.jpivot.table.span.Span;
27 import com.tonbeller.wcf.component.RendererParameters;
28 import com.tonbeller.wcf.controller.Dispatcher;
29 import com.tonbeller.wcf.controller.DispatcherSupport;
30 import com.tonbeller.wcf.controller.RequestContext;
31 import com.tonbeller.wcf.controller.RequestListener;
32 import com.tonbeller.wcf.scroller.Scroller;
33 import com.tonbeller.wcf.utils.DomUtils;
34
35 /**
36  * adds expand-node, collapse-node functionality.
37  * Decorates the controller (dispatcher) and view (renderer) of the table component.
38  *
39  * @author av
40  */

41 public abstract class DrillExpandUI extends TableComponentExtensionSupport implements ModelChangeListener {
42   boolean available;
43   boolean renderActions;
44
45   Dispatcher dispatcher = new DispatcherSupport();
46
47   public void initialize(RequestContext context, TableComponent table) throws Exception JavaDoc {
48     super.initialize(context, table);
49     table.getOlapModel().addModelChangeListener(this);
50
51     // does the underlying data model support drillexpand?
52
available = initializeExtension();
53
54     // extend the controller
55
table.getDispatcher().addRequestListener(null, null, dispatcher);
56
57     // add some decorators via table.get/setRenderer
58
AxisBuilder rab = table.getRowAxisBuilder();
59     DomDecorator rhr = new DomDecorator(rab.getSpanBuilder());
60     rab.setSpanBuilder(rhr);
61
62     AxisBuilder cab = table.getColumnAxisBuilder();
63     DomDecorator chr = new DomDecorator(cab.getSpanBuilder());
64     cab.setSpanBuilder(chr);
65   }
66
67   public void startBuild(RequestContext context) {
68     super.startBuild(context);
69     renderActions = RendererParameters.isRenderActions(context);
70     if (renderActions)
71       dispatcher.clear();
72   }
73
74   class DomDecorator extends SpanBuilderDecorator {
75
76     DomDecorator(SpanBuilder delegate) {
77       super(delegate);
78     }
79
80     public Element JavaDoc build(SBContext sbctx, Span span, boolean even) {
81       Element JavaDoc parent = super.build(sbctx, span, even);
82
83       if (!enabled || !renderActions || !available)
84         return parent;
85
86       String JavaDoc id = DomUtils.randomId();
87       if (canExpand(span)) {
88         Element JavaDoc elem = table.insert("drill-expand", parent);
89         elem.setAttribute("id", id);
90         elem.setAttribute("img", getExpandImage());
91         dispatcher.addRequestListener(id, null, new ExpandHandler(span));
92       } else if (canCollapse(span)) {
93         Element JavaDoc elem = table.insert("drill-collapse", parent);
94         elem.setAttribute("id", id);
95         elem.setAttribute("img", getCollapseImage());
96         dispatcher.addRequestListener(id, null, new CollapseHandler(span));
97       } else {
98         Element JavaDoc elem = table.insert("drill-other", parent);
99         elem.setAttribute("img", getOtherImage());
100       }
101       return parent;
102     }
103   }
104
105   class ExpandHandler implements RequestListener {
106     Span span;
107     ExpandHandler(Span span) {
108       this.span = span;
109     }
110     public void request(RequestContext context) throws Exception JavaDoc {
111       Scroller.enableScroller(context);
112       if (canExpand(span)) // back button etc
113
expand(span);
114     }
115   }
116
117   class CollapseHandler implements RequestListener {
118     Span span;
119     CollapseHandler(Span span) {
120       this.span = span;
121     }
122     public void request(RequestContext context) throws Exception JavaDoc {
123       Scroller.enableScroller(context);
124       if (canCollapse(span)) // back button etc
125
collapse(span);
126     }
127   }
128
129   /** @return true if extension is available */
130   protected abstract boolean initializeExtension();
131   protected abstract boolean canExpand(Span span);
132   protected abstract void expand(Span span);
133   protected abstract boolean canCollapse(Span span);
134   protected abstract void collapse(Span span);
135   protected abstract String JavaDoc getExpandImage();
136   protected abstract String JavaDoc getCollapseImage();
137   protected abstract String JavaDoc getOtherImage();
138
139   public boolean isAvailable() {
140     return available;
141   }
142
143   /** utility */
144   static int indexOf(Object JavaDoc[] array, Object JavaDoc obj) {
145     for (int i = 0; i < array.length; i++)
146       if (array[i] == obj)
147         return i;
148     return -1;
149   }
150
151   /**
152    * true, s represents a member that is in the original result position.
153    * false, if s is not a member or if s is a member, that had been added to
154    * show the parents of the member
155    */

156   static boolean positionContainsMember(Span s) {
157     if (!s.isMember())
158       return false;
159     Member m = (Member) s.getMember().getRootDecoree();
160     Position p = (Position) s.getPosition().getRootDecoree();
161     return indexOf(p.getMembers(), m) >= 0;
162   }
163
164   public void modelChanged(ModelChangeEvent e) {
165   }
166
167   public void structureChanged(ModelChangeEvent e) {
168     available = initializeExtension();
169     dispatcher.clear();
170   }
171
172 }
173
Popular Tags