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