KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > navigator > member > TreeModelAdapter


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.member;
14
15 import java.util.Locale JavaDoc;
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 /**
27  * maps the MemberTree table extension to a wcf tree model
28  *
29  * @author av
30  */

31 public class TreeModelAdapter extends AbstractTreeModel implements TreeModel {
32
33   Hierarchy[] hiers;
34   MemberTree tree;
35   Level noChildrenLevel = null;
36   Locale JavaDoc 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   /**
47    * Constructor for TreeModelAdapter. Root nodes are the root nodes of the
48    * hierarchy.
49    */

50   public TreeModelAdapter(Hierarchy hier, MemberTree tree, Locale JavaDoc locale) {
51     this.hiers = new Hierarchy[] { hier};
52     this.tree = tree;
53     this.locale = locale;
54   }
55
56   /**
57    * Constructor for TreeModelAdapter. Root nodes are the hierarchies.
58    */

59   public TreeModelAdapter(Hierarchy[] hiers, MemberTree tree, Locale JavaDoc locale) {
60     this.hiers = hiers;
61     this.tree = tree;
62     this.locale = locale;
63   }
64
65   /**
66    * if one hierarchy return its root members. if multiple, return the
67    * hierarchies
68    */

69   public Object JavaDoc[] 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 JavaDoc 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 JavaDoc[] getChildren(Object JavaDoc 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   /**
106    * @see com.tonbeller.wcf.tree.TreeModel#getParent(Object)
107    */

108   public Object JavaDoc getParent(Object JavaDoc node) {
109     if (showSingleHierarchyNode || hiers.length > 1) {
110       if (node instanceof Hierarchy)
111         return null;
112       Object JavaDoc 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   /**
125    * members from this level will pretend to have no children
126    */

127   public Level getNoChildrenLevel() {
128     return noChildrenLevel;
129   }
130
131   /**
132    * members from this level will pretend to have no children
133    */

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 JavaDoc 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