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