KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > navigator > Navigator


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;
14
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import javax.servlet.http.HttpSession JavaDoc;
19
20 import org.w3c.dom.Document JavaDoc;
21
22 import com.tonbeller.jpivot.core.ModelChangeEvent;
23 import com.tonbeller.jpivot.core.ModelChangeListener;
24 import com.tonbeller.jpivot.navigator.hierarchy.HierarchyItem;
25 import com.tonbeller.jpivot.navigator.hierarchy.HierarchyItemClickHandler;
26 import com.tonbeller.jpivot.navigator.hierarchy.HierarchyNavigator;
27 import com.tonbeller.jpivot.navigator.member.MemberNavigator;
28 import com.tonbeller.jpivot.navigator.member.MemberSelectionModel;
29 import com.tonbeller.jpivot.olap.model.Hierarchy;
30 import com.tonbeller.jpivot.olap.model.OlapModel;
31 import com.tonbeller.jpivot.ui.Available;
32 import com.tonbeller.wcf.component.Component;
33 import com.tonbeller.wcf.component.ComponentSupport;
34 import com.tonbeller.wcf.controller.RequestContext;
35 import com.tonbeller.wcf.controller.RequestListener;
36 import com.tonbeller.wcf.tree.NodeSorter;
37
38 /**
39  * Root Component, contains MemberNavigator and HierarchyNavigator.
40  * Coordinates the activities between MemberNavigator and HierarchyNavigator (GOF Director pattern).
41  *
42  * @author av
43  */

44
45 public class Navigator extends ComponentSupport implements ModelChangeListener, Available {
46   OlapModel olapModel;
47   MemberNavigator memberNav;
48   HierarchyNavigator hierarchyNav;
49   HierarchyItem currentItem;
50   Component current;
51
52   public Navigator(String JavaDoc id, Component parent, OlapModel olapModel) {
53     super(id, parent);
54     this.olapModel = olapModel;
55     olapModel.addModelChangeListener(this);
56
57     RequestListener acceptHandler = createMemberNavAcceptHandler();
58     RequestListener cancelHandler = createMemberNavCancelHandler();
59     memberNav = new MemberNavigator(id + ".membernav", this, olapModel, acceptHandler, cancelHandler);
60     hierarchyNav = new HierarchyNavigator(id + ".hiernav", this, olapModel);
61     if (memberNav.isAvailable())
62       hierarchyNav.setHierarchyItemClickHandler(createHierarchyItemClickHandler());
63     current = hierarchyNav;
64   }
65
66   /**
67    * lifecycle
68    */

69   public void initialize(RequestContext context) throws Exception JavaDoc {
70     super.initialize(context);
71     memberNav.initialize(context);
72     hierarchyNav.initialize(context);
73   }
74
75   /**
76    * lifecycle
77    */

78   public void destroy(HttpSession JavaDoc session) throws Exception JavaDoc {
79     memberNav.destroy(session);
80     hierarchyNav.destroy(session);
81     super.destroy(session);
82   }
83
84   public Document JavaDoc render(RequestContext context) throws Exception JavaDoc {
85     return current.render(context);
86   }
87
88   /**
89    * sets component to visible
90    */

91   public void show(Component component) {
92     if (component == null)
93       this.current = hierarchyNav;
94     else
95       this.current = component;
96   }
97
98   /**
99    * invoked when the user clicks on a Hierarchy in the HierarchyNavigator
100    */

101   private class HierarchyItemClickAdapter implements HierarchyItemClickHandler {
102     public void itemClicked(
103       RequestContext context,
104       HierarchyItem item,
105       MemberSelectionModel selection,
106       boolean allowChangeOrder) {
107       
108       currentItem = item;
109       Hierarchy[] hiers = item.getDimension().getHierarchies();
110       memberNav.setHierarchies(hiers, allowChangeOrder, selection, item.getDeleted());
111       
112       show(memberNav);
113     }
114   }
115
116   private class MemberNavAcceptHandler implements RequestListener {
117     public void request(RequestContext context) throws Exception JavaDoc {
118       // read user selection (checkboxes)
119
boolean valid = memberNav.validate(context);
120
121       // the selection is an unsorted set.
122
// Order the selected items into tree order.
123
Set JavaDoc set = memberNav.getSelectionModel().getSelection();
124       List JavaDoc list = NodeSorter.preOrder(set, memberNav.getModel());
125       String JavaDoc errmesg = currentItem.validateSelection(list);
126       if (errmesg != null)
127         memberNav.setError(errmesg);
128       else if (valid) {
129         currentItem.setSelection(list);
130         currentItem.setDeleted(memberNav.getDeleteNodeModel().getDeleted());
131         show(hierarchyNav);
132       }
133     }
134   }
135
136   private class MemberNavCancelHandler implements RequestListener {
137     public void request(RequestContext context) throws Exception JavaDoc {
138       memberNav.revert(context);
139       show(hierarchyNav);
140     }
141   }
142
143   /**
144    * factory method that allows derived classes to install their own handlers
145    */

146   protected RequestListener createMemberNavAcceptHandler() {
147     return new MemberNavAcceptHandler();
148   }
149
150   /**
151    * factory method that allows derived classes to install their own handlers
152    */

153   protected HierarchyItemClickHandler createHierarchyItemClickHandler() {
154     return new HierarchyItemClickAdapter();
155   }
156
157   /**
158    * factory method that allows derived classes to install their own handlers
159    */

160   protected RequestListener createMemberNavCancelHandler() {
161     return new MemberNavCancelHandler();
162   }
163
164   /**
165    * Returns the visible.
166    * @return boolean
167    */

168   public boolean isVisible() {
169     return hierarchyNav.isVisible();
170   }
171
172   /**
173    * Sets the visible.
174    * @param visible The visible to set
175    */

176   public void setVisible(boolean visible) {
177     hierarchyNav.setVisible(visible);
178   }
179
180   public void modelChanged(ModelChangeEvent e) {
181     show(hierarchyNav);
182   }
183
184   public void structureChanged(ModelChangeEvent e) {
185     if (memberNav.isAvailable())
186       hierarchyNav.setHierarchyItemClickHandler(createHierarchyItemClickHandler());
187     else
188       hierarchyNav.setHierarchyItemClickHandler(null);
189     show(hierarchyNav);
190   }
191
192   public HierarchyNavigator getHierarchyNav() {
193     return hierarchyNav;
194   }
195
196   public MemberNavigator getMemberNav() {
197     return memberNav;
198   }
199   
200   public boolean isAvailable() {
201     return hierarchyNav.isAvailable();
202   }
203
204 }
205
Popular Tags