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-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2014 ForgeRock AS
026 */
027
028package org.opends.quicksetup.ui;
029
030import java.awt.GridBagConstraints;
031import java.awt.GridBagLayout;
032import java.awt.Insets;
033import java.awt.datatransfer.StringSelection;
034import java.awt.event.ActionEvent;
035import java.awt.event.ActionListener;
036
037import javax.swing.Box;
038import javax.swing.JButton;
039import javax.swing.JDialog;
040import javax.swing.JFrame;
041import javax.swing.JPanel;
042import javax.swing.text.JTextComponent;
043
044import org.opends.quicksetup.event.MinimumSizeComponentListener;
045import org.opends.quicksetup.util.WebBrowserException;
046import org.forgerock.i18n.LocalizableMessage;
047import static org.opends.messages.QuickSetupMessages.*;
048
049/**
050 * This class is a dialog that appears when we could not launch the user web
051 * browser after the user clicked on a link.
052 *
053 * The dialog displays the URL to be displayed and provides a 'Copy URL' button
054 * to copy it to the system clipboard.  This way (even if not ideal) the user
055 * can view the contents of the URL we display by pasting manually the URL
056 * in his/her browser.
057 *
058 */
059public class WebBrowserErrorDialog extends JDialog
060{
061  private static final long serialVersionUID = 1063837373763193941L;
062
063  private JFrame parent;
064
065  private String url;
066
067  /**
068   * Constructor of the WebBrowserErrorDialog.
069   * @param parent the parent frame for this dialog.
070   * @param ex the WebBrowserException.
071   */
072  public WebBrowserErrorDialog(JFrame parent, WebBrowserException ex)
073  {
074    super(parent);
075    setTitle(INFO_ERROR_BROWSER_DISPLAY_TITLE.get().toString());
076    this.parent = parent;
077    this.url = ex.getUrl();
078    getContentPane().add(createPanel());
079  }
080
081  /**
082   * Packs and displays this dialog.
083   *
084   */
085  public void packAndShow()
086  {
087    pack();
088    int minWidth = (int) getPreferredSize().getWidth();
089    int minHeight = (int) getPreferredSize().getHeight();
090    addComponentListener(new MinimumSizeComponentListener(this,
091        minWidth, minHeight));
092    Utilities.centerOnComponent(this, parent);
093    setVisible(true);
094  }
095
096  /**
097   * Creates and returns the panel of the dialog.
098   * @return the panel of the dialog.
099   */
100  private JPanel createPanel()
101  {
102    JPanel p1 = new JPanel(new GridBagLayout());
103    p1.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
104    p1.setBorder(UIFactory.DIALOG_PANEL_BORDER);
105    GridBagConstraints gbc = new GridBagConstraints();
106    gbc.gridwidth = 3;
107    gbc.anchor = GridBagConstraints.NORTHWEST;
108    gbc.insets = UIFactory.getCurrentStepPanelInsets();
109    p1.add(UIFactory.makeJLabel(UIFactory.IconType.WARNING_LARGE, null,
110        UIFactory.TextStyle.NO_STYLE), gbc);
111    gbc.weightx = 1.0;
112    gbc.gridwidth = GridBagConstraints.RELATIVE;
113    Insets pInsets = UIFactory.getCurrentStepPanelInsets();
114    gbc.insets.left = 0;
115    gbc.fill = GridBagConstraints.BOTH;
116    LocalizableMessage msg = INFO_ERROR_BROWSER_DISPLAY_MSG.get(url);
117    JTextComponent tf =
118        UIFactory.makeHtmlPane(msg,
119            UIFactory.ERROR_DIALOG_FONT);
120    tf.setOpaque(false);
121    tf.setEditable(false);
122    p1.add(tf, gbc);
123
124    gbc.weightx = 0.0;
125    gbc.gridwidth = GridBagConstraints.REMAINDER;
126    JButton copyButton =
127        UIFactory.makeJButton(INFO_ERROR_BROWSER_COPY_BUTTON_LABEL.get(),
128            INFO_ERROR_BROWSER_COPY_BUTTON_TOOLTIP.get());
129    copyButton.addActionListener(new ActionListener()
130    {
131      public void actionPerformed(ActionEvent ev)
132      {
133        StringSelection s = new StringSelection(url);
134        getToolkit().getSystemClipboard().setContents(s, s);
135      }
136    });
137    gbc.insets.left = UIFactory.LEFT_INSET_COPY_BUTTON;
138    gbc.insets.right = pInsets.right;
139    gbc.fill = GridBagConstraints.NONE;
140    p1.add(copyButton, gbc);
141    gbc.weighty = 1.0;
142    gbc.fill = GridBagConstraints.VERTICAL;
143    p1.add(Box.createVerticalGlue(), gbc);
144
145    JPanel p2 = new JPanel(new GridBagLayout());
146    p2.setOpaque(false);
147    gbc.fill = GridBagConstraints.HORIZONTAL;
148    gbc.weightx = 1.0;
149    gbc.insets = UIFactory.getEmptyInsets();
150    gbc.gridwidth = GridBagConstraints.RELATIVE;
151    p2.add(Box.createHorizontalGlue(), gbc);
152    JButton closeButton =
153        UIFactory.makeJButton(INFO_CLOSE_BUTTON_LABEL.get(),
154            INFO_ERROR_BROWSER_CLOSE_BUTTON_TOOLTIP.get());
155    gbc.fill = GridBagConstraints.NONE;
156    gbc.weightx = 0.0;
157    gbc.gridwidth = GridBagConstraints.REMAINDER;
158    p2.add(closeButton, gbc);
159    closeButton.addActionListener(new ActionListener()
160    {
161      public void actionPerformed(ActionEvent ev)
162      {
163        dispose();
164      }
165    });
166
167    JPanel p = new JPanel(new GridBagLayout());
168    p.setBackground(UIFactory.DEFAULT_BACKGROUND);
169    gbc.insets = UIFactory.getEmptyInsets();
170    gbc.fill = GridBagConstraints.BOTH;
171    gbc.gridwidth = GridBagConstraints.REMAINDER;
172    gbc.weightx = 1.0;
173    gbc.weighty = 1.0;
174    p.add(p1, gbc);
175    gbc.weighty = 0.0;
176    gbc.insets = UIFactory.getButtonsPanelInsets();
177    p.add(p2, gbc);
178    getRootPane().setDefaultButton(copyButton);
179    return p;
180  }
181
182  /**
183   * Method written for testing purposes.
184   * @param args the arguments to be passed to the test program.
185   */
186  public static void main(String[] args)
187  {
188    try
189    {
190      // UIFactory.initialize();
191      WebBrowserErrorDialog dlg =
192          new WebBrowserErrorDialog(new JFrame(),
193              new WebBrowserException("http://opendj.org",
194                      LocalizableMessage.raw("toto"), null));
195      dlg.packAndShow();
196    } catch (Exception ex)
197    {
198      ex.printStackTrace();
199    }
200  }
201}