View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.menu;
20  
21  import java.util.Map;
22  
23  import javax.el.ELContext;
24  import javax.el.ExpressionFactory;
25  import javax.el.MethodExpression;
26  import javax.faces.context.FacesContext;
27  import javax.faces.event.ActionEvent;
28  
29  import org.apache.myfaces.trinidad.util.ContainerUtils;
30  
31  /**
32   * Code specific to a Menu Model's ItemNode.
33   *     
34   */
35  
36  public class ItemNode extends MenuNode
37  {
38    /**
39      * Constructs an ItemNode
40      */
41    public ItemNode()
42    {
43      super();
44    }
45    
46    /**
47      * Sets the action of the node.  This is obtained from the menu
48      * metadata file and is the string value of the "action" 
49      * property.
50      * 
51      * @param action - the string value of the ItemNode's "action" property.
52      */
53  
54    public void setAction(String action)
55    {
56      _action = action;
57    }  
58    
59    /**
60      * Gets the value of the node's action property.  The action attr value
61      * could be one of 2 things:
62      * 1) An EL expression
63      * 2) An outcome referencing a navigation rule in the faces_config file.
64      * 
65      * Since this method is called only when an ItemNode is clicked, the model 
66      * is notified that this node is the currently selected node.
67      * 
68      * @return String value of the ItemNode's "action" property.
69      */
70    @Override
71    public String doAction()
72    {
73      String value = _action;
74  
75      if (value != null)
76      {
77        FacesContext facesContext = FacesContext.getCurrentInstance();
78        ExpressionFactory expressionFactory =
79            facesContext.getApplication().getExpressionFactory();
80        ELContext context = facesContext.getELContext();
81        MethodExpression methodExpression =
82            expressionFactory.createMethodExpression(context, value,
83                String.class, new Class<?>[]
84                {});
85        value = (String) methodExpression.invoke(context, null);
86      }
87  
88      // Post me as the selected Node for the request
89      postSelectedNode(this);
90  
91      return value;
92    }
93  
94    /**
95      * setActionListener - sets the value of the Menu Node's actionListener
96      * atribute.
97      * 
98      * @param actionListener - El expression method reference to an 
99      * action listener
100     */
101   public void setActionListener(String actionListener)
102   {
103     _actionListener = actionListener;
104   }  
105   
106   /**
107     * getActionListener - gets the value of the Menu Node's actionListener
108     * attribute.
109     * 
110     * @return String  - method reference to an 
111     * action listener
112     */
113   public String getActionListener()
114   {
115     String value = _actionListener;
116 
117     // Could be EL expression
118     if (   value != null
119         && ContainerUtils.isValueReference(value)
120        )
121     {
122       // Value of action is EL method binding, so we 
123       // need to evaluate it
124       value = MenuUtils.getBoundValue(value, String.class);
125       setActionListener(value);
126     }
127 
128     return value;
129   }
130   
131   public void actionListener(ActionEvent event)
132   {
133     String value = _actionListener;
134     if (value != null)
135     {
136       FacesContext facesContext = FacesContext.getCurrentInstance();
137       ExpressionFactory expressionFactory =
138           facesContext.getApplication().getExpressionFactory();
139       ELContext context = facesContext.getELContext();
140 
141       MethodExpression methodExpression =
142           expressionFactory.createMethodExpression(context, value, Void.TYPE,
143               new Class<?>[]
144               { ActionEvent.class });
145       methodExpression.invoke(context, new Object[]{ event });
146     }
147 
148   }
149   
150   /**
151     * setLaunchListener - sets the value of the Menu Node's launchListener
152     * atribute.
153     * 
154     * @param launchListener - El expression method reference to a 
155     * launch listener
156     */
157   public void setLaunchListener(String launchListener)
158   {
159     _launchListener = launchListener;
160   }  
161   
162   /**
163     * getLaunchListener - gets the value of the Menu Node's launchListener
164     * attribute.
165     * 
166     * @return String  - method reference to an 
167     * launch listener
168     */
169   public String getLaunchListener()
170   {
171     String value = _launchListener;
172 
173     // Could be EL expression
174     if (   value != null
175         && ContainerUtils.isValueReference(value)
176        )
177     {
178       // Value of action is EL method binding, so we 
179       // need to evaluate it
180       value = MenuUtils.getBoundValue(value, String.class);
181       setLaunchListener(value);
182     }
183 
184     return value;
185   }
186   
187   /**
188     * setReturnListener - sets the value of the Menu Node's returnListener
189     * atribute.
190     * 
191     * @param returnListener - El expression method reference to a 
192     * return listener
193     */
194   public void setReturnListener(String returnListener)
195   {
196     _returnListener = returnListener;
197   }  
198   
199   /**
200     * getReturnListener - gets the value of the Menu Node's returnListener
201     * attribute.
202     * 
203     * @return String  - method reference to an 
204     * return listener
205     */
206   public String getReturnListener()
207   {
208     String value = _returnListener;
209 
210     // Could be EL expression
211     if (   value != null
212         && ContainerUtils.isValueReference(value)
213        )
214     {
215       // Value of action is EL method binding, so we 
216       // need to evaluate it
217       value = MenuUtils.getBoundValue(value, String.class);
218       setReturnListener(value);
219     }
220 
221     return value;
222   }
223   
224   /**
225     * Sets the immediate attribute of the menu item.  
226     *  
227     * @param immediate - boolean indicating whether or not data validation - 
228     * client-side or server-side - should take place when 
229     * events are generated by this component. 
230     */
231   public void setImmediate(boolean immediate)
232   {
233     _immediateStr = immediate ? "true" : "false";
234   }
235 
236   /**
237     * Sets the immediate attribute of the menu item.  
238     * 
239     * @param immediateStr - string representing a boolean value
240     * or an EL expression
241     */
242   public void setImmediate(String immediateStr)
243   {
244     _immediateStr = immediateStr;
245   }
246       
247   /**
248     * Gets the immediate attribute of the menu item.  
249     *
250     * @return boolean - indicating whether or not data validation - 
251     * client-side or server-side - should take place when events 
252     * are generated by this component. 
253     */
254   public boolean getImmediate()
255   {
256     boolean immediate = MenuUtils.evalBoolean(_immediateStr, false);
257     return immediate;
258   }
259       
260   /**
261     * Sets the useWindow attribute of the menu item.  
262     *  
263     * @param useWindow - boolean indicating whether
264     * or not to use a new window when launching dialogs. 
265     */
266   public void setUseWindow(boolean useWindow)
267   {
268     _useWindowStr = useWindow ? "true" : "false";
269   }
270 
271   /**
272     * Sets the useWindow attribute of the menu item.  
273     * 
274     * @param useWindowStr - string representing a boolean value or
275     * an EL Expression
276     */
277   public void setUseWindow(String useWindowStr)
278   {
279     _useWindowStr = useWindowStr;
280   }
281       
282   /**
283     * Gets the useWindow attribute of the menu item.  
284     *
285     * @return boolean - indicating whether
286     * or not to use a new window when launching dialogs. 
287     */
288   public boolean getUseWindow()
289   {
290     boolean useWindow = MenuUtils.evalBoolean(_useWindowStr, false);
291     return useWindow;
292   }
293       
294   /**
295     * Sets the windowHeight attribute of the menu item.  
296     *  
297     * @param windowHeight - int height of the window, if 
298     * this command is used to launch a window.
299     */
300   public void setWindowHeight(int windowHeight)
301   {
302     _windowHeightStr = Integer.toString(windowHeight);
303   }
304 
305   /**
306     * Sets the windowHeight attribute of the menu item.  
307     *  
308     * @param windowHeightStr - String Height of the window, if 
309     * this command is used to launch a window. Could be an
310     * EL expression
311     */
312   public void setWindowHeight(String windowHeightStr)
313   {
314     _windowHeightStr = windowHeightStr;
315   }
316 
317   /**
318     * Gets the windowHeight attribute of the menu item.  
319     *
320     * @return int height of the window, if 
321     * this command is used to launch a window. 
322     */
323   public int getWindowHeight()
324   {
325     int windowHeight = MenuUtils.evalInt(_windowHeightStr);
326     return windowHeight;
327   }
328       
329   /**
330     * Sets the windowWidth attribute of the menu item.  
331     *  
332     * @param windowWidth - int width of the window, if 
333     * this command is used to launch a window.
334     */
335   public void setWindowWidth(int windowWidth)
336   {
337     _windowWidthStr = Integer.toString(windowWidth);
338   }
339 
340   /**
341     * Sets the windowWidth attribute of the menu item.  
342     *  
343     * @param windowWidthStr - String width of the window, if 
344     * this command is used to launch a window. Could be an
345     * EL expression
346     */
347   public void setWindowWidth(String windowWidthStr)
348   {
349     _windowWidthStr = windowWidthStr;
350   }
351 
352   /**
353     * Gets the windowWidth attribute of the menu item.  
354     *
355     * @return int width of the window, if 
356     * this command is used to launch a window. 
357     */
358   public int getWindowWidth()
359   {
360     int windowWidth = MenuUtils.evalInt(_windowWidthStr);
361     return windowWidth;
362   }
363 
364   /**
365     * Sets the destination of the node.  
366     * 
367     * This is obtained from the metadata file and is the string
368     * value of the "destination" property.
369     *
370     * @param destination - either a URI or an EL method binding expression.
371     */
372   public void setDestination(String destination)
373   {
374     _destination = destination;
375   }  
376   
377   /**
378     * Gets the value of the node's destination property.
379     * The destination attr value could be one of 2 things:
380     * 1) a uri
381     * 2) An EL expression
382     * 
383     * So that the model can identify this node as the currently selected
384     * node, the node's id is appended to the destination as a parameter
385     * that is picked up when the getFocusRowKey() method of the model 
386     * is called to get the focus path.
387     * 
388     * @return destination - the String value of the destinationNode's
389     *                       "destination" property.
390     */
391   @Override
392   public String getDestination()
393   {
394     String value = _destination;
395       
396     // Could be EL expression
397     if (   value != null
398         && ContainerUtils.isValueReference(value)
399        ) 
400     {
401       // Value of action is EL method binding, so we 
402       // need to evaluate it
403       value = MenuUtils.getBoundValue(value, String.class);
404     }
405 
406     // Appending nodeId to URL so that we can identify the node
407     // when getFocusRowKey() is called on the model.
408     return value != null ? value + "?nodeId=" + getUniqueId() : value;
409   }
410   
411   /**
412    * setTargetFrame - sets the value of the Destination Node's
413    * targetFrame attribute
414    * 
415    * @param targetFrame - the target frame for the goCommandMenuItem.
416    */
417   public void setTargetFrame(String targetFrame) 
418   {
419     _targetFrame = targetFrame; 
420   }
421 
422   /**
423    * getTargetFrame - gets the value of the Destination Node's
424    * targetFrame attribute
425    * 
426    * @return the target frame for the goCommandMenuItem.
427    */
428   public String getTargetFrame()
429   {
430     String value = _targetFrame;
431     
432     // Could be EL expression
433     if (   value != null
434         && ContainerUtils.isValueReference(value)
435        )
436     {
437       // Value of destination is EL value binding, so we 
438       // need to evaluate it
439       value = MenuUtils.getBoundValue(value, String.class);
440       setTargetFrame(value);
441     }
442      
443     return value;
444   }
445   
446   /**
447    * Get the Attributes containing the custom attributes on this node. This 
448    * needs to be public so that the menu model can get them.
449    * 
450    * @return Attributes list containing the custom attributes on this node
451    */
452   public Map<String, String> getCustomPropList()
453   {
454     return _customPropList;
455   }
456   
457   
458   public final Map<String, String> getCustomPropListProperty()
459   {
460     return _customPropList;
461   }
462 
463   public final String getDestinationProperty()
464   {
465     return _destination;
466   }
467 
468   public final String getTargetFrameProperty()
469   {
470     return _targetFrame;
471   }
472 
473   public final String getActionProperty()
474   {
475     return _action;
476   }
477 
478   public final String getActionListenerProperty()
479   {
480     return _actionListener;
481   }
482 
483   public final String getLaunchListenerProperty()
484   {
485     return _launchListener;
486   }
487 
488   public final String getReturnListenerProperty()
489   {
490     return _returnListener;
491   }
492 
493   public final String getImmediateProperty()
494   {
495     return _immediateStr;
496   }
497 
498   public final String getUseWindowProperty()
499   {
500     return _useWindowStr;
501   }
502 
503   public final String getWindowHeightProperty()
504   {
505     return _windowHeightStr;
506   }
507 
508   public final String getWindowWidthProperty()
509   {
510     return _windowWidthStr;
511   }
512   /**
513    * Set the list of custom attributes.
514    * 
515    * @param attrMap Map of attibute name/values for this node
516    * from MenuContentHandlerImpl
517    */
518   public void setCustomPropList(Map<String, String> attrMap)
519   {
520     _customPropList = attrMap;
521   }
522   
523   public MenuNode getThreadSafeCopy()
524   {
525     return new ImmutableItemNode(this);
526   }
527   
528   // Map for Custom attributes (properties)
529   private Map<String, String> _customPropList = null;
530   
531   private String _destination     = null;
532   private String _targetFrame     = null;   
533   private String _action          = null;
534   private String _actionListener  = null;
535   private String _launchListener  = null;
536   private String _returnListener  = null;
537   private String _immediateStr    = null;
538   private String _useWindowStr    = null;
539   private String _windowHeightStr = null;
540   private String _windowWidthStr  = null;
541   
542  
543 }