1 13 package org.openi.xmla.navigator; 14 15 import java.util.List ; 16 import java.util.Set ; 17 18 import javax.servlet.http.HttpSession ; 19 20 import org.w3c.dom.Document ; 21 22 import com.tonbeller.jpivot.core.ModelChangeEvent; 23 import com.tonbeller.jpivot.core.ModelChangeListener; 24 import org.openi.xmla.navigator.hierarchy.*; 25 import com.tonbeller.jpivot.navigator.member.MemberNavigator; 26 import com.tonbeller.jpivot.olap.model.Hierarchy; 27 import com.tonbeller.jpivot.olap.model.OlapModel; 28 import com.tonbeller.jpivot.ui.Available; 29 import com.tonbeller.wcf.component.Component; 30 import com.tonbeller.wcf.component.ComponentSupport; 31 import com.tonbeller.wcf.controller.RequestContext; 32 import com.tonbeller.wcf.controller.RequestListener; 33 import com.tonbeller.wcf.selection.SelectionModel; 34 import com.tonbeller.wcf.tree.NodeSorter; 35 36 42 43 public class Navigator extends ComponentSupport implements ModelChangeListener, Available { 44 OlapModel olapModel; 45 MemberNavigator memberNav; 46 HierarchyNavigator hierarchyNav; 47 HierarchyItem currentItem; 48 Component current; 49 50 public Navigator(String id, Component parent, OlapModel olapModel) { 51 super(id, parent); 52 this.olapModel = olapModel; 53 olapModel.addModelChangeListener(this); 54 55 RequestListener acceptHandler = createMemberNavAcceptHandler(); 56 RequestListener cancelHandler = createMemberNavCancelHandler(); 57 memberNav = new MemberNavigator(id + ".membernav", this, olapModel, acceptHandler, cancelHandler); 58 hierarchyNav = new HierarchyNavigator(id + ".hiernav", this, olapModel); 59 if (memberNav.isAvailable()) 60 hierarchyNav.setHierarchyItemClickHandler(createHierarchyItemClickHandler()); 61 current = hierarchyNav; 62 } 63 64 67 public void initialize(RequestContext context) throws Exception { 68 super.initialize(context); 69 memberNav.initialize(context); 70 hierarchyNav.initialize(context); 71 } 72 73 76 public void destroy(HttpSession session) throws Exception { 77 memberNav.destroy(session); 78 hierarchyNav.destroy(session); 79 super.destroy(session); 80 } 81 82 public Document render(RequestContext context) throws Exception { 83 return current.render(context); 84 } 85 86 89 public void show(Component component) { 90 if (component == null) 91 this.current = hierarchyNav; 92 else 93 this.current = component; 94 } 95 96 99 private class HierarchyItemClickAdapter implements HierarchyItemClickHandler { 100 public void itemClicked( 101 RequestContext context, 102 HierarchyItem item, 103 SelectionModel selection, 104 boolean allowChangeOrder) { 105 106 currentItem = item; 107 Hierarchy[] hiers = item.getDimension().getHierarchies(); 108 memberNav.setHierarchies(hiers, allowChangeOrder); 109 memberNav.setSelectionModel(selection); 110 memberNav.expandSelected(false); 111 Set deleted = memberNav.getDeleteNodeModel().getDeleted(); 112 deleted.clear(); 113 deleted.addAll(item.getDeleted()); 114 show(memberNav); 115 } 116 } 117 118 private class MemberNavAcceptHandler implements RequestListener { 119 public void request(RequestContext context) throws Exception { 120 boolean valid = memberNav.validate(context); 122 123 Set set = memberNav.getSelectionModel().getSelection(); 126 List list = NodeSorter.preOrder(set, memberNav.getModel()); 127 String errmesg = currentItem.validateSelection(list); 128 if (errmesg != null) 129 memberNav.setError(errmesg); 130 else if (valid) { 131 currentItem.setSelection(list); 132 currentItem.setDeleted(memberNav.getDeleteNodeModel().getDeleted()); 133 show(hierarchyNav); 134 hierarchyNav.getAcceptHandler().request(context); 136 } 137 } 138 } 139 140 private class MemberNavCancelHandler implements RequestListener { 141 public void request(RequestContext context) throws Exception { 142 memberNav.revert(context); 143 show(hierarchyNav); 144 } 145 } 146 147 150 protected RequestListener createMemberNavAcceptHandler() { 151 return new MemberNavAcceptHandler(); 152 } 153 154 157 protected HierarchyItemClickHandler createHierarchyItemClickHandler() { 158 return new HierarchyItemClickAdapter(); 159 } 160 161 164 protected RequestListener createMemberNavCancelHandler() { 165 return new MemberNavCancelHandler(); 166 } 167 168 172 public boolean isVisible() { 173 return hierarchyNav.isVisible(); 174 } 175 176 180 public void setVisible(boolean visible) { 181 hierarchyNav.setVisible(visible); 182 } 183 184 public void modelChanged(ModelChangeEvent e) { 185 show(hierarchyNav); 186 } 187 188 public void structureChanged(ModelChangeEvent e) { 189 if (memberNav.isAvailable()) 190 hierarchyNav.setHierarchyItemClickHandler(createHierarchyItemClickHandler()); 191 else 192 hierarchyNav.setHierarchyItemClickHandler(null); 193 show(hierarchyNav); 194 } 195 196 public HierarchyNavigator getHierarchyNav() { 197 return hierarchyNav; 198 } 199 200 public MemberNavigator getMemberNav() { 201 return memberNav; 202 } 203 204 public boolean isAvailable() { 205 return hierarchyNav.isAvailable(); 206 } 207 208 } 209 | Popular Tags |