KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > navigation > HtmlPanelNavigation


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.navigation;
17
18 import org.apache.myfaces.component.html.ext.HtmlPanelGroup;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIViewRoot;
25 import javax.faces.context.FacesContext;
26 import javax.faces.el.ValueBinding;
27 import java.io.IOException JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * Panel, that includes navigation items ({@link HtmlCommandNavigation}) and other
33  * components (separators).
34  *
35  * @author Manfred Geiler (latest modification by $Author: matze $)
36  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:50:57 $
37  */

38 public class HtmlPanelNavigation
39         extends HtmlPanelGroup
40 {
41     private static final Log log = LogFactory.getLog(HtmlPanelNavigation.class);
42
43     private static final String JavaDoc PREVIOUS_VIEW_ROOT = HtmlPanelNavigation.class.getName() + ".PREVIOUS_VIEW_ROOT";
44     private boolean _itemOpenActiveStatesRestored = false;
45
46     public void decode(FacesContext context)
47     {
48         super.decode(context); //To change body of overridden methods use File | Settings | File Templates.
49

50         //Save the current view root for later reference...
51
context.getExternalContext().getRequestMap().put(PREVIOUS_VIEW_ROOT, context.getViewRoot());
52         //...and remember that this instance needs NO special treatment on rendering:
53
_itemOpenActiveStatesRestored = true;
54     }
55
56     public void encodeBegin(FacesContext context) throws IOException JavaDoc
57     {
58         if (!_itemOpenActiveStatesRestored && getChildCount() > 0)
59         {
60             UIViewRoot previousRoot = (UIViewRoot)context.getExternalContext().getRequestMap().get(PREVIOUS_VIEW_ROOT);
61             if (previousRoot != null)
62             {
63                 restoreOpenActiveStates(context, previousRoot, getChildren());
64             }
65             else
66             {
67                 //no previous root, means no decode was done
68
//--> a new request
69
}
70         }
71         
72         super.encodeBegin(context); //To change body of overridden methods use File | Settings | File Templates.
73
}
74     
75     public void restoreOpenActiveStates(FacesContext facesContext,
76                                         UIViewRoot previousRoot,
77                                         List JavaDoc children)
78     {
79         for (Iterator JavaDoc it = children.iterator(); it.hasNext(); )
80         {
81             UIComponent child = (UIComponent)it.next();
82             if (child instanceof HtmlCommandNavigation)
83             {
84                 HtmlCommandNavigation previousItem = (HtmlCommandNavigation)previousRoot.findComponent(child.getClientId(facesContext));
85                 if (previousItem != null)
86                 {
87                     ((HtmlCommandNavigation)child).setOpen(previousItem.isOpen());
88                     ((HtmlCommandNavigation)child).setActive(previousItem.isActive());
89                 }
90                 else
91                 {
92                     log.error("Navigation item " + child.getClientId(facesContext) + " not found in previous view.");
93                 }
94                 if (child.getChildCount() > 0)
95                 {
96                     restoreOpenActiveStates(facesContext, previousRoot, child.getChildren());
97                 }
98             }
99         }
100     }
101
102     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
103

104     public static final String JavaDoc COMPONENT_TYPE = "org.apache.myfaces.HtmlPanelNavigation";
105     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Panel";
106     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Navigation";
107
108     private String JavaDoc _itemClass = null;
109     private String JavaDoc _openItemClass = null;
110     private String JavaDoc _activeItemClass = null;
111     private String JavaDoc _separatorClass = null;
112     private String JavaDoc _itemStyle = null;
113     private String JavaDoc _openItemStyle = null;
114     private String JavaDoc _activeItemStyle = null;
115     private String JavaDoc _separatorStyle = null;
116
117     public HtmlPanelNavigation()
118     {
119         setRendererType(DEFAULT_RENDERER_TYPE);
120     }
121
122     public String JavaDoc getFamily()
123     {
124         return COMPONENT_FAMILY;
125     }
126
127     public void setItemClass(String JavaDoc itemClass)
128     {
129         _itemClass = itemClass;
130     }
131
132     public String JavaDoc getItemClass()
133     {
134         if (_itemClass != null) return _itemClass;
135         ValueBinding vb = getValueBinding("itemClass");
136         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
137     }
138
139     public void setOpenItemClass(String JavaDoc openItemClass)
140     {
141         _openItemClass = openItemClass;
142     }
143
144     public String JavaDoc getOpenItemClass()
145     {
146         if (_openItemClass != null) return _openItemClass;
147         ValueBinding vb = getValueBinding("openItemClass");
148         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
149     }
150
151     public void setActiveItemClass(String JavaDoc activeItemClass)
152     {
153         _activeItemClass = activeItemClass;
154     }
155
156     public String JavaDoc getActiveItemClass()
157     {
158         if (_activeItemClass != null) return _activeItemClass;
159         ValueBinding vb = getValueBinding("activeItemClass");
160         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
161     }
162
163     public void setSeparatorClass(String JavaDoc separatorClass)
164     {
165         _separatorClass = separatorClass;
166     }
167
168     public String JavaDoc getSeparatorClass()
169     {
170         if (_separatorClass != null) return _separatorClass;
171         ValueBinding vb = getValueBinding("separatorClass");
172         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
173     }
174
175     public void setItemStyle(String JavaDoc itemStyle)
176     {
177         _itemStyle = itemStyle;
178     }
179
180     public String JavaDoc getItemStyle()
181     {
182         if (_itemStyle != null) return _itemStyle;
183         ValueBinding vb = getValueBinding("itemStyle");
184         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
185     }
186
187     public void setOpenItemStyle(String JavaDoc openItemStyle)
188     {
189         _openItemStyle = openItemStyle;
190     }
191
192     public String JavaDoc getOpenItemStyle()
193     {
194         if (_openItemStyle != null) return _openItemStyle;
195         ValueBinding vb = getValueBinding("openItemStyle");
196         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
197     }
198
199     public void setActiveItemStyle(String JavaDoc activeItemStyle)
200     {
201         _activeItemStyle = activeItemStyle;
202     }
203
204     public String JavaDoc getActiveItemStyle()
205     {
206         if (_activeItemStyle != null) return _activeItemStyle;
207         ValueBinding vb = getValueBinding("activeItemStyle");
208         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
209     }
210
211     public void setSeparatorStyle(String JavaDoc separatorStyle)
212     {
213         _separatorStyle = separatorStyle;
214     }
215
216     public String JavaDoc getSeparatorStyle()
217     {
218         if (_separatorStyle != null) return _separatorStyle;
219         ValueBinding vb = getValueBinding("separatorStyle");
220         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
221     }
222
223
224     public Object JavaDoc saveState(FacesContext context)
225     {
226         Object JavaDoc values[] = new Object JavaDoc[9];
227         values[0] = super.saveState(context);
228         values[1] = _itemClass;
229         values[2] = _openItemClass;
230         values[3] = _activeItemClass;
231         values[4] = _separatorClass;
232         values[5] = _itemStyle;
233         values[6] = _openItemStyle;
234         values[7] = _activeItemStyle;
235         values[8] = _separatorStyle;
236         return ((Object JavaDoc) (values));
237     }
238
239     public void restoreState(FacesContext context, Object JavaDoc state)
240     {
241         Object JavaDoc values[] = (Object JavaDoc[])state;
242         super.restoreState(context, values[0]);
243         _itemClass = (String JavaDoc)values[1];
244         _openItemClass = (String JavaDoc)values[2];
245         _activeItemClass = (String JavaDoc)values[3];
246         _separatorClass = (String JavaDoc)values[4];
247         _itemStyle = (String JavaDoc)values[5];
248         _openItemStyle = (String JavaDoc)values[6];
249         _activeItemStyle = (String JavaDoc)values[7];
250         _separatorStyle = (String JavaDoc)values[8];
251     }
252     //------------------ GENERATED CODE END ---------------------------------------
253
}
254
Popular Tags