KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > xmla > 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 org.openi.xmla.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 org.openi.xmla.navigator.hierarchy.*;
25 import com.tonbeller.jpivot.navigator.member.MemberNavigator;
26 import com.tonbeller.jpivot.olap.model.Hierarchy;
27 import com.tonbeller.jpivot.olap.model.OlapModel;
28 import com.tonbeller.jpivot.ui.Available;
29 import com.tonbeller.wcf.component.Component;
30 import com.tonbeller.wcf.component.ComponentSupport;
31 import com.tonbeller.wcf.controller.RequestContext;
32 import com.tonbeller.wcf.controller.RequestListener;
33 import com.tonbeller.wcf.selection.SelectionModel;
34 import com.tonbeller.wcf.tree.NodeSorter;
35
36 /**
37  * Root Component, contains MemberNavigator and HierarchyNavigator.
38  * Coordinates the activities between MemberNavigator and HierarchyNavigator (GOF Director pattern).
39  *
40  * @author av
41  */

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

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

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

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

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

150   protected RequestListener createMemberNavAcceptHandler() {
151     return new MemberNavAcceptHandler();
152   }
153
154   /**
155    * factory method that allows derived classes to install their own handlers
156    */

157   protected HierarchyItemClickHandler createHierarchyItemClickHandler() {
158     return new HierarchyItemClickAdapter();
159   }
160
161   /**
162    * factory method that allows derived classes to install their own handlers
163    */

164   protected RequestListener createMemberNavCancelHandler() {
165     return new MemberNavCancelHandler();
166   }
167
168   /**
169    * Returns the visible.
170    * @return boolean
171    */

172   public boolean isVisible() {
173     return hierarchyNav.isVisible();
174   }
175
176   /**
177    * Sets the visible.
178    * @param visible The visible to set
179    */

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