1 13 package com.tonbeller.jpivot.navigator.hierarchy; 14 15 import java.util.ArrayList ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 import org.apache.log4j.Logger; 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.Element ; 24 25 import com.tonbeller.jpivot.core.ModelChangeEvent; 26 import com.tonbeller.jpivot.core.ModelChangeListener; 27 import com.tonbeller.jpivot.navigator.member.MemberSelectionModel; 28 import com.tonbeller.jpivot.olap.model.Axis; 29 import com.tonbeller.jpivot.olap.model.Hierarchy; 30 import com.tonbeller.jpivot.olap.model.OlapException; 31 import com.tonbeller.jpivot.olap.model.OlapModel; 32 import com.tonbeller.jpivot.olap.model.Result; 33 import com.tonbeller.jpivot.olap.navi.ChangeSlicer; 34 import com.tonbeller.jpivot.olap.navi.MemberDeleter; 35 import com.tonbeller.jpivot.olap.navi.PlaceHierarchiesOnAxes; 36 import com.tonbeller.jpivot.olap.navi.PlaceMembersOnAxes; 37 import com.tonbeller.jpivot.ui.Available; 38 import com.tonbeller.tbutils.res.Resources; 39 import com.tonbeller.wcf.catedit.CategoryEditor; 40 import com.tonbeller.wcf.catedit.CategoryModelSupport; 41 import com.tonbeller.wcf.component.Component; 42 import com.tonbeller.wcf.controller.Dispatcher; 43 import com.tonbeller.wcf.controller.DispatcherSupport; 44 import com.tonbeller.wcf.controller.RequestContext; 45 import com.tonbeller.wcf.controller.RequestListener; 46 47 52 public class HierarchyNavigator extends CategoryEditor implements ModelChangeListener, Available { 53 54 public class CancelHandler implements RequestListener { 55 private boolean hide; 56 57 public CancelHandler(boolean hide) { 58 this.hide = hide; 59 } 60 61 public void request(RequestContext context) throws Exception { 62 editing = false; 63 revert(context); 65 if (hide) 66 setVisible(false); 67 } 68 } 69 70 public class OkHandler implements RequestListener { 71 private boolean hide; 72 73 public OkHandler(boolean hide) { 74 this.hide = hide; 75 } 76 77 public void request(RequestContext context) throws Exception { 78 editing = false; 79 boolean valid = validate(context); 80 81 for (Iterator it = categories.iterator(); it.hasNext();) 83 ((AbstractCategory) it.next()).deleteDeleted(); 84 for (Iterator it = categories.iterator(); it.hasNext();) 85 ((AbstractCategory) it.next()).prepareApplyChanges(); 86 for (Iterator it = categories.iterator(); it.hasNext();) 87 ((AbstractCategory) it.next()).applyChanges(); 88 89 if (valid && hide) 90 setVisible(false); 91 } 92 } 93 94 private String acceptButtonId; 95 private String cancelButtonId; 96 private RequestListener acceptHandler; 97 private RequestListener revertHandler; 98 private String okButtonId; 99 private String revertButtonId; 100 private List categories = new ArrayList (); 101 private Resources resources; 102 103 108 private boolean editing = false; 109 110 private HierarchyItemClickHandler hierarchyItemClickHandler; 111 private OlapModel olapModel; 112 private CategoryModelSupport categoryModel; 113 private Dispatcher tempDispatcher = new DispatcherSupport(); 114 private SlicerCategory slicerCategory; 115 116 private static Logger logger = Logger.getLogger(HierarchyNavigator.class); 117 118 121 public HierarchyNavigator(String id, Component parent, OlapModel olapModel) { 122 super(id, parent); 123 124 logger.info("creating instance: " + this); 125 126 acceptButtonId = id + ".accept"; 127 cancelButtonId = id + ".cancel"; 128 okButtonId = id + ".ok"; 129 revertButtonId = id + ".revert"; 130 131 this.olapModel = olapModel; 132 olapModel.addModelChangeListener(this); 133 134 acceptHandler = new OkHandler(false); 135 revertHandler = new CancelHandler(false); 136 super.getDispatcher().addRequestListener(acceptButtonId, null, acceptHandler); 137 super.getDispatcher().addRequestListener(revertButtonId, null, revertHandler); 138 super.getDispatcher().addRequestListener(okButtonId, null, new OkHandler(true)); 139 super.getDispatcher().addRequestListener(cancelButtonId, null, new CancelHandler(true)); 140 super.getDispatcher().addRequestListener(null, null, tempDispatcher); 141 categoryModel = new CategoryModelSupport() { 142 public List getCategories() { 143 return categories; 144 } 145 }; 146 super.setModel(categoryModel); 147 super.setItemRenderer(new HierarchyItemRenderer()); 148 } 149 150 public void initialize(RequestContext context) throws Exception { 151 super.initialize(context); 152 resources = context.getResources(HierarchyNavigator.class); 153 } 154 155 159 public PlaceHierarchiesOnAxes getHierarchyExtension() { 160 return (PlaceHierarchiesOnAxes) olapModel.getExtension(PlaceHierarchiesOnAxes.ID); 161 } 162 163 167 public HierarchyItemClickHandler getHierarchyItemClickHandler() { 168 return hierarchyItemClickHandler; 169 } 170 171 175 public PlaceMembersOnAxes getMemberExtension() { 176 return (PlaceMembersOnAxes) olapModel.getExtension(PlaceMembersOnAxes.ID); 177 } 178 179 public MemberDeleter getDeleterExtension() { 180 return (MemberDeleter) olapModel.getExtension(MemberDeleter.ID); 181 } 182 183 187 public OlapModel getOlapModel() { 188 return olapModel; 189 } 190 191 195 public ChangeSlicer getSlicerExtension() { 196 return (ChangeSlicer) olapModel.getExtension(ChangeSlicer.ID); 197 } 198 199 203 Dispatcher getTempDispatcher() { 204 return tempDispatcher; 205 } 206 207 210 void initializeCategories() throws OlapException { 211 categories.clear(); 212 213 Result result = olapModel.getResult(); 214 Axis[] axes = result.getAxes(); 215 for (int index = 0; index < axes.length; index++) { 216 Axis axis = axes[index]; 217 String name = resources.getString("axis." + index + ".name"); 218 String icon = resources.getString("axis." + index + ".icon"); 219 AxisCategory axisCat = new AxisCategory(this, axis, name, icon); 220 categories.add(axisCat); 221 } 222 223 String name = resources.getString("slicer.name"); 225 String icon = resources.getString("slicer.icon"); 226 slicerCategory = new SlicerCategory(this, name, icon); 227 categories.add(slicerCategory); 228 } 229 230 234 public boolean isEditing() { 235 return editing; 236 } 237 238 void itemClicked(RequestContext context, HierarchyItem item, MemberSelectionModel selection, 239 boolean allowChangeOrder) { 240 if (hierarchyItemClickHandler != null) 241 hierarchyItemClickHandler.itemClicked(context, item, selection, allowChangeOrder); 242 } 243 244 public Element render(RequestContext context, Document factory) throws Exception { 245 if (!editing) { 246 tempDispatcher.clear(); 247 initializeCategories(); 248 } 249 250 Element elem = super.render(context, factory); 251 252 elem.setAttribute("accept-id", acceptButtonId); 253 elem.setAttribute("accept-title", resources.getString("accept.title")); 254 elem.setAttribute("revert-id", revertButtonId); 255 elem.setAttribute("revert-title", resources.getString("revert.title")); 256 elem.setAttribute("ok-id", okButtonId); 257 elem.setAttribute("ok-title", resources.getString("ok.title")); 258 elem.setAttribute("cancel-id", cancelButtonId); 259 elem.setAttribute("cancel-title", resources.getString("cancel.title")); 260 261 return elem; 262 } 263 264 public void setEditing(boolean editing) { 265 this.editing = editing; 266 } 267 268 272 public void setHierarchyItemClickHandler(HierarchyItemClickHandler hierarchyItemClickHandler) { 273 this.hierarchyItemClickHandler = hierarchyItemClickHandler; 274 } 275 276 public void modelChanged(ModelChangeEvent e) { 277 editing = false; 279 } 280 281 public void structureChanged(ModelChangeEvent e) { 282 logger.info("cleaning up"); 283 setEditing(false); 285 tempDispatcher.clear(); 286 categories.clear(); 287 categoryModel.fireModelChanged(); 289 } 290 291 292 297 public HierarchyItem findHierarchyItem(Hierarchy hier) { 298 for (Iterator ci = categoryModel.getCategories().iterator(); ci.hasNext();) { 299 AbstractCategory ac = (AbstractCategory) ci.next(); 300 for (Iterator ii = ac.getItems().iterator(); ii.hasNext();) { 301 HierarchyItem hi = (HierarchyItem) ii.next(); 302 if (hi.getHierarchy().equals(hier)) 303 return hi; 304 } 305 } 306 return null; 307 } 308 309 public RequestListener getAcceptHandler() { 310 return acceptHandler; 311 } 312 313 public RequestListener getRevertHandler() { 314 return revertHandler; 315 } 316 317 325 public Set getSlicerDimensions() { 326 Set set = new HashSet (); 327 for (Iterator it = slicerCategory.getItems().iterator(); it.hasNext();) { 328 HierarchyItem hi = (HierarchyItem) it.next(); 329 set.add(hi.getHierarchy().getDimension()); 330 } 331 return set; 332 } 333 334 public Resources getRes() { 335 return resources; 336 } 337 338 public boolean isAvailable() { 339 return getHierarchyExtension() != null; 340 } 341 342 } | Popular Tags |