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-2016 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 */
037public class URLWorker extends BackgroundTask<Object>
038{
039  private QuickSetupStepPanel panel;
040
041  private String url;
042
043  /**
044   * Constructs a URLWorker.
045   * @param panel the panel that created this URLWorker and to which we will
046   * notify when we are over.
047   * @param url the url to be displayed.
048   */
049  public URLWorker(QuickSetupStepPanel panel, String url)
050  {
051    this.panel = panel;
052    this.url = url;
053  }
054
055  @Override
056  public Object processBackgroundTask() throws WebBrowserException
057  {
058    try
059    {
060      WebBrowserLauncher.openURL(url);
061    } catch (Throwable t)
062    {
063      // TODO: i18n
064      throw new WebBrowserException(url, LocalizableMessage.raw("Bug: throwable"), t);
065    }
066    return null;
067  }
068
069  @Override
070  public void backgroundTaskCompleted(Object returnValue,
071      Throwable throwable)
072  {
073    WebBrowserException ex = (WebBrowserException) throwable;
074    if (ex != null)
075    {
076      WebBrowserErrorDialog dlg =
077          new WebBrowserErrorDialog((JFrame) panel.getMainWindow(), ex);
078      dlg.setModal(false);
079      dlg.packAndShow();
080    }
081    // Notify to the panel that the worker has finished.
082    panel.urlWorkerFinished(this);
083  }
084
085  /**
086   * Returns the URL that we are trying to launch in the users browser.
087   * @return the URL that we are trying to launch in the users browser.
088   */
089  public String getURL()
090  {
091    return url;
092  }
093}