KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpSession JavaDoc;
16
17 import org.w3c.dom.Element JavaDoc;
18
19 import com.tonbeller.jpivot.core.ModelChangeEvent;
20 import com.tonbeller.jpivot.core.ModelChangeListener;
21 import com.tonbeller.jpivot.olap.model.Cell;
22 import com.tonbeller.jpivot.olap.model.OlapModel;
23 import com.tonbeller.jpivot.olap.navi.DrillThrough;
24 import com.tonbeller.jpivot.table.CellBuilder;
25 import com.tonbeller.jpivot.table.CellBuilderDecorator;
26 import com.tonbeller.jpivot.table.TableComponent;
27 import com.tonbeller.jpivot.table.TableComponentExtensionSupport;
28 import com.tonbeller.jpivot.mondrian.MondrianDrillThroughTableModel;
29 import com.tonbeller.wcf.component.RendererParameters;
30 import com.tonbeller.wcf.controller.Dispatcher;
31 import com.tonbeller.wcf.controller.DispatcherSupport;
32 import com.tonbeller.wcf.controller.RequestContext;
33 import com.tonbeller.wcf.controller.RequestListener;
34 import com.tonbeller.wcf.table.*;
35 import com.tonbeller.wcf.utils.DomUtils;
36
37 /**
38  *
39  * @author Robin Bagot
40  */

41 public class DrillThroughUI extends TableComponentExtensionSupport implements ModelChangeListener {
42
43   boolean available;
44   boolean renderActions;
45   Dispatcher dispatcher = new DispatcherSupport();
46   DrillThrough extension;
47
48   TableModelDecorator tableModel = new TableModelDecorator(EmptyTableModel.instance());
49
50   public static final String JavaDoc ID = "drillThrough";
51   public String JavaDoc getId() {
52     return ID;
53   }
54
55   public void initialize(RequestContext context, TableComponent table) throws Exception JavaDoc {
56     super.initialize(context, table);
57     table.getOlapModel().addModelChangeListener(this);
58
59     // does the underlying data model support drill?
60
if (!initializeExtension()) {
61       available = false;
62       return;
63     }
64     available = true;
65
66     // extend the controller
67
table.getDispatcher().addRequestListener(null, null, dispatcher);
68
69     // add some decorators via table.get/setRenderer
70
CellBuilder cb = table.getCellBuilder();
71     DomDecorator cr = new DomDecorator(table.getCellBuilder());
72     table.setCellBuilder(cr);
73
74   }
75
76   public void startBuild(RequestContext context) {
77     super.startBuild(context);
78     renderActions = RendererParameters.isRenderActions(context);
79     if (renderActions)
80       dispatcher.clear();
81   }
82
83   class DomDecorator extends CellBuilderDecorator {
84
85     DomDecorator(CellBuilder delegate) {
86       super(delegate);
87     }
88
89     public Element JavaDoc build(Cell cell, boolean even) {
90       Element JavaDoc parent = super.build(cell, even);
91
92       if (!enabled || !renderActions || extension == null)
93         return parent;
94
95       String JavaDoc id = DomUtils.randomId();
96       if (canDrillThrough(cell) && (!cell.isNull())) {
97         // add a drill through child node to cell element
98
Element JavaDoc elem = table.insert("drill-through", parent);
99         elem.setAttribute("id", id);
100         elem.setAttribute("title", "Show source data");
101         dispatcher.addRequestListener(id, null, new DrillThroughHandler(cell));
102       } else {
103         // dont add anything
104
}
105
106       return parent;
107     }
108   }
109
110   class DrillThroughHandler implements RequestListener {
111     Cell cell;
112     DrillThroughHandler(Cell cell) {
113       this.cell = cell;
114     }
115     public void request(RequestContext context) throws Exception JavaDoc {
116       if (canDrillThrough(cell)) {
117         HttpSession JavaDoc session = context.getSession();
118         final String JavaDoc drillTableRef = table.getOlapModel().getID() + ".drillthroughtable";
119         ITableComponent tc =
120           (ITableComponent) session.getAttribute(drillTableRef);
121         // get a new drill through table model
122
TableModel tm = drillThrough(cell);
123         tc.setModel(tm);
124         tc.setVisible(true);
125         TableColumn[] tableColumns =
126           ((EditableTableComponent) tc).getTableComp().getTableColumns();
127         for (int i = 0; i < tableColumns.length; i++) {
128           TableColumn tableColumn = tableColumns[i];
129           tableColumn.setHidden(false);
130         }
131       }
132     }
133   }
134
135   /** @return true if extension is available */
136   protected boolean initializeExtension() {
137     OlapModel om = table.getOlapModel();
138     extension = (DrillThrough) om.getExtension(DrillThrough.ID);
139     return extension != null;
140   }
141
142   protected boolean canDrillThrough(Cell cell) {
143     return extension.canDrillThrough((Cell) cell.getRootDecoree());
144   }
145
146   /**
147    * returns a DrillThroughTableModel object for the drill through
148    * @param cell
149    * @return
150    */

151   protected TableModel drillThrough(Cell cell) {
152     return extension.drillThrough((Cell) cell.getRootDecoree());
153   }
154
155   public boolean isAvailable() {
156     return available;
157   }
158
159   public void modelChanged(ModelChangeEvent e) {
160   }
161
162   public void structureChanged(ModelChangeEvent e) {
163     initializeExtension();
164     dispatcher.clear();
165   }
166
167   public TableModel getTableModel() {
168     return tableModel;
169   }
170 }
171
Popular Tags