1 package org.apache.turbine.util; 2 3 56 57 import java.text.DateFormatSymbols ; 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 98 public class DateSelector 99 { 100 101 public static final String DEFAULT_PREFIX = "DateSelector"; 102 103 104 public static final String DAY_SUFFIX = "_day"; 105 106 107 public static final String MONTH_SUFFIX = "_month"; 108 109 110 public static final String YEAR_SUFFIX = "_year"; 111 112 private Calendar useDate = null; 113 private String selName = null; 114 private static final String [] monthName = 115 new DateFormatSymbols ().getMonths(); 116 private String onChange = null; 117 private boolean onChangeSet = false; 118 private boolean showDays = true; 119 private int setDay = 0; 120 private boolean useYears = false; 121 private int firstYear = 0; 122 private int lastYear = 0; 123 private int selectedYear = 0; 124 125 129 public DateSelector( ) 130 { 131 this.selName = DEFAULT_PREFIX; 132 this.useDate = Calendar.getInstance(); 133 this.useDate.setTime ( new Date () ); 134 } 135 136 143 public DateSelector( String selName, Calendar useDate ) 144 { 145 this.useDate = useDate; 146 this.selName = selName; 147 } 148 149 154 public DateSelector( String selName ) 155 { 156 this.selName = selName; 157 this.useDate = Calendar.getInstance(); 158 this.useDate.setTime ( new Date () ); 159 } 160 161 171 public DateSelector setOnChange ( String onChange ) 172 { 173 if (onChange != null) 174 { 175 this.onChange = onChange; 176 this.onChangeSet = true; 177 } 178 else 179 { 180 this.onChange = null; 181 this.onChangeSet = false; 182 } 183 return this; 184 } 185 186 194 public DateSelector setDay( int day ) 195 { 196 this.setDay = day; 197 this.showDays = false; 198 return this; 199 } 200 201 210 public DateSelector setShowDay ( boolean show ) 211 { 212 this.showDays = false; 213 return this; 214 } 215 216 222 public void setSelName( String selName ) 223 { 224 this.selName = selName; 225 } 226 227 232 public String getSelName() 233 { 234 return selName; 235 } 236 237 243 public static Select getMonthSelector(String name) 244 { 245 return(getMonthSelector(name, Calendar.getInstance())); 246 } 247 248 259 public static Select getMonthSelector(String name, 260 Calendar now) 261 { 262 Select monthSelect = new Select().setName(name); 263 264 for (int curMonth = 0;curMonth <= 11; curMonth++) 265 { 266 Option o = new Option(); 267 o.addElement(monthName[curMonth]); 268 o.setValue(curMonth); 269 if ((now.get(Calendar.MONTH)) == curMonth) 270 { 271 o.setSelected(true); 272 } 273 monthSelect.addElement(o); 274 } 275 return(monthSelect); 276 } 277 278 284 public static Select getDaySelector(String name) 285 { 286 return(getDaySelector(name, Calendar.getInstance())); 287 } 288 289 296 public static Select getDaySelector(String name, 297 Calendar now) 298 { 299 Select daySelect = new Select().setName(name); 300 301 for(int currentDay=1; currentDay <= 31; currentDay++) 302 { 303 Option o = new Option(); 304 o.addElement(Integer.toString(currentDay)); 305 o.setValue(currentDay); 306 if (now.get(Calendar.DAY_OF_MONTH) == currentDay) 307 { 308 o.setSelected(true); 309 } 310 daySelect.addElement(o); 311 } 312 return(daySelect); 313 } 314 315 322 public static Select getYearSelector(String name) 323 { 324 return(getYearSelector(name, Calendar.getInstance())); 325 } 326 327 335 public static Select getYearSelector(String name, 336 Calendar now) 337 { 338 int startYear = now.get(Calendar.YEAR); 339 return(getYearSelector(name, startYear-5, startYear+5, startYear)); 340 } 341 342 352 public static Select getYearSelector(String name, 353 int firstYear, int lastYear, 354 int selectedYear ) 355 { 356 Select yearSelect = new Select().setName(name); 357 358 for(int currentYear = firstYear; 359 currentYear <= lastYear; 360 currentYear++) 361 { 362 Option o = new Option(); 363 o.addElement(Integer.toString(currentYear)); 364 o.setValue(currentYear); 365 if (currentYear == selectedYear) 366 { 367 o.setSelected(true); 368 } 369 yearSelect.addElement(o); 370 } 371 return(yearSelect); 372 } 373 374 375 383 public boolean setYear( int firstYear, int lastYear, int selectedYear ) 384 { 385 if (firstYear <= lastYear && firstYear <= selectedYear 386 && selectedYear <= lastYear) 387 { 388 this.useYears = true; 389 this.firstYear = firstYear; 390 this.lastYear = lastYear; 391 this.selectedYear = selectedYear; 392 return true; 393 } 394 else 395 { 396 return false; 397 } 398 } 399 400 418 public String output() 419 { 420 return(ecsOutput().toString()); 421 } 422 423 440 public String toString() 441 { 442 return(ecsOutput().toString()); 443 } 444 445 451 public ElementContainer ecsOutput() 452 { 453 if ( this.useDate == null ) 454 { 455 this.useDate.setTime ( new Date () ); 456 } 457 458 Select monthSelect = getMonthSelector(selName + MONTH_SUFFIX, useDate); 459 ConcreteElement daySelect = null; 460 if (!showDays) 461 { 462 daySelect = new Input(Input.hidden, 463 selName + DAY_SUFFIX, 464 setDay); 465 } 466 else 467 { 468 Select tmp = getDaySelector(selName + DAY_SUFFIX, useDate); 469 if (onChangeSet) 470 tmp.setOnChange(onChange); 471 daySelect = tmp; 472 } 473 Select yearSelect = null; 474 if (useYears) 475 { 476 yearSelect = getYearSelector(selName + YEAR_SUFFIX, 477 firstYear, lastYear, selectedYear); 478 } 479 else 480 { 481 yearSelect = getYearSelector(selName + YEAR_SUFFIX, useDate); 482 } 483 if (onChangeSet) 484 { 485 monthSelect.setOnChange(onChange); 486 yearSelect.setOnChange(onChange); 487 } 488 ElementContainer ec = new ElementContainer(); 489 ec.addElement(monthSelect); 491 ec.addElement(daySelect); 492 ec.addElement(yearSelect); 493 return (ec); 495 } 496 } 497 | Popular Tags |