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-2014 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 *
048 */
049public class WebBrowserErrorDialog extends JDialog
050{
051  private static final long serialVersionUID = 1063837373763193941L;
052
053  private JFrame parent;
054
055  private String url;
056
057  /**
058   * Constructor of the WebBrowserErrorDialog.
059   * @param parent the parent frame for this dialog.
060   * @param ex the WebBrowserException.
061   */
062  public WebBrowserErrorDialog(JFrame parent, WebBrowserException ex)
063  {
064    super(parent);
065    setTitle(INFO_ERROR_BROWSER_DISPLAY_TITLE.get().toString());
066    this.parent = parent;
067    this.url = ex.getUrl();
068    getContentPane().add(createPanel());
069  }
070
071  /**
072   * Packs and displays this dialog.
073   *
074   */
075  public void packAndShow()
076  {
077    pack();
078    int minWidth = (int) getPreferredSize().getWidth();
079    int minHeight = (int) getPreferredSize().getHeight();
080    addComponentListener(new MinimumSizeComponentListener(this,
081        minWidth, minHeight));
082    Utilities.centerOnComponent(this, parent);
083    setVisible(true);
084  }
085
086  /**
087   * Creates and returns the panel of the dialog.
088   * @return the panel of the dialog.
089   */
090  private JPanel createPanel()
091  {
092    JPanel p1 = new JPanel(new GridBagLayout());
093    p1.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
094    p1.setBorder(UIFactory.DIALOG_PANEL_BORDER);
095    GridBagConstraints gbc = new GridBagConstraints();
096    gbc.gridwidth = 3;
097    gbc.anchor = GridBagConstraints.NORTHWEST;
098    gbc.insets = UIFactory.getCurrentStepPanelInsets();
099    p1.add(UIFactory.makeJLabel(UIFactory.IconType.WARNING_LARGE, null,
100        UIFactory.TextStyle.NO_STYLE), gbc);
101    gbc.weightx = 1.0;
102    gbc.gridwidth = GridBagConstraints.RELATIVE;
103    Insets pInsets = UIFactory.getCurrentStepPanelInsets();
104    gbc.insets.left = 0;
105    gbc.fill = GridBagConstraints.BOTH;
106    LocalizableMessage msg = INFO_ERROR_BROWSER_DISPLAY_MSG.get(url);
107    JTextComponent tf =
108        UIFactory.makeHtmlPane(msg,
109            UIFactory.ERROR_DIALOG_FONT);
110    tf.setOpaque(false);
111    tf.setEditable(false);
112    p1.add(tf, gbc);
113
114    gbc.weightx = 0.0;
115    gbc.gridwidth = GridBagConstraints.REMAINDER;
116    JButton copyButton =
117        UIFactory.makeJButton(INFO_ERROR_BROWSER_COPY_BUTTON_LABEL.get(),
118            INFO_ERROR_BROWSER_COPY_BUTTON_TOOLTIP.get());
119    copyButton.addActionListener(new ActionListener()
120    {
121      public void actionPerformed(ActionEvent ev)
122      {
123        StringSelection s = new StringSelection(url);
124        getToolkit().getSystemClipboard().setContents(s, s);
125      }
126    });
127    gbc.insets.left = UIFactory.LEFT_INSET_COPY_BUTTON;
128    gbc.insets.right = pInsets.right;
129    gbc.fill = GridBagConstraints.NONE;
130    p1.add(copyButton, gbc);
131    gbc.weighty = 1.0;
132    gbc.fill = GridBagConstraints.VERTICAL;
133    p1.add(Box.createVerticalGlue(), gbc);
134
135    JPanel p2 = new JPanel(new GridBagLayout());
136    p2.setOpaque(false);
137    gbc.fill = GridBagConstraints.HORIZONTAL;
138    gbc.weightx = 1.0;
139    gbc.insets = UIFactory.getEmptyInsets();
140    gbc.gridwidth = GridBagConstraints.RELATIVE;
141    p2.add(Box.createHorizontalGlue(), gbc);
142    JButton closeButton =
143        UIFactory.makeJButton(INFO_CLOSE_BUTTON_LABEL.get(),
144            INFO_ERROR_BROWSER_CLOSE_BUTTON_TOOLTIP.get());
145    gbc.fill = GridBagConstraints.NONE;
146    gbc.weightx = 0.0;
147    gbc.gridwidth = GridBagConstraints.REMAINDER;
148    p2.add(closeButton, gbc);
149    closeButton.addActionListener(new ActionListener()
150    {
151      public void actionPerformed(ActionEvent ev)
152      {
153        dispose();
154      }
155    });
156
157    JPanel p = new JPanel(new GridBagLayout());
158    p.setBackground(UIFactory.DEFAULT_BACKGROUND);
159    gbc.insets = UIFactory.getEmptyInsets();
160    gbc.fill = GridBagConstraints.BOTH;
161    gbc.gridwidth = GridBagConstraints.REMAINDER;
162    gbc.weightx = 1.0;
163    gbc.weighty = 1.0;
164    p.add(p1, gbc);
165    gbc.weighty = 0.0;
166    gbc.insets = UIFactory.getButtonsPanelInsets();
167    p.add(p2, gbc);
168    getRootPane().setDefaultButton(copyButton);
169    return p;
170  }
171
172  /**
173   * Method written for testing purposes.
174   * @param args the arguments to be passed to the test program.
175   */
176  public static void main(String[] args)
177  {
178    try
179    {
180      // UIFactory.initialize();
181      WebBrowserErrorDialog dlg =
182          new WebBrowserErrorDialog(new JFrame(),
183              new WebBrowserException("http://opendj.org",
184                      LocalizableMessage.raw("toto"), null));
185      dlg.packAndShow();
186    } catch (Exception ex)
187    {
188      ex.printStackTrace();
189    }
190  }
191}