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