/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE
* or https://OpenDS.dev.java.net/OpenDS.LICENSE.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Portions Copyright 2006-2007 Sun Microsystems, Inc.
*/
package org.opends.quicksetup.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import org.opends.quicksetup.i18n.ResourceProvider;
import org.opends.quicksetup.ui.UIFactory;
/**
* This is an implementation of the ProgressMessageFormatter class that
* provides format in HTML.
*
*/
public class HtmlProgressMessageFormatter implements ProgressMessageFormatter
{
private String doneHtml;
/**
* The line break in HTML.
*/
private static String LINE_BREAK = "
";
/**
* The constant used to separate parameters in an URL.
*/
private String PARAM_SEPARATOR = "&&&&";
/**
* The space in HTML.
*/
private static String SPACE = " ";
/**
* Returns the HTML representation of the text without providing any style.
* @param text the source text from which we want to get the HTML
* representation
* @return the HTML representation for the given text.
*/
public String getFormattedText(String text)
{
return getHtml(text);
}
/**
* Returns the HTML representation of the text that is the summary of the
* installation process (the one that goes in the UI next to the progress
* bar).
* @param text the source text from which we want to get the formatted
* representation
* @return the HTML representation of the summary for the given text.
*/
public String getFormattedSummary(String text)
{
return ""+UIFactory.applyFontToHtml(text, UIFactory.PROGRESS_FONT);
}
/**
* Returns the HTML representation of an error for a given text.
* @param text the source text from which we want to get the HTML
* representation
* @param applyMargin specifies whether we apply a margin or not to the
* resulting HTML.
* @return the HTML representation of an error for the given text.
*/
public String getFormattedError(String text, boolean applyMargin)
{
String html =
UIFactory.getIconHtml(UIFactory.IconType.ERROR_LARGE)
+ SPACE
+ SPACE
+ UIFactory.applyFontToHtml(getHtml(text),
UIFactory.PROGRESS_ERROR_FONT);
String result = UIFactory.applyErrorBackgroundToHtml(html);
if (applyMargin)
{
result =
UIFactory.applyMargin(result,
UIFactory.TOP_INSET_ERROR_MESSAGE, 0, 0, 0);
}
return result;
}
/**
* Returns the HTML representation of a warning for a given text.
* @param text the source text from which we want to get the HTML
* representation
* @param applyMargin specifies whether we apply a margin or not to the
* resulting HTML.
* @return the HTML representation of a warning for the given text.
*/
public String getFormattedWarning(String text, boolean applyMargin)
{
String html =
UIFactory.getIconHtml(UIFactory.IconType.WARNING_LARGE)
+ SPACE
+ SPACE
+ UIFactory.applyFontToHtml(getHtml(text),
UIFactory.PROGRESS_WARNING_FONT);
String result = UIFactory.applyWarningBackgroundToHtml(html);
if (applyMargin)
{
result =
UIFactory.applyMargin(result,
UIFactory.TOP_INSET_ERROR_MESSAGE, 0, 0, 0);
}
return result;
}
/**
* Returns the HTML representation of a success message for a given text.
* @param text the source text from which we want to get the HTML
* representation
* @return the HTML representation of a success message for the given text.
*/
public String getFormattedSuccess(String text)
{
// Note: the text we get already is in HTML form
String html =
UIFactory.getIconHtml(UIFactory.IconType.INFORMATION_LARGE) + SPACE
+ SPACE + UIFactory.applyFontToHtml(text, UIFactory.PROGRESS_FONT);
String result = UIFactory.applySuccessfulBackgroundToHtml(html);
return result;
}
/**
* Returns the HTML representation of a log error message for a given
* text.
* @param text the source text from which we want to get the HTML
* representation
* @return the HTML representation of a log error message for the given
* text.
*/
public String getFormattedLogError(String text)
{
String html = getHtml(text);
return UIFactory.applyFontToHtml(html,
UIFactory.PROGRESS_LOG_ERROR_FONT);
}
/**
* Returns the HTML representation of a log message for a given text.
* @param text the source text from which we want to get the HTML
* representation
* @return the HTML representation of a log message for the given text.
*/
public String getFormattedLog(String text)
{
String html = getHtml(text);
return UIFactory.applyFontToHtml(html, UIFactory.PROGRESS_LOG_FONT);
}
/**
* Returns the HTML representation of the 'Done' text string.
* @return the HTML representation of the 'Done' text string.
*/
public String getFormattedDone()
{
if (doneHtml == null)
{
String html = getHtml(getMsg("progress-done"));
doneHtml = UIFactory.applyFontToHtml(html,
UIFactory.PROGRESS_DONE_FONT);
}
return doneHtml;
}
/**
* Returns the HTML representation of the argument text to which we add
* points. For instance if we pass as argument 'Configuring Server' the
* return value will be 'Configuring Server .....'.
* @param text the String to which add points.
* @return the HTML representation of the '.....' text string.
*/
public String getFormattedWithPoints(String text)
{
String html = getHtml(text);
String points = SPACE + getHtml(getMsg("progress-points")) + SPACE;
StringBuffer buf = new StringBuffer();
buf.append(UIFactory.applyFontToHtml(html, UIFactory.PROGRESS_FONT))
.append(
UIFactory.applyFontToHtml(points, UIFactory.PROGRESS_POINTS_FONT));
return buf.toString();
}
/**
* Returns the formatted representation of a progress message for a given
* text.
* @param text the source text from which we want to get the formatted
* representation
* @return the formatted representation of a progress message for the given
* text.
*/
public String getFormattedProgress(String text)
{
return UIFactory.applyFontToHtml(getHtml(text),
UIFactory.PROGRESS_FONT);
}
/**
* Returns the HTML representation of an error message for a given exception.
* This method applies a margin if the applyMargin parameter is
* true.
* @param ex the exception.
* @param applyMargin specifies whether we apply a margin or not to the
* resulting HTML.
* @return the HTML representation of an error message for the given
* exception.
*/
public String getFormattedError(Exception ex, boolean applyMargin)
{
String openDiv = "
true the resulting HTML will display
* the stack.
* @return the HTML representation of an exception in the progress log for a
* given url.
*/
private String getErrorWithStackHtml(String url, boolean inverse)
{
String p = url.substring("http://".length());
try
{
p = URLDecoder.decode(p, "UTF-8");
} catch (UnsupportedEncodingException uee)
{
// Bug
throw new IllegalStateException("UTF-8 is not supported ", uee);
}
String params[] = p.split(PARAM_SEPARATOR);
int i = 0;
String openDiv = params[i++];
String hideText = params[i++];
String showText = params[i++];
String stackText = params[i++];
String closeDiv = params[i++];
boolean isHide = new Boolean(params[i]);
if (isHide)
{
return getErrorWithStackHtml(openDiv, hideText, showText, stackText,
closeDiv, !inverse);
} else
{
return getErrorWithStackHtml(openDiv, hideText, showText, stackText,
closeDiv, inverse);
}
}
}