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-2016 ForgeRock AS.
016 */
017package org.opends.quicksetup.util;
018
019import static org.opends.messages.QuickSetupMessages.*;
020
021import org.forgerock.i18n.LocalizableMessage;
022import org.forgerock.i18n.LocalizableMessageBuilder;
023import org.opends.quicksetup.Constants;
024
025/**
026 * This is an implementation of the ProgressMessageFormatter class that
027 * provides format in plain text.
028 */
029public class PlainTextProgressMessageFormatter implements ProgressMessageFormatter
030{
031  private LocalizableMessage doneText;
032  private LocalizableMessage errorText;
033
034  /** The space in plain text. */
035  private static String SPACE = " ";
036
037  @Override
038  public LocalizableMessage getFormattedText(LocalizableMessage text)
039  {
040    return text;
041  }
042
043  @Override
044  public LocalizableMessage getFormattedSummary(LocalizableMessage text)
045  {
046    return text;
047  }
048
049  @Override
050  public LocalizableMessage getFormattedError(LocalizableMessage text, boolean applyMargin)
051  {
052    if (applyMargin)
053    {
054      return new LocalizableMessageBuilder().append(Constants.LINE_SEPARATOR).append(text).toMessage();
055    }
056    return text;
057  }
058
059  @Override
060  public LocalizableMessage getFormattedWarning(LocalizableMessage text, boolean applyMargin)
061  {
062    if (applyMargin)
063    {
064      return new LocalizableMessageBuilder(Constants.LINE_SEPARATOR).append(text).toMessage();
065    }
066    return text;
067  }
068
069  @Override
070  public LocalizableMessage getFormattedSuccess(LocalizableMessage text)
071  {
072    return text;
073  }
074
075  @Override
076  public LocalizableMessage getFormattedLogError(LocalizableMessage text)
077  {
078    return text;
079  }
080
081  @Override
082  public LocalizableMessage getFormattedLog(LocalizableMessage text)
083  {
084    return text;
085  }
086
087  @Override
088  public LocalizableMessage getFormattedDone()
089  {
090    if (doneText == null)
091    {
092      doneText = INFO_PROGRESS_DONE.get();
093    }
094    return doneText;
095  }
096
097  @Override
098  public LocalizableMessage getFormattedError()
099  {
100    if (errorText == null)
101    {
102      errorText = INFO_PROGRESS_ERROR.get();
103    }
104    return errorText;
105  }
106
107  @Override
108  public LocalizableMessage getFormattedWithPoints(LocalizableMessage text)
109  {
110    return new LocalizableMessageBuilder(text).append(SPACE)
111            .append(INFO_PROGRESS_POINTS.get()).append(SPACE).toMessage();
112  }
113
114  @Override
115  public LocalizableMessage getFormattedPoint()
116  {
117    return LocalizableMessage.raw(".");
118  }
119
120  @Override
121  public LocalizableMessage getSpace()
122  {
123    return LocalizableMessage.raw(SPACE);
124  }
125
126  @Override
127  public LocalizableMessage getFormattedProgress(LocalizableMessage text)
128  {
129    return text;
130  }
131
132  @Override
133  public LocalizableMessage getFormattedError(Throwable t, boolean applyMargin)
134  {
135    String msg = t.getMessage();
136    if (msg == null)
137    {
138      msg = t.toString();
139    }
140    String result;
141    if (applyMargin)
142    {
143      result = Constants.LINE_SEPARATOR+msg;
144    } else
145    {
146      result = msg;
147    }
148    return LocalizableMessage.raw(result);
149  }
150
151  @Override
152  public LocalizableMessage getLineBreak()
153  {
154    return LocalizableMessage.raw(Constants.LINE_SEPARATOR);
155  }
156
157  @Override
158  public LocalizableMessage getTab()
159  {
160    return LocalizableMessage.raw("     ");
161  }
162
163  @Override
164  public LocalizableMessage getTaskSeparator()
165  {
166    return LocalizableMessage.raw(
167        Constants.LINE_SEPARATOR+
168        "-----------------------------------------------------------------"+
169        Constants.LINE_SEPARATOR+Constants.LINE_SEPARATOR);
170  }
171
172  @Override
173  public LocalizableMessage getFormattedAfterUrlClick(String url, LocalizableMessage lastText)
174  {
175    throw new IllegalStateException(
176        "PlainTextProgressMessageFormatter.getFormattedAfterUrlClick must not "+
177        "be called");
178  }
179}