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