001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/directedit/CmsDirectEditDefaultProvider.java,v $
003: * Date : $Date: 2008-02-27 12:05:54 $
004: * Version: $Revision: 1.6 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.editors.directedit;
033:
034: import org.opencms.cache.CmsMemoryObjectCache;
035: import org.opencms.file.CmsFile;
036: import org.opencms.file.CmsObject;
037: import org.opencms.file.CmsProperty;
038: import org.opencms.file.CmsPropertyDefinition;
039: import org.opencms.i18n.CmsEncoder;
040: import org.opencms.main.CmsException;
041: import org.opencms.main.CmsLog;
042: import org.opencms.main.OpenCms;
043: import org.opencms.util.CmsMacroResolver;
044: import org.opencms.workplace.CmsWorkplace;
045: import org.opencms.workplace.editors.Messages;
046:
047: import javax.servlet.jsp.JspException;
048: import javax.servlet.jsp.PageContext;
049:
050: import org.apache.commons.logging.Log;
051:
052: /**
053: * Provider for the OpenCms default graphical "direct edit" buttons.<p>
054: *
055: * Since OpenCms version 6.2.3,
056: * this provider is configured as the standard direct edit provider in a common OpenCms installation.<p>
057: *
058: * This provider DOES NOT support {@link CmsDirectEditMode#MANUAL} mode.<p>
059: *
060: * @author Alexander Kandzior
061: *
062: * @version $Revision: 1.6 $
063: *
064: * @since 6.2.3
065: */
066: public class CmsDirectEditDefaultProvider extends
067: A_CmsDirectEditProvider implements I_CmsDirectEditProvider {
068:
069: /** The log object for this class. */
070: private static final Log LOG = CmsLog
071: .getLog(CmsDirectEditDefaultProvider.class);
072:
073: /** Indicates the permissions for the last element the was opened. */
074: protected int m_lastPermissionMode;
075:
076: /** The include file used by this provider. */
077: private String m_headerInclude;
078:
079: /**
080: * Returns the end HTML for a disabled direct edit button.<p>
081: *
082: * @return the end HTML for a disabled direct edit button
083: */
084: public String endDirectEditDisabled() {
085:
086: return "</div>\n<!-- EDIT BLOCK END (DISABLED) -->\n";
087: }
088:
089: /**
090: * Returns the end HTML for an enabled direct edit button.<p>
091: *
092: * @return the end HTML for an enabled direct edit button
093: */
094: public String endDirectEditEnabled() {
095:
096: return "</div>\n<!-- EDIT BLOCK END (ENABLED) -->\n";
097: }
098:
099: /**
100: * Returns the direct edit include HTML to insert in the page beginning.<p> t
101: *
102: * @param params the parameters for the direct edit includes
103: *
104: * @return the direct edit include HTML to insert in the page beginning
105: */
106: public String getDirectEditIncludes(CmsDirectEditParams params) {
107:
108: String closeLink = getLink(params.getLinkForClose());
109: String deleteLink = getLink(params.getLinkForDelete());
110: String titleForNew = m_messages
111: .key(Messages.GUI_EDITOR_TITLE_NEW_0);
112: String skinUri = CmsWorkplace.getSkinUri();
113:
114: // resolve macros in include header
115: CmsMacroResolver resolver = CmsMacroResolver.newInstance();
116: resolver.addMacro("closeLink", closeLink);
117: resolver.addMacro("deleteLink", deleteLink);
118: resolver.addMacro("titleForNew", titleForNew);
119: resolver.addMacro("skinUri", skinUri);
120:
121: return resolver.resolveMacros(m_headerInclude);
122: }
123:
124: /**
125: * @see org.opencms.workplace.editors.directedit.A_CmsDirectEditProvider#init(org.opencms.file.CmsObject, org.opencms.workplace.editors.directedit.CmsDirectEditMode, java.lang.String)
126: */
127: public void init(CmsObject cms, CmsDirectEditMode mode,
128: String fileName) {
129:
130: super .init(cms, mode, fileName);
131:
132: // check if the selected include file is available in the cache
133: CmsMemoryObjectCache cache = CmsMemoryObjectCache.getInstance();
134: m_headerInclude = (String) cache.getCachedObject(
135: CmsDirectEditDefaultProvider.class, m_fileName);
136:
137: if (m_headerInclude == null) {
138: // the file is not available in the cache
139: try {
140: CmsFile file = m_cms.readFile(m_fileName);
141: // get the encoding for the resource
142: CmsProperty p = m_cms
143: .readPropertyObject(
144: file,
145: CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING,
146: true);
147: String e = p.getValue();
148: if (e == null) {
149: e = OpenCms.getSystemInfo().getDefaultEncoding();
150: }
151: // create a String with the right encoding
152: m_headerInclude = CmsEncoder.createString(file
153: .getContents(), e);
154: // store this in the cache
155: cache.putCachedObject(
156: CmsDirectEditDefaultProvider.class, m_fileName,
157: m_headerInclude);
158:
159: } catch (CmsException e) {
160: // this should better not happen
161: m_headerInclude = "";
162: LOG.error(
163: Messages.get().getBundle().key(
164: Messages.LOG_DIRECT_EDIT_NO_HEADER_1,
165: fileName), e);
166: }
167: }
168: }
169:
170: /**
171: * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditEnd(javax.servlet.jsp.PageContext)
172: */
173: public void insertDirectEditEnd(PageContext context)
174: throws JspException {
175:
176: String content;
177: switch (m_lastPermissionMode) {
178:
179: case 1: // disabled
180: content = endDirectEditDisabled();
181: break;
182: case 2: // enabled
183: content = endDirectEditEnabled();
184: break;
185: default: // inactive or undefined
186: content = null;
187: }
188: m_lastPermissionMode = 0;
189: print(context, content);
190: }
191:
192: /**
193: * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditIncludes(javax.servlet.jsp.PageContext, org.opencms.workplace.editors.directedit.CmsDirectEditParams)
194: */
195: public void insertDirectEditIncludes(PageContext context,
196: CmsDirectEditParams params) throws JspException {
197:
198: print(context, getDirectEditIncludes(params));
199: }
200:
201: /**
202: * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditStart(javax.servlet.jsp.PageContext, org.opencms.workplace.editors.directedit.CmsDirectEditParams)
203: */
204: public boolean insertDirectEditStart(PageContext context,
205: CmsDirectEditParams params) throws JspException {
206:
207: String content;
208: // check the direct edit permissions of the current user
209: CmsDirectEditResourceInfo resourceInfo = getResourceInfo(params
210: .getResourceName());
211: // check the permission mode
212: m_lastPermissionMode = resourceInfo.getPermissions()
213: .getPermission();
214: switch (m_lastPermissionMode) {
215: case 1: // disabled
216: content = startDirectEditDisabled(params, resourceInfo);
217: break;
218: case 2: // enabled
219: content = startDirectEditEnabled(params, resourceInfo);
220: break;
221: default: // inactive or undefined
222: content = null;
223: }
224: print(context, content);
225: return content != null;
226: }
227:
228: /**
229: * Returns <code>false</code> because the default provider does not support manual button placement.<p>
230: *
231: * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#isManual(org.opencms.workplace.editors.directedit.CmsDirectEditMode)
232: */
233: public boolean isManual(CmsDirectEditMode mode) {
234:
235: return false;
236: }
237:
238: /**
239: * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#newInstance()
240: */
241: public I_CmsDirectEditProvider newInstance() {
242:
243: CmsDirectEditDefaultProvider result = new CmsDirectEditDefaultProvider();
244: result.m_configurationParameters = m_configurationParameters;
245: return result;
246: }
247:
248: /**
249: * Returns the start HTML for a disabled direct edit button.<p>
250: *
251: * @param params the direct edit parameters
252: * @param resourceInfo contains information about the resource to edit
253: *
254: * @return the start HTML for a disabled direct edit button
255: */
256: public String startDirectEditDisabled(CmsDirectEditParams params,
257: CmsDirectEditResourceInfo resourceInfo) {
258:
259: String editId = getNextDirectEditId();
260: StringBuffer result = new StringBuffer(256);
261:
262: result.append("<!-- EDIT BLOCK START (DISABLED): ");
263: result.append(params.m_resourceName);
264: result.append(" [");
265: result.append(resourceInfo.getResource().getState());
266: result.append("] ");
267: if (!resourceInfo.getLock().isUnlocked()) {
268: result.append(" locked ");
269: result
270: .append(resourceInfo.getLock().getProject()
271: .getName());
272: }
273: result.append(" -->\n");
274:
275: result.append("<script type=\"text/javascript\">\n");
276: result.append("registerButtonOcms(\"").append(editId).append(
277: "\");\n");
278: result.append("</script>\n");
279: result.append("<div class=\"ocms_de_bt\" id=\"buttons_")
280: .append(editId).append("\">\n");
281: result.append("<span onmouseover=\"activateOcms(\'").append(
282: editId).append("\');\" onmouseout=\"deactivateOcms(\'")
283: .append(editId).append("\');\">\n");
284: result
285: .append(
286: "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"table_")
287: .append(editId).append("\"><tr>\n");
288: result
289: .append("<td class=\"ocms_de\"><span class=\"ocms_disabled\">");
290: if (m_editButtonStyle == 1) {
291: result
292: .append(
293: "<span class=\"ocms_combobutton\" style=\"background-image: url(\'")
294: .append(CmsWorkplace.getSkinUri())
295: .append("buttons/directedit_in.png\');\"> ")
296: .append(
297: m_messages
298: .key(Messages.GUI_EDITOR_FRONTEND_BUTTON_LOCKED_0))
299: .append("</span>");
300: } else if (m_editButtonStyle == 2) {
301: result
302: .append(
303: "<span class=\"ocms_combobutton\" style=\"padding-left: 4px;\">")
304: .append(
305: m_messages
306: .key(Messages.GUI_EDITOR_FRONTEND_BUTTON_LOCKED_0))
307: .append("</span>");
308: } else {
309: result
310: .append("<img border=\"0\" src=\"")
311: .append(CmsWorkplace.getSkinUri())
312: .append("buttons/directedit_in.png\" title=\"")
313: .append(
314: m_messages
315: .key(Messages.GUI_EDITOR_FRONTEND_BUTTON_LOCKED_0))
316: .append("\" alt=\"\"/>");
317: }
318: result.append("</span></td>\n");
319: result.append("</tr></table>\n");
320: result.append("</span>\n");
321: result.append("</div>\n");
322: result.append("<div id=\"").append(editId).append(
323: "\" class=\"ocms_de_norm\">\n");
324:
325: return result.toString();
326: }
327:
328: /**
329: * Returns the start HTML for an enabled direct edit button.<p>
330: *
331: * @param params the direct edit parameters
332: * @param resourceInfo contains information about the resource to edit
333: *
334: * @return the start HTML for an enabled direct edit button
335: */
336: public String startDirectEditEnabled(CmsDirectEditParams params,
337: CmsDirectEditResourceInfo resourceInfo) {
338:
339: String editLocale = m_cms.getRequestContext().getLocale()
340: .toString();
341: String editId = getNextDirectEditId();
342: StringBuffer result = new StringBuffer(512);
343:
344: String uri = m_cms.getRequestContext().getUri();
345:
346: String editLink = getLink(params.getLinkForEdit());
347: String editNewLink = CmsEncoder.encode(params.getLinkForNew());
348:
349: result.append("<!-- EDIT BLOCK START (ENABLED): ");
350: result.append(params.m_resourceName);
351: result.append(" [");
352: result.append(resourceInfo.getResource().getState());
353: result.append("]");
354: if (!resourceInfo.getLock().isUnlocked()) {
355: result.append(" locked ");
356: result
357: .append(resourceInfo.getLock().getProject()
358: .getName());
359: }
360: result.append(" -->\n");
361:
362: result.append("<script type=\"text/javascript\">\n");
363: result.append("registerButtonOcms(\"").append(editId).append(
364: "\");\n");
365: result.append("</script>\n");
366: result.append("<div class=\"ocms_de_bt\" id=\"buttons_")
367: .append(editId).append("\">\n");
368: result
369: .append("<form name=\"form_")
370: .append(editId)
371: .append("\" id=\"form_")
372: .append(editId)
373: .append("\" method=\"post\" action=\"")
374: .append(editLink)
375: .append("\" class=\"ocms_nomargin\" target=\"_top\">\n");
376: result.append(
377: "<input type=\"hidden\" name=\"resource\" value=\"")
378: .append(params.getResourceName()).append("\"/>\n");
379: result
380: .append("<input type=\"hidden\" name=\"directedit\" value=\"true\"/>\n");
381: result
382: .append(
383: "<input type=\"hidden\" name=\"elementlanguage\" value=\"")
384: .append(editLocale).append("\"/>\n");
385: result.append(
386: "<input type=\"hidden\" name=\"elementname\" value=\"")
387: .append(params.getElement()).append("\"/>\n");
388: result.append(
389: "<input type=\"hidden\" name=\"backlink\" value=\"")
390: .append(uri).append("\"/>\n");
391: result.append("<input type=\"hidden\" name=\"newlink\"/>\n");
392: result.append("<input type=\"hidden\" name=\"closelink\"/>\n");
393: result
394: .append("<input type=\"hidden\" name=\"redirect\" value=\"true\"/>\n");
395: result
396: .append("<input type=\"hidden\" name=\"editortitle\"/>\n");
397: result.append("</form>\n");
398: result.append("<table onmouseover=\"activateOcms(\'").append(
399: editId).append("\');\" onmouseout=\"deactivateOcms(\'")
400: .append(editId).append("\');\"");
401: result
402: .append(
403: " border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"table_")
404: .append(editId).append("\"><tr>\n");
405: if (params.getButtonSelection().isShowEdit()) {
406: result
407: .append(
408: "<td class=\"ocms_de\"><a href=\"#\" onclick=\"javascript:submitOcms(\'")
409: .append(editId)
410: .append("\', \'")
411: .append(CmsDirectEditButtonSelection.VALUE_EDIT)
412: .append(
413: "\');\" class=\"ocms_button\"><span class=\"ocms_over\" onmouseover=\"className=\'ocms_over\'\" onmouseout=\"className=\'ocms_over\'\" onmousedown=\"className=\'ocms_push\'\" onmouseup=\"className=\'ocms_over\'\">");
414: if (m_editButtonStyle == 1) {
415: result
416: .append("<span id=\"bt_")
417: .append(editId)
418: .append(
419: "\" class=\"ocms_combobutton\" style=\"background-image: url(\'")
420: .append(CmsWorkplace.getSkinUri())
421: .append(
422: "buttons/directedit_cl.png\');\"> ")
423: .append(
424: m_messages
425: .key(Messages.GUI_EDITOR_FRONTEND_BUTTON_EDIT_0))
426: .append("</span>");
427: } else if (m_editButtonStyle == 2) {
428: result
429: .append(
430: "<span class=\"ocms_combobutton\" style=\"padding-left: 4px;\">")
431: .append(
432: m_messages
433: .key(Messages.GUI_EDITOR_FRONTEND_BUTTON_EDIT_0))
434: .append("</span>");
435: } else {
436: result
437: .append("<span id=\"bt_")
438: .append(editId)
439: .append(
440: "\" class=\"ocms_combobutton\" style=\"padding-left: 15px; padding-right: 1px; background-image: url(\'")
441: .append(CmsWorkplace.getSkinUri())
442: .append(
443: "buttons/directedit_cl.png\'); background-position: 0px 0px;\" title=\"")
444: .append(
445: m_messages
446: .key(Messages.GUI_EDITOR_FRONTEND_BUTTON_EDIT_0))
447: .append("\"> </span>");
448: }
449: result.append("</span></a></td>\n");
450: }
451: if (params.getButtonSelection().isShowDelete()) {
452: result
453: .append(
454: "<td class=\"ocms_de\"><a href=\"#\" onclick=\"javascript:submitOcms(\'")
455: .append(editId)
456: .append("\', \'")
457: .append(CmsDirectEditButtonSelection.VALUE_DELETE)
458: .append(
459: "\');\" class=\"ocms_button\"><span class=\"ocms_over\" onmouseover=\"className=\'ocms_over\'\" onmouseout=\"className=\'ocms_over\'\" onmousedown=\"className=\'ocms_push\'\" onmouseup=\"className=\'ocms_over\'\">");
460: if (m_editButtonStyle == 1) {
461: result
462: .append("<span id=\"del_")
463: .append(editId)
464: .append(
465: "\" class=\"ocms_combobutton\" style=\"background-image: url(\'")
466: .append(CmsWorkplace.getSkinUri())
467: .append(
468: "buttons/deletecontent.png\');\"> ")
469: .append(
470: m_messages
471: .key(Messages.GUI_BUTTON_DELETE_0))
472: .append("</span>");
473: } else if (m_editButtonStyle == 2) {
474: result
475: .append(
476: "<span class=\"ocms_combobutton\" style=\"padding-left: 4px;\">")
477: .append(
478: m_messages
479: .key(Messages.GUI_BUTTON_DELETE_0))
480: .append("</span>");
481: } else {
482: result.append("<img border=\"0\" src=\"").append(
483: CmsWorkplace.getSkinUri()).append(
484: "buttons/deletecontent.png\" title=\"").append(
485: m_messages.key(Messages.GUI_BUTTON_DELETE_0))
486: .append("\" alt=\"\"/>");
487: }
488: result.append("</span></a></td>\n");
489: }
490: if (params.getButtonSelection().isShowNew()) {
491: result
492: .append(
493: "<td class=\"ocms_de\"><a href=\"#\" onclick=\"javascript:submitOcms(\'")
494: .append(editId)
495: .append("\', \'")
496: .append(CmsDirectEditButtonSelection.VALUE_NEW)
497: .append("\', \'")
498: .append(editNewLink)
499: .append(
500: "\');\" class=\"ocms_button\"><span class=\"ocms_over\" onmouseover=\"className=\'ocms_over\'\" onmouseout=\"className=\'ocms_over\'\" onmousedown=\"className=\'ocms_push\'\" onmouseup=\"className=\'ocms_over\'\">");
501: if (m_editButtonStyle == 1) {
502: result
503: .append("<span id=\"new_")
504: .append(editId)
505: .append(
506: "\" class=\"ocms_combobutton\" style=\"background-image: url(\'")
507: .append(CmsWorkplace.getSkinUri())
508: .append("buttons/new.png\');\"> ")
509: .append(
510: m_messages
511: .key(Messages.GUI_BUTTON_NEW_0))
512: .append("</span>");
513: } else if (m_editButtonStyle == 2) {
514: result
515: .append(
516: "<span class=\"ocms_combobutton\" style=\"padding-left: 4px;\">")
517: .append(
518: m_messages
519: .key(Messages.GUI_BUTTON_NEW_0))
520: .append("</span>");
521: } else {
522: result.append("<img border=\"0\" src=\"").append(
523: CmsWorkplace.getSkinUri()).append(
524: "buttons/new.png\" title=\"").append(
525: m_messages.key(Messages.GUI_BUTTON_NEW_0))
526: .append("\" alt=\"\"/>");
527: }
528: result.append("</span></a></td>");
529: }
530: result.append("</tr></table>\n");
531: result.append("</div>\n");
532: result.append("<div id=\"").append(editId).append(
533: "\" class=\"ocms_de_norm\">");
534: return result.toString();
535: }
536: }
|