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 */
026package org.opends.guitools.controlpanel.ui.components;
027
028import static org.opends.messages.AdminToolMessages.*;
029
030import java.awt.GridBagConstraints;
031import java.awt.GridBagLayout;
032import java.awt.event.ActionEvent;
033import java.awt.event.ActionListener;
034import java.text.DateFormat;
035import java.util.Date;
036
037import javax.swing.Box;
038import javax.swing.JButton;
039import javax.swing.JLabel;
040import javax.swing.JPanel;
041
042import org.opends.guitools.controlpanel.datamodel.ScheduleType;
043import org.opends.guitools.controlpanel.ui.GenericDialog;
044import org.opends.guitools.controlpanel.ui.TaskToSchedulePanel;
045import org.opends.guitools.controlpanel.util.Utilities;
046
047/**
048 * A class used as component displaying the string representation of a schedule
049 * and the possibility of updating it clicking a button.
050 */
051public class ScheduleSummaryPanel extends JPanel
052{
053  private static final long serialVersionUID = 3111141404599060028L;
054  private ScheduleType schedule = ScheduleType.createLaunchNow();
055  private JLabel label;
056  private JButton change;
057  private TaskToSchedulePanel schedulePanel;
058  private GenericDialog scheduleDlg;
059  private String taskName;
060  private DateFormat formatter =
061    DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT);
062
063  /**
064   * Default constructor.
065   * @param taskName the name of the task to be scheduled.
066   */
067  public ScheduleSummaryPanel(String taskName)
068  {
069    super(new GridBagLayout());
070    setOpaque(false);
071    this.taskName = taskName;
072    createLayout();
073  }
074
075  /**
076   * Returns the schedule represented by this panel.
077   * @return the schedule represented by this panel.
078   */
079  public ScheduleType getSchedule()
080  {
081    return schedule;
082  }
083
084  /**
085   * Sets the schedule represented by this panel.
086   * @param schedule the schedule represented by this panel.
087   */
088  public void setSchedule(ScheduleType schedule)
089  {
090    this.schedule = schedule;
091    updateLabel(schedule);
092  }
093
094  /**
095   * Returns whether the change button is enabled or not.
096   * @return <CODE>true</CODE> if the change button is enabled and
097   * <CODE>false</CODE> otherwise.
098   */
099  public boolean isChangeEnabled()
100  {
101    return change.isEnabled();
102  }
103
104  /**
105   * Sets the enable state of the change button.
106   * @param enable whether the change button must be enabled or not.
107   */
108  public void setChangeEnabled(boolean enable)
109  {
110    change.setEnabled(enable);
111  }
112
113  private void createLayout()
114  {
115    GridBagConstraints gbc = new GridBagConstraints();
116    gbc.gridx = 0;
117    gbc.gridy = 0;
118    label = Utilities.createDefaultLabel();
119    change = Utilities.createButton(INFO_CTRL_PANEL_CHANGE_SCHEDULE.get());
120    change.addActionListener(new ActionListener()
121    {
122      public void actionPerformed(ActionEvent ev)
123      {
124        changeButtonClicked();
125      }
126    });
127    updateLabel(schedule);
128
129    gbc.fill = GridBagConstraints.NONE;
130    add(label, gbc);
131    gbc.gridx ++;
132    gbc.insets.left = 10;
133    add(change, gbc);
134    gbc.gridx ++;
135    gbc.weightx = 1.0;
136    gbc.fill = GridBagConstraints.HORIZONTAL;
137    gbc.insets.left = 0;
138    add(Box.createHorizontalGlue(), gbc);
139  }
140
141  private void updateLabel(ScheduleType schedule)
142  {
143    ScheduleType.Type type = schedule.getType();
144    if (type == ScheduleType.Type.LAUNCH_NOW)
145    {
146      label.setText(INFO_CTRL_PANEL_LAUNCH_NOW_SUMMARY.get().toString());
147    }
148    else if (type == ScheduleType.Type.LAUNCH_LATER)
149    {
150      Date date = schedule.getLaunchLaterDate();
151      String sDate = formatter.format(date);
152      label.setText(INFO_CTRL_PANEL_LAUNCH_LATER_SUMMARY.get(sDate).toString());
153    }
154    else if (type == ScheduleType.Type.LAUNCH_PERIODICALLY)
155    {
156      String cron = schedule.getCronValue();
157      label.setText(
158          INFO_CTRL_PANEL_LAUNCH_PERIODICALLY_SUMMARY.get(cron).toString());
159    }
160    else
161    {
162      throw new RuntimeException("Unknown schedule type: "+type);
163    }
164  }
165
166  private void changeButtonClicked()
167  {
168    if (schedulePanel == null)
169    {
170      schedulePanel = new TaskToSchedulePanel(taskName);
171      scheduleDlg = new GenericDialog(Utilities.getFrame(this), schedulePanel);
172      Utilities.centerGoldenMean(scheduleDlg, Utilities.getParentDialog(this));
173      scheduleDlg.setModal(true);
174    }
175    scheduleDlg.setVisible(true);
176    if (!schedulePanel.isCanceled())
177    {
178      setSchedule(schedulePanel.getSchedule());
179    }
180  }
181}