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