1 package org.jahia.taglibs.button; 2 3 import javax.servlet.http.HttpServletRequest ; 4 import javax.servlet.jsp.JspException ; 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 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 viewTitle = "Live mode"; 28 private String editTitle = "Edit draft mode"; 29 private String previewTitle = "Draft mode"; 30 private String style = ""; 31 private String separator = " "; 32 private boolean isEdit = false; 33 34 public void setViewTitle(String vtitle) { 35 36 this.viewTitle = vtitle; 37 } 38 39 public void setEditTitle(String etitle) { 40 this.editTitle = etitle; 41 } 42 43 public void setPreviewTitle(String ptitle) { 44 this.previewTitle = ptitle; 45 } 46 47 public void setSeparator(String separator) { 48 this.separator = separator; 49 } 50 51 public void setStyle(String style) { 52 this.style = style; 53 } 54 55 public String getTitle() { 56 57 return ""; 58 } 59 public String getLauncher(JahiaData jData) throws JahiaException { 60 return ""; 62 } 63 64 public String getStyle() { 65 return this.style; 66 } 67 68 public String buildButton (JahiaData jData, HttpServletRequest request) { 71 72 String url1 = null; 73 String url2 = null; 74 75 String title1 = null; 76 String 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 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 text = new StringBuffer (); 126 if (isFirstAllowed) { 127 text.append( "<a " ); 128 String 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 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 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 { 173 logger.debug("Recycling switch mode button tag."); 176 viewTitle = "Live mode"; 177 editTitle = "Edit draft mode"; 178 previewTitle = "Draft mode"; 179 style = ""; 180 separator = " "; 181 isEdit = false; 182 return EVAL_PAGE; 183 } 184 185 186 } 187 | Popular Tags |