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