1 19 package org.openharmonise.swing; 20 21 import java.awt.Color ; 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.Dimension ; 25 import java.awt.Font ; 26 import java.awt.LayoutManager ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.awt.event.KeyEvent ; 30 import java.awt.event.KeyListener ; 31 import java.text.ParseException ; 32 import java.text.SimpleDateFormat ; 33 import java.util.ArrayList ; 34 import java.util.Calendar ; 35 import java.util.Date ; 36 import java.util.Iterator ; 37 38 import javax.swing.BorderFactory ; 39 import javax.swing.JButton ; 40 import javax.swing.JLabel ; 41 import javax.swing.JPanel ; 42 import javax.swing.JSpinner ; 43 import javax.swing.SpinnerDateModel ; 44 import javax.swing.SpinnerModel ; 45 import javax.swing.SwingUtilities ; 46 import javax.swing.event.ChangeEvent ; 47 import javax.swing.event.ChangeListener ; 48 49 55 public class DateSelector extends JPanel 56 implements LayoutManager , ChangeListener , ActionListener , Runnable , KeyListener { 57 58 public static final String CLEAR_ACTION = "CLEAR"; 59 private static final String XSD_DATE_FORMAT = "yyyy-MM-dd G'T'HH:mm:ss"; 60 private SimpleDateFormat m_df = new java.text.SimpleDateFormat (XSD_DATE_FORMAT); 61 62 private JSpinner m_dateSpinner = null; 63 private JSpinner m_timeSpinner = null; 64 65 private JLabel m_dateLabel = null; 66 private JLabel m_timeLabel = null; 67 68 private Date m_maxDate = null; 69 private Date m_minDate = null; 70 private Date m_initDate = new Date (); 71 72 private boolean m_bShowTime = false; 73 private boolean m_bShowDate = true; 74 private boolean m_bShowClearButton = true; 75 76 private String m_sDateFormat = "yyyy/MM/dd"; 77 private String m_sTimeFormat = "HH:mm"; 78 79 private ArrayList m_changeListeners = new ArrayList (); 80 81 private JButton m_clearButton = null; 82 83 private boolean m_bCleared = false; 84 85 88 public DateSelector() { 89 super(); 90 this.setup(); 91 } 92 93 public boolean isCleared() { 94 return this.m_bCleared; 95 } 96 97 public void setInitalDate(Date initDate) { 98 this.m_initDate = initDate; 99 this.setup(); 100 } 101 102 public void setMaxDate(Date maxDate) { 103 this.m_maxDate = maxDate; 104 this.setup(); 105 } 106 107 public void setMinDate(Date minDate) { 108 this.m_minDate = minDate; 109 this.setup(); 110 } 111 112 public void setShowTime(boolean bShowTime) { 113 this.m_bShowTime = bShowTime; 114 this.setup(); 115 } 116 117 public boolean isShowingDate() { 118 return this.m_bShowDate; 119 } 120 121 public boolean isShowingTime() { 122 return this.m_bShowTime; 123 } 124 125 public boolean isShowingClearButton() { 126 return this.m_bShowClearButton; 127 } 128 129 public void setShowDate(boolean bShowDate) { 130 this.m_bShowDate = bShowDate; 131 this.setup(); 132 } 133 134 public void setDateFormat(String sDateFormat) { 135 this.m_sDateFormat = sDateFormat; 136 this.setup(); 137 } 138 139 public void setShowClearButton(boolean bShowClearButton) { 140 this.m_bShowClearButton = bShowClearButton; 141 this.setup(); 142 } 143 144 public void addChangeListener(ChangeListener changeListener) { 145 this.m_changeListeners.add(changeListener); 146 } 147 148 public void setTimeFormat(String sTimeFormat) { 149 this.m_sTimeFormat = sTimeFormat; 150 this.setup(); 151 } 152 153 private void setup() { 154 155 if(this.m_dateSpinner!=null) { 156 this.m_dateSpinner.getModel().removeChangeListener(this); 157 } 158 if(this.m_timeSpinner!=null) { 159 this.m_timeSpinner.getModel().removeChangeListener(this); 160 } 161 162 this.setLayout(this); 163 this.removeAll(); 164 165 if(this.m_bShowDate) { 166 167 this.m_dateLabel = new JLabel ("Date"); 168 this.m_dateLabel.setFont(this.getFont()); 169 this.add(this.m_dateLabel); 170 171 this.m_dateSpinner = this.getDateSpinner(); 172 this.add(m_dateSpinner); 173 174 this.m_dateSpinner.getModel().addChangeListener(this); 175 ((JSpinner.DateEditor )this.m_dateSpinner.getEditor()).getTextField().addKeyListener(this); 176 } 177 178 if(this.m_bShowTime) { 179 180 this.m_timeLabel = new JLabel ("Time"); 181 this.m_timeLabel.setFont(this.getFont()); 182 this.add(this.m_timeLabel); 183 184 this.m_timeSpinner = this.getTimeSpinner(); 185 this.add(m_timeSpinner); 186 187 this.m_timeSpinner.getModel().addChangeListener(this); 188 ((JSpinner.DateEditor )this.m_timeSpinner.getEditor()).getTextField().addKeyListener(this); 189 } 190 191 if(this.m_bShowClearButton) { 192 this.m_clearButton = new JButton ("Clear"); 193 this.m_clearButton.setFont(this.getFont()); 194 this.m_clearButton.setActionCommand(CLEAR_ACTION); 195 this.m_clearButton.addActionListener(this); 196 this.add(m_clearButton); 197 } 198 this.setCleared(this.m_bCleared); 199 } 200 201 public Date getValue() { 202 if(this.m_bCleared) { 203 return null; 204 } else { 205 StringBuffer sBuff = new StringBuffer (); 206 if(this.m_bShowDate) { 207 sBuff.append(this.getDateValue()); 208 } else { 209 SimpleDateFormat dtDF = new SimpleDateFormat ("yyyy-MM-dd G"); 210 sBuff.append( dtDF.format(new Date ()) ); 211 } 212 213 if(this.m_bShowTime) { 214 sBuff.append("T") 215 .append(this.getTimeValue()) 216 .append(":00"); 217 } else { 218 sBuff.append("T00:00:00"); 219 } 220 221 Date dtRetn = null; 222 223 try { 224 dtRetn = this.m_df.parse(sBuff.toString()); 225 } catch (ParseException e) { 226 e.printStackTrace(); 227 } 228 229 return dtRetn; 230 } 231 } 232 233 private String getDateValue() { 234 SimpleDateFormat dtDF = new SimpleDateFormat ( "yyyy-MM-dd G" ); 235 String sValue = dtDF.format( ((Date ) ((JSpinner )this.m_dateSpinner).getValue()) ); 236 return sValue.trim(); 237 } 238 239 private String getTimeValue() { 240 SimpleDateFormat dtDF = new SimpleDateFormat ( "HH:mm:ss" ); 241 String sValue = dtDF.format( ((Date ) ((JSpinner )this.m_timeSpinner).getValue()) ); 242 return sValue.trim(); 243 } 244 245 private JSpinner getDateSpinner() { 246 JSpinner spinner = null; 247 248 if(this.m_minDate!=null && this.m_initDate.getTime()<this.m_minDate.getTime()) { 249 this.m_initDate = this.m_minDate; 250 } 251 252 if(this.m_maxDate!=null && this.m_initDate.getTime()>this.m_maxDate.getTime()) { 253 this.m_initDate = this.m_maxDate; 254 } 255 256 SpinnerModel dateModel = new SpinnerDateModel (this.m_initDate,this.m_minDate,this.m_maxDate,Calendar.YEAR); 257 258 spinner = new JSpinner (dateModel); 259 spinner.setBorder(BorderFactory.createEtchedBorder()); 260 261 spinner.setEditor(new JSpinner.DateEditor (spinner, this.m_sDateFormat)); 262 263 return spinner; 264 } 265 266 private JSpinner getTimeSpinner() { 267 JSpinner spinner = null; 268 269 SpinnerModel dateModel = dateModel = new SpinnerDateModel (this.m_initDate,this.m_minDate,this.m_maxDate,Calendar.YEAR); 270 271 spinner = new JSpinner (dateModel); 272 spinner.setBorder(BorderFactory.createEtchedBorder()); 273 274 spinner.setEditor(new JSpinner.DateEditor (spinner, this.m_sTimeFormat)); 275 276 return spinner; 277 } 278 279 280 283 public void layoutContainer(Container arg0) { 284 int nXpos = 0; 285 286 if(this.m_bShowDate) { 287 this.m_dateLabel.setSize(25, 20); 288 this.m_dateLabel.setLocation(nXpos, 0); 289 nXpos = nXpos + this.m_dateLabel.getSize().width + 10; 290 291 this.m_dateSpinner.setSize(100, 20); 292 this.m_dateSpinner.setLocation(nXpos, 0); 293 nXpos = nXpos + this.m_dateSpinner.getSize().width + 10; 294 } 295 296 if(this.m_bShowTime) { 297 this.m_timeLabel.setSize(25, 20); 298 this.m_timeLabel.setLocation(nXpos, 0); 299 nXpos = nXpos + this.m_timeLabel.getSize().width + 10; 300 301 this.m_timeSpinner.setSize(50, 20); 302 this.m_timeSpinner.setLocation(nXpos, 0); 303 nXpos = nXpos + this.m_timeSpinner.getSize().width + 10; 304 } 305 306 if(this.m_bShowClearButton) { 307 this.m_clearButton.setSize(70, 20); 308 this.m_clearButton.setLocation(nXpos, 0); 309 } 310 } 311 312 315 public Dimension getPreferredSize() { 316 int nHeight = 25; 317 int nWidth = 0; 318 319 if(this.m_bShowTime) { 320 nWidth = nWidth + 135; 321 } 322 323 nWidth = nWidth + this.m_clearButton.getSize().width; 324 325 nWidth = nWidth + 215; 326 327 return new Dimension (nWidth, nHeight); 328 } 329 330 333 private DateSelector(boolean arg0) { 334 super(arg0); 335 } 336 337 340 private DateSelector(LayoutManager arg0) { 341 super(arg0); 342 } 343 344 348 private DateSelector(LayoutManager arg0, boolean arg1) { 349 super(arg0, arg1); 350 } 351 352 355 public void removeLayoutComponent(Component arg0) { 356 } 357 358 359 362 public void addLayoutComponent(String arg0, Component arg1) { 363 } 364 365 366 367 370 public Dimension minimumLayoutSize(Container arg0) { 371 return this.getPreferredSize(); 372 } 373 374 375 376 379 public Dimension preferredLayoutSize(Container arg0) { 380 return this.getPreferredSize(); 381 } 382 383 386 public void stateChanged(ChangeEvent ce) { 387 if(ce!=null) { 388 this.setCleared(false); 389 } 390 ChangeEvent ce2 = new ChangeEvent (this); 391 Iterator itor = this.m_changeListeners.iterator(); 392 while (itor.hasNext()) { 393 ChangeListener listener = (ChangeListener ) itor.next(); 394 listener.stateChanged(ce2); 395 } 396 } 397 398 401 public void keyReleased(KeyEvent ke) { 402 if(ke!=null) { 403 this.setCleared(false); 404 } 405 ChangeEvent ce2 = new ChangeEvent (this); 406 Iterator itor = this.m_changeListeners.iterator(); 407 while (itor.hasNext()) { 408 ChangeListener listener = (ChangeListener ) itor.next(); 409 listener.stateChanged(ce2); 410 } 411 } 412 413 public void setCleared(boolean bCleared) { 414 if(bCleared) { 415 this.m_bCleared = true; 416 417 if(this.m_dateSpinner!=null) { 418 ((JSpinner.DefaultEditor )((JSpinner )this.m_dateSpinner).getEditor()).getTextField().setForeground(Color.WHITE); 419 ((JSpinner.DefaultEditor )((JSpinner )this.m_dateSpinner).getEditor()).getTextField().setSelectedTextColor(Color.WHITE); 420 ((JSpinner.DefaultEditor )((JSpinner )this.m_dateSpinner).getEditor()).getTextField().setSelectionColor(Color.WHITE); 421 } 422 423 if(this.m_timeSpinner!=null) { 424 ((JSpinner.DefaultEditor )((JSpinner )this.m_timeSpinner).getEditor()).getTextField().setForeground(Color.WHITE); 425 ((JSpinner.DefaultEditor )((JSpinner )this.m_timeSpinner).getEditor()).getTextField().setSelectedTextColor(Color.WHITE); 426 ((JSpinner.DefaultEditor )((JSpinner )this.m_timeSpinner).getEditor()).getTextField().setSelectionColor(Color.WHITE); 427 } 428 SwingUtilities.invokeLater(this); 429 this.stateChanged(null); 430 } else { 431 this.m_bCleared = false; 432 433 if(this.m_dateSpinner!=null) { 434 ((JSpinner.DefaultEditor )((JSpinner )this.m_dateSpinner).getEditor()).getTextField().setForeground(Color.BLACK); 435 ((JSpinner.DefaultEditor )((JSpinner )this.m_dateSpinner).getEditor()).getTextField().setSelectedTextColor(Color.WHITE); 436 ((JSpinner.DefaultEditor )((JSpinner )this.m_dateSpinner).getEditor()).getTextField().setSelectionColor(Color.BLUE); 437 } 438 439 if(this.m_timeSpinner!=null) { 440 ((JSpinner.DefaultEditor )((JSpinner )this.m_timeSpinner).getEditor()).getTextField().setForeground(Color.BLACK); 441 ((JSpinner.DefaultEditor )((JSpinner )this.m_timeSpinner).getEditor()).getTextField().setSelectedTextColor(Color.WHITE); 442 ((JSpinner.DefaultEditor )((JSpinner )this.m_timeSpinner).getEditor()).getTextField().setSelectionColor(Color.BLUE); 443 } 444 SwingUtilities.invokeLater(this); 445 } 446 } 447 448 451 public void actionPerformed(ActionEvent ae) { 452 if(ae.getActionCommand().equals(CLEAR_ACTION)) { 453 this.setCleared(true); 454 } 455 } 456 457 460 public void run() { 461 this.revalidate(); 462 this.repaint(); 463 } 464 465 468 public void setEnabled(boolean bEnabled) { 469 super.setEnabled(bEnabled); 470 if(this.m_dateSpinner!=null) { 471 this.m_dateSpinner.setEnabled(bEnabled); 472 } 473 if(this.m_timeSpinner!=null) { 474 this.m_timeSpinner.setEnabled(bEnabled); 475 } 476 if(this.m_clearButton!=null) { 477 this.m_clearButton.setEnabled(bEnabled); 478 } 479 } 480 481 484 public void keyPressed(KeyEvent arg0) { 485 } 486 487 490 public void keyTyped(KeyEvent arg0) { 491 } 492 493 496 public void setFont(Font arg0) { 497 super.setFont(arg0); 498 if(this.m_dateLabel!=null) { 499 this.m_dateLabel.setFont(arg0); 500 } 501 if(this.m_timeLabel!=null) { 502 this.m_timeLabel.setFont(arg0); 503 } 504 } 505 506 } 507 | Popular Tags |