1 13 package com.tonbeller.jpivot.navigator.member; 14 15 import java.util.Locale ; 16 17 import org.apache.log4j.Logger; 18 19 import com.tonbeller.jpivot.olap.model.Hierarchy; 20 import com.tonbeller.jpivot.olap.model.Level; 21 import com.tonbeller.jpivot.olap.model.Member; 22 import com.tonbeller.jpivot.olap.navi.MemberTree; 23 import com.tonbeller.wcf.tree.AbstractTreeModel; 24 import com.tonbeller.wcf.tree.TreeModel; 25 26 31 public class TreeModelAdapter extends AbstractTreeModel implements TreeModel { 32 33 Hierarchy[] hiers; 34 MemberTree tree; 35 Level noChildrenLevel = null; 36 Locale locale; 37 boolean showSingleHierarchyNode = false; 38 39 interface OverflowListener { 40 void overflowOccured(); 41 } 42 OverflowListener overflowListener; 43 44 private static Logger logger = Logger.getLogger(TreeModelAdapter.class); 45 46 50 public TreeModelAdapter(Hierarchy hier, MemberTree tree, Locale locale) { 51 this.hiers = new Hierarchy[] { hier}; 52 this.tree = tree; 53 this.locale = locale; 54 } 55 56 59 public TreeModelAdapter(Hierarchy[] hiers, MemberTree tree, Locale locale) { 60 this.hiers = hiers; 61 this.tree = tree; 62 this.locale = locale; 63 } 64 65 69 public Object [] getRoots() { 70 try { 71 if (showSingleHierarchyNode || hiers.length > 1) 72 return hiers; 73 return tree.getRootMembers(hiers[0]); 74 } catch (MemberTree.TooManyMembersException e) { 75 overflowOccured(e); 76 return new Member[0]; 77 } 78 } 79 80 public boolean hasChildren(Object node) { 81 if (node instanceof Hierarchy) 82 return true; 83 if (noChildrenLevel != null) { 84 Member m = (Member) node; 85 if (noChildrenLevel.equals(m.getLevel())) 86 return false; 87 } 88 return tree.hasChildren((Member) node); 89 } 90 91 public Object [] getChildren(Object node) { 92 try { 93 if (node instanceof Hierarchy) 94 return tree.getRootMembers((Hierarchy) node); 95 Member[] children = (Member[]) tree.getChildren((Member) node); 96 if (children == null) 97 return new Member[0]; 98 return children; 99 } catch (MemberTree.TooManyMembersException e) { 100 overflowOccured(e); 101 return new Member[0]; 102 } 103 } 104 105 108 public Object getParent(Object node) { 109 if (showSingleHierarchyNode || hiers.length > 1) { 110 if (node instanceof Hierarchy) 111 return null; 112 Object parent = tree.getParent((Member) node); 113 if (parent == null) 114 return ((Member) node).getLevel().getHierarchy(); 115 return parent; 116 } 117 return tree.getParent((Member) node); 118 } 119 120 public void fireModelChanged() { 121 super.fireModelChanged(); 122 } 123 124 127 public Level getNoChildrenLevel() { 128 return noChildrenLevel; 129 } 130 131 134 public void setNoChildrenLevel(Level level) { 135 noChildrenLevel = level; 136 } 137 138 public void modelChanged() { 139 super.fireModelChanged(); 140 } 141 142 private void overflowOccured(Exception e) { 143 logger.error(null, e); 144 if (overflowListener != null) 145 overflowListener.overflowOccured(); 146 } 147 148 public OverflowListener getOverflowListener() { 149 return overflowListener; 150 } 151 public void setOverflowListener(OverflowListener overflowListener) { 152 this.overflowListener = overflowListener; 153 } 154 public boolean isShowSingleHierarchyNode() { 155 return showSingleHierarchyNode; 156 } 157 public void setShowSingleHierarchyNode(boolean showSingleHierarchyNode) { 158 this.showSingleHierarchyNode = showSingleHierarchyNode; 159 } 160 } | Popular Tags |