1 13 package com.tonbeller.jpivot.table.navi; 14 15 import org.w3c.dom.Element ; 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 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 { 48 super.initialize(context, table); 49 table.getOlapModel().addModelChangeListener(this); 50 51 available = initializeExtension(); 53 54 table.getDispatcher().addRequestListener(null, null, dispatcher); 56 57 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 build(SBContext sbctx, Span span, boolean even) { 81 Element parent = super.build(sbctx, span, even); 82 83 if (!enabled || !renderActions || !available) 84 return parent; 85 86 String id = DomUtils.randomId(); 87 if (canExpand(span)) { 88 Element 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 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 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 { 111 Scroller.enableScroller(context); 112 if (canExpand(span)) 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 { 123 Scroller.enableScroller(context); 124 if (canCollapse(span)) collapse(span); 126 } 127 } 128 129 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 getExpandImage(); 136 protected abstract String getCollapseImage(); 137 protected abstract String getOtherImage(); 138 139 public boolean isAvailable() { 140 return available; 141 } 142 143 144 static int indexOf(Object [] array, Object obj) { 145 for (int i = 0; i < array.length; i++) 146 if (array[i] == obj) 147 return i; 148 return -1; 149 } 150 151 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 |