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 2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.ui;
029
030import static org.opends.messages.AdminToolMessages.*;
031
032import java.awt.Component;
033import java.awt.GridBagConstraints;
034import java.awt.GridBagLayout;
035import java.awt.Insets;
036import java.awt.event.ActionEvent;
037import java.awt.event.ActionListener;
038
039import javax.swing.BorderFactory;
040import javax.swing.Box;
041import javax.swing.JButton;
042import javax.swing.JPanel;
043
044import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
045import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
046import org.opends.guitools.controlpanel.util.Utilities;
047import org.forgerock.i18n.LocalizableMessage;
048
049/**
050 * Dialog used to inform the user that there are unsaved changes in a panel.
051 * It proposes the user to save the changes, do not save them or cancel the
052 * action that make the dialog appear (for instance when the user is editing
053 * an entry and clicks on another node, this dialog appears).
054 *
055 */
056public class ConfirmInitializeAndImportDialog extends GenericDialog
057{
058  /**
059   * The different input that the user can provide.
060   *
061   */
062  public enum Result
063  {
064    /**
065     * The user asks to do the import and then the initialization.
066     */
067    INITIALIZE_ALL,
068    /**
069     * The user asks to only do the import locally.
070     */
071    IMPORT_ONLY,
072    /**
073     * The user asks to cancel the operation that made this dialog to appear.
074     */
075    CANCEL
076  }
077  private static final long serialVersionUID = -442311801035162311L;
078
079  /**
080   * Constructor of the dialog.
081   * @param parentDialog the parent dialog.
082   * @param info the control panel info.
083   */
084  public ConfirmInitializeAndImportDialog(Component parentDialog,
085      ControlPanelInfo info)
086  {
087    super(Utilities.getFrame(parentDialog), getPanel(info));
088    Utilities.centerGoldenMean(this, parentDialog);
089    getRootPane().setDefaultButton(
090        ((ConfirmInitializeAndImportPanel)panel).initializeAllButton);
091    setModal(true);
092  }
093
094  /**
095   * Sets the message to be displayed in this dialog.
096   * @param title the title of the message.
097   * @param details the details of the message.
098   */
099  public void setMessage(LocalizableMessage title, LocalizableMessage details)
100  {
101    panel.updateConfirmationPane(panel.errorPane, title,
102        ColorAndFontConstants.errorTitleFont, details,
103        ColorAndFontConstants.defaultFont);
104    invalidate();
105    pack();
106  }
107
108  /** {@inheritDoc} */
109  public void setVisible(boolean visible)
110  {
111    if (visible)
112    {
113      ((ConfirmInitializeAndImportPanel)panel).result = Result.CANCEL;
114    }
115    super.setVisible(visible);
116  }
117
118  /**
119   * Returns the option the user gave when closing this dialog.
120   * @return the option the user gave when closing this dialog.
121   */
122  public Result getResult()
123  {
124    return ((ConfirmInitializeAndImportPanel)panel).result;
125  }
126
127  /**
128   * Creates the panel to be displayed inside the dialog.
129   * @param info the control panel info.
130   * @return the panel to be displayed inside the dialog.
131   */
132  private static StatusGenericPanel getPanel(ControlPanelInfo info)
133  {
134    ConfirmInitializeAndImportPanel panel =
135      new ConfirmInitializeAndImportPanel();
136    panel.setInfo(info);
137    return panel;
138  }
139
140  /**
141   * The panel to be displayed inside the dialog.
142   *
143   */
144  private static class ConfirmInitializeAndImportPanel
145  extends StatusGenericPanel
146  {
147    private static final long serialVersionUID = -9890116762604059L;
148
149    private JButton initializeAllButton;
150    private JButton importOnlyButton;
151    private JButton cancelButton;
152
153    private Result result;
154
155    /**
156     * Default constructor.
157     *
158     */
159    public ConfirmInitializeAndImportPanel()
160    {
161      super();
162      GridBagConstraints gbc = new GridBagConstraints();
163      gbc.gridx = 0;
164      gbc.gridy = 0;
165      gbc.gridwidth = 1;
166      addErrorPane(gbc);
167      errorPane.setVisible(true);
168      gbc.gridy ++;
169      gbc.fill = GridBagConstraints.VERTICAL;
170      gbc.weighty = 1.0;
171      add(Box.createVerticalGlue(), gbc);
172      gbc.fill = GridBagConstraints.HORIZONTAL;
173//    The button panel
174      gbc.gridy ++;
175      gbc.weighty = 0.0;
176      gbc.insets = new Insets(0, 0, 0, 0);
177      add(createButtonsPanel(), gbc);
178    }
179
180    /** {@inheritDoc} */
181    public boolean requiresBorder()
182    {
183      return false;
184    }
185
186    /** {@inheritDoc} */
187    public boolean requiresScroll()
188    {
189      return false;
190    }
191
192    private JPanel createButtonsPanel()
193    {
194      JPanel buttonsPanel = new JPanel(new GridBagLayout());
195      buttonsPanel.setOpaque(true);
196      buttonsPanel.setBackground(ColorAndFontConstants.greyBackground);
197      GridBagConstraints gbc = new GridBagConstraints();
198      gbc.gridx = 0;
199      gbc.gridy = 0;
200      gbc.anchor = GridBagConstraints.WEST;
201      gbc.fill = GridBagConstraints.HORIZONTAL;
202      gbc.gridwidth = 1;
203      gbc.gridy = 0;
204      gbc.weightx = 1.0;
205      gbc.gridx ++;
206      buttonsPanel.add(Box.createHorizontalStrut(150));
207      buttonsPanel.add(Box.createHorizontalGlue(), gbc);
208
209      initializeAllButton = Utilities.createButton(
210          INFO_CTRL_PANEL_INITIALIZE_ALL_BUTTON_LABEL.get());
211      initializeAllButton.setOpaque(false);
212      gbc.insets = new Insets(10, 10, 10, 10);
213      gbc.weightx = 0.0;
214      gbc.gridx ++;
215      buttonsPanel.add(initializeAllButton, gbc);
216      initializeAllButton.addActionListener(new ActionListener()
217      {
218        public void actionPerformed(ActionEvent ev)
219        {
220          result = Result.INITIALIZE_ALL;
221          cancelClicked();
222        }
223      });
224
225      gbc.gridx ++;
226      importOnlyButton = Utilities.createButton(
227          INFO_CTRL_PANEL_IMPORT_ONLY_BUTTON_LABEL.get());
228      importOnlyButton.setOpaque(false);
229      gbc.gridx ++;
230      gbc.insets.left = 0;
231      gbc.insets.right = 10;
232      buttonsPanel.add(importOnlyButton, gbc);
233      importOnlyButton.addActionListener(new ActionListener()
234      {
235        /** {@inheritDoc} */
236        public void actionPerformed(ActionEvent ev)
237        {
238          result = Result.IMPORT_ONLY;
239          cancelClicked();
240        }
241      });
242
243      cancelButton = Utilities.createButton(
244          INFO_CTRL_PANEL_CANCEL_BUTTON_LABEL.get());
245      cancelButton.setOpaque(false);
246      gbc.insets.right = 10;
247      gbc.gridx ++;
248      buttonsPanel.add(cancelButton, gbc);
249      cancelButton.addActionListener(new ActionListener()
250      {
251        /** {@inheritDoc} */
252        public void actionPerformed(ActionEvent ev)
253        {
254          result = Result.CANCEL;
255          cancelClicked();
256        }
257      });
258
259      buttonsPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,
260          ColorAndFontConstants.defaultBorderColor));
261
262      return buttonsPanel;
263    }
264
265    /** {@inheritDoc} */
266    public Component getPreferredFocusComponent()
267    {
268      return initializeAllButton;
269    }
270
271    /** {@inheritDoc} */
272    public void okClicked()
273    {
274    }
275
276    /** {@inheritDoc} */
277    public LocalizableMessage getTitle()
278    {
279      return INFO_CTRL_PANEL_CONFIRM_INITIALIZE_TITLE.get();
280    }
281
282    /** {@inheritDoc} */
283    public void configurationChanged(ConfigurationChangeEvent ev)
284    {
285    }
286
287    /** {@inheritDoc} */
288    public GenericDialog.ButtonType getButtonType()
289    {
290      return GenericDialog.ButtonType.NO_BUTTON;
291    }
292  }
293}