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