1 19 20 package org.lucane.client.widgets; 21 22 import java.awt.Component ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.GridLayout ; 26 import java.awt.Insets ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.util.Calendar ; 30 import java.util.Date ; 31 32 import javax.swing.JButton ; 33 import javax.swing.JComboBox ; 34 import javax.swing.JFrame ; 35 import javax.swing.JLabel ; 36 import javax.swing.JPanel ; 37 import javax.swing.JSpinner ; 38 import javax.swing.event.ChangeEvent ; 39 import javax.swing.event.ChangeListener ; 40 41 import org.lucane.client.Plugin; 42 import org.lucane.client.util.Translation; 43 44 49 public class DateTimePicker extends JPanel implements ActionListener , ChangeListener { 50 private Plugin plugin; 51 52 private JComboBox day; 53 private JComboBox month; 54 private JComboBox year; 55 private JSpinner hour; 56 private JSpinner minute; 57 private JLabel timeSeparator; 58 private JLabel dateTimeSeparator; 59 60 private boolean showDate; 61 private boolean showTime; 62 63 private Calendar cal; 64 65 69 public DateTimePicker() { 70 this(new Date ()); 71 } 72 73 78 public DateTimePicker(Date date) { 79 cal = Calendar.getInstance(); 80 long t = date.getTime(); 81 cal.setTime(new Date ((t / 60000) * 60000)); 82 showDate=true; 83 showTime=true; 84 initLayout(); 85 updateComponents(); 86 } 87 88 private void initLayout() { 89 day = new JComboBox (); 90 month = new JComboBox (); 91 year = new JComboBox (); 92 hour = new JSpinner (); 93 minute = new JSpinner (); 94 timeSeparator = new JLabel (":"); 95 dateTimeSeparator = new JLabel ("-"); 96 GridBagLayout gbl = new GridBagLayout (); 97 98 setLayout(new GridBagLayout ()); 99 GridBagConstraints c = new GridBagConstraints (); 100 101 c.insets=new Insets (2,2,2,2); 102 c.anchor=GridBagConstraints.WEST; 103 c.fill=GridBagConstraints.HORIZONTAL; 104 c.gridy=0; 105 c.gridx=0; 106 c.weightx=0; 107 c.weighty=0; 108 c.fill=GridBagConstraints.BOTH; 109 110 c.gridx=1; 111 c.weightx=1; 112 add(day, c); 113 114 c.gridx=3; 115 c.weightx=1; 116 add(month, c); 117 118 c.gridx=5; 119 c.weightx=1; 120 add(year, c); 121 122 c.gridx=6; 123 c.weightx=0; 124 add(dateTimeSeparator, c); 125 126 c.gridx=7; 127 c.weightx=2; 128 add(hour, c); 129 130 c.gridx=8; 131 c.weightx=0; 132 add(timeSeparator, c); 133 134 c.gridx=9; 135 c.weightx=2; 136 add(minute, c); 137 } 138 139 private void updateComponents() 140 { 141 int maxDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 142 int currentDay = cal.get(Calendar.DAY_OF_MONTH); 143 day.removeActionListener(this); 144 day.removeAllItems(); 145 for(int i=1;i<=maxDays;i++) 146 day.addItem(new Integer (i)); 147 day.setSelectedIndex(currentDay-1); 148 day.addActionListener(this); 149 150 int monthIndex = cal.get(Calendar.MONTH); 151 month.removeActionListener(this); 152 month.removeAllItems(); 153 for(int i=1;i<=12;i++) 154 month.addItem(Translation.tr("DateTimePicker.month." + i)); 155 month.setSelectedIndex(monthIndex); 156 month.addActionListener(this); 157 158 int currentYear = cal.get(Calendar.YEAR); 159 year.removeActionListener(this); 160 year.removeAllItems(); 161 for(int i=-3;i<4;i++) 162 year.addItem(new Integer (currentYear+i)); 163 year.setSelectedIndex(3); 164 year.addActionListener(this); 165 166 int currentHour = cal.get(Calendar.HOUR_OF_DAY); 167 hour.removeChangeListener(this); 168 hour.setValue(new Integer (currentHour)); 169 hour.addChangeListener(this); 170 171 int currentMinute = cal.get(Calendar.MINUTE); 172 minute.removeChangeListener(this); 173 minute.setValue(new Integer (currentMinute)); 174 minute.addChangeListener(this); 175 176 } 177 178 182 public void actionPerformed(ActionEvent ae) 183 { 184 if(ae.getSource() == day) 185 { 186 cal.set(Calendar.DAY_OF_MONTH, day.getSelectedIndex()+1); 187 updateComponents(); 188 } 189 else if(ae.getSource() == month) 190 { 191 cal.set(Calendar.MONTH, month.getSelectedIndex()); 192 updateComponents(); 193 } 194 else if(ae.getSource() == year) 195 { 196 cal.set(Calendar.YEAR, ((Integer )year.getSelectedItem()).intValue()); 197 updateComponents(); 198 } 199 } 200 201 205 public void stateChanged(ChangeEvent ce) { 206 if(ce.getSource() == hour) 207 { 208 int currentHour = ((Integer )hour.getValue()).intValue(); 209 if (currentHour >= 24) 210 hour.setValue(new Integer (currentHour % 24)); 211 if (currentHour < 0) { 212 hour.setValue(new Integer (currentHour % 24 + 24)); 213 } 214 cal.set(Calendar.HOUR_OF_DAY, ((Integer )hour.getValue()).intValue()); 215 } 216 else if(ce.getSource() == minute) 217 { 218 int currentMinute = ((Integer )minute.getValue()).intValue(); 219 if (currentMinute >= 60) 220 minute.setValue(new Integer (currentMinute % 60)); 221 if (currentMinute < 0) 222 minute.setValue(new Integer (currentMinute % 60 + 60)); 223 cal.set(Calendar.MINUTE, ((Integer )minute.getValue()).intValue()); 224 } 225 } 226 227 228 232 public void setDate(Date date) { 233 long t = date.getTime(); 234 cal.setTime(new Date ((t / 60000) * 60000)); 235 updateComponents(); 236 } 237 238 242 public Date getDate() { 243 return cal.getTime(); 244 } 245 246 public boolean isShowDate() { 247 return showDate; 248 } 249 public void setShowDate(boolean showDate) { 250 this.showDate = showDate; 251 updateVisibility(); 252 } 253 public boolean isShowTime() { 254 return showTime; 255 } 256 public void setShowTime(boolean showTime) { 257 this.showTime = showTime; 258 updateVisibility(); 259 } 260 private void updateVisibility() { 261 day.setVisible(showDate); 262 month.setVisible(showDate); 263 year.setVisible(showDate); 264 dateTimeSeparator.setVisible(showDate && showTime); 265 hour.setVisible(showTime); 266 timeSeparator.setVisible(showTime); 267 minute.setVisible(showTime); 268 } 269 270 273 public static void main(String [] args) { 274 JFrame jf = new JFrame (); 275 276 final DateTimePicker dp = new DateTimePicker(); 277 jf.getContentPane().setLayout(new GridLayout (3,1)); 278 jf.getContentPane().add(dp); 279 281 JButton jbg = new JButton ("get date"); 282 jbg.addActionListener(new ActionListener () { 283 public void actionPerformed(ActionEvent e) { 284 DialogBox.info(dp.getDate().toString()); 285 } 286 }); 287 jf.getContentPane().add(jbg); 288 289 JButton jbs = new JButton ("set current date"); 290 jbs.addActionListener(new ActionListener () { 291 public void actionPerformed(ActionEvent e) { 292 dp.setDate(new Date ()); 293 } 294 }); 295 jf.getContentPane().add(jbs); 296 jf.setSize(300,300); 297 jf.show(); 298 } 299 300 public void setEnabled(boolean enabled) { 301 super.setEnabled(enabled); 302 Component [] c = getComponents(); 303 for (int i=0; i<c.length; i++) 304 c[i].setEnabled(enabled); 305 } 306 } | Popular Tags |