1 package org.apache.turbine.util; 2 3 56 57 import java.text.NumberFormat ; 58 import java.util.Calendar ; 59 import java.util.Date ; 60 import org.apache.ecs.ConcreteElement; 61 import org.apache.ecs.Element; 62 import org.apache.ecs.ElementContainer; 63 import org.apache.ecs.GenericElement; 64 import org.apache.ecs.html.Comment; 65 import org.apache.ecs.html.Input; 66 import org.apache.ecs.html.Option; 67 import org.apache.ecs.html.Select; 68 69 109 public class TimeSelector 110 { 111 112 public static final String DEFAULT_PREFIX = "TimeSelector"; 113 114 115 public static final String HOUR_SUFFIX = "_hour"; 116 117 118 public static final String MINUTE_SUFFIX = "_minute"; 119 120 121 public static final String SECOND_SUFFIX = "_second"; 122 123 124 public static final String AMPM_SUFFIX = "_ampm"; 125 126 127 public static final int TWELVE_HOUR = 0; 128 129 130 public static final int TWENTY_FOUR_HOUR = 1; 131 132 133 private static final NumberFormat nbrFmt; 134 135 private static final int DEFAULT_MINUTE_INTERVAL = 5; 136 private static final int DEFAULT_SECOND_INTERVAL = 5; 137 private static final int DEFAULT_TIME_FORMAT = TWELVE_HOUR; 138 139 private int timeFormat = DEFAULT_TIME_FORMAT; 140 private int minuteInterval = DEFAULT_MINUTE_INTERVAL; 141 private int secondInterval = DEFAULT_SECOND_INTERVAL; 142 143 private Calendar useDate = null; 144 private String selName = null; 145 private String onChange = null; 146 private boolean onChangeSet = false; 147 private boolean showSeconds = false; 148 private int setSeconds = 0; 149 150 static 151 { 152 nbrFmt = NumberFormat.getInstance(); 153 nbrFmt.setMinimumIntegerDigits(2); 154 nbrFmt.setMaximumIntegerDigits(2); 155 } 156 157 161 public TimeSelector( ) 162 { 163 this.selName = DEFAULT_PREFIX; 164 this.useDate = Calendar.getInstance(); 165 this.useDate.setTime ( new Date () ); 166 } 167 168 175 public TimeSelector( String selName, Calendar useDate ) 176 { 177 this.useDate = useDate; 178 this.selName = selName; 179 } 180 181 186 public TimeSelector( String selName ) 187 { 188 this.selName = selName; 189 this.useDate = Calendar.getInstance(); 190 this.useDate.setTime ( new Date () ); 191 } 192 193 204 public TimeSelector setOnChange ( String onChange ) 205 { 206 if (onChange != null) 207 { 208 this.onChange = onChange; 209 this.onChangeSet = true; 210 } 211 else 212 { 213 this.onChange = null; 214 this.onChangeSet = false; 215 } 216 return this; 217 } 218 219 227 public TimeSelector setSeconds( int seconds ) 228 { 229 this.setSeconds = seconds; 230 this.showSeconds = false; 231 return this; 232 } 233 234 242 public TimeSelector setMinuteInterval( int minutes ) 243 { 244 this.minuteInterval = minutes; 245 return this; 246 } 247 248 256 public TimeSelector setSecondInterval( int seconds ) 257 { 258 this.secondInterval = seconds; 259 return this; 260 } 261 262 270 public TimeSelector setTimeFormat( int format ) 271 { 272 this.timeFormat = format; 273 return this; 274 } 275 276 285 public TimeSelector setShowSeconds ( boolean show ) 286 { 287 this.showSeconds = show; 288 return this; 289 } 290 291 297 public void setSelName( String selName ) 298 { 299 this.selName = selName; 300 } 301 302 307 public String getSelName() 308 { 309 return selName; 310 } 311 312 318 public static Select getSecondSelector(String name) 319 { 320 return(getSecondSelector(name, Calendar.getInstance())); 321 } 322 323 330 public static Select getSecondSelector(String name, Calendar now) 331 { 332 return(getSecondSelector(name, Calendar.getInstance(), 333 DEFAULT_SECOND_INTERVAL)); 334 } 335 336 344 public static Select getSecondSelector(String name, Calendar now, 345 int interval) 346 { 347 Select secondSelect = new Select().setName(name); 348 349 for(int currentSecond=0; currentSecond <= 59; currentSecond+=interval) 350 { 351 Option o = new Option(); 352 o.addElement(nbrFmt.format(currentSecond)); 353 o.setValue(currentSecond); 354 int nearestSecond = 355 ((now.get(Calendar.SECOND) / interval) * interval); 356 357 if (nearestSecond == currentSecond) 358 { 359 o.setSelected(true); 360 } 361 secondSelect.addElement(o); 362 } 363 return(secondSelect); 364 } 365 366 372 public static Select getMinuteSelector(String name) 373 { 374 return(getMinuteSelector(name, Calendar.getInstance())); 375 } 376 377 383 public static Select getMinuteSelector(String name, Calendar now) 384 { 385 return(getMinuteSelector(name, now, DEFAULT_MINUTE_INTERVAL)); 386 } 387 388 396 public static Select getMinuteSelector(String name, Calendar now, 397 int interval) 398 { 399 Select minuteSelect = new Select().setName(name); 400 401 for (int curMinute = 0;curMinute <= 59; curMinute += interval) 402 { 403 Option o = new Option(); 404 o.addElement(nbrFmt.format(curMinute)); 405 o.setValue(curMinute); 406 int nearestMinute = 407 ((now.get(Calendar.MINUTE)) / interval) * interval; 408 409 if (nearestMinute == curMinute) 410 { 411 o.setSelected(true); 412 } 413 minuteSelect.addElement(o); 414 } 415 return(minuteSelect); 416 } 417 418 424 public static Select getHourSelector(String name) 425 { 426 return(getHourSelector(name, Calendar.getInstance())); 427 } 428 429 436 public static Select getHourSelector(String name, Calendar now) 437 { 438 return(getHourSelector(name, Calendar.getInstance(), TWELVE_HOUR)); 439 } 440 441 450 public static Select getHourSelector(String name, Calendar now, int format) 451 { 452 Select hourSelect = new Select().setName(name); 453 454 if (format == TWENTY_FOUR_HOUR) 455 { 456 for (int currentHour = 0; currentHour <= 23; currentHour++) 457 { 458 Option o = new Option(); 459 o.addElement(nbrFmt.format(currentHour)); 460 o.setValue(currentHour); 461 if (now.get(Calendar.HOUR_OF_DAY) == currentHour) 462 { 463 o.setSelected(true); 464 } 465 hourSelect.addElement(o); 466 } 467 } 468 else 469 { 470 for (int curHour = 1;curHour <= 12; curHour++) 471 { 472 Option o = new Option(); 473 474 o.addElement(nbrFmt.format((long)curHour)); 475 o.setValue(curHour); 476 if (now.get(Calendar.AM_PM) == Calendar.AM) 477 { 478 if (((now.get(Calendar.HOUR_OF_DAY)) == 0) && 479 (curHour == 12)) 480 { 481 o.setSelected(true); 482 } 483 else 484 { 485 if (now.get(Calendar.HOUR_OF_DAY) == curHour) 486 { 487 o.setSelected(true); 488 } 489 } 490 } 491 else 492 { 493 if (((now.get(Calendar.HOUR_OF_DAY)) == 12) && 494 (curHour == 12)) 495 { 496 o.setSelected(true); 497 } 498 else 499 { 500 if (now.get(Calendar.HOUR_OF_DAY) == curHour+12) 501 { 502 o.setSelected(true); 503 } 504 } 505 } 506 hourSelect.addElement(o); 507 } 508 } 509 return(hourSelect); 510 } 511 512 518 public static Select getAMPMSelector(String name) 519 { 520 Calendar c = Calendar.getInstance(); 521 c.setTime(new Date ()); 522 return(getAMPMSelector(name, c)); 523 } 524 525 532 public static Select getAMPMSelector(String name, 533 Calendar now) 534 { 535 Select ampmSelect = new Select().setName(name); 536 537 Option o = new Option(); 538 o.addElement("am"); 539 o.setValue(Calendar.AM); 540 if (now.get(Calendar.AM_PM) == Calendar.AM) 541 { 542 o.setSelected(true); 543 } 544 ampmSelect.addElement(o); 545 546 o = new Option(); 547 o.addElement("pm"); 548 o.setValue(Calendar.PM); 549 if (now.get(Calendar.AM_PM) == Calendar.PM) 550 { 551 o.setSelected(true); 552 } 553 ampmSelect.addElement(o); 554 555 return(ampmSelect); 556 } 557 558 582 public String output() 583 { 584 return(ecsOutput().toString()); 585 } 586 587 611 public String toString() 612 { 613 return(ecsOutput().toString()); 614 } 615 616 621 public ElementContainer ecsOutput() 622 { 623 if ( this.useDate == null ) 624 { 625 this.useDate = Calendar.getInstance(); 626 this.useDate.setTime ( new Date () ); 627 } 628 629 ConcreteElement secondSelect = null; 630 631 Select ampmSelect = getAMPMSelector(selName + AMPM_SUFFIX, useDate); 632 633 Select hourSelect = getHourSelector(selName + HOUR_SUFFIX, 634 useDate, this.timeFormat); 635 636 Select minuteSelect = getMinuteSelector(selName + MINUTE_SUFFIX, 637 useDate, this.minuteInterval); 638 639 if (this.showSeconds) 640 { 641 Select tmp = getSecondSelector(selName + SECOND_SUFFIX, useDate, 642 this.secondInterval); 643 if (onChangeSet) 644 tmp.setOnChange(onChange); 645 secondSelect = tmp; 646 } 647 else 648 { 649 secondSelect = new Input(Input.hidden, 650 selName + SECOND_SUFFIX, 651 setSeconds); 652 } 653 654 if (onChangeSet) 655 { 656 hourSelect.setOnChange(onChange); 657 minuteSelect.setOnChange(onChange); 658 ampmSelect.setOnChange(onChange); 659 } 660 661 ElementContainer ec = new ElementContainer(); 662 ec.addElement(new Comment( 663 "== BEGIN org.apache.turbine.util.TimeSelector.ecsOutput() ==")); 664 ec.addElement(hourSelect); 665 ec.addElement(":"); 666 ec.addElement(minuteSelect); 667 if (this.showSeconds == true) 668 ec.addElement(":"); 669 ec.addElement(secondSelect); 670 if (this.timeFormat == this.TWELVE_HOUR) 671 { 672 ec.addElement(ampmSelect); 673 } 674 ec.addElement(new Comment( 675 "== END org.apache.turbine.util.TimeSelector.ecsOutput() ==")); 676 return (ec); 677 } 678 } 679 | Popular Tags |