|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2002-2008 Innoopract Informationssysteme GmbH. All rights |
| 3 |
* reserved. This program and the accompanying materials are made available |
| 4 |
* under the terms of the Eclipse Public License v1.0 which accompanies this |
| 5 |
* distribution, and is available at http://www.eclipse.org/legal/epl-v10.html |
| 6 |
* Contributors: Innoopract Informationssysteme GmbH - initial API and |
| 7 |
* implementation |
| 8 |
******************************************************************************/ |
| 9 |
package org.eclipse.swt.widgets; |
| 10 |
|
| 11 |
import java.text.*; |
| 12 |
import java.util.*; |
| 13 |
|
| 14 |
import org.eclipse.rwt.RWT; |
| 15 |
import org.eclipse.swt.SWT; |
| 16 |
import org.eclipse.swt.SWTException; |
| 17 |
import org.eclipse.swt.events.SelectionEvent; |
| 18 |
import org.eclipse.swt.events.SelectionListener; |
| 19 |
import org.eclipse.swt.graphics.*; |
| 20 |
import org.eclipse.swt.internal.graphics.TextSizeDetermination; |
| 21 |
import org.eclipse.swt.internal.widgets.IDateTimeAdapter; |
| 22 |
|
| 23 |
/** |
| 24 |
* Instances of this class are selectable user interface objects that allow the |
| 25 |
* user to enter and modify date or time values. |
| 26 |
* <p> |
| 27 |
* Note that although this class is a subclass of <code>Composite</code>, it |
| 28 |
* does not make sense to add children to it, or set a layout on it. |
| 29 |
* </p> |
| 30 |
* <dl> |
| 31 |
* <dt><b>Styles:</b></dt> |
| 32 |
* <dd>DATE, TIME, CALENDAR, SHORT, MEDIUM, LONG</dd> |
| 33 |
* <dt><b>Events:</b></dt> |
| 34 |
* <dd>Selection</dd> |
| 35 |
* </dl> |
| 36 |
* <p> |
| 37 |
* Note: Only one of the styles DATE, TIME, or CALENDAR may be specified, and |
| 38 |
* only one of the styles SHORT, MEDIUM, or LONG may be specified. |
| 39 |
* </p> |
| 40 |
* <p> |
| 41 |
* IMPORTANT: This class is <em>not</em> intended to be subclassed. |
| 42 |
* </p> |
| 43 |
* |
| 44 |
* @since x.x |
| 45 |
*/ |
| 46 |
public class DateTime extends Composite { |
| 47 |
|
| 48 |
private final class DateTimeAdapter implements IDateTimeAdapter { |
| 49 |
|
| 50 |
public Rectangle getBounds( final int widget ) { |
| 51 |
Rectangle result = new Rectangle( 0, 0, 0, 0); |
| 52 |
switch( widget ) { |
| 53 |
case WEEKDAY_TEXTFIELD: |
| 54 |
result = weekdayTextFieldBounds; |
| 55 |
break; |
| 56 |
case DAY_TEXTFIELD: |
| 57 |
result = dayTextFieldBounds; |
| 58 |
break; |
| 59 |
case MONTH_TEXTFIELD: |
| 60 |
result = monthTextFieldBounds; |
| 61 |
break; |
| 62 |
case YEAR_TEXTFIELD: |
| 63 |
result = yearTextFieldBounds; |
| 64 |
break; |
| 65 |
case WEEKDAY_MONTH_SEPARATOR: |
| 66 |
result = separator0Bounds; |
| 67 |
break; |
| 68 |
case MONTH_DAY_SEPARATOR: |
| 69 |
result = separator1Bounds; |
| 70 |
break; |
| 71 |
case DAY_YEAR_SEPARATOR: |
| 72 |
result = separator2Bounds; |
| 73 |
break; |
| 74 |
case SPINNER: |
| 75 |
result = spinnerBounds; |
| 76 |
break; |
| 77 |
case HOURS_TEXTFIELD: |
| 78 |
result = hoursTextFieldBounds; |
| 79 |
break; |
| 80 |
case MINUTES_TEXTFIELD: |
| 81 |
result = minutesTextFieldBounds; |
| 82 |
break; |
| 83 |
case SECONDS_TEXTFIELD: |
| 84 |
result = secondsTextFieldBounds; |
| 85 |
break; |
| 86 |
case HOURS_MINUTES_SEPARATOR: |
| 87 |
result = separator3Bounds; |
| 88 |
break; |
| 89 |
case MINUTES_SECONDS_SEPARATOR: |
| 90 |
result = separator4Bounds; |
| 91 |
break; |
| 92 |
} |
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
public String[] getMonthNames() { |
| 97 |
return MONTH_NAMES; |
| 98 |
} |
| 99 |
|
| 100 |
public String[] getWeekdayNames() { |
| 101 |
return WEEKDAY_NAMES; |
| 102 |
} |
| 103 |
|
| 104 |
public String getDateSeparator() { |
| 105 |
return DATE_SEPARATOR; |
| 106 |
} |
| 107 |
|
| 108 |
public String getDatePattern() { |
| 109 |
return DATE_PATTERN; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
private int V_PADDING = 6; |
| 114 |
private int H_PADDING = 6; |
| 115 |
|
| 116 |
private String[] MONTH_NAMES; |
| 117 |
private String[] WEEKDAY_NAMES; |
| 118 |
private String DATE_SEPARATOR; |
| 119 |
private String DATE_PATTERN; |
| 120 |
|
| 121 |
private final IDateTimeAdapter dateTimeAdapter; |
| 122 |
private Calendar rightNow; |
| 123 |
|
| 124 |
// Date fields |
| 125 |
private Rectangle weekdayTextFieldBounds; |
| 126 |
private Rectangle dayTextFieldBounds; |
| 127 |
private Rectangle monthTextFieldBounds; |
| 128 |
private Rectangle yearTextFieldBounds; |
| 129 |
private Rectangle separator0Bounds; |
| 130 |
private Rectangle separator1Bounds; |
| 131 |
private Rectangle separator2Bounds; |
| 132 |
private Rectangle spinnerBounds; |
| 133 |
// Time fields |
| 134 |
private Rectangle hoursTextFieldBounds; |
| 135 |
private Rectangle minutesTextFieldBounds; |
| 136 |
private Rectangle secondsTextFieldBounds; |
| 137 |
private Rectangle separator3Bounds; |
| 138 |
private Rectangle separator4Bounds; |
| 139 |
|
| 140 |
/** |
| 141 |
* Constructs a new instance of this class given its parent and a style value |
| 142 |
* describing its behavior and appearance. |
| 143 |
* <p> |
| 144 |
* The style value is either one of the style constants defined in class |
| 145 |
* <code>SWT</code> which is applicable to instances of this class, or must |
| 146 |
* be built by <em>bitwise OR</em>'ing together (that is, using the |
| 147 |
* <code>int</code> "|" operator) two or more of those <code>SWT</code> |
| 148 |
* style constants. The class description lists the style constants that are |
| 149 |
* applicable to the class. Style bits are also inherited from superclasses. |
| 150 |
* </p> |
| 151 |
* |
| 152 |
* @param parent a composite control which will be the parent of the new |
| 153 |
* instance (cannot be null) |
| 154 |
* @param style the style of control to construct |
| 155 |
* @exception IllegalArgumentException |
| 156 |
* <ul> |
| 157 |
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
| 158 |
* </ul> |
| 159 |
* @exception SWTException |
| 160 |
* <ul> |
| 161 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 162 |
* thread that created the parent</li> |
| 163 |
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed |
| 164 |
* subclass</li> |
| 165 |
* </ul> |
| 166 |
* @see SWT#DATE |
| 167 |
* @see SWT#TIME |
| 168 |
* @see SWT#CALENDAR |
| 169 |
* @see Widget#checkSubclass |
| 170 |
* @see Widget#getStyle |
| 171 |
*/ |
| 172 |
public DateTime( final Composite parent, final int style ) { |
| 173 |
super( parent, checkStyle( style ) ); |
| 174 |
dateTimeAdapter = new DateTimeAdapter(); |
| 175 |
rightNow = Calendar.getInstance(); |
| 176 |
DateFormatSymbols symbols = new DateFormatSymbols( RWT.getLocale() ); |
| 177 |
MONTH_NAMES = symbols.getMonths(); |
| 178 |
WEEKDAY_NAMES = symbols.getWeekdays(); |
| 179 |
DATE_SEPARATOR = getDateSeparator(); |
| 180 |
DATE_PATTERN = getDatePattern( DATE_SEPARATOR ); |
| 181 |
computeSubWidgetsBounds(); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Adds the listener to the collection of listeners who will be notified when |
| 186 |
* the control is selected by the user, by sending it one of the messages |
| 187 |
* defined in the <code>SelectionListener</code> interface. |
| 188 |
* <p> |
| 189 |
* <code>widgetSelected</code> is called when the user changes the control's |
| 190 |
* value. <code>widgetDefaultSelected</code> is not called. |
| 191 |
* </p> |
| 192 |
* |
| 193 |
* @param listener the listener which should be notified |
| 194 |
* @exception IllegalArgumentException |
| 195 |
* <ul> |
| 196 |
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li> |
| 197 |
* </ul> |
| 198 |
* @exception SWTException |
| 199 |
* <ul> |
| 200 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 201 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 202 |
* thread that created the receiver</li> |
| 203 |
* </ul> |
| 204 |
* @see SelectionListener |
| 205 |
* @see #removeSelectionListener |
| 206 |
* @see SelectionEvent |
| 207 |
*/ |
| 208 |
public void addSelectionListener( final SelectionListener listener ) { |
| 209 |
SelectionEvent.addListener( this, listener ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Removes the listener from the collection of listeners who will be notified |
| 214 |
* when the control is selected by the user. |
| 215 |
* |
| 216 |
* @param listener the listener which should no longer be notified |
| 217 |
* @exception IllegalArgumentException |
| 218 |
* <ul> |
| 219 |
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li> |
| 220 |
* </ul> |
| 221 |
* @exception SWTException |
| 222 |
* <ul> |
| 223 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 224 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 225 |
* thread that created the receiver</li> |
| 226 |
* </ul> |
| 227 |
* @see SelectionListener |
| 228 |
* @see #addSelectionListener |
| 229 |
*/ |
| 230 |
public void removeSelectionListener( final SelectionListener listener ) { |
| 231 |
SelectionEvent.removeListener( this, listener ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Returns the receiver's hours. |
| 236 |
* <p> |
| 237 |
* Hours is an integer between 0 and 23. |
| 238 |
* </p> |
| 239 |
* |
| 240 |
* @return an integer between 0 and 23 |
| 241 |
* @exception SWTException |
| 242 |
* <ul> |
| 243 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 244 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 245 |
* thread that created the receiver</li> |
| 246 |
* </ul> |
| 247 |
*/ |
| 248 |
public int getHours() { |
| 249 |
checkWidget(); |
| 250 |
return rightNow.get( Calendar.HOUR_OF_DAY ); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Returns the receiver's minutes. |
| 255 |
* <p> |
| 256 |
* Minutes is an integer between 0 and 59. |
| 257 |
* </p> |
| 258 |
* |
| 259 |
* @return an integer between 0 and 59 |
| 260 |
* @exception SWTException |
| 261 |
* <ul> |
| 262 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 263 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 264 |
* thread that created the receiver</li> |
| 265 |
* </ul> |
| 266 |
*/ |
| 267 |
public int getMinutes() { |
| 268 |
checkWidget(); |
| 269 |
return rightNow.get( Calendar.MINUTE ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Returns the receiver's seconds. |
| 274 |
* <p> |
| 275 |
* Seconds is an integer between 0 and 59. |
| 276 |
* </p> |
| 277 |
* |
| 278 |
* @return an integer between 0 and 59 |
| 279 |
* @exception SWTException |
| 280 |
* <ul> |
| 281 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 282 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 283 |
* thread that created the receiver</li> |
| 284 |
* </ul> |
| 285 |
*/ |
| 286 |
public int getSeconds() { |
| 287 |
checkWidget(); |
| 288 |
return rightNow.get( Calendar.SECOND ); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Returns the receiver's date, or day of the month. |
| 293 |
* <p> |
| 294 |
* The first day of the month is 1, and the last day depends on the month and |
| 295 |
* year. |
| 296 |
* </p> |
| 297 |
* |
| 298 |
* @return a positive integer beginning with 1 |
| 299 |
* @exception SWTException |
| 300 |
* <ul> |
| 301 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 302 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 303 |
* thread that created the receiver</li> |
| 304 |
* </ul> |
| 305 |
*/ |
| 306 |
public int getDay() { |
| 307 |
checkWidget(); |
| 308 |
return rightNow.get( Calendar.DATE ); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Returns the receiver's month. |
| 313 |
* <p> |
| 314 |
* The first month of the year is 0, and the last month is 11. |
| 315 |
* </p> |
| 316 |
* |
| 317 |
* @return an integer between 0 and 11 |
| 318 |
* @exception SWTException |
| 319 |
* <ul> |
| 320 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 321 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 322 |
* thread that created the receiver</li> |
| 323 |
* </ul> |
| 324 |
*/ |
| 325 |
public int getMonth() { |
| 326 |
checkWidget(); |
| 327 |
return rightNow.get( Calendar.MONTH ); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Returns the receiver's year. |
| 332 |
* <p> |
| 333 |
* The first year is 1752 and the last year is 9999. |
| 334 |
* </p> |
| 335 |
* |
| 336 |
* @return an integer between 1752 and 9999 |
| 337 |
* @exception SWTException |
| 338 |
* <ul> |
| 339 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 340 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 341 |
* thread that created the receiver</li> |
| 342 |
* </ul> |
| 343 |
*/ |
| 344 |
public int getYear() { |
| 345 |
checkWidget(); |
| 346 |
return rightNow.get( Calendar.YEAR ); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Sets the receiver's hours. |
| 351 |
* <p> |
| 352 |
* Hours is an integer between 0 and 23. |
| 353 |
* </p> |
| 354 |
* |
| 355 |
* @param hours an integer between 0 and 23 |
| 356 |
* @exception SWTException |
| 357 |
* <ul> |
| 358 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 359 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 360 |
* thread that created the receiver</li> |
| 361 |
* </ul> |
| 362 |
*/ |
| 363 |
public void setHours( final int hours ) { |
| 364 |
checkWidget(); |
| 365 |
if( hours >= 0 && hours <= 23 ) { |
| 366 |
rightNow.set( Calendar.HOUR_OF_DAY, hours ); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Sets the receiver's minutes. |
| 372 |
* <p> |
| 373 |
* Minutes is an integer between 0 and 59. |
| 374 |
* </p> |
| 375 |
* |
| 376 |
* @param minutes an integer between 0 and 59 |
| 377 |
* @exception SWTException |
| 378 |
* <ul> |
| 379 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 380 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 381 |
* thread that created the receiver</li> |
| 382 |
* </ul> |
| 383 |
*/ |
| 384 |
public void setMinutes( final int minutes ) { |
| 385 |
checkWidget(); |
| 386 |
if( minutes >= 0 && minutes <= 59 ) { |
| 387 |
rightNow.set( Calendar.MINUTE, minutes ); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Sets the receiver's seconds. |
| 393 |
* <p> |
| 394 |
* Seconds is an integer between 0 and 59. |
| 395 |
* </p> |
| 396 |
* |
| 397 |
* @param seconds an integer between 0 and 59 |
| 398 |
* @exception SWTException |
| 399 |
* <ul> |
| 400 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 401 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 402 |
* thread that created the receiver</li> |
| 403 |
* </ul> |
| 404 |
*/ |
| 405 |
public void setSeconds( final int seconds ) { |
| 406 |
checkWidget(); |
| 407 |
if( seconds >= 0 && seconds <= 59 ) { |
| 408 |
rightNow.set( Calendar.SECOND, seconds ); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Sets the receiver's date, or day of the month, to the specified day. |
| 414 |
* <p> |
| 415 |
* The first day of the month is 1, and the last day depends on the month and |
| 416 |
* year. |
| 417 |
* </p> |
| 418 |
* |
| 419 |
* @param day a positive integer beginning with 1 |
| 420 |
* @exception SWTException |
| 421 |
* <ul> |
| 422 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 423 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 424 |
* thread that created the receiver</li> |
| 425 |
* </ul> |
| 426 |
*/ |
| 427 |
public void setDay( final int day ) { |
| 428 |
checkWidget(); |
| 429 |
int month = rightNow.get( Calendar.MONTH ); |
| 430 |
int year = rightNow.get( Calendar.YEAR ); |
| 431 |
if( day >= 1 && day <= getDaysInMonth( month, year ) ) { |
| 432 |
rightNow.set( Calendar.DATE, day ); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Sets the receiver's month. |
| 438 |
* <p> |
| 439 |
* The first month of the year is 0, and the last month is 11. |
| 440 |
* </p> |
| 441 |
* |
| 442 |
* @param month an integer between 0 and 11 |
| 443 |
* @exception SWTException |
| 444 |
* <ul> |
| 445 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 446 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 447 |
* thread that created the receiver</li> |
| 448 |
* </ul> |
| 449 |
*/ |
| 450 |
public void setMonth( final int month ) { |
| 451 |
checkWidget(); |
| 452 |
int date = rightNow.get( Calendar.DATE ); |
| 453 |
int year = rightNow.get( Calendar.YEAR ); |
| 454 |
if( month >= 0 && month <= 11 && date <= getDaysInMonth( month, year ) ) { |
| 455 |
rightNow.set( Calendar.MONTH, month ); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* Sets the receiver's year. |
| 461 |
* <p> |
| 462 |
* The first year is 1752 and the last year is 9999. |
| 463 |
* </p> |
| 464 |
* |
| 465 |
* @param year an integer between 1752 and 9999 |
| 466 |
* @exception SWTException |
| 467 |
* <ul> |
| 468 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 469 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
| 470 |
* thread that created the receiver</li> |
| 471 |
* </ul> |
| 472 |
*/ |
| 473 |
public void setYear( final int year ) { |
| 474 |
checkWidget(); |
| 475 |
int date = rightNow.get( Calendar.DATE ); |
| 476 |
int month = rightNow.get( Calendar.MONTH ); |
| 477 |
if( year >= 1752 && year <= 9999 && date <= getDaysInMonth( month, year ) ) { |
| 478 |
rightNow.set( Calendar.YEAR, year ); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Sets the font that the receiver will use to paint textual information |
| 484 |
* to the font specified by the argument, or to the default font for that |
| 485 |
* kind of control if the argument is null. |
| 486 |
* |
| 487 |
* @param font the new font (or null) |
| 488 |
* |
| 489 |
* @exception IllegalArgumentException <ul> |
| 490 |
* <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> |
| 491 |
* </ul> |
| 492 |
* @exception SWTException <ul> |
| 493 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| 494 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| 495 |
* </ul> |
| 496 |
*/ |
| 497 |
public void setFont( final Font font ) { |
| 498 |
if( font != getFont() ) { |
| 499 |
super.setFont( font ); |
| 500 |
} |
| 501 |
computeSubWidgetsBounds(); |
| 502 |
} |
| 503 |
|
| 504 |
public Object getAdapter( final Class adapter ) { |
| 505 |
Object result; |
| 506 |
if( adapter == IDateTimeAdapter.class ) { |
| 507 |
result = dateTimeAdapter; |
| 508 |
} else { |
| 509 |
result = super.getAdapter( adapter ); |
| 510 |
} |
| 511 |
return result; |
| 512 |
} |
| 513 |
|
| 514 |
public Point computeSize( final int wHint, |
| 515 |
final int hHint, |
| 516 |
final boolean changed ) { |
| 517 |
checkWidget(); |
| 518 |
int width = 0, height = 0; |
| 519 |
if( wHint == SWT.DEFAULT || hHint == SWT.DEFAULT ) { |
| 520 |
Point size = computeSubWidgetsBounds(); |
| 521 |
width = size.x; |
| 522 |
height = size.y; |
| 523 |
} |
| 524 |
if( width == 0 ) { |
| 525 |
width = DEFAULT_WIDTH; |
| 526 |
} |
| 527 |
if( height == 0 ) { |
| 528 |
height = DEFAULT_HEIGHT; |
| 529 |
} |
| 530 |
if( wHint != SWT.DEFAULT ) { |
| 531 |
width = wHint; |
| 532 |
} |
| 533 |
if( hHint != SWT.DEFAULT ) { |
| 534 |
height = hHint; |
| 535 |
} |
| 536 |
int border = getBorderWidth(); |
| 537 |
width += border * 2; |
| 538 |
height += border * 2; |
| 539 |
return new Point( width, height ); |
| 540 |
} |
| 541 |
|
| 542 |
private Point computeSubWidgetsBounds() { |
| 543 |
Font font = getFont(); |
| 544 |
int width = 0, height = 0; |
| 545 |
if( ( style & SWT.CALENDAR ) != 0 ) { |
| 546 |
width = 168; |
| 547 |
height = 140; |
| 548 |
} else if( ( style & SWT.DATE ) != 0 ) { |
| 549 |
Point prefSize = new Point( 0, 0 ); |
| 550 |
if( DATE_PATTERN.equals( "MDY" ) ) { |
| 551 |
prefSize = computeMDYBounds( font ); |
| 552 |
} else if( DATE_PATTERN.equals( "DMY" ) ) { |
| 553 |
prefSize = computeDMYBounds( font ); |
| 554 |
} else { |
| 555 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 556 |
prefSize = computeYMDBounds( font ); |
| 557 |
} else { |
| 558 |
prefSize = computeMDYBounds( font ); |
| 559 |
} |
| 560 |
} |
| 561 |
// Overall widget size |
| 562 |
width = prefSize.x; |
| 563 |
height = prefSize.y; |
| 564 |
} else if( ( style & SWT.TIME ) != 0 ) { |
| 565 |
// Hours text field |
| 566 |
hoursTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 567 |
hoursTextFieldBounds.width |
| 568 |
= TextSizeDetermination.stringExtent( font, "88" ).x + H_PADDING; |
| 569 |
hoursTextFieldBounds.height |
| 570 |
= TextSizeDetermination.stringExtent( font, "88" ).y + V_PADDING; |
| 571 |
// Hours minutes separator |
| 572 |
separator3Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 573 |
separator3Bounds.x = hoursTextFieldBounds.x + hoursTextFieldBounds.width; |
| 574 |
separator3Bounds.width |
| 575 |
= TextSizeDetermination.stringExtent( font, ":" ).x; |
| 576 |
separator3Bounds.height = hoursTextFieldBounds.height; |
| 577 |
// Minutes text field |
| 578 |
minutesTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 579 |
minutesTextFieldBounds.x = separator3Bounds.x + separator3Bounds.width; |
| 580 |
minutesTextFieldBounds.width = hoursTextFieldBounds.width; |
| 581 |
minutesTextFieldBounds.height = hoursTextFieldBounds.height; |
| 582 |
// Minutes seconds separator |
| 583 |
separator4Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 584 |
separator4Bounds.x = minutesTextFieldBounds.x |
| 585 |
+ minutesTextFieldBounds.width; |
| 586 |
separator4Bounds.width = separator3Bounds.width; |
| 587 |
separator4Bounds.height = hoursTextFieldBounds.height; |
| 588 |
// Seconds text field |
| 589 |
secondsTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 590 |
secondsTextFieldBounds.x = separator4Bounds.x + separator4Bounds.width; |
| 591 |
secondsTextFieldBounds.width = hoursTextFieldBounds.width; |
| 592 |
secondsTextFieldBounds.height = hoursTextFieldBounds.height; |
| 593 |
// The spinner bounds |
| 594 |
spinnerBounds = new Rectangle( 0, 0, 0, 0 ); |
| 595 |
spinnerBounds.x = minutesTextFieldBounds.x |
| 596 |
+ minutesTextFieldBounds.width; |
| 597 |
if( ( style & SWT.MEDIUM ) != 0 || ( style & SWT.LONG) != 0 ) { |
| 598 |
spinnerBounds.x = secondsTextFieldBounds.x |
| 599 |
+ secondsTextFieldBounds.width; |
| 600 |
} |
| 601 |
spinnerBounds.width = 17; |
| 602 |
spinnerBounds.height = hoursTextFieldBounds.height; |
| 603 |
// Overall widget size |
| 604 |
width = spinnerBounds.x + spinnerBounds.width; |
| 605 |
height = hoursTextFieldBounds.height; |
| 606 |
} |
| 607 |
return new Point( width, height ); |
| 608 |
} |
| 609 |
|
| 610 |
private Point computeMDYBounds( final Font font ) { |
| 611 |
// The weekday text field bounds |
| 612 |
weekdayTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 613 |
if( ( style & SWT.LONG ) != 0 ) { |
| 614 |
weekdayTextFieldBounds.width |
| 615 |
= getMaxStringLength( font, WEEKDAY_NAMES ) + H_PADDING + 2; |
| 616 |
} |
| 617 |
weekdayTextFieldBounds.height |
| 618 |
= TextSizeDetermination.stringExtent( font, WEEKDAY_NAMES[1] ).y |
| 619 |
+ V_PADDING; |
| 620 |
// The weekday month separator bounds |
| 621 |
separator0Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 622 |
separator0Bounds.x |
| 623 |
= weekdayTextFieldBounds.x + weekdayTextFieldBounds.width; |
| 624 |
if( ( style & SWT.LONG ) != 0 ) { |
| 625 |
separator0Bounds.width |
| 626 |
= TextSizeDetermination.stringExtent( font, "," ).x; |
| 627 |
} |
| 628 |
separator0Bounds.height = weekdayTextFieldBounds.height; |
| 629 |
// The month text field bounds |
| 630 |
monthTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 631 |
monthTextFieldBounds.x = separator0Bounds.x + separator0Bounds.width; |
| 632 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 633 |
monthTextFieldBounds.width |
| 634 |
= TextSizeDetermination.stringExtent( font, "88" ).x + H_PADDING; |
| 635 |
} else { |
| 636 |
monthTextFieldBounds.width |
| 637 |
= getMaxStringLength( font, MONTH_NAMES ) + H_PADDING + 2; |
| 638 |
} |
| 639 |
monthTextFieldBounds.height = weekdayTextFieldBounds.height; |
| 640 |
// The month date separator bounds |
| 641 |
separator1Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 642 |
separator1Bounds.x = monthTextFieldBounds.x + monthTextFieldBounds.width; |
| 643 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 644 |
separator1Bounds.width |
| 645 |
= TextSizeDetermination.stringExtent( font, DATE_SEPARATOR ).x; |
| 646 |
} |
| 647 |
separator1Bounds.height = weekdayTextFieldBounds.height; |
| 648 |
// The date text field bounds |
| 649 |
dayTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 650 |
dayTextFieldBounds.x = separator1Bounds.x + separator1Bounds.width; |
| 651 |
if( ( style & SWT.SHORT ) == 0 ) { |
| 652 |
dayTextFieldBounds.width |
| 653 |
= TextSizeDetermination.stringExtent( font, "88" ).x + H_PADDING; |
| 654 |
} |
| 655 |
dayTextFieldBounds.height = weekdayTextFieldBounds.height; |
| 656 |
// The date year separator bounds |
| 657 |
separator2Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 658 |
separator2Bounds.x = dayTextFieldBounds.x + dayTextFieldBounds.width; |
| 659 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 660 |
separator2Bounds.width |
| 661 |
= TextSizeDetermination.stringExtent( font, DATE_SEPARATOR ).x; |
| 662 |
} else { |
| 663 |
separator2Bounds.width |
| 664 |
= TextSizeDetermination.stringExtent( font, "," ).x; |
| 665 |
} |
| 666 |
separator2Bounds.height = weekdayTextFieldBounds.height; |
| 667 |
// The year text field bounds |
| 668 |
yearTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 669 |
yearTextFieldBounds.x = separator2Bounds.x + separator2Bounds.width; |
| 670 |
yearTextFieldBounds.width |
| 671 |
= TextSizeDetermination.stringExtent( font, "8888" ).x + H_PADDING; |
| 672 |
yearTextFieldBounds.height = weekdayTextFieldBounds.height; |
| 673 |
// The spinner bounds |
| 674 |
spinnerBounds = new Rectangle( 0, 0, 0, 0 ); |
| 675 |
spinnerBounds.x = yearTextFieldBounds.x + yearTextFieldBounds.width; |
| 676 |
spinnerBounds.width = 17; |
| 677 |
spinnerBounds.height = weekdayTextFieldBounds.height; |
| 678 |
// Overall widget size |
| 679 |
int width = spinnerBounds.x + spinnerBounds.width; |
| 680 |
int height = weekdayTextFieldBounds.height; |
| 681 |
return new Point( width, height ); |
| 682 |
} |
| 683 |
|
| 684 |
private Point computeDMYBounds( final Font font ) { |
| 685 |
// The weekday text field bounds |
| 686 |
weekdayTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 687 |
if( ( style & SWT.LONG ) != 0 ) { |
| 688 |
weekdayTextFieldBounds.width |
| 689 |
= getMaxStringLength( font, WEEKDAY_NAMES ) + H_PADDING + 2; |
| 690 |
} |
| 691 |
weekdayTextFieldBounds.height |
| 692 |
= TextSizeDetermination.stringExtent( font, WEEKDAY_NAMES[1] ).y |
| 693 |
+ V_PADDING; |
| 694 |
// The weekday day separator bounds |
| 695 |
separator0Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 696 |
separator0Bounds.x |
| 697 |
= weekdayTextFieldBounds.x + weekdayTextFieldBounds.width; |
| 698 |
if( ( style & SWT.LONG ) != 0 ) { |
| 699 |
separator0Bounds.width |
| 700 |
= TextSizeDetermination.stringExtent( font, "," ).x; |
| 701 |
} |
| 702 |
separator0Bounds.height = weekdayTextFieldBounds.height; |
| 703 |
// The day text field bounds |
| 704 |
dayTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 705 |
dayTextFieldBounds.x = separator0Bounds.x + separator0Bounds.width; |
| 706 |
if( ( style & SWT.SHORT ) == 0 ) { |
| 707 |
dayTextFieldBounds.width |
| 708 |
= TextSizeDetermination.stringExtent( font, "88" ).x + H_PADDING; |
| 709 |
} |
| 710 |
dayTextFieldBounds.height = weekdayTextFieldBounds.height; |
| 711 |
// The day month separator bounds |
| 712 |
separator1Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 713 |
separator1Bounds.x = dayTextFieldBounds.x + dayTextFieldBounds.width; |
| 714 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 715 |
separator1Bounds.width |
| 716 |
= TextSizeDetermination.stringExtent( font, DATE_SEPARATOR ).x; |
| 717 |
} |
| 718 |
separator1Bounds.height = weekdayTextFieldBounds.height; |
| 719 |
// The month text field bounds |
| 720 |
monthTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 721 |
monthTextFieldBounds.x = separator1Bounds.x + separator1Bounds.width; |
| 722 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 723 |
monthTextFieldBounds.width |
| 724 |
= TextSizeDetermination.stringExtent( font, "88" ).x + H_PADDING; |
| 725 |
} else { |
| 726 |
monthTextFieldBounds.width |
| 727 |
= getMaxStringLength( font, MONTH_NAMES ) + H_PADDING + 2; |
| 728 |
} |
| 729 |
monthTextFieldBounds.height = weekdayTextFieldBounds.height; |
| 730 |
// The month year separator bounds |
| 731 |
separator2Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 732 |
separator2Bounds.x = monthTextFieldBounds.x + monthTextFieldBounds.width; |
| 733 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 734 |
separator2Bounds.width |
| 735 |
= TextSizeDetermination.stringExtent( font, DATE_SEPARATOR ).x; |
| 736 |
} else { |
| 737 |
separator2Bounds.width |
| 738 |
= TextSizeDetermination.stringExtent( font, "," ).x; |
| 739 |
} |
| 740 |
separator2Bounds.height = weekdayTextFieldBounds.height; |
| 741 |
// The year text field bounds |
| 742 |
yearTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 743 |
yearTextFieldBounds.x = separator2Bounds.x + separator2Bounds.width; |
| 744 |
yearTextFieldBounds.width |
| 745 |
= TextSizeDetermination.stringExtent( font, "8888" ).x + H_PADDING; |
| 746 |
yearTextFieldBounds.height = weekdayTextFieldBounds.height; |
| 747 |
// The spinner bounds |
| 748 |
spinnerBounds = new Rectangle( 0, 0, 0, 0 ); |
| 749 |
spinnerBounds.x = yearTextFieldBounds.x + yearTextFieldBounds.width; |
| 750 |
spinnerBounds.width = 17; |
| 751 |
spinnerBounds.height = weekdayTextFieldBounds.height; |
| 752 |
// Overall widget size |
| 753 |
int width = spinnerBounds.x + spinnerBounds.width; |
| 754 |
int height = weekdayTextFieldBounds.height; |
| 755 |
return new Point( width, height ); |
| 756 |
} |
| 757 |
|
| 758 |
private Point computeYMDBounds( final Font font ) { |
| 759 |
// The weekday text field bounds |
| 760 |
weekdayTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 761 |
if( ( style & SWT.LONG ) != 0 ) { |
| 762 |
weekdayTextFieldBounds.width |
| 763 |
= getMaxStringLength( font, WEEKDAY_NAMES ) + H_PADDING + 2; |
| 764 |
} |
| 765 |
weekdayTextFieldBounds.height |
| 766 |
= TextSizeDetermination.stringExtent( font, WEEKDAY_NAMES[1] ).y |
| 767 |
+ V_PADDING; |
| 768 |
// The weekday day separator bounds |
| 769 |
separator0Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 770 |
separator0Bounds.x |
| 771 |
= weekdayTextFieldBounds.x + weekdayTextFieldBounds.width; |
| 772 |
if( ( style & SWT.LONG ) != 0 ) { |
| 773 |
separator0Bounds.width |
| 774 |
= TextSizeDetermination.stringExtent( font, "," ).x; |
| 775 |
} |
| 776 |
separator0Bounds.height = weekdayTextFieldBounds.height; |
| 777 |
// The year text field bounds |
| 778 |
yearTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 779 |
yearTextFieldBounds.x = separator0Bounds.x + separator0Bounds.width; |
| 780 |
yearTextFieldBounds.width |
| 781 |
= TextSizeDetermination.stringExtent( font, "8888" ).x + H_PADDING; |
| 782 |
yearTextFieldBounds.height = weekdayTextFieldBounds.height; |
| 783 |
// The year month separator bounds |
| 784 |
separator1Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 785 |
separator1Bounds.x = yearTextFieldBounds.x + yearTextFieldBounds.width; |
| 786 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 787 |
separator1Bounds.width |
| 788 |
= TextSizeDetermination.stringExtent( font, DATE_SEPARATOR ).x; |
| 789 |
} |
| 790 |
// The month text field bounds |
| 791 |
monthTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 792 |
monthTextFieldBounds.x = separator1Bounds.x + separator1Bounds.width; |
| 793 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 794 |
monthTextFieldBounds.width |
| 795 |
= TextSizeDetermination.stringExtent( font, "88" ).x + H_PADDING; |
| 796 |
} else { |
| 797 |
monthTextFieldBounds.width |
| 798 |
= getMaxStringLength( font, MONTH_NAMES ) + H_PADDING + 2; |
| 799 |
} |
| 800 |
monthTextFieldBounds.height = weekdayTextFieldBounds.height; |
| 801 |
// The month day separator bounds |
| 802 |
separator2Bounds = new Rectangle( 0, 0, 0, 0 ); |
| 803 |
separator2Bounds.x = monthTextFieldBounds.x + monthTextFieldBounds.width; |
| 804 |
if( ( style & SWT.MEDIUM ) != 0 ) { |
| 805 |
separator2Bounds.width |
| 806 |
= TextSizeDetermination.stringExtent( font, DATE_SEPARATOR ).x; |
| 807 |
} else { |
| 808 |
separator2Bounds.width |
| 809 |
= TextSizeDetermination.stringExtent( font, "," ).x; |
| 810 |
} |
| 811 |
separator2Bounds.height = weekdayTextFieldBounds.height; |
| 812 |
// The day text field bounds |
| 813 |
dayTextFieldBounds = new Rectangle( 0, 0, 0, 0 ); |
| 814 |
dayTextFieldBounds.x = separator2Bounds.x + separator2Bounds.width; |
| 815 |
if( ( style & SWT.SHORT ) == 0 ) { |
| 816 |
dayTextFieldBounds.width |
| 817 |
= TextSizeDetermination.stringExtent( font, "88" ).x + H_PADDING; |
| 818 |
} |
| 819 |
dayTextFieldBounds.height = weekdayTextFieldBounds.height; |
| 820 |
|
| 821 |
separator1Bounds.height = weekdayTextFieldBounds.height; |
| 822 |
// The spinner bounds |
| 823 |
spinnerBounds = new Rectangle( 0, 0, 0, 0 ); |
| 824 |
spinnerBounds.x = dayTextFieldBounds.x + dayTextFieldBounds.width; |
| 825 |
spinnerBounds.width = 17; |
| 826 |
spinnerBounds.height = weekdayTextFieldBounds.height; |
| 827 |
// Overall widget size |
| 828 |
int width = spinnerBounds.x + spinnerBounds.width; |
| 829 |
int height = weekdayTextFieldBounds.height; |
| 830 |
return new Point( width, height ); |
| 831 |
} |
| 832 |
|
| 833 |
private int getDaysInMonth( final int month, final int year ) { |
| 834 |
GregorianCalendar cal = new GregorianCalendar( year, month, 1 ); |
| 835 |
return cal.getActualMaximum( Calendar.DAY_OF_MONTH ); |
| 836 |
} |
| 837 |
|
| 838 |
private int getMaxStringLength( final Font font, final String[] strings ) { |
| 839 |
int maxLength = 0; |
| 840 |
for( int i = 0; i < strings.length; i++ ) { |
| 841 |
int currentStringWidth |
| 842 |
= TextSizeDetermination.stringExtent( font, strings[i] ).x; |
| 843 |
maxLength = Math.max( maxLength, currentStringWidth ); |
| 844 |
} |
| 845 |
return maxLength; |
| 846 |
} |
| 847 |
|
| 848 |
private String getDateSeparator() { |
| 849 |
DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT ); |
| 850 |
String datePattern = ( ( SimpleDateFormat )df ).toPattern(); |
| 851 |
String result = ""; |
| 852 |
int index = 0; |
| 853 |
while( Character.isLetter( datePattern.charAt( index ) ) ) { |
| 854 |
index++; |
| 855 |
} |
| 856 |
result = Character.toString( datePattern.charAt( index ) ); |
| 857 |
return result; |
| 858 |
} |
| 859 |
|
| 860 |
private String getDatePattern( final String dateSeparator ) { |
| 861 |
DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT ); |
| 862 |
String datePattern = ( ( SimpleDateFormat )df ).toPattern(); |
| 863 |
String result = ""; |
| 864 |
StringTokenizer st = new StringTokenizer( datePattern, |
| 865 |
dateSeparator ); |
| 866 |
while ( st.hasMoreTokens() ) { |
| 867 |
String token = st.nextToken(); |
| 868 |
result += Character.toString( token.charAt( 0 ) ); |
| 869 |
} |
| 870 |
return result.toUpperCase(); |
| 871 |
} |
| 872 |
|
| 873 |
String getNameText() { |
| 874 |
return "DateTime"; |
| 875 |
} |
| 876 |
|
| 877 |
static int checkStyle( final int value ) { |
| 878 |
/* |
| 879 |
* Even though it is legal to create this widget with scroll bars, they |
| 880 |
* serve no useful purpose because they do not automatically scroll the |
| 881 |
* widget's client area. The fix is to clear the SWT style. |
| 882 |
*/ |
| 883 |
int style = value; |
| 884 |
style &= ~( SWT.H_SCROLL | SWT.V_SCROLL ); |
| 885 |
style = checkBits( style, SWT.DATE, SWT.TIME, SWT.CALENDAR, 0, 0, 0 ); |
| 886 |
return checkBits( style, SWT.MEDIUM, SWT.SHORT, SWT.LONG, 0, 0, 0 ); |
| 887 |
} |
| 888 |
|
| 889 |
protected void checkSubclass() { |
| 890 |
if( !isValidSubclass() ) { |
| 891 |
error( SWT.ERROR_INVALID_SUBCLASS ); |
| 892 |
} |
| 893 |
} |
| 894 |
} |