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