KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > test > olap > TestDrillExpandPosition


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.test.olap;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import com.tonbeller.jpivot.olap.model.Member;
20 import com.tonbeller.jpivot.olap.model.Position;
21 import com.tonbeller.jpivot.olap.model.impl.PositionSpan;
22 import com.tonbeller.jpivot.olap.navi.DrillExpandPosition;
23
24 /**
25  * Created on 03.12.2002
26  *
27  * @author av
28  */

29 public class TestDrillExpandPosition extends TestExtensionSupport implements DrillExpandPosition {
30
31   public boolean canExpand(Position position, Member member) {
32     return canExpand((TestPosition)position, (TestMember)member);
33   }
34
35   public boolean canCollapse(Position position, Member member) {
36     return canCollapse((TestPosition)position, (TestMember)member);
37   }
38
39   public void expand(Position position, Member member) {
40     expand((TestPosition)position, (TestMember)member);
41     fireModelChanged();
42   }
43
44   public void collapse(Position position, Member member) {
45     collapse((TestPosition)position, (TestMember)member);
46     fireModelChanged();
47   }
48
49
50   void expand(TestPosition position, TestMember member) {
51     List JavaDoc childPositions = new ArrayList JavaDoc();
52     TestAxis axis = position.getAxis();
53     int memberIndex = position.indexOf(member);
54
55     PositionSpan ps = new PositionSpan(axis, position, member);
56     List JavaDoc span = axis.getPositions().subList(ps.getStartIndex(), ps.getEndIndex());
57     for (Iterator JavaDoc mi = member.getChildMember().iterator(); mi.hasNext();) {
58       TestMember child = (TestMember) mi.next();
59       for (Iterator JavaDoc pi = span.iterator(); pi.hasNext();) {
60         Member[] members = ((Position)pi.next()).getMembers();
61         members = (Member[])members.clone();
62         TestPosition p = new TestPosition(axis);
63         members[memberIndex] = child;
64         p.setMembers(members);
65         childPositions.add(p);
66       }
67     }
68     axis.getPositions().addAll(ps.getEndIndex(), childPositions);
69   }
70
71
72
73   /**
74    * removes all direct children
75    */

76   void collapse(TestPosition position, TestMember member) {
77     List JavaDoc list = getDescendantPositions(position, member);
78     int memberIndex = position.indexOf(member);
79     for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
80       TestPosition p = (TestPosition)it.next();
81       ((TestMember)p.getMembers()[memberIndex]).setVisible(false);
82     }
83     
84     position.getAxis().getPositions().removeAll(list);
85   }
86
87
88   boolean canExpand(TestPosition position, TestMember member) {
89     if (!member.hasChildren())
90       return false;
91     return getDescendantPositions(position, member).size() == 0;
92   }
93
94
95   boolean canCollapse(TestPosition position, TestMember member) {
96     if (!member.hasChildren())
97       return false;
98     return getDescendantPositions(position, member).size() > 0;
99   }
100   
101   /**
102    * returns next positions, that contain descendants of member
103    */

104   List JavaDoc getDescendantPositions(TestPosition position, TestMember member) {
105     TestAxis axis = position.getAxis();
106     PositionSpan ps = new PositionSpan(axis, position, member);
107     Iterator JavaDoc iter = axis.getPositions().listIterator(ps.getEndIndex());
108     int memberIndex = position.indexOf(member);
109     List JavaDoc descendantPositions = new ArrayList JavaDoc();
110     
111     loop:while (iter.hasNext()) {
112       Position p = (Position)iter.next();
113       TestMember candidate = (TestMember)p.getMembers()[memberIndex];
114       if (candidate.getRootDistance() <= member.getRootDistance())
115         break loop;
116       descendantPositions.add(p);
117     }
118     return descendantPositions;
119     
120   }
121   
122 }
123
Popular Tags