1 31 32 package org.opencms.workplace.commons; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsProperty; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.i18n.CmsEncoder; 39 import org.opencms.i18n.CmsMessages; 40 import org.opencms.jsp.CmsJspActionElement; 41 import org.opencms.jsp.CmsJspNavBuilder; 42 import org.opencms.jsp.CmsJspNavElement; 43 import org.opencms.main.CmsException; 44 import org.opencms.main.CmsLog; 45 import org.opencms.main.OpenCms; 46 import org.opencms.security.CmsPermissionSet; 47 import org.opencms.util.CmsStringUtil; 48 import org.opencms.workplace.CmsDialog; 49 import org.opencms.workplace.CmsWorkplace; 50 import org.opencms.workplace.CmsWorkplaceSettings; 51 52 import java.util.ArrayList ; 53 import java.util.List ; 54 55 import javax.servlet.http.HttpServletRequest ; 56 import javax.servlet.http.HttpServletResponse ; 57 import javax.servlet.jsp.JspException ; 58 import javax.servlet.jsp.PageContext ; 59 60 import org.apache.commons.logging.Log; 61 62 77 public class CmsChnav extends CmsDialog { 78 79 80 public static final int ACTION_CHNAV = 100; 81 82 83 public static final String DIALOG_TYPE = "chnav"; 84 85 86 public static final String PARAM_NAVPOS = "navpos"; 87 88 89 public static final String PARAM_NAVTEXT = "navtext"; 90 91 92 private static final Log LOG = CmsLog.getLog(CmsChnav.class); 93 94 private String m_paramNavpos; 95 96 private String m_paramNavtext; 97 98 103 public CmsChnav(CmsJspActionElement jsp) { 104 105 super(jsp); 106 } 107 108 115 public CmsChnav(PageContext context, HttpServletRequest req, HttpServletResponse res) { 116 117 this(new CmsJspActionElement(context, req, res)); 118 } 119 120 130 public static String buildNavPosSelector(CmsObject cms, String filename, String attributes, CmsMessages messages) { 131 132 List navList = new ArrayList (); 133 List options = new ArrayList (); 134 List values = new ArrayList (); 135 136 CmsJspNavElement curNav = CmsJspNavBuilder.getNavigationForResource(cms, filename); 138 139 filename = CmsResource.getParentFolder(filename); 141 142 navList = CmsJspNavBuilder.getNavigationForFolder(cms, filename); 144 float maxValue = 0; 145 float nextPos = 0; 146 147 float firstValue = 1; 149 if (navList.size() > 0) { 150 try { 151 CmsJspNavElement ne = (CmsJspNavElement)navList.get(0); 152 maxValue = ne.getNavPosition(); 153 } catch (Exception e) { 154 LOG.error(e.getLocalizedMessage()); 156 } 157 } 158 159 if (maxValue != 0) { 160 firstValue = maxValue / 2; 161 } 162 163 options.add(messages.key(Messages.GUI_CHNAV_POS_FIRST_0)); 165 values.add(firstValue + ""); 166 167 for (int i = 0; i < navList.size(); i++) { 169 CmsJspNavElement ne = (CmsJspNavElement)navList.get(i); 170 String navText = ne.getNavText(); 171 float navPos = ne.getNavPosition(); 172 nextPos = navPos + 2; 174 if ((i + 1) < navList.size()) { 175 nextPos = ((CmsJspNavElement)navList.get(i + 1)).getNavPosition(); 176 } 177 float newPos; 179 if ((nextPos - navPos) > 1) { 180 newPos = navPos + 1; 181 } else { 182 newPos = (navPos + nextPos) / 2; 183 } 184 185 if (navPos > maxValue) { 187 maxValue = navPos; 188 } 189 190 if (curNav.getNavText().equals(navText) && curNav.getNavPosition() == navPos) { 192 options.add(CmsEncoder.escapeHtml(messages.key( 193 Messages.GUI_CHNAV_POS_CURRENT_1, 194 new Object [] {ne.getFileName()}))); 195 values.add("-1"); 196 } else { 197 options.add(CmsEncoder.escapeHtml(navText + " [" + ne.getFileName() + "]")); 198 values.add(newPos + ""); 199 } 200 } 201 202 options.add(messages.key(Messages.GUI_CHNAV_POS_LAST_0)); 204 values.add((maxValue + 1) + ""); 205 206 options.add(messages.key(Messages.GUI_CHNAV_NO_CHANGE_0)); 208 if (curNav.getNavPosition() == Float.MAX_VALUE) { 209 values.add((maxValue + 1) + ""); 211 } else { 212 values.add("-1"); 214 } 215 216 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(attributes)) { 217 attributes = " " + attributes; 218 } else { 219 attributes = ""; 220 } 221 return CmsWorkplace.buildSelect( 222 "name=\"" + PARAM_NAVPOS + "\"" + attributes, 223 options, 224 values, 225 values.size() - 1, 226 true); 227 } 228 229 234 public void actionChangeNav() throws JspException { 235 236 getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this); 238 239 String filename = getParamResource(); 241 String newText = getParamNavtext(); 242 String selectedPosString = getParamNavpos(); 243 244 try { 245 checkLock(getParamResource()); 247 if (newText != null) { 249 CmsProperty newNavText = new CmsProperty(); 250 newNavText.setName(CmsPropertyDefinition.PROPERTY_NAVTEXT); 251 CmsProperty oldNavText = getCms().readPropertyObject( 252 filename, 253 CmsPropertyDefinition.PROPERTY_NAVTEXT, 254 false); 255 if (oldNavText.isNullProperty()) { 256 if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) { 258 newNavText.setStructureValue(newText); 259 } else { 260 newNavText.setResourceValue(newText); 261 } 262 } else { 263 if (oldNavText.getStructureValue() != null) { 264 newNavText.setStructureValue(newText); 265 newNavText.setResourceValue(oldNavText.getResourceValue()); 266 } else { 267 newNavText.setResourceValue(newText); 268 } 269 } 270 271 String oldStructureValue = oldNavText.getStructureValue(); 272 String newStructureValue = newNavText.getStructureValue(); 273 if (CmsStringUtil.isEmpty(oldStructureValue)) { 274 oldStructureValue = CmsProperty.DELETE_VALUE; 275 } 276 if (CmsStringUtil.isEmpty(newStructureValue)) { 277 newStructureValue = CmsProperty.DELETE_VALUE; 278 } 279 280 String oldResourceValue = oldNavText.getResourceValue(); 281 String newResourceValue = newNavText.getResourceValue(); 282 if (CmsStringUtil.isEmpty(oldResourceValue)) { 283 oldResourceValue = CmsProperty.DELETE_VALUE; 284 } 285 if (CmsStringUtil.isEmpty(newResourceValue)) { 286 newResourceValue = CmsProperty.DELETE_VALUE; 287 } 288 289 if (!oldResourceValue.equals(newResourceValue) || !oldStructureValue.equals(newStructureValue)) { 291 getCms().writePropertyObject(getParamResource(), newNavText); 292 } 293 } 294 295 float selectedPos = -1; 297 try { 298 selectedPos = Float.parseFloat(selectedPosString); 299 } catch (Exception e) { 300 if (LOG.isInfoEnabled()) { 302 LOG.info(e.getLocalizedMessage()); 303 } 304 } 305 306 if (selectedPos != -1) { 308 CmsProperty newNavPos = new CmsProperty(); 309 newNavPos.setName(CmsPropertyDefinition.PROPERTY_NAVPOS); 310 CmsProperty oldNavPos = getCms().readPropertyObject( 311 filename, 312 CmsPropertyDefinition.PROPERTY_NAVPOS, 313 false); 314 if (oldNavPos.isNullProperty()) { 315 if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) { 317 newNavPos.setStructureValue(selectedPosString); 318 } else { 319 newNavPos.setResourceValue(selectedPosString); 320 } 321 } else { 322 if (oldNavPos.getStructureValue() != null) { 323 newNavPos.setStructureValue(selectedPosString); 324 newNavPos.setResourceValue(oldNavPos.getResourceValue()); 325 } else { 326 newNavPos.setResourceValue(selectedPosString); 327 } 328 } 329 getCms().writePropertyObject(filename, newNavPos); 330 } 331 } catch (Throwable e) { 332 includeErrorpage(this, e); 334 } 335 actionCloseDialog(); 337 } 338 339 344 public String buildNavPosSelector() { 345 346 return buildNavPosSelector(getCms(), getParamResource(), null, getMessages()); 347 } 348 349 354 public String getCurrentNavText() { 355 356 try { 357 String navText = getCms().readPropertyObject( 358 getParamResource(), 359 CmsPropertyDefinition.PROPERTY_NAVTEXT, 360 false).getValue(); 361 if (navText == null) { 362 navText = ""; 363 } 364 return CmsEncoder.escapeXml(navText); 365 } catch (CmsException e) { 366 if (LOG.isInfoEnabled()) { 368 LOG.info(e.getLocalizedMessage()); 369 } 370 return ""; 371 } 372 373 } 374 375 384 public String getParamNavpos() { 385 386 return m_paramNavpos; 387 } 388 389 398 public String getParamNavtext() { 399 400 return m_paramNavtext; 401 } 402 403 408 public void setParamNavpos(String value) { 409 410 m_paramNavpos = value; 411 } 412 413 418 public void setParamNavtext(String value) { 419 420 m_paramNavtext = value; 421 } 422 423 426 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 427 428 fillParamValues(request); 430 431 if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) { 433 setParamAction(DIALOG_CANCEL); 435 } 436 437 setParamDialogtype(DIALOG_TYPE); 439 if (DIALOG_TYPE.equals(getParamAction())) { 441 setAction(ACTION_CHNAV); 442 } else if (DIALOG_CANCEL.equals(getParamAction())) { 443 setAction(ACTION_CANCEL); 444 } else { 445 setAction(ACTION_DEFAULT); 446 setParamTitle(key(Messages.GUI_CHNAV_1, new Object [] {CmsResource.getName(getParamResource())})); 448 } 449 } 450 451 } 452 | Popular Tags |