001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/comparison/A_CmsDiffViewDialog.java,v $
003: * Date : $Date: 2008-02-27 12:05:55 $
004: * Version: $Revision: 1.7 $
005: *
006: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
007: * All rights reserved.
008: *
009: * This source code is the intellectual property of Alkacon Software GmbH.
010: * It is PROPRIETARY and CONFIDENTIAL.
011: * Use of this source code is subject to license terms.
012: *
013: * In order to use this source code, you need written permission from
014: * Alkacon Software GmbH. Redistribution of this source code, in modified
015: * or unmodified form, is not allowed unless written permission by
016: * Alkacon Software GmbH has been given.
017: *
018: * ALKACON SOFTWARE GMBH MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
019: * OF THIS SOURCE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
020: * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
021: * PURPOSE, OR NON-INFRINGEMENT. ALKACON SOFTWARE GMBH SHALL NOT BE LIABLE FOR ANY
022: * DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
023: * THIS SOURCE CODE OR ITS DERIVATIVES.
024: *
025: * For further information about Alkacon Software GmbH, please see the
026: * company website: http://www.alkacon.com
027: */
028:
029: package org.opencms.workplace.comparison;
030:
031: import com.alkacon.diff.Diff;
032:
033: import org.opencms.jsp.CmsJspActionElement;
034: import org.opencms.util.CmsStringUtil;
035: import org.opencms.workplace.CmsDialog;
036: import org.opencms.workplace.CmsWorkplace;
037: import org.opencms.workplace.CmsWorkplaceSettings;
038: import org.opencms.workplace.list.A_CmsListDialog;
039: import org.opencms.workplace.tools.A_CmsHtmlIconButton;
040: import org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum;
041:
042: import java.io.BufferedReader;
043: import java.io.IOException;
044: import java.io.StringReader;
045:
046: import javax.servlet.http.HttpServletRequest;
047: import javax.servlet.jsp.JspException;
048: import javax.servlet.jsp.JspWriter;
049:
050: /**
051: * Provides a GUI for the configuration file comparison dialog.<p>
052: *
053: * @author Michael Moossen
054: * @author Jan Baudisch
055: *
056: * @version $Revision: 1.7 $
057: *
058: * @since 6.0.0
059: */
060: public abstract class A_CmsDiffViewDialog extends CmsDialog {
061:
062: /** Diff mode. */
063: private CmsDiffViewMode m_mode;
064:
065: /**
066: * Default constructor.<p>
067: *
068: * @param jsp an initialized JSP action element
069: */
070: protected A_CmsDiffViewDialog(CmsJspActionElement jsp) {
071:
072: super (jsp);
073: setParamStyle(STYLE_NEW);
074: }
075:
076: /**
077: * Performs the dialog actions depending on the initialized action and displays the dialog form.<p>
078: *
079: * @throws Exception if writing to the JSP out fails
080: */
081: public void displayDialog() throws Exception {
082:
083: if (getAction() == ACTION_CANCEL) {
084: actionCloseDialog();
085: }
086: JspWriter out = getJsp().getJspContext().getOut();
087: out.print("<link rel='stylesheet' type='text/css' href='");
088: out.print(getStyleUri(getJsp()));
089: out.println("diff.css'>");
090: out.println(dialogContentStart(getParamTitle()));
091: out.print("<form name='diff-form' method='post' action='");
092: out.print(getDialogUri());
093: out.println("'>");
094: out.println(allParamsAsHidden());
095: out.println("</form>");
096: // icon is displayed on the right in order that the user needs not scroll to the icon for long lines
097: out.println("<p>");
098: out.println(getDiffOnlyButtonsHtml());
099: out.println("</p>");
100: out.println(dialogBlockStart(null));
101: out
102: .println("<table cellspacing='0' cellpadding='0' class='xmlTable'>\n<tr><td><pre style='overflow:auto'>");
103: try {
104: CmsHtmlDifferenceConfiguration conf = new CmsHtmlDifferenceConfiguration(
105: getMode() == CmsDiffViewMode.ALL ? -1
106: : getLinesBeforeSkip(), getLocale());
107: String diff = Diff.diffAsHtml(getOriginalSource(),
108: getCopySource(), conf);
109: if (CmsStringUtil.isNotEmpty(diff)) {
110: out.println(diff);
111: } else {
112: // print original source, if there are no differences
113: out.println(wrapLinesWithUnchangedStyle(CmsStringUtil
114: .substitute(CmsStringUtil
115: .escapeHtml(getOriginalSource()),
116: "<br/>", "")));
117: }
118: } catch (Exception e) {
119: out.print(e);
120: }
121: out.println("</pre></td></tr>\n</table>");
122: out.println(dialogBlockEnd());
123: out.println(dialogContentEnd());
124: out.println(dialogEnd());
125: out.println(bodyEnd());
126: out.println(htmlEnd());
127: }
128:
129: /**
130: * Returns the html code for the buttons 'show only differences' and 'show everything'.<p>
131: *
132: * @return the html code for the buttons 'show only differences' and 'show everything'
133: */
134: String getDiffOnlyButtonsHtml() {
135:
136: StringBuffer result = new StringBuffer();
137: if (!getOriginalSource().equals(getCopySource())) {
138: String onClick1 = "javascript:document.forms['diff-form'].mode.value = '";
139: String onClick2 = "javascript:document.forms['diff-form'].mode.value = '";
140: onClick1 += CmsDiffViewMode.ALL;
141: onClick2 += CmsDiffViewMode.DIFF_ONLY;
142: onClick1 += "'; document.forms['diff-form'].submit();";
143: onClick2 += "'; document.forms['diff-form'].submit();";
144: result.append(getTwoButtonsHtml(CmsDiffViewMode.DIFF_ONLY
145: .getName().key(getLocale()), CmsDiffViewMode.ALL
146: .getName().key(getLocale()), onClick1, onClick2,
147: getMode() == CmsDiffViewMode.DIFF_ONLY));
148: } else {
149: // display all text, if there are no differences
150: setMode(CmsDiffViewMode.ALL);
151: }
152: return result.toString();
153: }
154:
155: /**
156: * Returns the html for two buttons, whereby the third parameter determines which one is active.<p>
157: *
158: * @param label1 the label for the first button
159: * @param label2 the label for the second button
160: * @param firstActive a flag indicating wheter the first or second button is active
161: * @param onClick1 the action to be performed if the first button is clicked
162: * @param onClick2 the action to be performed if the second button is clicked
163: *
164: * @return the html for two buttons, whereby the third parameter determines which one is active
165: */
166: public String getTwoButtonsHtml(String label1, String label2,
167: String onClick1, String onClick2, boolean firstActive) {
168:
169: StringBuffer result = new StringBuffer();
170: if (firstActive) {
171: result.append(A_CmsHtmlIconButton.defaultButtonHtml(
172: CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT, "id",
173: label1, null, true,
174: A_CmsListDialog.ICON_DETAILS_SHOW, null, onClick1));
175: result.append(" ");
176: result.append(deactivatedEmphasizedButtonHtml(label2,
177: A_CmsListDialog.ICON_DETAILS_HIDE));
178: } else {
179:
180: result.append(deactivatedEmphasizedButtonHtml(label1,
181: A_CmsListDialog.ICON_DETAILS_HIDE));
182: result.append(" ");
183: result.append(A_CmsHtmlIconButton.defaultButtonHtml(
184: CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT, "id",
185: label2, null, true,
186: A_CmsListDialog.ICON_DETAILS_SHOW, null, onClick2));
187: }
188: result.append(" ");
189: return result.toString();
190: }
191:
192: /**
193: * Returns the html code for a deactivated empfasized button.<p>
194: *
195: * @param name the label of the button
196: * @param iconPath the path to the icon
197: *
198: * @return the html code for a deactivated empfasized button
199: */
200: public String deactivatedEmphasizedButtonHtml(String name,
201: String iconPath) {
202:
203: StringBuffer result = new StringBuffer();
204: result
205: .append("<span style='vertical-align:middle;'><img style='width:20px;height:20px;display:inline;vertical-align:middle;text-decoration:none;' src=\'");
206: result.append(CmsWorkplace.getSkinUri());
207: result.append(iconPath);
208: result.append("\' alt=\'");
209: result.append(name);
210: result.append("\' title=\'");
211: result.append(name);
212: result.append("\'> <b>");
213: result.append(name);
214: result.append("</b></span>");
215: return result.toString();
216: }
217:
218: /**
219: * Returns the mode.<p>
220: *
221: * @return the mode
222: */
223: public CmsDiffViewMode getMode() {
224:
225: return m_mode;
226: }
227:
228: /**
229: * Returns the parameter value for the Mode.<p>
230: *
231: * @return the parameter value for the Mode
232: */
233: public String getParamMode() {
234:
235: if (m_mode == null) {
236: return null;
237: }
238: return m_mode.getMode();
239: }
240:
241: /**
242: * Sets the mode.<p>
243: *
244: * @param mode the mode to set
245: */
246: public void setMode(CmsDiffViewMode mode) {
247:
248: m_mode = mode;
249: }
250:
251: /**
252: * Sets the parameter value for the Mode.<p>
253: *
254: * @param mode the parameter value for the Mode to set
255: */
256: public void setParamMode(String mode) {
257:
258: m_mode = CmsDiffViewMode.valueOf(mode);
259: }
260:
261: /**
262: * Returns the text to compare as copy.<p>
263: *
264: * @return the text to compare as copy
265: */
266: protected abstract String getCopySource();
267:
268: /**
269: * Returns the number of lines to show before they are skipped.<p>
270: *
271: * @return the number of lines to show before they are skipped
272: */
273: protected abstract int getLinesBeforeSkip();
274:
275: /**
276: * Returns the text to compare as original.<p>
277: *
278: * @return the text to compare as original
279: */
280: protected abstract String getOriginalSource();
281:
282: /**
283: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
284: */
285: protected void initWorkplaceRequestValues(
286: CmsWorkplaceSettings settings, HttpServletRequest request) {
287:
288: super .initWorkplaceRequestValues(settings, request);
289: if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamMode())) {
290: // ensure a valid mode is set
291: m_mode = CmsDiffViewMode.DIFF_ONLY;
292: }
293: // test the needed parameters
294: try {
295: validateParamaters();
296: } catch (Exception e) {
297: // close if parameters not available
298: setAction(ACTION_CANCEL);
299: try {
300: actionCloseDialog();
301: } catch (JspException e1) {
302: // noop
303: }
304: return;
305: }
306: }
307:
308: /**
309: * Validates the parameters.<p>
310: *
311: * @throws Exception if something goes wrong
312: */
313: protected abstract void validateParamaters() throws Exception;
314:
315: /**
316: *
317: * Returns a diff text wrapped with formatting style.<p>
318: *
319: * @param diff the text to wrap with CSS formatting
320: * @return the text with formatting styles wrapped
321: * @throws IOException if something goes wrong
322: */
323: protected String wrapLinesWithUnchangedStyle(String diff)
324: throws IOException {
325:
326: String line;
327: StringBuffer result = new StringBuffer();
328: BufferedReader br = new BufferedReader(new StringReader(diff));
329: while ((line = br.readLine()) != null) {
330: if ("".equals(line.trim())) {
331: line = " ";
332: }
333: result.append(
334: "<div class=\"df-unc\"><span class=\"df-unc\">")
335: .append(line).append("</span></div>\n");
336: }
337: return result.toString();
338: }
339:
340: }
|