KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.apache.log4j.Logger;
21
22 import com.tonbeller.jpivot.olap.model.Dimension;
23 import com.tonbeller.jpivot.olap.model.Hierarchy;
24 import com.tonbeller.jpivot.olap.model.Member;
25 import com.tonbeller.jpivot.olap.model.OlapUtils;
26 import com.tonbeller.jpivot.olap.navi.ChangeSlicer;
27 import com.tonbeller.jpivot.olap.navi.PlaceMembersOnAxes;
28 import com.tonbeller.wcf.catedit.Item;
29 import com.tonbeller.wcf.controller.RequestContext;
30 import com.tonbeller.wcf.controller.RequestListener;
31 import com.tonbeller.wcf.utils.DomUtils;
32
33 /**
34  * Wraps a hierarchy for HierarchyNavigator.
35  * Contains two selections, one multiple selection for axis view and
36  * another one, single selection for slicer view. If the item is moved
37  * around between axis and slicer category it does not forget its
38  * selection.
39  *
40  * @author av
41  */

42 public class HierarchyItem implements Item, RequestListener, Comparable JavaDoc {
43   
44   private static final Logger logger = Logger.getLogger(HierarchyItem.class);
45   
46   // the GUI component
47
private HierarchyNavigator navigator;
48   
49   // one of rows, columns or slicer
50
private AbstractCategory category;
51   
52   // the hierarchy this item is representing
53
private Hierarchy hierarchy;
54
55   // the selection in case this is contained in axis category
56
private List JavaDoc axisSelection;
57   private boolean axisSelectionDirty;
58
59   // the selection in case this is contained in slicer category
60
private List JavaDoc slicerSelection;
61   private boolean slicerSelectionDirty;
62
63   // list of (calculated) members that are about to be deleted in this hierarchy
64
private List JavaDoc deleted = new ArrayList JavaDoc();
65
66   // some expression to be placed on the axis instead of a hierarchy or selection
67
private Object JavaDoc expression;
68
69   private String JavaDoc id = DomUtils.randomId();
70
71   private Dimension dimension;
72
73   public String JavaDoc getId() {
74     return id;
75   }
76
77   public HierarchyItem(AbstractCategory category, Hierarchy hierarchy) {
78     this.category = category;
79     this.hierarchy = hierarchy;
80     this.dimension = hierarchy.getDimension();
81     this.navigator = category.getNavigator();
82     navigator.getTempDispatcher().addRequestListener(id, null, this);
83   }
84
85   void initializeSlicerSelection() {
86     ChangeSlicer slicerExtension = navigator.getSlicerExtension();
87     slicerSelection = new ArrayList JavaDoc();
88     if (slicerExtension != null) {
89       Member[] members = slicerExtension.getSlicer();
90       loop : for (int i = 0; i < members.length; i++) {
91         if (members[i].getLevel().getHierarchy().equals(hierarchy)) {
92           slicerSelection.add(members[i]);
93           break loop;
94         }
95       }
96     }
97   }
98
99   void initializeAxisSelection() {
100     PlaceMembersOnAxes memberExtension = navigator.getMemberExtension();
101     axisSelection = new ArrayList JavaDoc();
102     if (memberExtension != null) {
103       List JavaDoc members = memberExtension.findVisibleMembers(hierarchy);
104       axisSelection.addAll(members);
105     }
106   }
107   
108   private void clear() {
109     axisSelection = null;
110     slicerSelection = null;
111     deleted.clear();
112     expression = null;
113   }
114
115   /**
116    * this item has been moved from one category to another. e.g., this hierarchy has been moved
117    * from rows to filters.
118    */

119   public void setCategory(AbstractCategory category) {
120     this.category = category;
121   }
122
123   public AbstractCategory getCategory() {
124     return category;
125   }
126
127   /**
128    * Returns the current hierarchy.
129    * @return Hierarchy
130    */

131   public Hierarchy getHierarchy() {
132     return hierarchy;
133   }
134   
135   /**
136    * returns the Dimension of this HierarchyItem
137    */

138   public Dimension getDimension() {
139     return dimension;
140   }
141
142   public String JavaDoc getLabel() {
143     return hierarchy.getLabel();
144   }
145
146   /**
147    * called when the user clicks on this item.
148    */

149   public void request(RequestContext context) throws Exception JavaDoc {
150     category.itemClicked(context, this);
151   }
152
153   /**
154    * Returns the axisSelection.
155    * @return Set
156    */

157   public List JavaDoc getAxisSelection() {
158     if (axisSelection == null)
159       initializeAxisSelection();
160     return axisSelection;
161   }
162
163   /**
164    * Returns the slicerSelection.
165    * @return Set
166    */

167   public List JavaDoc getSlicerSelection() {
168     if (slicerSelection == null)
169       initializeSlicerSelection();
170     return slicerSelection;
171   }
172
173   /**
174    * Sets the axisSelection.
175    * @param axisSelection The axisSelection to set
176    */

177   public void setAxisSelection(Collection JavaDoc selection) {
178     if (selection.equals(axisSelection)) {
179        // Nothing has changed, just return
180
return;
181     }
182     clear();
183     updateHierarchy(selection);
184     if (axisSelection == null)
185       axisSelection = new ArrayList JavaDoc();
186     else
187       axisSelection.clear();
188     axisSelection.addAll(selection);
189     axisSelectionDirty = true;
190     category.setDirty(true);
191     expression = null;
192   }
193
194   /**
195    * Sets the slicerSelection.
196    * @param slicerSelection The slicerSelection to set
197    */

198   public void setSlicerSelection(Collection JavaDoc selection) {
199     clear();
200     updateHierarchy(selection);
201     if (slicerSelection == null)
202       slicerSelection = new ArrayList JavaDoc();
203     else
204       slicerSelection.clear();
205     slicerSelection.addAll(selection);
206     slicerSelectionDirty = true;
207     category.setDirty(true);
208     expression = null;
209   }
210
211   private void updateHierarchy(Collection JavaDoc selection) {
212     if (selection == null || selection.isEmpty())
213       hierarchy = dimension.getHierarchies()[0];
214     else {
215       Member m = (Member) selection.iterator().next();
216       hierarchy = m.getLevel().getHierarchy();
217       if (!hierarchy.getDimension().equals(dimension))
218         logger.error("invalid dimension in " + hierarchy.getLabel());
219     }
220     
221   }
222
223   /**
224    * Returns the axisSelectionDirty.
225    * @return boolean
226    */

227   public boolean isAxisSelectionDirty() {
228     return axisSelectionDirty;
229   }
230
231   /**
232    * Returns the slicerSelectionDirty.
233    * @return boolean
234    */

235   public boolean isSlicerSelectionDirty() {
236     return slicerSelectionDirty;
237   }
238
239   /**
240    * Sets the axisSelectionDirty.
241    * @param axisSelectionDirty The axisSelectionDirty to set
242    */

243   public void setAxisSelectionDirty(boolean axisSelectionDirty) {
244     this.axisSelectionDirty = axisSelectionDirty;
245   }
246
247   /**
248    * Sets the slicerSelectionDirty.
249    * @param slicerSelectionDirty The slicerSelectionDirty to set
250    */

251   public void setSlicerSelectionDirty(boolean slicerSelectionDirty) {
252     this.slicerSelectionDirty = slicerSelectionDirty;
253   }
254
255   public void setSelection(Collection JavaDoc selection) {
256     category.setSelection(this, selection);
257   }
258   /**
259    * validates the selection.
260    * @param selection
261    * @return null on success, error message on error
262    */

263   public String JavaDoc validateSelection(Collection JavaDoc selection) {
264     return category.validateSelection(this, selection);
265   }
266
267   /**
268    * lexical compare for GUI lists
269    */

270   public int compareTo(Object JavaDoc arg) {
271     HierarchyItem that = (HierarchyItem) arg;
272     return this.hierarchy.getLabel().compareTo(that.getHierarchy().getLabel());
273   }
274
275   /**
276    * removes deleted members from the selections.
277    */

278   public void removeFromSelection(Set JavaDoc deleted) {
279     if (axisSelection != null)
280       axisSelection.removeAll(deleted);
281     if (slicerSelection != null)
282       slicerSelection.removeAll(deleted);
283   }
284
285   /**
286    * the collection of members that shall be deleted from the query.
287    * (i.e. remove calculated members)
288    */

289   public Collection JavaDoc getDeleted() {
290     return deleted;
291   }
292
293   /**
294    * the collection of members that shall be deleted from the query.
295    * (i.e. remove calculated members)
296    */

297   public void setDeleted(Collection JavaDoc c) {
298     deleted.clear();
299     deleted.addAll(c);
300   }
301
302   public Object JavaDoc getExpression() {
303     return expression;
304   }
305
306   public void setExpression(Object JavaDoc object) {
307     clear();
308     expression = object;
309     category.setDirty(true);
310   }
311
312   public boolean isMovable() {
313     return !OlapUtils.isSingleRecord(hierarchy);
314   }
315   
316   public boolean isClickable() {
317     return navigator.getHierarchyItemClickHandler() != null;
318   }
319
320 }
321
Popular Tags