001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2015 ForgeRock AS. 016 */ 017package org.opends.quicksetup.util; 018 019import org.forgerock.i18n.LocalizableMessage; 020import org.forgerock.i18n.LocalizableMessageBuilder; 021 022import org.opends.quicksetup.Constants; 023 024import static org.opends.messages.QuickSetupMessages.*; 025 026/** 027 * This is an implementation of the ProgressMessageFormatter class that 028 * provides format in plain text. 029 */ 030public class PlainTextProgressMessageFormatter 031implements ProgressMessageFormatter 032{ 033 private LocalizableMessage doneText; 034 private LocalizableMessage errorText; 035 036 /** 037 * The space in plain text. 038 */ 039 private static String SPACE = " "; 040 041 /** 042 * Returns the text representation of the text without providing any style. 043 * @param text the source text from which we want to get the text 044 * representation 045 * @return the text representation for the given text. 046 */ 047 public LocalizableMessage getFormattedText(LocalizableMessage text) 048 { 049 return text; 050 } 051 052 /** 053 * Returns the plain text representation of the text that is the summary of 054 * the installation process (the one that goes in the UI next to the progress 055 * bar). 056 * @param text the source text from which we want to get the formatted 057 * representation 058 * @return the text representation of the summary for the given text. 059 */ 060 public LocalizableMessage getFormattedSummary(LocalizableMessage text) 061 { 062 return text; 063 } 064 065 /** 066 * Returns the plain text representation of an error for a given text. 067 * @param text the source text from which we want to get the plain text 068 * representation 069 * @param applyMargin specifies whether we apply a margin or not to the 070 * resulting formatted text. 071 * @return the plain text representation of an error for the given text. 072 */ 073 public LocalizableMessage getFormattedError(LocalizableMessage text, boolean applyMargin) 074 { 075 LocalizableMessage result; 076 if (applyMargin) 077 { 078 result = new LocalizableMessageBuilder().append(Constants.LINE_SEPARATOR) 079 .append(text).toMessage(); 080 } else 081 { 082 result = text; 083 } 084 return result; 085 } 086 087 /** 088 * Returns the plain text representation of a warning for a given text. 089 * @param text the source text from which we want to get the plain text 090 * representation 091 * @param applyMargin specifies whether we apply a margin or not to the 092 * resulting formatted text. 093 * @return the plain text representation of a warning for the given text. 094 */ 095 public LocalizableMessage getFormattedWarning(LocalizableMessage text, boolean applyMargin) 096 { 097 LocalizableMessage result; 098 if (applyMargin) 099 { 100 result = new LocalizableMessageBuilder(Constants.LINE_SEPARATOR) 101 .append(text).toMessage(); 102 } else 103 { 104 result = text; 105 } 106 return result; 107 } 108 109 /** 110 * Returns the plain text representation of a success message for a given 111 * text. 112 * @param text the source text from which we want to get the plain text 113 * representation 114 * @return the plain text representation of a success message for the given 115 * text. 116 */ 117 public LocalizableMessage getFormattedSuccess(LocalizableMessage text) 118 { 119 return text; 120 } 121 122 /** 123 * Returns the plain text representation of a log error message for a given 124 * text. 125 * @param text the source text from which we want to get the plain text 126 * representation 127 * @return the plain text representation of a log error message for the given 128 * text. 129 */ 130 public LocalizableMessage getFormattedLogError(LocalizableMessage text) 131 { 132 return text; 133 } 134 135 136 /** 137 * Returns the plain text representation of a log message for a given text. 138 * @param text the source text from which we want to get the plain text 139 * representation 140 * @return the plain text representation of a log message for the given text. 141 */ 142 public LocalizableMessage getFormattedLog(LocalizableMessage text) 143 { 144 return text; 145 } 146 147 /** 148 * Returns the plain text representation of the 'Done' text string. 149 * @return the plain text representation of the 'Done' text string. 150 */ 151 public LocalizableMessage getFormattedDone() 152 { 153 if (doneText == null) 154 { 155 doneText = INFO_PROGRESS_DONE.get(); 156 } 157 return doneText; 158 } 159 160 /** 161 * Returns the plain text representation of the 'Error' text string. 162 * @return the plain text representation of the 'Error' text string. 163 */ 164 public LocalizableMessage getFormattedError() 165 { 166 if (errorText == null) 167 { 168 errorText = INFO_PROGRESS_ERROR.get(); 169 } 170 return errorText; 171 } 172 173 /** 174 * Returns the plain text representation of the argument text to which we add 175 * points. For instance if we pass as argument 'Configuring Server' the 176 * return value will be 'Configuring Server .....'. 177 * @param text the String to which add points. 178 * @return the plain text representation of the '.....' text string. 179 */ 180 public LocalizableMessage getFormattedWithPoints(LocalizableMessage text) 181 { 182 return new LocalizableMessageBuilder(text).append(SPACE) 183 .append(INFO_PROGRESS_POINTS.get()).append(SPACE).toMessage(); 184 } 185 186 /** 187 * Returns the formatted representation of a point. 188 * @return the formatted representation of the '.' text string. 189 */ 190 public LocalizableMessage getFormattedPoint() 191 { 192 return LocalizableMessage.raw("."); 193 } 194 195 /** 196 * Returns the formatted representation of a space. 197 * @return the formatted representation of the ' ' text string. 198 */ 199 public LocalizableMessage getSpace() 200 { 201 return LocalizableMessage.raw(SPACE); 202 } 203 204 /** 205 * Returns the formatted representation of a progress message for a given 206 * text. 207 * @param text the source text from which we want to get the formatted 208 * representation 209 * @return the formatted representation of a progress message for the given 210 * text. 211 */ 212 public LocalizableMessage getFormattedProgress(LocalizableMessage text) 213 { 214 return text; 215 } 216 217 /** 218 * Returns the plain text representation of an error message for a given 219 * throwable. 220 * This method applies a margin if the applyMargin parameter is 221 * <CODE>true</CODE>. 222 * @param t the exception. 223 * @param applyMargin specifies whether we apply a margin or not to the 224 * resulting plain text. 225 * @return the plain text representation of an error message for the given 226 * exception. 227 */ 228 public LocalizableMessage getFormattedError(Throwable t, boolean applyMargin) 229 { 230 String msg = t.getMessage(); 231 if (msg == null) 232 { 233 msg = t.toString(); 234 } 235 String result; 236 if (applyMargin) 237 { 238 result = Constants.LINE_SEPARATOR+msg; 239 } else 240 { 241 result = msg; 242 } 243 return LocalizableMessage.raw(result); 244 } 245 246 /** 247 * Returns the line break in plain text. 248 * @return the line break in plain text. 249 */ 250 public LocalizableMessage getLineBreak() 251 { 252 return LocalizableMessage.raw(Constants.LINE_SEPARATOR); 253 } 254 255 /** 256 * Returns the tab in plain text. 257 * @return the tab in plain text. 258 */ 259 public LocalizableMessage getTab() 260 { 261 return LocalizableMessage.raw(" "); 262 } 263 264 /** 265 * Returns the task separator in plain text. 266 * @return the task separator in plain text. 267 */ 268 public LocalizableMessage getTaskSeparator() 269 { 270 return LocalizableMessage.raw( 271 Constants.LINE_SEPARATOR+ 272 "-----------------------------------------------------------------"+ 273 Constants.LINE_SEPARATOR+Constants.LINE_SEPARATOR); 274 } 275 276 /** {@inheritDoc} */ 277 public LocalizableMessage getFormattedAfterUrlClick(String url, LocalizableMessage lastText) 278 { 279 throw new IllegalStateException( 280 "PlainTextProgressMessageFormatter.getFormattedAfterUrlClick must not "+ 281 "be called"); 282 } 283 284}