KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > toolbar > ToolBarComponentSupport


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 com.tonbeller.wcf.toolbar;
14
15 import com.tonbeller.wcf.controller.RequestContext;
16
17 /**
18  * visibility of a toolbar button is computed as followd
19    * <ol>
20    * <li>if a role attribute was supplied, and the user is authenticated, and the user is not member of that role,
21    * the button is not shown</li>
22    * <li>otherwise, if visibleExpr is given (either a ${...} EL expression or "true" or "false"), then
23    * its result is returned</li>
24    * <li>otherwise, the value of the boolean visible property is returned which
25    * defaults to true.</li>
26    * </ol>
27  */

28 public abstract class ToolBarComponentSupport implements ToolBarComponent {
29
30   String JavaDoc role;
31   String JavaDoc visibleExpr;
32   boolean visible = true;
33   String JavaDoc id;
34   
35   /**
36    * returns true if this Component is visible.
37    * Visability is computed as follows
38    * <ol>
39    * <li>If the <code>visible</code> property is set to false, the button is not visible.
40    * <li>if a role attribute was supplied, and the user is authenticated, and the user is not member of that role,
41    * the button is not shown</li>
42    * <li>otherwise, if visibleExpr is given (either a ${...} EL expression or "true" or "false"), then
43    * its result is returned</li>
44    * <li>otherwise, the value of the boolean visible property is returned which
45    * defaults to true.</li>
46    * </ol>
47    */

48   public boolean isVisible(RequestContext context) {
49     if (!visible)
50       return false;
51
52     // if !allowed, the user can never see this button
53
if (!context.isUserInRole(role))
54       return false;
55
56     // otherwise evaluate visibleExpr
57
if (visibleExpr != null) {
58       if ("true".equals(visibleExpr))
59         return true;
60       if ("false".equals(visibleExpr))
61         return false;
62       Object JavaDoc val = context.getModelReference(visibleExpr);
63       String JavaDoc s = String.valueOf(val);
64       boolean b = Boolean.valueOf(s).booleanValue();
65       return b;
66     }
67     
68     return true;
69   }
70
71   /**
72    * @return
73    */

74   public String JavaDoc getVisibleExpr() {
75     return visibleExpr;
76   }
77
78   /**
79    * @param string
80    */

81   public void setVisibleExpr(String JavaDoc string) {
82     visibleExpr = string;
83   }
84
85   /**
86    * @return
87    */

88   public String JavaDoc getRole() {
89     return role;
90   }
91
92   /**
93    * @param role
94    */

95   public void setRole(String JavaDoc role) {
96     this.role = role;
97   }
98
99   public boolean isVisible() {
100     return visible;
101   }
102   public void setVisible(boolean visible) {
103     this.visible = visible;
104   }
105   public String JavaDoc getId() {
106     return id;
107   }
108   public void setId(String JavaDoc id) {
109     this.id = id;
110   }
111 }
112
Popular Tags