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