1 13 package com.tonbeller.jpivot.table.navi; 14 15 import javax.servlet.http.HttpSession ; 16 17 import org.w3c.dom.Element ; 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 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 ID = "drillThrough"; 51 public String getId() { 52 return ID; 53 } 54 55 public void initialize(RequestContext context, TableComponent table) throws Exception { 56 super.initialize(context, table); 57 table.getOlapModel().addModelChangeListener(this); 58 59 if (!initializeExtension()) { 61 available = false; 62 return; 63 } 64 available = true; 65 66 table.getDispatcher().addRequestListener(null, null, dispatcher); 68 69 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 build(Cell cell, boolean even) { 90 Element parent = super.build(cell, even); 91 92 if (!enabled || !renderActions || extension == null) 93 return parent; 94 95 String id = DomUtils.randomId(); 96 if (canDrillThrough(cell) && (!cell.isNull())) { 97 Element 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 } 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 { 116 if (canDrillThrough(cell)) { 117 HttpSession session = context.getSession(); 118 final String drillTableRef = table.getOlapModel().getID() + ".drillthroughtable"; 119 ITableComponent tc = 120 (ITableComponent) session.getAttribute(drillTableRef); 121 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 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 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 |