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