KickJava   Java API By Example, From Geeks To Geeks.

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


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.HtmlCommandLink;
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.context.FacesContext;
25 import javax.faces.event.AbortProcessingException;
26 import javax.faces.event.ActionEvent;
27 import javax.faces.event.FacesEvent;
28 import javax.faces.event.PhaseId;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * Command, that represents a navigation item.
34  *
35  * @author Manfred Geiler (latest modification by $Author: tomsp $)
36  * @version $Revision: 1.5 $ $Date: 2005/01/09 21:56:33 $
37  */

38 public class HtmlCommandNavigation
39         extends HtmlCommandLink
40 {
41     private static final Log log = LogFactory.getLog(HtmlCommandNavigation.class);
42
43     private boolean _open = false;
44     private boolean _active = false;
45
46     public boolean isImmediate()
47     {
48         //always immediate
49
return true;
50     }
51
52     public void setImmediate(boolean immediate)
53     {
54         if (log.isWarnEnabled()) log.warn("Immediate property of HtmlCommandNavigation cannot be set --> ignored.");
55     }
56
57     public boolean isOpen()
58     {
59         return _open;
60     }
61
62     public void setOpen(boolean open)
63     {
64         _open = open;
65     }
66
67     public boolean isActive()
68     {
69         return _active;
70     }
71
72     public void setActive(boolean active)
73     {
74         _active = active;
75     }
76
77     /**
78      * @return false, if this item is child of another HtmlCommandNavigation, which is closed
79      */

80     public boolean isRendered()
81     {
82         if (! super.isRendered()) {
83             return false;
84         }
85         UIComponent parent = getParent();
86         while (parent != null)
87         {
88             if (parent instanceof HtmlCommandNavigation)
89             {
90                 if (!((HtmlCommandNavigation)parent).isOpen())
91                 {
92                     return false;
93                 }
94             }
95
96             if (parent instanceof HtmlPanelNavigation)
97             {
98                 break;
99             }
100             else
101             {
102                 parent = parent.getParent();
103             }
104         }
105
106         return true;
107     }
108
109
110     public void toggleOpen()
111     {
112         if (isOpen())
113         {
114             if (getChildCount() > 0)
115             {
116                 //item is a menu group --> close item
117
setOpen(false);
118             }
119         }
120         else
121         {
122             UIComponent parent = getParent();
123
124             //close all siblings
125
closeAllChildren(parent.getChildren().iterator());
126
127             //open all parents (to be sure) and search HtmlPanelNavigation
128
UIComponent p = parent;
129             while (p != null && !(p instanceof HtmlPanelNavigation))
130             {
131                 if (p instanceof HtmlCommandNavigation)
132                 {
133                     ((HtmlCommandNavigation)p).setOpen(true);
134                 }
135                 p = p.getParent();
136             }
137             // p is now the HtmlPanelNavigation
138

139             if (!hasCommandNavigationChildren())
140             {
141                 //item is an end node --> deactivate all other nodes, and then...
142
if (!(p instanceof HtmlPanelNavigation))
143                 {
144                     log.error("HtmlCommandNavigation without parent HtmlPanelNavigation ?!");
145                 }
146                 else
147                 {
148                     //deactivate all other items
149
deactivateAllChildren(p.getChildren().iterator());
150                 }
151                 //...activate this item
152
setActive(true);
153             }
154             else
155             {
156                 //open item
157
setOpen(true);
158             }
159         }
160     }
161
162     private boolean hasCommandNavigationChildren()
163     {
164         if (getChildCount() == 0)
165         {
166             return false;
167         }
168         List JavaDoc list = getChildren();
169         for (int i = 0, sizei = list.size(); i < sizei; i++)
170         {
171             if (list.get(i) instanceof HtmlCommandNavigation)
172             {
173                 return true;
174             }
175         }
176         return false;
177     }
178
179
180     private static void deactivateAllChildren(Iterator JavaDoc children)
181     {
182         while (children.hasNext())
183         {
184             UIComponent ni = (UIComponent)children.next();
185             if (ni instanceof HtmlCommandNavigation)
186             {
187                 ((HtmlCommandNavigation)ni).setActive(false);
188                 if (ni.getChildCount() > 0)
189                 {
190                     deactivateAllChildren(ni.getChildren().iterator());
191                 }
192             }
193         }
194     }
195
196     private static void closeAllChildren(Iterator JavaDoc children)
197     {
198         while (children.hasNext())
199         {
200             UIComponent ni = (UIComponent)children.next();
201             if (ni instanceof HtmlCommandNavigation)
202             {
203                 ((HtmlCommandNavigation)ni).setOpen(false);
204                 if (ni.getChildCount() > 0)
205                 {
206                     closeAllChildren(ni.getChildren().iterator());
207                 }
208             }
209         }
210     }
211
212
213     public void broadcast(FacesEvent event) throws AbortProcessingException
214     {
215         if (event instanceof ActionEvent)
216         {
217             ActionEvent actionEvent = (ActionEvent)event;
218             if (actionEvent.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES)
219             {
220                 HtmlCommandNavigation navItem = (HtmlCommandNavigation)actionEvent.getComponent();
221                 navItem.toggleOpen();
222                 FacesContext.getCurrentInstance().renderResponse();
223             }
224         }
225         super.broadcast(event);
226     }
227
228
229     public Object JavaDoc saveState(FacesContext context)
230     {
231         Object JavaDoc values[] = new Object JavaDoc[3];
232         values[0] = super.saveState(context);
233         values[1] = Boolean.valueOf(_open);
234         values[2] = Boolean.valueOf(_active);
235         return ((Object JavaDoc) (values));
236     }
237
238     public void restoreState(FacesContext context, Object JavaDoc state)
239     {
240         Object JavaDoc values[] = (Object JavaDoc[])state;
241         super.restoreState(context, values[0]);
242         _open = ((Boolean JavaDoc)values[1]).booleanValue();
243         _active = ((Boolean JavaDoc)values[2]).booleanValue();
244     }
245
246
247     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
248

249     public static final String JavaDoc COMPONENT_TYPE = "org.apache.myfaces.HtmlCommandNavigation";
250     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Command";
251     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "javax.faces.Link";
252
253
254     public HtmlCommandNavigation()
255     {
256         setRendererType(DEFAULT_RENDERER_TYPE);
257     }
258
259     public String JavaDoc getFamily()
260     {
261         return COMPONENT_FAMILY;
262     }
263
264
265     //------------------ GENERATED CODE END ---------------------------------------
266

267 }
268
Popular Tags