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 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2015 ForgeRock AS.
016 */
017
018package org.opends.quicksetup;
019import org.forgerock.i18n.LocalizableMessage;
020
021import org.opends.quicksetup.event.ProgressUpdateListener;
022import org.opends.quicksetup.event.ProgressUpdateEvent;
023import java.util.HashSet;
024
025/**
026 * Delegate class for handling progress notification listeners and events.
027 */
028public class ProgressUpdateListenerDelegate {
029
030  private HashSet<ProgressUpdateListener> listeners = new HashSet<>();
031
032  /**
033   * Creates a parameterized instance.
034   */
035  public ProgressUpdateListenerDelegate() {
036  }
037
038  /**
039   * Adds a ProgressUpdateListener that will be notified of updates in
040   * the install progress.
041   *
042   * @param l the ProgressUpdateListener to be added.
043   */
044  public void addProgressUpdateListener(ProgressUpdateListener l) {
045    listeners.add(l);
046  }
047
048  /**
049   * Removes a ProgressUpdateListener.
050   *
051   * @param l the ProgressUpdateListener to be removed.
052   */
053  public void removeProgressUpdateListener(ProgressUpdateListener l) {
054    listeners.remove(l);
055  }
056
057  /**
058   * This method notifies the ProgressUpdateListeners that there was an
059   * update in the installation progress.
060   *
061   * @param current             progress step
062   * @param ratio               the integer that specifies which percentage of
063 *                            the whole installation has been completed.
064   * @param currentPhaseSummary the localized summary message for the
065*                            current installation progress in formatted form.
066   * @param newLogDetail        the new log messages that we have for the
067   */
068  public void notifyListeners(ProgressStep current, Integer ratio,
069                              LocalizableMessage currentPhaseSummary,
070                              LocalizableMessage newLogDetail) {
071    ProgressUpdateEvent ev =
072            new ProgressUpdateEvent(current, ratio,
073                    currentPhaseSummary, newLogDetail);
074    for (ProgressUpdateListener l : listeners) {
075      l.progressUpdate(ev);
076    }
077  }
078
079  /**
080   * Notify listeners about a change in log detail.
081   * @param msg log detail
082   */
083  protected void notifyListeners(LocalizableMessage msg) {
084    notifyListeners(null, null, null, msg);
085  }
086
087}