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 2014-2015 ForgeRock AS
026 */
027package org.opends.quicksetup.ui;
028
029import org.forgerock.i18n.LocalizableMessage;
030import static org.opends.messages.QuickSetupMessages.*;
031
032import org.opends.quicksetup.ButtonName;
033import org.opends.quicksetup.WizardStep;
034import org.opends.quicksetup.LicenseFile;
035import org.opends.quicksetup.event.ButtonActionListener;
036import org.opends.quicksetup.event.ButtonEvent;
037
038import javax.swing.*;
039import java.awt.*;
040import java.awt.event.ActionEvent;
041import java.awt.event.ActionListener;
042import java.util.HashSet;
043
044/**
045 * This class contains the buttons in the bottom of the Install/Uninstall
046 * dialog.  There is only one of this instances for the QuickSetupDialog.
047 * The layout is updated calling setCurrentStep method.
048 *
049 */
050public class ButtonsPanel extends QuickSetupPanel
051{
052  private static final long serialVersionUID = -8460400337486357976L;
053
054  private HashSet<ButtonActionListener> buttonListeners = new HashSet<>();
055
056  private JButton nextButton;
057  private JButton previousButton;
058  private JButton quitButton;
059  private JButton closeButton;
060  private JButton finishButton;
061
062  /**
063   * Default constructor.
064   * @param application Application running in QuickSetup
065   */
066  public ButtonsPanel(GuiApplication application)
067  {
068    super(application);
069    createButtons();
070    layoutButtons();
071  }
072
073  /**
074   * Adds a button listener.  All the button listeners will be notified when
075   * the buttons are clicked (by the user or programatically).
076   * @param l the ButtonActionListener to be added.
077   */
078  public void addButtonActionListener(ButtonActionListener l)
079  {
080    buttonListeners.add(l);
081  }
082
083  /**
084   * Removes a button listener.
085   * @param l the ButtonActionListener to be removed.
086   */
087  public void removeButtonActionListener(ButtonActionListener l)
088  {
089    buttonListeners.remove(l);
090  }
091
092  /**
093   * Updates the layout of the panel so that it corresponds to the Step passed
094   * as parameter.
095   *
096   * @param step the step in the wizard.
097   */
098  public void updateButtons(WizardStep step)
099  {
100    GuiApplication application = getApplication();
101    previousButton.setVisible(application.canGoBack(step));
102    if (application.canFinish(step)) {
103      finishButton.setVisible(true);
104      nextButton.setVisible(false);
105    } else {
106      finishButton.setVisible(false);
107      nextButton.setVisible(application.canGoForward(step));
108    }
109
110    // The quit button appears on all the panels leading up
111    // to the progress panel
112    quitButton.setVisible(!step.isProgressStep() && !step.isFinishedStep());
113
114    // The close button is only used on the progress panel and
115    // is only enabled once progress has finished or cancelled.
116    closeButton.setVisible(step.isProgressStep() || step.isFinishedStep());
117    closeButton.setEnabled(application.getCurrentProgressStep().isLast());
118
119    // The next button is always enabled in all steps except the license step.
120    // In that step, the next button will be enabled only if the user has
121    // approved the license
122    nextButton.setEnabled(!step.isLicenseStep() || LicenseFile.getApproval());
123  }
124
125  /**
126   * Returns the button corresponding to the buttonName.
127   * @param buttonName the ButtonName for which we want to get the button.
128   * @return the button corresponding to the buttonName.
129   */
130  public JButton getButton(ButtonName buttonName)
131  {
132    JButton b = null;
133    if (buttonName != null) {
134      switch (buttonName) {
135        case NEXT:
136          b = nextButton;
137          break;
138
139        case PREVIOUS:
140          b = previousButton;
141          break;
142
143        case QUIT:
144          b = quitButton;
145          break;
146
147        case CLOSE:
148          b = closeButton;
149          break;
150
151        case FINISH:
152          b = finishButton;
153          break;
154
155        default:
156          throw new IllegalArgumentException("Unknown button name: " +
157                  buttonName);
158      }
159    }
160
161    return b;
162  }
163
164  private void createButtons()
165  {
166    GuiApplication application = getApplication();
167    nextButton = createButton(
168            INFO_NEXT_BUTTON_LABEL.get(),
169            INFO_NEXT_BUTTON_TOOLTIP.get(),
170            ButtonName.NEXT);
171
172    previousButton = createButton(
173            INFO_PREVIOUS_BUTTON_LABEL.get(),
174            INFO_PREVIOUS_BUTTON_TOOLTIP.get(),
175            ButtonName.PREVIOUS);
176
177    quitButton = createButton(
178            INFO_QUIT_BUTTON_LABEL.get(),
179            application.getQuitButtonToolTip(),
180            ButtonName.QUIT);
181
182    closeButton = createButton(
183            INFO_CLOSE_BUTTON_LABEL.get(),
184            application.getCloseButtonToolTip(),
185            ButtonName.CLOSE);
186
187    finishButton = createButton(
188            application.getFinishButtonLabel(),
189            application.getFinishButtonToolTip(),
190            ButtonName.FINISH);
191
192  }
193
194  /**
195   * Do the layout of the panel.
196   *
197   */
198  private void layoutButtons()
199  {
200    setLayout(new GridBagLayout());
201    GridBagConstraints gbc = new GridBagConstraints();
202
203    GridBagConstraints gbcAux = new GridBagConstraints();
204    gbcAux.gridwidth = GridBagConstraints.REMAINDER;
205    gbcAux.fill = GridBagConstraints.HORIZONTAL;
206    JPanel previousPanel = new JPanel(new GridBagLayout());
207    // Set as opaque to inherit the background color of ButtonsPanel
208    previousPanel.setOpaque(false);
209    previousPanel.add(previousButton, gbcAux);
210    int width = (int) previousButton.getPreferredSize().getWidth();
211    previousPanel.add(Box.createHorizontalStrut(width), gbcAux);
212
213    gbc.gridwidth = 5;
214    gbc.weightx = 0.0;
215    gbc.weighty = 0.0;
216    gbc.insets.bottom = 0;
217    gbc.insets.right = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS;
218    gbc.anchor = GridBagConstraints.WEST;
219    gbc.fill = GridBagConstraints.NONE;
220    add(previousPanel, gbc);
221    gbc.gridwidth--;
222
223    JPanel nextFinishPanel = new JPanel(new GridBagLayout());
224    // Set as opaque to inherit the background color of ButtonsPanel
225    nextFinishPanel.setOpaque(false);
226    nextFinishPanel.add(nextButton, gbcAux);
227
228    if (getApplication().finishOnLeft()) {
229      nextFinishPanel.add(finishButton, gbcAux);
230    }
231    width =
232        (int) Math.max(nextButton.getPreferredSize().getWidth(), finishButton
233            .getPreferredSize().getWidth());
234    nextFinishPanel.add(Box.createHorizontalStrut(width), gbcAux);
235    add(nextFinishPanel, gbc);
236
237    gbc.gridwidth--;
238    gbc.weightx = 1.0;
239    gbc.fill = GridBagConstraints.HORIZONTAL;
240    gbc.insets.right = 0;
241    add(Box.createHorizontalGlue(), gbc);
242
243    gbc.gridwidth = GridBagConstraints.RELATIVE;
244    gbc.weightx = 0.0;
245    gbc.fill = GridBagConstraints.NONE;
246    gbc.insets.left = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS;
247
248    if (!getApplication().finishOnLeft()) {
249      gbc.insets.right = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS;
250      add(finishButton, gbc);
251      gbc.insets.right = 0;
252    }
253
254    gbc.gridwidth = GridBagConstraints.REMAINDER;
255    gbc.weightx = 0.0;
256    gbc.fill = GridBagConstraints.NONE;
257    gbc.insets.left = 0;
258    JPanel quitClosePanel = new JPanel(new GridBagLayout());
259    // Set as opaque to inherit the background color of ButtonsPanel
260    quitClosePanel.setOpaque(false);
261    quitClosePanel.add(
262        Box.createHorizontalStrut(UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS),
263        gbcAux);
264    quitClosePanel.add(quitButton, gbcAux);
265    quitClosePanel.add(closeButton, gbcAux);
266    width =
267        (int) Math.max(quitButton.getPreferredSize().getWidth(), closeButton
268            .getPreferredSize().getWidth());
269    quitClosePanel.add(Box.createHorizontalStrut(width), gbcAux);
270    add(quitClosePanel, gbc);
271  }
272
273  /**
274   * Create a button.
275   * @param label the label of the button.
276   * @param tooltip the tooltip of the button.
277   * @param buttonName the ButtonName.
278   * @return a new button with the specified parameters.
279   */
280  private JButton createButton(LocalizableMessage label, LocalizableMessage tooltip,
281      ButtonName buttonName)
282  {
283    JButton b = UIFactory.makeJButton(label, tooltip);
284
285    final ButtonName fButtonName = buttonName;
286
287    ActionListener actionListener = new ActionListener()
288    {
289      public void actionPerformed(ActionEvent ev)
290      {
291        ButtonEvent be = new ButtonEvent(ev.getSource(), fButtonName);
292        for (ButtonActionListener li : buttonListeners)
293        {
294          li.buttonActionPerformed(be);
295        }
296      }
297    };
298
299    b.addActionListener(actionListener);
300
301    return b;
302  }
303}