KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > comparison > A_CmsDiffViewDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/comparison/A_CmsDiffViewDialog.java,v $
3  * Date : $Date: 2006/03/28 18:46:30 $
4  * Version: $Revision: 1.4 $
5  *
6  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
7  * All rights reserved.
8  *
9  * This source code is the intellectual property of Alkacon Software GmbH.
10  * It is PROPRIETARY and CONFIDENTIAL.
11  * Use of this source code is subject to license terms.
12  *
13  * In order to use this source code, you need written permission from
14  * Alkacon Software GmbH. Redistribution of this source code, in modified
15  * or unmodified form, is not allowed unless written permission by
16  * Alkacon Software GmbH has been given.
17  *
18  * ALKACON SOFTWARE GMBH MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
19  * OF THIS SOURCE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
20  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21  * PURPOSE, OR NON-INFRINGEMENT. ALKACON SOFTWARE GMBH SHALL NOT BE LIABLE FOR ANY
22  * DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
23  * THIS SOURCE CODE OR ITS DERIVATIVES.
24  *
25  * For further information about Alkacon Software GmbH, please see the
26  * company website: http://www.alkacon.com
27  */

28
29 package org.opencms.workplace.comparison;
30
31 import com.alkacon.diff.Diff;
32
33 import org.opencms.jsp.CmsJspActionElement;
34 import org.opencms.util.CmsStringUtil;
35 import org.opencms.workplace.CmsDialog;
36 import org.opencms.workplace.CmsWorkplace;
37 import org.opencms.workplace.CmsWorkplaceSettings;
38 import org.opencms.workplace.list.A_CmsListDialog;
39 import org.opencms.workplace.tools.A_CmsHtmlIconButton;
40 import org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum;
41
42 import java.io.BufferedReader JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.StringReader JavaDoc;
45
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47 import javax.servlet.jsp.JspException JavaDoc;
48 import javax.servlet.jsp.JspWriter JavaDoc;
49
50 /**
51  * Provides a GUI for the configuration file comparison dialog.<p>
52  *
53  * @author Michael Moossen
54  * @author Jan Baudisch
55  *
56  * @version $Revision: 1.4 $
57  *
58  * @since 6.0.0
59  */

60 public abstract class A_CmsDiffViewDialog extends CmsDialog {
61
62     /** Diff mode. */
63     private CmsDiffViewMode m_mode;
64
65     /**
66      * Default constructor.<p>
67      *
68      * @param jsp an initialized JSP action element
69      */

70     protected A_CmsDiffViewDialog(CmsJspActionElement jsp) {
71
72         super(jsp);
73         setParamStyle(STYLE_NEW);
74     }
75
76     /**
77      * Performs the dialog actions depending on the initialized action and displays the dialog form.<p>
78      *
79      * @throws Exception if writing to the JSP out fails
80      */

81     public void displayDialog() throws Exception JavaDoc {
82
83         if (getAction() == ACTION_CANCEL) {
84             actionCloseDialog();
85         }
86         JspWriter JavaDoc out = getJsp().getJspContext().getOut();
87         out.print("<link rel='stylesheet' type='text/css' HREF='");
88         out.print(getStyleUri(getJsp()));
89         out.println("diff.css'>");
90         out.println(dialogContentStart(getParamTitle()));
91         out.print("<form name='diff-form' method='post' action='");
92         out.print(getDialogUri());
93         out.println("'>");
94         out.println(allParamsAsHidden());
95         out.println("</form>");
96         // icon is displayed on the right in order that the user needs not scroll to the icon for long lines
97
out.println("<p>");
98         out.println(getDiffOnlyButtonsHtml());
99         out.println("</p>");
100         out.println(dialogBlockStart(null));
101         out.println("<table cellspacing='0' cellpadding='0' class='xmlTable'>\n<tr><td><pre style='overflow:auto'>");
102         try {
103             CmsHtmlDifferenceConfiguration conf = new CmsHtmlDifferenceConfiguration(
104                 getMode() == CmsDiffViewMode.ALL ? -1 : getLinesBeforeSkip(),
105                 getLocale());
106             String JavaDoc diff = Diff.diffAsHtml(getOriginalSource(), getCopySource(), conf);
107             if (CmsStringUtil.isNotEmpty(diff)) {
108                 out.println(diff);
109             } else {
110                 // print original source, if there are no differences
111
out.println(wrapLinesWithUnchangedStyle(CmsStringUtil.substitute(CmsStringUtil.escapeHtml(getOriginalSource()), "<br/>", "")));
112             }
113         } catch (Exception JavaDoc e) {
114             out.print(e);
115         }
116         out.println("</pre></td></tr>\n</table>");
117         out.println(dialogBlockEnd());
118         out.println(dialogContentEnd());
119         out.println(dialogEnd());
120         out.println(bodyEnd());
121         out.println(htmlEnd());
122     }
123
124     /**
125      * Returns the html code for the buttons 'show only differences' and 'show everything'.<p>
126      *
127      * @return the html code for the buttons 'show only differences' and 'show everything'
128      */

129     String JavaDoc getDiffOnlyButtonsHtml() {
130
131         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
132         if (!getOriginalSource().equals(getCopySource())) {
133             String JavaDoc onClick1 = "javascript:document.forms['diff-form'].mode.value = '";
134             String JavaDoc onClick2 = "javascript:document.forms['diff-form'].mode.value = '";
135             onClick1 += CmsDiffViewMode.ALL;
136             onClick2 += CmsDiffViewMode.DIFF_ONLY;
137             onClick1 += "'; document.forms['diff-form'].submit();";
138             onClick2 += "'; document.forms['diff-form'].submit();";
139             result.append(getTwoButtonsHtml(CmsDiffViewMode.DIFF_ONLY.getName().key(getLocale()),
140                 CmsDiffViewMode.ALL.getName().key(getLocale()), onClick1, onClick2,
141                 getMode() == CmsDiffViewMode.DIFF_ONLY));
142         } else {
143             // display all text, if there are no differences
144
setMode(CmsDiffViewMode.ALL);
145         }
146         return result.toString();
147     }
148     
149     /**
150      * Returns the html for two buttons, whereby the third parameter determines which one is active.<p>
151      *
152      * @param label1 the label for the first button
153      * @param label2 the label for the second button
154      * @param firstActive a flag indicating wheter the first or second button is active
155      * @param onClick1 the action to be performed if the first button is clicked
156      * @param onClick2 the action to be performed if the second button is clicked
157      *
158      * @return the html for two buttons, whereby the third parameter determines which one is active
159      */

160     public String JavaDoc getTwoButtonsHtml(String JavaDoc label1, String JavaDoc label2, String JavaDoc onClick1, String JavaDoc onClick2, boolean firstActive) {
161         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
162         if (firstActive) {
163             result.append(A_CmsHtmlIconButton.defaultButtonHtml(
164                 getJsp(),
165                 CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
166                 "id",
167                 label1,
168                 null,
169                 true,
170                 A_CmsListDialog.ICON_DETAILS_SHOW,
171                 null,
172                 onClick1));
173             result.append("&nbsp;&nbsp;");
174             result.append(deactivatedEmphasizedButtonHtml(
175                 label2,
176                 A_CmsListDialog.ICON_DETAILS_HIDE));
177         } else {
178               
179             result.append(deactivatedEmphasizedButtonHtml(
180                 label1,
181                 A_CmsListDialog.ICON_DETAILS_HIDE));
182             result.append("&nbsp;&nbsp;");
183             result.append(A_CmsHtmlIconButton.defaultButtonHtml(
184                 getJsp(),
185                 CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
186                 "id",
187                 label2,
188                 null,
189                 true,
190                 A_CmsListDialog.ICON_DETAILS_SHOW,
191                 null,
192                 onClick2));
193         }
194         result.append("&nbsp;&nbsp;");
195         return result.toString();
196     }
197     
198     /**
199      * Returns the html code for a deactivated empfasized button.<p>
200      *
201      * @param name the label of the button
202      * @param iconPath the path to the icon
203      *
204      * @return the html code for a deactivated empfasized button
205      */

206     public String JavaDoc deactivatedEmphasizedButtonHtml(String JavaDoc name, String JavaDoc iconPath) {
207         
208         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
209         result.append("<span style='vertical-align:middle;'><img style='width:20px;height:20px;display:inline;vertical-align:middle;text-decoration:none;' SRC=\'");
210         result.append(CmsWorkplace.getSkinUri());
211         result.append(iconPath);
212         result.append("\' alt=\'");
213         result.append(name);
214         result.append("\' title=\'");
215         result.append(name);
216         result.append("\'>&nbsp;<b>");
217         result.append(name);
218         result.append("</b></span>");
219         return result.toString();
220     }
221     
222     /**
223      * Returns the mode.<p>
224      *
225      * @return the mode
226      */

227     public CmsDiffViewMode getMode() {
228
229         return m_mode;
230     }
231
232     /**
233      * Returns the parameter value for the Mode.<p>
234      *
235      * @return the parameter value for the Mode
236      */

237     public String JavaDoc getParamMode() {
238
239         if (m_mode == null) {
240             return null;
241         }
242         return m_mode.getMode();
243     }
244
245     /**
246      * Sets the mode.<p>
247      *
248      * @param mode the mode to set
249      */

250     public void setMode(CmsDiffViewMode mode) {
251
252         m_mode = mode;
253     }
254
255     /**
256      * Sets the parameter value for the Mode.<p>
257      *
258      * @param mode the parameter value for the Mode to set
259      */

260     public void setParamMode(String JavaDoc mode) {
261
262         m_mode = CmsDiffViewMode.valueOf(mode);
263     }
264
265     /**
266      * Returns the text to compare as copy.<p>
267      *
268      * @return the text to compare as copy
269      */

270     protected abstract String JavaDoc getCopySource();
271
272     /**
273      * Returns the number of lines to show before they are skipped.<p>
274      *
275      * @return the number of lines to show before they are skipped
276      */

277     protected abstract int getLinesBeforeSkip();
278
279     /**
280      * Returns the text to compare as original.<p>
281      *
282      * @return the text to compare as original
283      */

284     protected abstract String JavaDoc getOriginalSource();
285
286     /**
287      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
288      */

289     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
290
291         super.initWorkplaceRequestValues(settings, request);
292         if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamMode())) {
293             // ensure a valid mode is set
294
m_mode = CmsDiffViewMode.DIFF_ONLY;
295         }
296         // test the needed parameters
297
try {
298             validateParamaters();
299         } catch (Exception JavaDoc e) {
300             // close if parameters not available
301
setAction(ACTION_CANCEL);
302             try {
303                 actionCloseDialog();
304             } catch (JspException JavaDoc e1) {
305                 // noop
306
}
307             return;
308         }
309     }
310
311     /**
312      * Validates the parameters.<p>
313      *
314      * @throws Exception if something goes wrong
315      */

316     protected abstract void validateParamaters() throws Exception JavaDoc;
317
318     /**
319      *
320      * Returns a diff text wrapped with formatting style.<p>
321      *
322      * @param diff the text to wrap with CSS formatting
323      * @return the text with formatting styles wrapped
324      * @throws IOException if something goes wrong
325      */

326     protected String JavaDoc wrapLinesWithUnchangedStyle(String JavaDoc diff) throws IOException JavaDoc {
327
328         String JavaDoc line;
329         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
330         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new StringReader JavaDoc(diff));
331         while ((line = br.readLine()) != null) {
332             if ("".equals(line.trim())) {
333                 line = "&nbsp;";
334             }
335             result.append("<div class=\"df-unc\"><span class=\"df-unc\">").append(line).append("</span></div>\n");
336         }
337         return result.toString();
338     }
339
340 }
Popular Tags