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-2016 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 */
043public class QuickSetupErrorPanel extends QuickSetupPanel
044{
045  private static final long serialVersionUID = 1765037717593522233L;
046
047  private HashSet<ButtonActionListener> buttonListeners = new HashSet<>();
048
049  private JButton quitButton;
050  private JButton continueButton;
051
052  /**
053   * Constructor of the QuickSetupErrorPanel.
054   * @param application Application this panel represents
055   * @param installStatus the current install status.
056   */
057  public QuickSetupErrorPanel(GuiApplication application,
058                              CurrentInstallStatus installStatus)
059  {
060    this(application, installStatus.getInstallationMsg());
061    continueButton.setVisible(installStatus.canOverwriteCurrentInstall());
062  }
063
064  /**
065   * Constructor of the QuickSetupErrorPanel.
066   * @param application Application this panel represents
067   * @param msg the error message to display formatted in HTML.
068   */
069  public QuickSetupErrorPanel(GuiApplication application,
070                              LocalizableMessage msg)
071  {
072    super(application);
073    JPanel p1 = new JPanel(new GridBagLayout());
074    p1.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
075    p1.setBorder(UIFactory.DIALOG_PANEL_BORDER);
076    GridBagConstraints gbc = new GridBagConstraints();
077    gbc.gridwidth = GridBagConstraints.RELATIVE;
078    gbc.anchor = GridBagConstraints.NORTHWEST;
079    gbc.insets = UIFactory.getCurrentStepPanelInsets();
080    p1.add(UIFactory.makeJLabel(UIFactory.IconType.WARNING_LARGE, null,
081        UIFactory.TextStyle.NO_STYLE), gbc);
082    gbc.weightx = 1.0;
083    gbc.gridwidth = GridBagConstraints.REMAINDER;
084    gbc.fill = GridBagConstraints.BOTH;
085    gbc.insets.left = 0;
086    JTextComponent tf =
087            UIFactory.makeHtmlPane(msg,
088                    UIFactory.INSTRUCTIONS_FONT);
089    tf.setOpaque(false);
090    tf.setEditable(false);
091    p1.add(tf, gbc);
092
093    gbc.weighty = 1.0;
094    gbc.fill = GridBagConstraints.VERTICAL;
095    p1.add(Box.createVerticalGlue(), gbc);
096
097    JPanel p2 = new JPanel(new GridBagLayout());
098    p2.setOpaque(false);
099    gbc.fill = GridBagConstraints.HORIZONTAL;
100    gbc.weightx = 1.0;
101    gbc.insets = UIFactory.getEmptyInsets();
102    gbc.gridwidth = 3;
103    p2.add(Box.createHorizontalGlue(), gbc);
104    quitButton =
105        UIFactory.makeJButton(INFO_QUIT_BUTTON_LABEL.get(),
106            INFO_QUIT_BUTTON_INSTALL_TOOLTIP.get());
107
108    final ButtonName fQuitButtonName = ButtonName.QUIT;
109
110    ActionListener quitListener = new ActionListener()
111    {
112      @Override
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      @Override
132      public void actionPerformed(ActionEvent ev)
133      {
134        ButtonEvent be = new ButtonEvent(ev.getSource(), fContinueButtonName);
135        for (ButtonActionListener li : buttonListeners)
136        {
137          li.buttonActionPerformed(be);
138        }
139      }
140    };
141    continueButton.addActionListener(continueListener);
142
143    gbc.fill = GridBagConstraints.NONE;
144    gbc.weightx = 0.0;
145
146    gbc.gridwidth = GridBagConstraints.RELATIVE;
147    p2.add(continueButton, gbc);
148    continueButton.setVisible(false);
149
150    gbc.insets.left = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS;
151    gbc.gridwidth = GridBagConstraints.REMAINDER;
152    p2.add(quitButton, gbc);
153
154    setLayout(new GridBagLayout());
155    setBackground(UIFactory.DEFAULT_BACKGROUND);
156    setOpaque(true);
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    add(p1, gbc);
163    gbc.weighty = 0.0;
164    gbc.insets = UIFactory.getButtonsPanelInsets();
165    add(p2, gbc);
166  }
167
168  /**
169   * Adds a button listener.  All the button listeners will be notified when
170   * the buttons are clicked (by the user or programatically).
171   * @param l the ButtonActionListener to be added.
172   */
173  public void addButtonActionListener(ButtonActionListener l)
174  {
175    buttonListeners.add(l);
176  }
177
178  /**
179   * Removes a button listener.
180   * @param l the ButtonActionListener to be removed.
181   */
182  public void removeButtonActionListener(ButtonActionListener l)
183  {
184    buttonListeners.remove(l);
185  }
186
187  /**
188   * Returns the quit button.
189   * @return the quit button.
190   */
191  public JButton getQuitButton()
192  {
193    return quitButton;
194  }
195
196  /**
197   * Returns the continue install button.
198   * @return the continue install button.
199   */
200  public JButton getContinueInstallButton()
201  {
202    return continueButton;
203  }
204}