KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > navigator > hierarchy > HierarchyNavigator


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.navigator.hierarchy;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.apache.log4j.Logger;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
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 /**
48  * Navigation dialog
49  *
50  * @author av
51  */

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 JavaDoc {
62       editing = false;
63       // we will recreate everything on the next render()
64
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 JavaDoc {
78       editing = false;
79       boolean valid = validate(context);
80
81       // the following will fire multiple ModelChangeEvents
82
for (Iterator JavaDoc it = categories.iterator(); it.hasNext();)
83         ((AbstractCategory) it.next()).deleteDeleted();
84       for (Iterator JavaDoc it = categories.iterator(); it.hasNext();)
85         ((AbstractCategory) it.next()).prepareApplyChanges();
86       for (Iterator JavaDoc it = categories.iterator(); it.hasNext();)
87         ((AbstractCategory) it.next()).applyChanges();
88
89       if (valid && hide)
90         setVisible(false);
91     }
92   }
93
94   private String JavaDoc acceptButtonId;
95   private String JavaDoc cancelButtonId;
96   private RequestListener acceptHandler;
97   private RequestListener revertHandler;
98   private String JavaDoc okButtonId;
99   private String JavaDoc revertButtonId;
100   private List JavaDoc categories = new ArrayList JavaDoc();
101   private Resources resources;
102
103   /**
104    * after the user has started editing, the CategoryModel is no longer synchronized with the
105    * OlapModel. This means, the user may do one or more changes, and then apply these changes
106    * at once to the OlapModel.
107    */

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   /**
119    * Constructor for HierNavigator.
120    */

121   public HierarchyNavigator(String JavaDoc 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 JavaDoc 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 JavaDoc {
151     super.initialize(context);
152     resources = context.getResources(HierarchyNavigator.class);
153   }
154
155   /**
156    * Returns the hierExtension.
157    * @return PlaceHierarchiesOnAxes
158    */

159   public PlaceHierarchiesOnAxes getHierarchyExtension() {
160     return (PlaceHierarchiesOnAxes) olapModel.getExtension(PlaceHierarchiesOnAxes.ID);
161   }
162
163   /**
164    * Returns the hierarchyItemClickHandler.
165    * @return HierarchyItemClickHandler
166    */

167   public HierarchyItemClickHandler getHierarchyItemClickHandler() {
168     return hierarchyItemClickHandler;
169   }
170
171   /**
172    * Returns the memberExtension.
173    * @return PlaceMembersOnAxes
174    */

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   /**
184    * Returns the olapModel.
185    * @return OlapModel
186    */

187   public OlapModel getOlapModel() {
188     return olapModel;
189   }
190
191   /**
192    * Returns the slicerExtension.
193    * @return ChangeSlicer
194    */

195   public ChangeSlicer getSlicerExtension() {
196     return (ChangeSlicer) olapModel.getExtension(ChangeSlicer.ID);
197   }
198
199   /**
200    * Returns the tempDispatcher.
201    * @return Dispatcher
202    */

203   Dispatcher getTempDispatcher() {
204     return tempDispatcher;
205   }
206
207   /**
208    * initializes the CategoryModel to reflect the OlapModel
209    */

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 JavaDoc name = resources.getString("axis." + index + ".name");
218       String JavaDoc icon = resources.getString("axis." + index + ".icon");
219       AxisCategory axisCat = new AxisCategory(this, axis, name, icon);
220       categories.add(axisCat);
221     }
222
223     // the rest is added to the slicer
224
String JavaDoc name = resources.getString("slicer.name");
225     String JavaDoc icon = resources.getString("slicer.icon");
226     slicerCategory = new SlicerCategory(this, name, icon);
227     categories.add(slicerCategory);
228   }
229
230   /**
231    * true if the user has changed the axis/hierarchy mapping.
232    * @return boolean
233    */

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 JavaDoc render(RequestContext context, Document JavaDoc factory) throws Exception JavaDoc {
245     if (!editing) {
246       tempDispatcher.clear();
247       initializeCategories();
248     }
249
250     Element JavaDoc 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   /**
269    * Sets the hierarchyItemClickHandler.
270    * @param hierarchyItemClickHandler The hierarchyItemClickHandler to set
271    */

272   public void setHierarchyItemClickHandler(HierarchyItemClickHandler hierarchyItemClickHandler) {
273     this.hierarchyItemClickHandler = hierarchyItemClickHandler;
274   }
275
276   public void modelChanged(ModelChangeEvent e) {
277     // recreate everything on next render()
278
editing = false;
279   }
280
281   public void structureChanged(ModelChangeEvent e) {
282     logger.info("cleaning up");
283     // force reload of members, hierarchies etc
284
setEditing(false);
285     tempDispatcher.clear();
286     categories.clear();
287     // invalidate hyperlinks
288
categoryModel.fireModelChanged();
289   }
290
291
292   /**
293    * finds the HierarchyItem for <code>hier</code>
294    * @param hier the Hierarchy
295    * @return null or the HierarchyItem
296    */

297   public HierarchyItem findHierarchyItem(Hierarchy hier) {
298     for (Iterator JavaDoc ci = categoryModel.getCategories().iterator(); ci.hasNext();) {
299       AbstractCategory ac = (AbstractCategory) ci.next();
300       for (Iterator JavaDoc 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   /**
318    * returns the set of dimensions that are currently on the
319    * slicer axis. This includes those dimensions that the
320    * user has moved to the slicer in the navigator but not yet
321    * committed by pressing the OK button.
322    *
323    * @return empty set if this component has not been rendered yet
324    */

325   public Set JavaDoc getSlicerDimensions() {
326     Set JavaDoc set = new HashSet JavaDoc();
327     for (Iterator JavaDoc 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