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 2014-2015 ForgeRock AS.
016 */
017
018package org.opends.quicksetup.ui;
019
020import java.awt.GridBagConstraints;
021import java.awt.GridBagLayout;
022import java.awt.event.ActionEvent;
023import java.awt.event.ActionListener;
024import java.util.HashSet;
025
026import javax.swing.Box;
027import javax.swing.JButton;
028import javax.swing.JPanel;
029import javax.swing.text.JTextComponent;
030
031import org.forgerock.i18n.LocalizableMessage;
032import org.opends.quicksetup.ButtonName;
033import org.opends.quicksetup.CurrentInstallStatus;
034import org.opends.quicksetup.event.ButtonActionListener;
035import org.opends.quicksetup.event.ButtonEvent;
036import static org.opends.messages.QuickSetupMessages.*;
037
038/**
039 * This class is a panel that contains an error message and a quit button.
040 * It is used for instance when we try to install Open DS but it is already
041 * installed.
042 *
043 */
044public class QuickSetupErrorPanel extends QuickSetupPanel
045{
046  private static final long serialVersionUID = 1765037717593522233L;
047
048  private HashSet<ButtonActionListener> buttonListeners = new HashSet<>();
049
050  private JButton quitButton;
051  private JButton continueButton;
052
053  /**
054   * Constructor of the QuickSetupErrorPanel.
055   * @param application Application this panel represents
056   * @param installStatus the current install status.
057   */
058  public QuickSetupErrorPanel(GuiApplication application,
059                              CurrentInstallStatus installStatus)
060  {
061    this(application, installStatus.getInstallationMsg());
062    continueButton.setVisible(installStatus.canOverwriteCurrentInstall());
063  }
064
065  /**
066   * Constructor of the QuickSetupErrorPanel.
067   * @param application Application this panel represents
068   * @param msg the error message to display formatted in HTML.
069   */
070  public QuickSetupErrorPanel(GuiApplication application,
071                              LocalizableMessage msg)
072  {
073    super(application);
074    JPanel p1 = new JPanel(new GridBagLayout());
075    p1.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
076    p1.setBorder(UIFactory.DIALOG_PANEL_BORDER);
077    GridBagConstraints gbc = new GridBagConstraints();
078    gbc.gridwidth = GridBagConstraints.RELATIVE;
079    gbc.anchor = GridBagConstraints.NORTHWEST;
080    gbc.insets = UIFactory.getCurrentStepPanelInsets();
081    p1.add(UIFactory.makeJLabel(UIFactory.IconType.WARNING_LARGE, null,
082        UIFactory.TextStyle.NO_STYLE), gbc);
083    gbc.weightx = 1.0;
084    gbc.gridwidth = GridBagConstraints.REMAINDER;
085    gbc.fill = GridBagConstraints.BOTH;
086    gbc.insets.left = 0;
087    JTextComponent tf =
088            UIFactory.makeHtmlPane(msg,
089                    UIFactory.INSTRUCTIONS_FONT);
090    tf.setOpaque(false);
091    tf.setEditable(false);
092    p1.add(tf, gbc);
093
094    gbc.weighty = 1.0;
095    gbc.fill = GridBagConstraints.VERTICAL;
096    p1.add(Box.createVerticalGlue(), gbc);
097
098    JPanel p2 = new JPanel(new GridBagLayout());
099    p2.setOpaque(false);
100    gbc.fill = GridBagConstraints.HORIZONTAL;
101    gbc.weightx = 1.0;
102    gbc.insets = UIFactory.getEmptyInsets();
103    gbc.gridwidth = 3;
104    p2.add(Box.createHorizontalGlue(), gbc);
105    quitButton =
106        UIFactory.makeJButton(INFO_QUIT_BUTTON_LABEL.get(),
107            INFO_QUIT_BUTTON_INSTALL_TOOLTIP.get());
108
109    final ButtonName fQuitButtonName = ButtonName.QUIT;
110
111    ActionListener quitListener = new ActionListener()
112    {
113      public void actionPerformed(ActionEvent ev)
114      {
115        ButtonEvent be = new ButtonEvent(ev.getSource(), fQuitButtonName);
116        for (ButtonActionListener li : buttonListeners)
117        {
118          li.buttonActionPerformed(be);
119        }
120      }
121    };
122    quitButton.addActionListener(quitListener);
123
124    continueButton =
125      UIFactory.makeJButton(INFO_CONTINUE_BUTTON_LABEL.get(),
126          INFO_CONTINUE_BUTTON_INSTALL_TOOLTIP.get());
127    final ButtonName fContinueButtonName = ButtonName.CONTINUE_INSTALL;
128
129    ActionListener continueListener = new ActionListener()
130    {
131      public void actionPerformed(ActionEvent ev)
132      {
133        ButtonEvent be = new ButtonEvent(ev.getSource(), fContinueButtonName);
134        for (ButtonActionListener li : buttonListeners)
135        {
136          li.buttonActionPerformed(be);
137        }
138      }
139    };
140    continueButton.addActionListener(continueListener);
141
142    gbc.fill = GridBagConstraints.NONE;
143    gbc.weightx = 0.0;
144
145    gbc.gridwidth = GridBagConstraints.RELATIVE;
146    p2.add(continueButton, gbc);
147    continueButton.setVisible(false);
148
149    gbc.insets.left = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS;
150    gbc.gridwidth = GridBagConstraints.REMAINDER;
151    p2.add(quitButton, gbc);
152
153    setLayout(new GridBagLayout());
154    setBackground(UIFactory.DEFAULT_BACKGROUND);
155    setOpaque(true);
156    gbc.insets = UIFactory.getEmptyInsets();
157    gbc.fill = GridBagConstraints.BOTH;
158    gbc.gridwidth = GridBagConstraints.REMAINDER;
159    gbc.weightx = 1.0;
160    gbc.weighty = 1.0;
161    add(p1, gbc);
162    gbc.weighty = 0.0;
163    gbc.insets = UIFactory.getButtonsPanelInsets();
164    add(p2, gbc);
165  }
166
167  /**
168   * Adds a button listener.  All the button listeners will be notified when
169   * the buttons are clicked (by the user or programatically).
170   * @param l the ButtonActionListener to be added.
171   */
172  public void addButtonActionListener(ButtonActionListener l)
173  {
174    buttonListeners.add(l);
175  }
176
177  /**
178   * Removes a button listener.
179   * @param l the ButtonActionListener to be removed.
180   */
181  public void removeButtonActionListener(ButtonActionListener l)
182  {
183    buttonListeners.remove(l);
184  }
185
186  /**
187   * Returns the quit button.
188   * @return the quit button.
189   */
190  public JButton getQuitButton()
191  {
192    return quitButton;
193  }
194
195  /**
196   * Returns the continue install button.
197   * @return the continue install button.
198   */
199  public JButton getContinueInstallButton()
200  {
201    return continueButton;
202  }
203}