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-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.quicksetup.util;
028import org.forgerock.i18n.LocalizableMessage;
029
030import javax.swing.JFrame;
031
032import org.opends.quicksetup.ui.WebBrowserErrorDialog;
033import org.opends.quicksetup.ui.QuickSetupStepPanel;
034
035/**
036 * This class is used to try to launch a URL in the user web browser.
037 *
038 * The class extends SwingWorker and tries to launch the URL using
039 * a WebBrowserLauncher object in the construct method.
040 * If there is a problem launching the user's browser, this class will display
041 * a WebBrowserErrorDialog to allow the user to copy to the system clipboard the
042 * URL we wanted to display.
043 *
044 * When is finished (successfully or unsuccessfully) it notifies the
045 * QuickSetupStepPanel passed in the constructor.
046 *
047 */
048public class URLWorker extends BackgroundTask<Object>
049{
050  private QuickSetupStepPanel panel;
051
052  private String url;
053
054  /**
055   * Constructs a URLWorker.
056   * @param panel the panel that created this URLWorker and to which we will
057   * notify when we are over.
058   * @param url the url to be displayed.
059   */
060  public URLWorker(QuickSetupStepPanel panel, String url)
061  {
062    this.panel = panel;
063    this.url = url;
064  }
065
066  /** {@inheritDoc} */
067  public Object processBackgroundTask() throws WebBrowserException
068  {
069    try
070    {
071      WebBrowserLauncher.openURL(url);
072    } catch (Throwable t)
073    {
074      // TODO: i18n
075      throw new WebBrowserException(url, LocalizableMessage.raw("Bug: throwable"), t);
076    }
077    return null;
078  }
079
080  /** {@inheritDoc} */
081  public void backgroundTaskCompleted(Object returnValue,
082      Throwable throwable)
083  {
084    WebBrowserException ex = (WebBrowserException) throwable;
085    if (ex != null)
086    {
087      WebBrowserErrorDialog dlg =
088          new WebBrowserErrorDialog((JFrame) panel.getMainWindow(), ex);
089      dlg.setModal(false);
090      dlg.packAndShow();
091    }
092    // Notify to the panel that the worker has finished.
093    panel.urlWorkerFinished(this);
094  }
095
096  /**
097   * Returns the URL that we are trying to launch in the users browser.
098   * @return the URL that we are trying to launch in the users browser.
099   */
100  public String getURL()
101  {
102    return url;
103  }
104}