1 13 package com.tonbeller.jpivot.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 com.tonbeller.jpivot.navigator.hierarchy.HierarchyItem; 25 import com.tonbeller.jpivot.navigator.hierarchy.HierarchyItemClickHandler; 26 import com.tonbeller.jpivot.navigator.hierarchy.HierarchyNavigator; 27 import com.tonbeller.jpivot.navigator.member.MemberNavigator; 28 import com.tonbeller.jpivot.navigator.member.MemberSelectionModel; 29 import com.tonbeller.jpivot.olap.model.Hierarchy; 30 import com.tonbeller.jpivot.olap.model.OlapModel; 31 import com.tonbeller.jpivot.ui.Available; 32 import com.tonbeller.wcf.component.Component; 33 import com.tonbeller.wcf.component.ComponentSupport; 34 import com.tonbeller.wcf.controller.RequestContext; 35 import com.tonbeller.wcf.controller.RequestListener; 36 import com.tonbeller.wcf.tree.NodeSorter; 37 38 44 45 public class Navigator extends ComponentSupport implements ModelChangeListener, Available { 46 OlapModel olapModel; 47 MemberNavigator memberNav; 48 HierarchyNavigator hierarchyNav; 49 HierarchyItem currentItem; 50 Component current; 51 52 public Navigator(String id, Component parent, OlapModel olapModel) { 53 super(id, parent); 54 this.olapModel = olapModel; 55 olapModel.addModelChangeListener(this); 56 57 RequestListener acceptHandler = createMemberNavAcceptHandler(); 58 RequestListener cancelHandler = createMemberNavCancelHandler(); 59 memberNav = new MemberNavigator(id + ".membernav", this, olapModel, acceptHandler, cancelHandler); 60 hierarchyNav = new HierarchyNavigator(id + ".hiernav", this, olapModel); 61 if (memberNav.isAvailable()) 62 hierarchyNav.setHierarchyItemClickHandler(createHierarchyItemClickHandler()); 63 current = hierarchyNav; 64 } 65 66 69 public void initialize(RequestContext context) throws Exception { 70 super.initialize(context); 71 memberNav.initialize(context); 72 hierarchyNav.initialize(context); 73 } 74 75 78 public void destroy(HttpSession session) throws Exception { 79 memberNav.destroy(session); 80 hierarchyNav.destroy(session); 81 super.destroy(session); 82 } 83 84 public Document render(RequestContext context) throws Exception { 85 return current.render(context); 86 } 87 88 91 public void show(Component component) { 92 if (component == null) 93 this.current = hierarchyNav; 94 else 95 this.current = component; 96 } 97 98 101 private class HierarchyItemClickAdapter implements HierarchyItemClickHandler { 102 public void itemClicked( 103 RequestContext context, 104 HierarchyItem item, 105 MemberSelectionModel selection, 106 boolean allowChangeOrder) { 107 108 currentItem = item; 109 Hierarchy[] hiers = item.getDimension().getHierarchies(); 110 memberNav.setHierarchies(hiers, allowChangeOrder, selection, item.getDeleted()); 111 112 show(memberNav); 113 } 114 } 115 116 private class MemberNavAcceptHandler implements RequestListener { 117 public void request(RequestContext context) throws Exception { 118 boolean valid = memberNav.validate(context); 120 121 Set set = memberNav.getSelectionModel().getSelection(); 124 List list = NodeSorter.preOrder(set, memberNav.getModel()); 125 String errmesg = currentItem.validateSelection(list); 126 if (errmesg != null) 127 memberNav.setError(errmesg); 128 else if (valid) { 129 currentItem.setSelection(list); 130 currentItem.setDeleted(memberNav.getDeleteNodeModel().getDeleted()); 131 show(hierarchyNav); 132 } 133 } 134 } 135 136 private class MemberNavCancelHandler implements RequestListener { 137 public void request(RequestContext context) throws Exception { 138 memberNav.revert(context); 139 show(hierarchyNav); 140 } 141 } 142 143 146 protected RequestListener createMemberNavAcceptHandler() { 147 return new MemberNavAcceptHandler(); 148 } 149 150 153 protected HierarchyItemClickHandler createHierarchyItemClickHandler() { 154 return new HierarchyItemClickAdapter(); 155 } 156 157 160 protected RequestListener createMemberNavCancelHandler() { 161 return new MemberNavCancelHandler(); 162 } 163 164 168 public boolean isVisible() { 169 return hierarchyNav.isVisible(); 170 } 171 172 176 public void setVisible(boolean visible) { 177 hierarchyNav.setVisible(visible); 178 } 179 180 public void modelChanged(ModelChangeEvent e) { 181 show(hierarchyNav); 182 } 183 184 public void structureChanged(ModelChangeEvent e) { 185 if (memberNav.isAvailable()) 186 hierarchyNav.setHierarchyItemClickHandler(createHierarchyItemClickHandler()); 187 else 188 hierarchyNav.setHierarchyItemClickHandler(null); 189 show(hierarchyNav); 190 } 191 192 public HierarchyNavigator getHierarchyNav() { 193 return hierarchyNav; 194 } 195 196 public MemberNavigator getMemberNav() { 197 return memberNav; 198 } 199 200 public boolean isAvailable() { 201 return hierarchyNav.isAvailable(); 202 } 203 204 } 205 | Popular Tags |