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 2008-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 UnsavedChangesDialog 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 save the changes.
056     */
057    SAVE,
058    /**
059     * The user asks to not to save the changes.
060     */
061    DO_NOT_SAVE,
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 = -4436794801035162388L;
068
069  /**
070   * Constructor of the dialog.
071   * @param parentDialog the parent dialog.
072   * @param info the control panel info.
073   */
074  public UnsavedChangesDialog(Component parentDialog,
075      ControlPanelInfo info)
076  {
077    super(Utilities.getFrame(parentDialog), getPanel(info));
078    Utilities.centerGoldenMean(this, parentDialog);
079    getRootPane().setDefaultButton(
080        ((UnsavedChangesPanel)panel).saveButton);
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      ((UnsavedChangesPanel)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 ((UnsavedChangesPanel)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    UnsavedChangesPanel panel = new UnsavedChangesPanel();
125    panel.setInfo(info);
126    return panel;
127  }
128
129  /**
130   * The panel to be displayed inside the dialog.
131   *
132   */
133  private static class UnsavedChangesPanel extends StatusGenericPanel
134  {
135    private static final long serialVersionUID = -1528939816762604059L;
136
137    private JButton saveButton;
138    private JButton doNotSaveButton;
139    private JButton cancelButton;
140
141    private Result result;
142
143    /**
144     * Default constructor.
145     *
146     */
147    public UnsavedChangesPanel()
148    {
149      super();
150      GridBagConstraints gbc = new GridBagConstraints();
151      gbc.gridx = 0;
152      gbc.gridy = 0;
153      gbc.gridwidth = 1;
154      addErrorPane(gbc);
155      errorPane.setVisible(true);
156      gbc.gridy ++;
157      gbc.fill = GridBagConstraints.VERTICAL;
158      gbc.weighty = 1.0;
159      add(Box.createVerticalGlue(), gbc);
160      gbc.fill = GridBagConstraints.HORIZONTAL;
161//    The button panel
162      gbc.gridy ++;
163      gbc.weighty = 0.0;
164      gbc.insets = new Insets(0, 0, 0, 0);
165      add(createButtonsPanel(), gbc);
166    }
167
168    /** {@inheritDoc} */
169    public boolean requiresBorder()
170    {
171      return false;
172    }
173
174    /** {@inheritDoc} */
175    public boolean requiresScroll()
176    {
177      return false;
178    }
179
180    private JPanel createButtonsPanel()
181    {
182      JPanel buttonsPanel = new JPanel(new GridBagLayout());
183      GridBagConstraints gbc = new GridBagConstraints();
184      gbc.gridx = 0;
185      gbc.gridy = 0;
186      gbc.anchor = GridBagConstraints.WEST;
187      gbc.fill = GridBagConstraints.HORIZONTAL;
188      gbc.gridwidth = 1;
189      gbc.gridy = 0;
190      doNotSaveButton =
191        Utilities.createButton(INFO_CTRL_PANEL_DO_NOT_SAVE_BUTTON_LABEL.get());
192      doNotSaveButton.setOpaque(false);
193      gbc.insets = new Insets(10, 10, 10, 10);
194      buttonsPanel.add(doNotSaveButton, gbc);
195      doNotSaveButton.addActionListener(new ActionListener()
196      {
197        public void actionPerformed(ActionEvent ev)
198        {
199          result = Result.DO_NOT_SAVE;
200          cancelClicked();
201        }
202      });
203      gbc.weightx = 1.0;
204      gbc.gridx ++;
205      buttonsPanel.add(Box.createHorizontalStrut(150));
206      buttonsPanel.add(Box.createHorizontalGlue(), gbc);
207      buttonsPanel.setOpaque(true);
208      buttonsPanel.setBackground(ColorAndFontConstants.greyBackground);
209      gbc.gridx ++;
210      gbc.weightx = 0.0;
211      buttonsPanel.add(Box.createHorizontalStrut(100));
212      gbc.gridx ++;
213      cancelButton = Utilities.createButton(
214          INFO_CTRL_PANEL_CANCEL_BUTTON_LABEL.get());
215      cancelButton.setOpaque(false);
216      gbc.insets.right = 0;
217      buttonsPanel.add(cancelButton, gbc);
218      cancelButton.addActionListener(new ActionListener()
219      {
220        /** {@inheritDoc} */
221        public void actionPerformed(ActionEvent ev)
222        {
223          result = Result.CANCEL;
224          cancelClicked();
225        }
226      });
227      saveButton = Utilities.createButton(
228          INFO_CTRL_PANEL_SAVE_BUTTON_LABEL.get());
229      saveButton.setOpaque(false);
230      gbc.gridx ++;
231      gbc.insets.left = 5;
232      gbc.insets.right = 10;
233      buttonsPanel.add(saveButton, gbc);
234      saveButton.addActionListener(new ActionListener()
235      {
236        /** {@inheritDoc} */
237        public void actionPerformed(ActionEvent ev)
238        {
239          result = Result.SAVE;
240          cancelClicked();
241        }
242      });
243
244      buttonsPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,
245          ColorAndFontConstants.defaultBorderColor));
246
247      return buttonsPanel;
248    }
249
250    /** {@inheritDoc} */
251    public Component getPreferredFocusComponent()
252    {
253      return doNotSaveButton;
254    }
255
256    /** {@inheritDoc} */
257    public void okClicked()
258    {
259    }
260
261    /** {@inheritDoc} */
262    public LocalizableMessage getTitle()
263    {
264      return INFO_CTRL_PANEL_UNSAVED_CHANGES_DIALOG_TITLE.get();
265    }
266
267    /** {@inheritDoc} */
268    public void configurationChanged(ConfigurationChangeEvent ev)
269    {
270    }
271
272    /** {@inheritDoc} */
273    public GenericDialog.ButtonType getButtonType()
274    {
275      return GenericDialog.ButtonType.NO_BUTTON;
276    }
277
278    /** {@inheritDoc} */
279    public boolean isDisposeOnClose()
280    {
281      return true;
282    }
283  }
284}