View Javadoc

1   /*
2    * AbstractMenuDisplayer.java
3    *
4    * Created on March 30, 2002, 3:34 PM
5    */
6   package net.sf.navigator.displayer;
7   
8   import net.sf.navigator.menu.MenuComponent;
9   import net.sf.navigator.menu.PermissionsAdapter;
10  import net.sf.navigator.util.MessageResources;
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  
14  import javax.servlet.jsp.JspException;
15  import javax.servlet.jsp.JspWriter;
16  import javax.servlet.jsp.PageContext;
17  import java.io.IOException;
18  
19  /**
20   * Abstract implementation of <code>MenuDisplayer</code> that can be used as
21   * a basis for other menu displayers.
22   *
23   * @author  ssayles
24   * @version
25   */
26  public abstract class AbstractMenuDisplayer implements MenuDisplayer {
27      //~ Instance fields ========================================================
28  
29      protected final transient Log log = LogFactory.getLog(getClass());
30      protected String name;
31      protected MessageResources displayStrings;
32      protected JspWriter out;
33      protected String target;
34  
35      /** Holds value of property permissionsAdapter. */
36      protected PermissionsAdapter permissionsAdapter;
37      protected MenuDisplayerMapping mapping;
38  
39      //~ Methods ================================================================
40  
41      public String getName() {
42          return name;
43      }
44  
45      public void setName(String name) {
46          this.name = name;
47      }
48  
49      public String getConfig() {
50          String config = null;
51  
52          if (displayStrings != null) {
53              config = displayStrings.getConfig();
54          }
55  
56          return config;
57      }
58  
59      public void setConfig(String config) {
60          displayStrings = MessageResources.getMessageResources(config);
61      }
62  
63      public String getTarget() {
64          return target;
65      }
66  
67      /**
68       * Convenience method that will first return the target for the displayer
69       * if it is not null.  If the displayer target is null, then it will
70       * return <code>menu.getTarget()</code>.
71       *
72       * @return the target for the menu link.
73       */
74      protected String getTarget(MenuComponent menu) {
75          String theTarget = null;
76  
77          if (target == null) {
78              if (menu.getTarget() != null) {
79                  theTarget = menu.getTarget();
80              }
81          } else {
82              theTarget = target;
83          }
84  
85          return theTarget;
86      }
87  
88      public void setTarget(String target) {
89          this.target = target;
90      }
91  
92      /**
93       * Getter for property permissionsAdapter.
94       * @return Value of property permissionsAdapter.
95       */
96      public PermissionsAdapter getPermissionsAdapter() {
97          return this.permissionsAdapter;
98      }
99  
100     /**
101      * Setter for property permissionsAdapter.
102      * @param permissionsAdapter New value of property permissionsAdapter.
103      */
104     public void setPermissionsAdapter(PermissionsAdapter permissionsAdapter) {
105         this.permissionsAdapter = permissionsAdapter;
106     }
107 
108     /**
109      * Lifecycle method that should be called when the <code>MenuDisplayer</code>
110      * is being prepared for use.
111      *
112      * @param pageContext The JSP pageContext to give the displayer access
113      *              to any resources it may need.
114      * @param mapping The menu displayer mapping used to embody the xml
115      *              definition.
116      */
117     public void init(PageContext pageContext, MenuDisplayerMapping mapping) {
118         this.out = pageContext.getOut();
119         this.mapping = mapping;
120     }
121 
122     public abstract void display(MenuComponent menu)
123     throws JspException, IOException;
124 
125     public void end(PageContext pageContext) {}
126 
127     /**
128      * Returns <code>true</code> if the specified component is usable.
129      * If <code>permissionsAdapter</code> is not defined, this method will
130      * return <code>true</code>.  Otherwise, the adapter will be used to check
131      * permissions on the menu.
132      *
133      * @return <code>true</code> if the menu component is usable.
134      * @param menu The menu component.
135      */
136     public boolean isAllowed(MenuComponent menu) {
137         return permissionsAdapter == null || permissionsAdapter.isAllowed(menu);
138     }
139 }