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-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.guitools.controlpanel.ui;
028
029import static org.opends.messages.AdminToolMessages.*;
030
031import java.awt.Component;
032import java.awt.GridBagConstraints;
033import java.util.LinkedHashSet;
034
035import javax.swing.JEditorPane;
036import javax.swing.JLabel;
037import javax.swing.JTextField;
038
039import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
040import org.opends.guitools.controlpanel.util.Utilities;
041import org.forgerock.i18n.LocalizableMessage;
042
043/**
044 * The panel that displays the refresh options of the control panel.  Basically
045 * it allows to set the refreshing period used by the control panel.
046 *
047 */
048public class RefreshOptionsPanel extends StatusGenericPanel
049{
050  private static final long serialVersionUID = 641533296295459469L;
051  private JTextField period;
052  private JLabel lPeriod;
053
054  private boolean isCanceled = true;
055
056  private int MAX_VALUE = 5000;
057
058  /**
059   * Default constructor.
060   *
061   */
062  public RefreshOptionsPanel()
063  {
064    super();
065    createLayout();
066  }
067
068  /** {@inheritDoc} */
069  public LocalizableMessage getTitle()
070  {
071    return INFO_CTRL_PANEL_REFRESH_PANEL_TITLE.get();
072  }
073
074  /**
075   * Creates the layout of the panel (but the contents are not populated here).
076   */
077  private void createLayout()
078  {
079    GridBagConstraints gbc = new GridBagConstraints();
080    gbc.anchor = GridBagConstraints.WEST;
081    gbc.weightx = 0.0;
082    gbc.gridx = 0;
083    gbc.gridy = 0;
084    gbc.gridwidth = 2;
085    gbc.weightx = 1.0;
086    gbc.fill = GridBagConstraints.BOTH;
087
088    String text = INFO_CTRL_PANEL_REFRESH_OPTIONS_PANEL_TEXT.get().toString();
089
090    JEditorPane pane = Utilities.makeHtmlPane(text,
091        ColorAndFontConstants.defaultFont);
092
093    Utilities.updatePreferredSize(pane, 60, text,
094        ColorAndFontConstants.defaultFont, false);
095    gbc.weighty = 0.0;
096    add(pane, gbc);
097
098    gbc.gridy = 1;
099    gbc.gridwidth = 1;
100    gbc.weightx = 0.0;
101    gbc.weighty = 0.0;
102    lPeriod =Utilities.createPrimaryLabel(
103        INFO_CTRL_PANEL_REFRESH_OPTIONS_LABEL.get());
104    gbc.insets.top = 10;
105    add(lPeriod, gbc);
106    period = Utilities.createShortTextField();
107    gbc.insets.left = 10;
108    gbc.gridx = 1;
109    gbc.weightx = 1.0;
110    add(period, gbc);
111
112    gbc.gridwidth = 2;
113    addBottomGlue(gbc);
114  }
115
116  /** {@inheritDoc} */
117  public GenericDialog.ButtonType getButtonType()
118  {
119    return GenericDialog.ButtonType.OK_CANCEL;
120  }
121
122  /** {@inheritDoc} */
123  public Component getPreferredFocusComponent()
124  {
125    return period;
126  }
127
128  /** {@inheritDoc} */
129  public void configurationChanged(ConfigurationChangeEvent ev)
130  {
131  }
132
133  /** {@inheritDoc} */
134  public void okClicked()
135  {
136    isCanceled = true;
137
138    setPrimaryValid(lPeriod);
139    LinkedHashSet<LocalizableMessage> errors = new LinkedHashSet<>();
140    long t = -1;
141    try
142    {
143      t = Long.parseLong(period.getText());
144    }
145    catch (Throwable th)
146    {
147    }
148    if (t <= 0 || t > MAX_VALUE)
149    {
150      errors.add(INFO_CTRL_PANEL_INVALID_PERIOD_VALUE.get(MAX_VALUE));
151    }
152
153    if (!errors.isEmpty())
154    {
155      displayErrorDialog(errors);
156    }
157    else
158    {
159      isCanceled = false;
160      Utilities.getParentDialog(this).setVisible(false);
161    }
162  }
163
164  /**
165   * Returns whether this dialog has been cancelled or not.
166   * @return whether this dialog has been cancelled or not.
167   */
168  public boolean isCanceled()
169  {
170    return isCanceled;
171  }
172
173  /** {@inheritDoc} */
174  public void toBeDisplayed(boolean visible)
175  {
176    if (visible)
177    {
178      isCanceled = true;
179      long timeInSeconds = getInfo().getPoolingPeriod() / 1000;
180      period.setText(String.valueOf(timeInSeconds));
181    }
182  }
183
184  /**
185   * Returns the time specified by the user in milliseconds.
186   * @return the time specified by the user in milliseconds.
187   */
188  public long getPoolingPeriod()
189  {
190    long t = -1;
191    try
192    {
193      t = 1000 * Long.parseLong(period.getText());
194    }
195    catch (Throwable th)
196    {
197    }
198    return t;
199  }
200}