1 13 package com.tonbeller.jpivot.test.olap; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 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 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 childPositions = new ArrayList (); 52 TestAxis axis = position.getAxis(); 53 int memberIndex = position.indexOf(member); 54 55 PositionSpan ps = new PositionSpan(axis, position, member); 56 List span = axis.getPositions().subList(ps.getStartIndex(), ps.getEndIndex()); 57 for (Iterator mi = member.getChildMember().iterator(); mi.hasNext();) { 58 TestMember child = (TestMember) mi.next(); 59 for (Iterator 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 76 void collapse(TestPosition position, TestMember member) { 77 List list = getDescendantPositions(position, member); 78 int memberIndex = position.indexOf(member); 79 for (Iterator 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 104 List getDescendantPositions(TestPosition position, TestMember member) { 105 TestAxis axis = position.getAxis(); 106 PositionSpan ps = new PositionSpan(axis, position, member); 107 Iterator iter = axis.getPositions().listIterator(ps.getEndIndex()); 108 int memberIndex = position.indexOf(member); 109 List descendantPositions = new ArrayList (); 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 |