KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > button > SwitchModeButtonTag


1 package org.jahia.taglibs.button;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.jsp.JspException JavaDoc;
5
6 import org.jahia.data.JahiaData;
7 import org.jahia.exceptions.JahiaException;
8 import org.jahia.registries.ServicesRegistry;
9 import org.jahia.services.usermanager.JahiaUser;
10 import org.jahia.services.usermanager.JahiaUserManagerService;
11 import org.jahia.taglibs.util.Utils;
12
13
14 /**
15  * Class SwitchModeButtonTag : return the full link ("<a href...>...</a>") allowing to
16  * switch between edit mode and view mode
17  *
18  * @author Jerome Tamiotti
19  * @author Serge Huber
20  *
21  */

22 public class SwitchModeButtonTag extends AbstractButtonTag {
23
24     private static org.apache.log4j.Logger logger =
25             org.apache.log4j.Logger.getLogger(SwitchModeButtonTag.class);
26
27     private String JavaDoc viewTitle = "Live&nbsp;mode";
28     private String JavaDoc editTitle = "Edit draft&nbsp;mode";
29     private String JavaDoc previewTitle = "Draft&nbsp;mode";
30     private String JavaDoc style = "";
31     private String JavaDoc separator = " ";
32     private boolean isEdit = false;
33
34     public void setViewTitle(String JavaDoc vtitle) {
35
36         this.viewTitle = vtitle;
37     }
38
39     public void setEditTitle(String JavaDoc etitle) {
40         this.editTitle = etitle;
41     }
42
43     public void setPreviewTitle(String JavaDoc ptitle) {
44         this.previewTitle = ptitle;
45     }
46
47     public void setSeparator(String JavaDoc separator) {
48         this.separator = separator;
49     }
50
51     public void setStyle(String JavaDoc style) {
52         this.style = style;
53     }
54
55     public String JavaDoc getTitle() {
56         /* not used */
57         return "";
58     }
59     public String JavaDoc getLauncher(JahiaData jData) throws JahiaException {
60         // not used.
61
return "";
62     }
63
64     public String JavaDoc getStyle() {
65         return this.style;
66     }
67
68     // we override this method because we need to define more than one button
69
// for the various modes.
70
public String JavaDoc buildButton (JahiaData jData, HttpServletRequest JavaDoc request) {
71
72         String JavaDoc url1 = null;
73         String JavaDoc url2 = null;
74
75         String JavaDoc title1 = null;
76         String JavaDoc title2 = null;
77
78         boolean isFirstAllowed = false;
79         boolean isSecondAllowed = false;
80
81         boolean stagingActivated = jData.params().getSite().isStagingEnabled();
82
83         try {
84             if (jData.gui().isEditMode()) {
85                 url1 = jData.gui().drawNormalModeLink();
86                 title1 = viewTitle;
87
88                 if (stagingActivated) {
89                     url2 = jData.gui().drawPreviewModeLink();
90                     title2 = previewTitle;
91                 }
92
93             } else if (jData.gui().isPreviewMode()) {
94                 url1 = jData.gui().drawNormalModeLink();
95                 title1 = viewTitle;
96
97                 title2 = editTitle;
98                 url2 = jData.gui().drawEditModeLink();
99             } else {
100                 // we assume normal mode if we cannot guess it.
101
if (stagingActivated) {
102                     url1 = jData.gui().drawPreviewModeLink();
103                     title1 = previewTitle;
104                 } else {
105                     url1 = jData.gui().drawEditModeLink();
106                     title1 = editTitle;
107                 }
108             }
109         } catch (JahiaException je) {
110             return "";
111         }
112
113         if ( url1 == null || url1.trim().equals("")) {
114             isFirstAllowed = false;
115         } else {
116             isFirstAllowed = true;
117         }
118
119         if ( url2 == null || url2.trim().equals("")) {
120             isSecondAllowed = false;
121         } else {
122             isSecondAllowed = true;
123         }
124
125         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
126         if (isFirstAllowed) {
127             text.append( "<a " );
128             String JavaDoc style = getStyle();
129             if ( !style.equals("") ) {
130                 text.append( "class=\"" );
131                 text.append( style );
132                 text.append( "\" " );
133             }
134             text.append( "href=\"" );
135             text.append( url1 );
136             text.append( "\">" );
137             text.append( Utils.insertContextPath( jData.gui().drawHttpJspContext(request), title1 ) );
138             text.append( "</a>" );
139         }
140
141         if (isFirstAllowed && isSecondAllowed) {
142             text.append(separator);
143         }
144
145         if (isSecondAllowed) {
146             text.append("<a ");
147             String JavaDoc style2 = getStyle();
148             if ( !style2.equals("") ) {
149                 text.append( "class=\"" );
150                 text.append( style2 );
151                 text.append( "\" " );
152             }
153             text.append( "href=\"" );
154             text.append( url2 );
155             text.append( "\">" );
156             text.append( Utils.insertContextPath( jData.gui().drawHttpJspContext(request), title2 ) );
157             text.append( "</a>" );
158
159         }
160         return text.toString();
161     }
162
163     public boolean testRights (JahiaData jData) {
164         // this button is displayed when the user is logged,
165
// or when the guest was set write access on the current page
166
JahiaUserManagerService userService = ServicesRegistry.getInstance().getJahiaUserManagerService();
167         JahiaUser theUser = userService.lookupUser( jData.page().getJahiaID(), JahiaUserManagerService.GUEST_USERNAME );
168
169         return jData.gui().isLogged() || jData.page().checkWriteAccess(theUser);
170     }
171
172     public int doEndTag() throws JspException JavaDoc {
173         // let's reinitialize the tag variables to allow tag object reuse in
174
// pooling.
175
logger.debug("Recycling switch mode button tag.");
176         viewTitle = "Live&nbsp;mode";
177         editTitle = "Edit draft&nbsp;mode";
178         previewTitle = "Draft&nbsp;mode";
179         style = "";
180         separator = " ";
181         isEdit = false;
182         return EVAL_PAGE;
183     }
184
185
186 }
187
Popular Tags