Home » Eclipse Projects » Remote Application Platform (RAP) » RAP example, looking like a website
RAP example, looking like a website [message #51317] |
Fri, 12 October 2007 13:44 |
Alexander Forschnr Messages: 21 Registered: July 2009 |
Junior Member |
|
|
Hi there.
Some weeks ago, I found some examples on the web, showing how to create
websites with RAP, but hiding RAP (startingin fullscreen, theming, etc.).
Does anyone know, where to find these pages? I've searched google,
www.innoopract.com and www.eclipse.org/rap several times, but I can't
find the sites.
Greets.
Alex
|
|
|
Re: RAP example, looking like a website [message #51401 is a reply to message #51317] |
Fri, 12 October 2007 15:25 |
Stefan Messages: 316 Registered: July 2009 |
Senior Member |
|
|
Hi Alex,
what exactly do you want to do? Hiding the bar of the main shell and the
menu bar can be achieved by some lines of code in your implementation of the
WorkbenchWindowAdvisor.
@Override
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
// Hide shell title bar
configurer.setShellStyle(SWT.NONE);
configurer.setShowStatusLine(false);
configurer.setShowMenuBar(false);
}
@Override
public void postWindowOpen(){
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
// set maximum shell bounds to current size of display (maximizes
main window)
configurer.getWindow().getShell().setMaximized(true);
}
For theming, take a look at the RAP wiki:
http://wiki.eclipse.org/RAP_Theming
Regards,
Stefan.
"Alexander Forschner" <alexander.forschner@uni-ulm.de> schrieb im
Newsbeitrag news:fentor$sbg$1@build.eclipse.org...
> Hi there.
>
> Some weeks ago, I found some examples on the web, showing how to create
> websites with RAP, but hiding RAP (startingin fullscreen, theming, etc.).
>
> Does anyone know, where to find these pages? I've searched google,
> www.innoopract.com and www.eclipse.org/rap several times, but I can't find
> the sites.
>
> Greets.
> Alex
|
|
| | | | |
Re: RAP example, looking like a website [message #54104 is a reply to message #54035] |
Mon, 22 October 2007 13:13 |
Eclipse User |
|
|
|
Originally posted by: fappel.innoopract.com
Hi,
not sure which application you mean. The actionbar advisor of
eclipsediscovery is implemented similar to the snippet available here:
http://dev.eclipse.org/newslists/news.eclipse.technology.rap /msg00794.html
The new demo's source isn't available yet. But we plan to publish the source
in the sandbox module of the CVS soon.The demo application does nothing
unusual in the actionbar advisor, but there is one little hack in the
postWindowOpen() method of the WorkbenchWindowAdvisor to place the banner
between toolbar and workbench page on the main perspective. We cast the
IWorkbenchPage to the internal type WorkbenchPage (ugly, ugly - I know) to
get access to the client composite of the page.
WorkbenchPage wPage = (
WorkbenchPage )getWindowConfigurer().getWindow().getActivePage();
Composite clientComposite = wPage.getClientComposite();
After that we are able to use a control listener to react on resizes of the
shell for example to readjust the page. The banner is made of some simple
labels with images parented to the shell.
Ciao
Frank
"Benedikt Arnold" <benedikt.arnold@beyondsoft.de> schrieb im Newsbeitrag
news:ffhk8m$3fn$1@build.eclipse.org...
> Hi,
>
> is it possible to get the source code for the WorkbenchWindowAdvisor of
> this application? Or is it available somewhere in the repository?
>
> Greetings,
> Ben
>
>
> "Benjamin Muskalla" <bmuskalla@innoopract.com> schrieb im Newsbeitrag
> news:feqvvi$55k$1@build.eclipse.org...
>> Hi Alex,
>>
>> i think you're talking about www.eclipsediscovery.org
>>
>> Just a few days ago we deployed another sample application with a web
>> look&feel at http://rap.eclipse.org/rapdemo/rms
>>
>> Greets
>> Benny
>>
>> Alexander Forschner wrote:
>>> Hi Stefan.
>>>
>>> Stefan R
|
|
|
Re: RAP example, looking like a website [message #54447 is a reply to message #54104] |
Tue, 23 October 2007 08:08 |
Eclipse User |
|
|
|
Originally posted by: benedikt.arnold.beyondsoft.de
Hi!
> The new demo's source isn't available yet. But we plan to publish the
> source in the sandbox module of the CVS soon.The demo application does
> nothing unusual in the actionbar advisor, but there is one little hack in
> the postWindowOpen() method of the WorkbenchWindowAdvisor to place the
> banner between toolbar and workbench page on the main perspective. We cast
> the IWorkbenchPage to the internal type WorkbenchPage (ugly, ugly - I
> know) to get access to the client composite of the page.
>
> WorkbenchPage wPage = (
> WorkbenchPage )getWindowConfigurer().getWindow().getActivePage();
> Composite clientComposite = wPage.getClientComposite();
>
> After that we are able to use a control listener to react on resizes of
> the shell for example to readjust the page. The banner is made of some
> simple labels with images parented to the shell.
I meant the new demo's source. I also needed an area to show a banner, but
not only for specific perspective. I've ovewritten
WorkbenchWindowAdvisor#createWindowContents to place a banner beetween the
menu bar and the toolbar.
I tried it some weeks ago and I failed because of my inexistent knowledge
about FormLayout, but yesterday I tried it another time and I was successful
:-)
Greetings
Ben
|
|
| |
Re: RAP example, looking like a website [message #61912 is a reply to message #54104] |
Thu, 22 November 2007 09:50 |
Eclipse User |
|
|
|
Originally posted by: benjamin.wolff.web.de
Hi,
the date picker of the demo is very interesting, in fact i'm in desperate
need of such a widget. unfortunately i'm not into javascript to create
such a custom widget by myself. maybe there is a description of how to
create this widget for "beginners"?? :))
regards,
ben
|
|
| | |
Re: RAP example, looking like a website [message #62144 is a reply to message #61912] |
Thu, 22 November 2007 13:50 |
Eclipse User |
|
|
|
Originally posted by: fappel.innoopract.com
Hi,
here comes the code for the date-picker of the RAP Workbench Demo. It's
build only using already existing SWT stuff. I didn't have the time to
create a fancy javascript based custom component so I used
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/SWTCalenda r.htm as an
inspiration. So surely the implementation could have been nicer and a custom
component should be faster, but it works...
/**
* Just a Q&D implementation of a datepicker. Should be replaced once there
is
* a component available in standard RWT.
*/
public class DatePickerDialog extends Dialog implements SelectionListener {
private final static SimpleDateFormat FORMATTER
= new SimpleDateFormat( "MMM yyyy" );
private Display display = null;
private Date dateSelection = null;
private String selectedDate = null;
private Shell shell = null;
private CLabel sunday = null;
private CLabel monday = null;
private CLabel tuesday = null;
private CLabel wednesday = null;
private CLabel thursday = null;
private CLabel friday = null;
private CLabel saturday = null;
private Button yearUp = null;
private Button yearNext = null;
private Button monthUp = null;
private Button monthNext = null;
private CLabel selectionLabel = null;
private Button[] days = new Button[ 42 ];
private Point location;
private Object lock;
public DatePickerDialog( final Shell parent, final int style ) {
super( parent, style );
}
public DatePickerDialog( final Shell parent ) {
this( parent, 0 );
}
public void setSelectedDate( final Date date ) {
this.dateSelection = date;
}
public void setLocation( final Point location ) {
this.location = location;
}
public Object open() {
Shell parent = getParent();
display = Display.getDefault();
shell = new Shell( parent, SWT.NO_TRIM );
GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing = 1;
gridLayout.verticalSpacing = 1;
gridLayout.numColumns = 7;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout( gridLayout );
Calendar now = Calendar.getInstance();
if( dateSelection == null ) {
dateSelection = new Date( now.getTimeInMillis() );
}
now.setTime( dateSelection );
createNavigation();
createWeekDays();
for( int i = 0; i < 42; i++ ) {
days[ i ] = new Button( shell, SWT.FLAT | SWT.CENTER );
int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
GridData gridData = new GridData(fillBoth );
gridData.heightHint = 25;
gridData.widthHint = 25;
days[ i ].setLayoutData( gridData );
days[ i ].setBackground( display.getSystemColor( SWT.COLOR_WHITE ) );
days[ i ].addSelectionListener( this );
}
setDayForDisplay( now );
if( location != null ) {
shell.setLocation( location );
}
shell.pack();
shell.addShellListener( new ShellAdapter() {
@Override
public void shellDeactivated( ShellEvent e ) {
doClose();
}
} );
shell.open();
shell.setActive();
lock = DatePickerDialogHelper.createLock();
DatePickerDialogHelper.block( lock, shell );
return selectedDate;
}
//////////////////////////////
// interface SelectionListener
public void widgetDefaultSelected( final SelectionEvent evt ) {
}
public void widgetSelected( final SelectionEvent evt ) {
Button day = ( Button )evt.getSource();
String[] split = selectionLabel.getText().split( " " );
this.selectedDate = split[ 0 ] + " " + day.getText() + " " + split[ 1 ];
doClose();
}
//////////////////
// helping methods
private int getLastDayOfMonth( final int year, final int month ) {
int result = 31;
if( month == 4 || month == 6 || month == 9 || month == 11 ) {
result = 30;
} else if( month == 2 ) {
if( isLeapYear( year ) ) {
result = 29;
} else {
result = 28;
}
}
return result;
}
private boolean isLeapYear( final int year ) {
return ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 );
}
private void moveTo( final int type, final int value ) {
Calendar now = Calendar.getInstance();
now.setTime( dateSelection );
now.add( type, value );
dateSelection = new Date( now.getTimeInMillis() );
selectionLabel.setText( FORMATTER.format( dateSelection ) );
setDayForDisplay( now );
shell.layout( true );
}
private void setDayForDisplay( final Calendar now ) {
int currentDay = now.get( Calendar.DATE );
now.add( Calendar.DAY_OF_MONTH, -( now.get( Calendar.DATE ) - 1 ) );
int startIndex = now.get( Calendar.DAY_OF_WEEK ) - 1;
int year = now.get( Calendar.YEAR );
int month = now.get( Calendar.MONTH ) + 1;
int lastDay = this.getLastDayOfMonth( year, month );
int endIndex = startIndex + lastDay - 1;
int startday = 1;
for( int i = 0; i < 42; i++ ) {
Color temp = days[ i ].getBackground();
if( temp.equals( display.getSystemColor( SWT.COLOR_BLUE ) ) ) {
days[ i ].setBackground( display.getSystemColor(
SWT.COLOR_WHITE ) );
}
}
for( int i = 0; i < 42; i++ ) {
if( i >= startIndex && i <= endIndex ) {
days[ i ].setVisible( true );
days[ i ].setText( String.valueOf( startday ) );
days[ i ].setForeground( display.getSystemColor(
SWT.COLOR_BLACK ) );
days[ i ].setBackground( display.getSystemColor(
SWT.COLOR_WHITE ) );
if( startday == currentDay ) {
Color selectionBg = DatePickerDialogHelper.getNavigationBgColor();
days[ i ].setBackground( selectionBg );
Color selectionFg = DatePickerDialogHelper.getNavigationFgColor();
days[ i ].setForeground( selectionFg );
days[ i ].setFocus();
shell.setDefaultButton( days[ i ] );
}
startday++;
} else {
days[ i ].setVisible( false );
days[ i ].setForeground( display.getSystemColor(
SWT.COLOR_WHITE ) );
}
}
}
private void previousYear() {
moveTo( Calendar.YEAR, -1 );
}
private void nextYear() {
moveTo( Calendar.YEAR, 1 );
}
private void nextMonth() {
moveTo( Calendar.MONTH, 1 );
}
private void previousMonth() {
moveTo( Calendar.MONTH, -1 );
}
private void createNavigation() {
Composite composite = new Composite( this.shell, SWT.NONE );
int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
GridData gdComposite = new GridData( fillBoth );
gdComposite.horizontalSpan = 7;
gdComposite.heightHint = 22;
composite.setLayoutData( gdComposite );
Color bgColor = DatePickerDialogHelper.getNavigationBgColor();
Color fgColor = DatePickerDialogHelper.getNavigationFgColor();
composite.setBackground( bgColor );
GridLayout gridLayout = new GridLayout( 7, true );
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 1;
gridLayout.horizontalSpacing = 1;
composite.setLayout( gridLayout );
int buttonStyle = SWT.PUSH;
yearUp = new Button( composite, buttonStyle );
setNavigationColors( yearUp, bgColor, fgColor );
yearUp.setText( "<" );
yearUp.setLayoutData( createNavigationLayoutData() );
yearUp.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( final SelectionEvent e ) {
previousYear();
}
} );
monthUp = new Button( composite, buttonStyle );
monthUp.setText( "<<" );
setNavigationColors( monthUp, bgColor, fgColor );
monthUp.setLayoutData( createNavigationLayoutData() );
monthUp.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( final SelectionEvent e ) {
previousMonth();
}
} );
selectionLabel = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
GridData gdNowLabel = new GridData( GridData.FILL_HORIZONTAL );
gdNowLabel.horizontalSpan = 3;
selectionLabel.setLayoutData( gdNowLabel );
selectionLabel.setText( FORMATTER.format( dateSelection ) );
setNavigationColors( selectionLabel, bgColor, fgColor );
monthNext = new Button( composite, buttonStyle );
monthNext.setText( ">>" );
setNavigationColors( monthNext, bgColor, fgColor );
monthNext.setLayoutData( createNavigationLayoutData() );
monthNext.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( final SelectionEvent e ) {
nextMonth();
}
} );
yearNext = new Button( composite, buttonStyle );
yearNext.setText( ">" );
setNavigationColors( yearNext, bgColor, fgColor );
yearNext.setLayoutData( createNavigationLayoutData() );
yearNext.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( final SelectionEvent e ) {
nextYear();
}
} );
}
private void setNavigationColors( final Control control,
final Color bgColor,
final Color fgColor )
{
control.setBackground( bgColor );
control.setForeground( fgColor );
}
private Object createNavigationLayoutData() {
GridData result = new GridData( GridData.FILL_HORIZONTAL );
result.heightHint = 20;
return result;
}
private void createWeekDays() {
Composite composite = new Composite( this.shell, SWT.NONE );
int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
GridData gridData = new GridData( fillBoth );
gridData.horizontalSpan = 7;
composite.setLayoutData( gridData );
GridLayout gridLayout = new GridLayout( 7, true );
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 1;
gridLayout.horizontalSpacing = 1;
composite.setLayout( gridLayout );
Color bgColor = DatePickerDialogHelper.getWeekDaysBgColor();
Color weekEndFgColor = DatePickerDialogHelper.getWeekEndFgColor();
sunday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
sunday.setText( "Sun" );
sunday.setLayoutData( createWeekDaysLayoutData() );
sunday.setForeground( weekEndFgColor );
sunday.setBackground( bgColor );
monday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
monday.setText( "Mon" );
monday.setBackground( bgColor );
monday.setLayoutData( createWeekDaysLayoutData() );
tuesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
tuesday.setText( "Tue" );
tuesday.setBackground( bgColor );
tuesday.setLayoutData( createWeekDaysLayoutData() );
wednesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
wednesday.setText( "Wed" );
wednesday.setBackground( bgColor );
wednesday.setLayoutData( createWeekDaysLayoutData() );
thursday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
thursday.setText( "Thu" );
thursday.setBackground( bgColor );
thursday.setLayoutData( createWeekDaysLayoutData() );
friday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
friday.setText( "Fri" );
friday.setBackground( bgColor );
friday.setLayoutData( createWeekDaysLayoutData() );
saturday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
saturday.setText( "Sat" );
saturday.setForeground( weekEndFgColor );
saturday.setBackground( bgColor );
saturday.setLayoutData( createWeekDaysLayoutData() );
}
private Object createWeekDaysLayoutData() {
GridData result = new GridData( GridData.FILL_HORIZONTAL );
result.heightHint = 20;
return result;
}
private void doClose() {
this.shell.close();
DatePickerDialogHelper.resume( lock );
}
}
public class DatePickerDialogHelper {
private static final Color COLOR_WEEK_DAYS_BG
= Graphics.getColor( 211, 231, 223 );
private static final Color COLOR_WEEK_END_FG
= Graphics.getColor( 255, 0, 0 );
private static final Color COLOR_NAVIGATION_BG
= Display.getDefault().getSystemColor( SWT.COLOR_LIST_SELECTION );
private static final Color COLOR_NAVIGATION_FG
= Graphics.getColor( 255, 255, 255 );
private DatePickerDialogHelper() {
// prevent instance creation
}
public static Object createLock() {
return new LifeCycleLock();
}
public static void block( final Object lock, final Shell shell ) {
LifeCycleControl.block( ( LifeCycleLock )lock );
}
public static void resume( final Object lock ) {
LifeCycleControl.resume( ( LifeCycleLock )lock );
}
public static Color getWeekDaysBgColor() {
return COLOR_WEEK_DAYS_BG;
}
public static Color getWeekEndFgColor() {
return COLOR_WEEK_END_FG;
}
public static Color getNavigationBgColor() {
return COLOR_NAVIGATION_BG;
}
public static Color getNavigationFgColor() {
return COLOR_NAVIGATION_FG;
}
}
The second class is just for single sourcing, since I also have an RCP
version of the demo, but that's still work in process. So if you like to use
the date picker you can also inline the class completely.
Ciao
Frank
"Ben W." <benjamin.wolff@web.de> schrieb im Newsbeitrag
news:19975ecc00781b8ec308a12ac492d596$1@www.eclipse.org...
> Hi,
>
> the date picker of the demo is very interesting, in fact i'm in desperate
> need of such a widget. unfortunately i'm not into javascript to create
> such a custom widget by myself. maybe there is a description of how to
> create this widget for "beginners"?? :))
>
>
> regards,
> ben
>
|
|
| |
Re: RAP example, looking like a website [message #63087 is a reply to message #61696] |
Sun, 25 November 2007 14:36 |
Eclipse User |
|
|
|
Originally posted by: benedikt.arnold.beyondsoft.de
Yes you may :-) Here we go
@Override
public void createWindowContents(Shell shell) {
final IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
if (configurer.getShowMenuBar()) {
final Menu menu = configurer.createMenuBar();
shell.setMenuBar(menu);
}
final FormLayout layout = new FormLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
shell.setLayout(layout);
Composite banner = createBannerArea(shell);
FormData data = new FormData();
data.top = new FormAttachment(0, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
banner.setLayoutData(data);
Control coolbar = configurer.createCoolBarControl(shell);
data = new FormData();
data.top = new FormAttachment(banner, 5, SWT.BOTTOM);
data.left = new FormAttachment(0, 5);
data.right = new FormAttachment(100, -5);
coolbar.setLayoutData(data);
page = configurer.createPageComposite(shell);
data = new FormData();
data.top = new FormAttachment(coolbar, 5, SWT.BOTTOM);
data.left = new FormAttachment(0, 5);
data.right = new FormAttachment(100, -5);
data.bottom = new FormAttachment(100, -5);
page.setLayoutData(data);
Control statusLine = configurer.createStatusLineControl(shell);
data = new FormData();
data.top = new FormAttachment(page, 5, SWT.BOTTOM);
data.left = new FormAttachment(0, 5);
data.right = new FormAttachment(100, -5);
data.bottom = new FormAttachment(100, -5);
statusLine.setLayoutData(data);
layout();
}
private Composite createBannerArea(Composite parent) {
Composite logo = new Composite(parent, SWT.NONE);
logo
.setBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_WHITE));
FormLayout fLayout = new FormLayout();
logo.setLayout(fLayout);
final Label left = new Label(logo, SWT.NONE);
left.setImage(VoIPConfWebUI.getImageDescriptor("images/voipconf.png ")
.createImage());
FormData fd = new FormData();
fd.top = new FormAttachment(0, 50);
left.setLayoutData(fd);
final Label right = new Label(logo, SWT.NONE);
right.setImage(VoIPConfWebUI.getImageDescriptor("images/BSLogo.png ")
.createImage());
fd = new FormData();
fd.right = new FormAttachment(100, 5);
right.setLayoutData(fd);
return logo;
}
|
|
|
Re: RAP example, looking like a website [message #103044 is a reply to message #62144] |
Sun, 24 August 2008 14:17 |
Eclipse User |
|
|
|
Originally posted by: akarypid.yahoo.gr
I'm trying to use this DatePickerDialog in my application, but the
LifeCycleLock and LifeCycleControl classes are unavailable. What plug-in
do I need to add to my dependencies to have them included in my classpath?
Frank Appel wrote:
> Hi,
>
> here comes the code for the date-picker of the RAP Workbench Demo. It's
> build only using already existing SWT stuff. I didn't have the time to
> create a fancy javascript based custom component so I used
> http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/SWTCalenda r.htm as an
> inspiration. So surely the implementation could have been nicer and a custom
> component should be faster, but it works...
>
>
> /**
> * Just a Q&D implementation of a datepicker. Should be replaced once there
> is
> * a component available in standard RWT.
> */
> public class DatePickerDialog extends Dialog implements SelectionListener {
> private final static SimpleDateFormat FORMATTER
> = new SimpleDateFormat( "MMM yyyy" );
> private Display display = null;
> private Date dateSelection = null;
> private String selectedDate = null;
> private Shell shell = null;
> private CLabel sunday = null;
> private CLabel monday = null;
> private CLabel tuesday = null;
> private CLabel wednesday = null;
> private CLabel thursday = null;
> private CLabel friday = null;
> private CLabel saturday = null;
> private Button yearUp = null;
> private Button yearNext = null;
> private Button monthUp = null;
> private Button monthNext = null;
> private CLabel selectionLabel = null;
> private Button[] days = new Button[ 42 ];
> private Point location;
> private Object lock;
>
>
> public DatePickerDialog( final Shell parent, final int style ) {
> super( parent, style );
> }
>
> public DatePickerDialog( final Shell parent ) {
> this( parent, 0 );
> }
>
> public void setSelectedDate( final Date date ) {
> this.dateSelection = date;
> }
>
> public void setLocation( final Point location ) {
> this.location = location;
> }
>
> public Object open() {
> Shell parent = getParent();
> display = Display.getDefault();
> shell = new Shell( parent, SWT.NO_TRIM );
>
> GridLayout gridLayout = new GridLayout();
> gridLayout.marginWidth = 0;
> gridLayout.marginHeight = 0;
> gridLayout.horizontalSpacing = 1;
> gridLayout.verticalSpacing = 1;
> gridLayout.numColumns = 7;
> gridLayout.makeColumnsEqualWidth = true;
> shell.setLayout( gridLayout );
>
> Calendar now = Calendar.getInstance();
> if( dateSelection == null ) {
> dateSelection = new Date( now.getTimeInMillis() );
> }
> now.setTime( dateSelection );
>
> createNavigation();
> createWeekDays();
>
> for( int i = 0; i < 42; i++ ) {
> days[ i ] = new Button( shell, SWT.FLAT | SWT.CENTER );
> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
> GridData gridData = new GridData(fillBoth );
> gridData.heightHint = 25;
> gridData.widthHint = 25;
> days[ i ].setLayoutData( gridData );
> days[ i ].setBackground( display.getSystemColor( SWT.COLOR_WHITE ) );
> days[ i ].addSelectionListener( this );
> }
> setDayForDisplay( now );
>
>
> if( location != null ) {
> shell.setLocation( location );
> }
> shell.pack();
> shell.addShellListener( new ShellAdapter() {
> @Override
> public void shellDeactivated( ShellEvent e ) {
> doClose();
> }
> } );
> shell.open();
> shell.setActive();
> lock = DatePickerDialogHelper.createLock();
> DatePickerDialogHelper.block( lock, shell );
> return selectedDate;
> }
>
>
> //////////////////////////////
> // interface SelectionListener
>
> public void widgetDefaultSelected( final SelectionEvent evt ) {
> }
>
> public void widgetSelected( final SelectionEvent evt ) {
> Button day = ( Button )evt.getSource();
> String[] split = selectionLabel.getText().split( " " );
> this.selectedDate = split[ 0 ] + " " + day.getText() + " " + split[ 1 ];
> doClose();
> }
>
>
> //////////////////
> // helping methods
>
> private int getLastDayOfMonth( final int year, final int month ) {
> int result = 31;
> if( month == 4 || month == 6 || month == 9 || month == 11 ) {
> result = 30;
> } else if( month == 2 ) {
> if( isLeapYear( year ) ) {
> result = 29;
> } else {
> result = 28;
> }
> }
> return result;
> }
>
> private boolean isLeapYear( final int year ) {
> return ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 );
> }
>
> private void moveTo( final int type, final int value ) {
> Calendar now = Calendar.getInstance();
> now.setTime( dateSelection );
> now.add( type, value );
> dateSelection = new Date( now.getTimeInMillis() );
> selectionLabel.setText( FORMATTER.format( dateSelection ) );
> setDayForDisplay( now );
> shell.layout( true );
> }
>
> private void setDayForDisplay( final Calendar now ) {
> int currentDay = now.get( Calendar.DATE );
> now.add( Calendar.DAY_OF_MONTH, -( now.get( Calendar.DATE ) - 1 ) );
> int startIndex = now.get( Calendar.DAY_OF_WEEK ) - 1;
> int year = now.get( Calendar.YEAR );
> int month = now.get( Calendar.MONTH ) + 1;
> int lastDay = this.getLastDayOfMonth( year, month );
> int endIndex = startIndex + lastDay - 1;
> int startday = 1;
> for( int i = 0; i < 42; i++ ) {
> Color temp = days[ i ].getBackground();
> if( temp.equals( display.getSystemColor( SWT.COLOR_BLUE ) ) ) {
> days[ i ].setBackground( display.getSystemColor(
> SWT.COLOR_WHITE ) );
> }
> }
> for( int i = 0; i < 42; i++ ) {
> if( i >= startIndex && i <= endIndex ) {
> days[ i ].setVisible( true );
> days[ i ].setText( String.valueOf( startday ) );
> days[ i ].setForeground( display.getSystemColor(
> SWT.COLOR_BLACK ) );
> days[ i ].setBackground( display.getSystemColor(
> SWT.COLOR_WHITE ) );
> if( startday == currentDay ) {
> Color selectionBg = DatePickerDialogHelper.getNavigationBgColor();
> days[ i ].setBackground( selectionBg );
> Color selectionFg = DatePickerDialogHelper.getNavigationFgColor();
> days[ i ].setForeground( selectionFg );
> days[ i ].setFocus();
> shell.setDefaultButton( days[ i ] );
> }
> startday++;
> } else {
> days[ i ].setVisible( false );
> days[ i ].setForeground( display.getSystemColor(
> SWT.COLOR_WHITE ) );
> }
> }
> }
>
> private void previousYear() {
> moveTo( Calendar.YEAR, -1 );
> }
>
> private void nextYear() {
> moveTo( Calendar.YEAR, 1 );
> }
>
> private void nextMonth() {
> moveTo( Calendar.MONTH, 1 );
> }
>
> private void previousMonth() {
> moveTo( Calendar.MONTH, -1 );
> }
>
> private void createNavigation() {
> Composite composite = new Composite( this.shell, SWT.NONE );
> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
> GridData gdComposite = new GridData( fillBoth );
> gdComposite.horizontalSpan = 7;
> gdComposite.heightHint = 22;
> composite.setLayoutData( gdComposite );
> Color bgColor = DatePickerDialogHelper.getNavigationBgColor();
> Color fgColor = DatePickerDialogHelper.getNavigationFgColor();
> composite.setBackground( bgColor );
>
> GridLayout gridLayout = new GridLayout( 7, true );
> gridLayout.marginWidth = 0;
> gridLayout.marginHeight = 0;
> gridLayout.verticalSpacing = 1;
> gridLayout.horizontalSpacing = 1;
> composite.setLayout( gridLayout );
>
> int buttonStyle = SWT.PUSH;
> yearUp = new Button( composite, buttonStyle );
> setNavigationColors( yearUp, bgColor, fgColor );
> yearUp.setText( "<" );
> yearUp.setLayoutData( createNavigationLayoutData() );
> yearUp.addSelectionListener( new SelectionAdapter() {
> public void widgetSelected( final SelectionEvent e ) {
> previousYear();
> }
> } );
>
> monthUp = new Button( composite, buttonStyle );
> monthUp.setText( "<<" );
> setNavigationColors( monthUp, bgColor, fgColor );
> monthUp.setLayoutData( createNavigationLayoutData() );
> monthUp.addSelectionListener( new SelectionAdapter() {
> public void widgetSelected( final SelectionEvent e ) {
> previousMonth();
> }
> } );
>
> selectionLabel = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> GridData gdNowLabel = new GridData( GridData.FILL_HORIZONTAL );
> gdNowLabel.horizontalSpan = 3;
> selectionLabel.setLayoutData( gdNowLabel );
> selectionLabel.setText( FORMATTER.format( dateSelection ) );
> setNavigationColors( selectionLabel, bgColor, fgColor );
>
> monthNext = new Button( composite, buttonStyle );
> monthNext.setText( ">>" );
> setNavigationColors( monthNext, bgColor, fgColor );
> monthNext.setLayoutData( createNavigationLayoutData() );
> monthNext.addSelectionListener( new SelectionAdapter() {
> public void widgetSelected( final SelectionEvent e ) {
> nextMonth();
> }
> } );
>
> yearNext = new Button( composite, buttonStyle );
> yearNext.setText( ">" );
> setNavigationColors( yearNext, bgColor, fgColor );
> yearNext.setLayoutData( createNavigationLayoutData() );
> yearNext.addSelectionListener( new SelectionAdapter() {
> public void widgetSelected( final SelectionEvent e ) {
> nextYear();
> }
> } );
> }
>
> private void setNavigationColors( final Control control,
> final Color bgColor,
> final Color fgColor )
> {
> control.setBackground( bgColor );
> control.setForeground( fgColor );
> }
>
> private Object createNavigationLayoutData() {
> GridData result = new GridData( GridData.FILL_HORIZONTAL );
> result.heightHint = 20;
> return result;
> }
>
> private void createWeekDays() {
> Composite composite = new Composite( this.shell, SWT.NONE );
> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
> GridData gridData = new GridData( fillBoth );
> gridData.horizontalSpan = 7;
> composite.setLayoutData( gridData );
> GridLayout gridLayout = new GridLayout( 7, true );
> gridLayout.marginWidth = 0;
> gridLayout.marginHeight = 0;
> gridLayout.verticalSpacing = 1;
> gridLayout.horizontalSpacing = 1;
> composite.setLayout( gridLayout );
>
>
> Color bgColor = DatePickerDialogHelper.getWeekDaysBgColor();
> Color weekEndFgColor = DatePickerDialogHelper.getWeekEndFgColor();
> sunday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> sunday.setText( "Sun" );
> sunday.setLayoutData( createWeekDaysLayoutData() );
> sunday.setForeground( weekEndFgColor );
> sunday.setBackground( bgColor );
> monday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> monday.setText( "Mon" );
> monday.setBackground( bgColor );
> monday.setLayoutData( createWeekDaysLayoutData() );
> tuesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> tuesday.setText( "Tue" );
> tuesday.setBackground( bgColor );
> tuesday.setLayoutData( createWeekDaysLayoutData() );
> wednesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> wednesday.setText( "Wed" );
> wednesday.setBackground( bgColor );
> wednesday.setLayoutData( createWeekDaysLayoutData() );
> thursday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> thursday.setText( "Thu" );
> thursday.setBackground( bgColor );
> thursday.setLayoutData( createWeekDaysLayoutData() );
> friday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> friday.setText( "Fri" );
> friday.setBackground( bgColor );
> friday.setLayoutData( createWeekDaysLayoutData() );
> saturday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> saturday.setText( "Sat" );
> saturday.setForeground( weekEndFgColor );
> saturday.setBackground( bgColor );
> saturday.setLayoutData( createWeekDaysLayoutData() );
> }
>
> private Object createWeekDaysLayoutData() {
> GridData result = new GridData( GridData.FILL_HORIZONTAL );
> result.heightHint = 20;
> return result;
> }
>
>
> private void doClose() {
> this.shell.close();
> DatePickerDialogHelper.resume( lock );
> }
> }
>
> public class DatePickerDialogHelper {
>
> private static final Color COLOR_WEEK_DAYS_BG
> = Graphics.getColor( 211, 231, 223 );
> private static final Color COLOR_WEEK_END_FG
> = Graphics.getColor( 255, 0, 0 );
> private static final Color COLOR_NAVIGATION_BG
> = Display.getDefault().getSystemColor( SWT.COLOR_LIST_SELECTION );
> private static final Color COLOR_NAVIGATION_FG
> = Graphics.getColor( 255, 255, 255 );
>
> private DatePickerDialogHelper() {
> // prevent instance creation
> }
>
> public static Object createLock() {
> return new LifeCycleLock();
> }
>
> public static void block( final Object lock, final Shell shell ) {
> LifeCycleControl.block( ( LifeCycleLock )lock );
> }
>
> public static void resume( final Object lock ) {
> LifeCycleControl.resume( ( LifeCycleLock )lock );
> }
>
> public static Color getWeekDaysBgColor() {
> return COLOR_WEEK_DAYS_BG;
> }
>
> public static Color getWeekEndFgColor() {
> return COLOR_WEEK_END_FG;
> }
>
> public static Color getNavigationBgColor() {
> return COLOR_NAVIGATION_BG;
> }
>
> public static Color getNavigationFgColor() {
> return COLOR_NAVIGATION_FG;
> }
> }
>
> The second class is just for single sourcing, since I also have an RCP
> version of the demo, but that's still work in process. So if you like to use
> the date picker you can also inline the class completely.
>
>
> Ciao
> Frank
>
>
> "Ben W." <benjamin.wolff@web.de> schrieb im Newsbeitrag
> news:19975ecc00781b8ec308a12ac492d596$1@www.eclipse.org...
>> Hi,
>>
>> the date picker of the demo is very interesting, in fact i'm in desperate
>> need of such a widget. unfortunately i'm not into javascript to create
>> such a custom widget by myself. maybe there is a description of how to
>> create this widget for "beginners"?? :))
>>
>>
>> regards,
>> ben
>>
>
>
|
|
|
Re: RAP example, looking like a website [message #103059 is a reply to message #103044] |
Sun, 24 August 2008 15:26 |
Eclipse User |
|
|
|
Originally posted by: benjamin.wolff.web.de
Hi Alexandros,
i'm using this datepicker in my apps and i advanced it a little.
i updated the code to work with the current rap release (removed lifecycle stuff
an added readAndDispatch), moreover i added calendarweeks.
in your programm you just open this datepicker like a usual dialog (open()). it
returns a string of the selected date. here is the source code. hope it helps!
feel free to ask if there are problems.
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.eclipse.rwt.graphics.Graphics;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* Just a Q&D implementation of a datepicker. Should be replaced once there is
* a component available in standard RWT.
*/
public class DatePickerDialog extends Dialog implements SelectionListener {
private static final Color COLOR_WEEK_DAYS_BG = Graphics.getColor( 211, 231, 223 );
private static final Color COLOR_WEEK_END_FG = Graphics.getColor( 255, 0, 0 );
private static final Color COLOR_NAVIGATION_BG =
Display.getDefault().getSystemColor( SWT.COLOR_LIST_SELECTION );
private static final Color COLOR_NAVIGATION_FG = Graphics.getColor( 255, 255, 255 );
private final SimpleDateFormat FORMATTER = new SimpleDateFormat("MMM yyyy");
private final SimpleDateFormat RETURN_FORMATTER = new
SimpleDateFormat("dd.MM.yyyy");
private Display display = null;
private Date dateSelection = null;
private Calendar selectedDate = null;
private Shell shell = null;
private CLabel sunday = null;
private CLabel monday = null;
private CLabel tuesday = null;
private CLabel wednesday = null;
private CLabel thursday = null;
private CLabel friday = null;
private CLabel saturday = null;
private CLabel kw = null;
private Button yearUp = null;
private Button yearNext = null;
private Button monthUp = null;
private Button monthNext = null;
private CLabel selectionLabel = null;
private Button[] days = new Button[42];
private Button[] kws = new Button[6];
private Point location = null;
/**
*
* @param parent
* @param style
*/
public DatePickerDialog( final Shell parent, final int style ) {
super( parent, style );
}
/**
*
* @param parent
*/
public DatePickerDialog( final Shell parent ) {
this( parent, 0 );
}
/**
*
* @param date
*/
public void setSelectedDate( final Date date ) {
this.dateSelection = date;
}
/**
*
* @param location
*/
public void setLocation( final Point location ) {
this.location = location;
}
/**
*
* @return
*/
public Object open() {
Shell parent = getParent();
display = Display.getDefault();
shell = new Shell( parent, SWT.NO_TRIM );
GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing = 1;
gridLayout.verticalSpacing = 1;
gridLayout.numColumns = 8;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout( gridLayout );
Calendar now = Calendar.getInstance();
if( dateSelection == null ) {
dateSelection = new Date( now.getTimeInMillis() );
}
now.setTime( dateSelection );
createNavigation();
createWeekDays();
int cnt1 = 0;
int cnt2 = 0;
for( int i = 0; i < 48; i++ ) {
if( ((i + 1) % 8) == 0 ) {
kws[ cnt2 ] = new Button( shell, SWT.FLAT | SWT.CENTER );
int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
GridData gridData = new GridData( fillBoth );
gridData.heightHint = 25;
gridData.widthHint = 25;
kws[ cnt2 ].setLayoutData( gridData );
kws[ cnt2 ].setBackground( COLOR_NAVIGATION_BG );
cnt2++;
} else {
days[ cnt1 ] = new Button( shell, SWT.FLAT | SWT.CENTER );
int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
GridData gridData = new GridData( fillBoth );
gridData.heightHint = 25;
gridData.widthHint = 25;
days[ cnt1 ].setLayoutData( gridData );
days[ cnt1 ].setBackground( display.getSystemColor( SWT.COLOR_WHITE ) );
days[ cnt1 ].addSelectionListener( this );
cnt1++;
}
}
setDayForDisplay( now );
if( location != null ) {
shell.setLocation( location );
}
shell.pack();
shell.addShellListener( new ShellAdapter() {
@Override
public void shellDeactivated( ShellEvent e ) {
doClose();
}
} );
shell.open();
shell.setActive();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
if (selectedDate != null) {
return RETURN_FORMATTER.format(selectedDate.getTime());
} else {
return null;
}
}
/**
*
*/
public void widgetDefaultSelected( final SelectionEvent evt ) {
}
/**
*
*/
public void widgetSelected( final SelectionEvent evt ) {
Button day = ( Button )evt.getSource();
selectedDate = Calendar.getInstance();
selectedDate.setTime(dateSelection);
selectedDate.set(Calendar.DATE, Integer.valueOf(day.getText()));
doClose();
}
/**
*
* @param year
* @param month
* @return
*/
private int getLastDayOfMonth( final int year, final int month ) {
int result = 31;
if( month == 4 || month == 6 || month == 9 || month == 11 ) {
result = 30;
} else if( month == 2 ) {
if( isLeapYear( year ) ) {
result = 29;
} else {
result = 28;
}
}
return result;
}
/**
*
* @param year
* @return
*/
private boolean isLeapYear( final int year ) {
return ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 );
}
/**
*
* @param type
* @param value
*/
private void moveTo( final int type, final int value ) {
Calendar now = Calendar.getInstance();
now.setTime( dateSelection );
now.add( type, value );
dateSelection = new Date( now.getTimeInMillis() );
selectionLabel.setText( FORMATTER.format( dateSelection ) );
setDayForDisplay( now );
shell.layout( true );
}
/**
*
* @param now
*/
private void setDayForDisplay( final Calendar now ) {
int currentDay = now.get( Calendar.DAY_OF_MONTH );
now.add( Calendar.DAY_OF_MONTH, -( now.get( Calendar.DAY_OF_MONTH ) - 1 ) );
int startIndex = ( now.get( Calendar.DAY_OF_WEEK ) > 1 )
? ( now.get( Calendar.DAY_OF_WEEK ) - 2 )
: ( Calendar.SATURDAY - 1);
int year = now.get( Calendar.YEAR );
int month = now.get( Calendar.MONTH ) + 1;
int lastDay = this.getLastDayOfMonth( year, month );
int endIndex = startIndex + lastDay - 1;
int startday = 1;
for( int i = 0; i < 42; i++ ) {
Color temp = days[ i ].getBackground();
if( temp.equals( display.getSystemColor( SWT.COLOR_BLUE ) ) ) {
days[ i ].setBackground( display.getSystemColor(
SWT.COLOR_WHITE ) );
}
}
for( int i = 0; i < 42; i++ ) {
if( i >= startIndex && i <= endIndex ) {
days[ i ].setVisible( true );
days[ i ].setText( String.valueOf( startday ) );
days[ i ].setForeground( display.getSystemColor( SWT.COLOR_BLACK ) );
days[ i ].setBackground( display.getSystemColor( SWT.COLOR_WHITE ) );
if( startday == currentDay ) {
Color selectionBg = COLOR_NAVIGATION_BG;
days[ i ].setBackground( selectionBg );
Color selectionFg = COLOR_NAVIGATION_FG;
days[ i ].setForeground( selectionFg );
days[ i ].setFocus();
shell.setDefaultButton( days[ i ] );
}
startday++;
} else {
days[ i ].setVisible( false );
days[ i ].setForeground( display.getSystemColor(
SWT.COLOR_WHITE ) );
}
}
// Letzte KW Anzeige sichtbar?
kws[ 5 ].setVisible( false );
for( int i = 35; i < 42; i++ ) {
if( days[ i ].isVisible( ) ) {
kws[ 5 ].setVisible( true );
break;
}
}
// KWs anzeigen
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis( now.getTimeInMillis() );
for( int i = 0; i < 6; i++ ) {
kws[ i ].setText( String.valueOf( now.get(Calendar.WEEK_OF_YEAR) ) );
now.add( Calendar.WEEK_OF_YEAR, 1 );
}
}
/**
*
*/
private void previousYear() {
moveTo( Calendar.YEAR, -1 );
}
/**
*
*/
private void nextYear() {
moveTo( Calendar.YEAR, 1 );
}
/**
*
*/
private void nextMonth() {
moveTo( Calendar.MONTH, 1 );
}
/**
*
*/
private void previousMonth() {
moveTo( Calendar.MONTH, -1 );
}
/**
*
*/
private void createNavigation() {
Composite composite = new Composite( this.shell, SWT.NONE );
int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
GridData gdComposite = new GridData( fillBoth );
gdComposite.horizontalSpan = 8;
gdComposite.heightHint = 22;
composite.setLayoutData( gdComposite );
Color bgColor = COLOR_NAVIGATION_BG;
Color fgColor = COLOR_NAVIGATION_FG;
composite.setBackground( bgColor );
GridLayout gridLayout = new GridLayout( 8, true );
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 1;
gridLayout.horizontalSpacing = 1;
composite.setLayout( gridLayout );
int buttonStyle = SWT.PUSH;
yearUp = new Button( composite, buttonStyle );
setNavigationColors( yearUp, bgColor, fgColor );
yearUp.setText( "<" );
yearUp.setLayoutData( createNavigationLayoutData() );
yearUp.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( final SelectionEvent e ) {
previousYear();
}
} );
monthUp = new Button( composite, buttonStyle );
monthUp.setText( "<<" );
setNavigationColors( monthUp, bgColor, fgColor );
monthUp.setLayoutData( createNavigationLayoutData() );
monthUp.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( final SelectionEvent e ) {
previousMonth();
}
} );
selectionLabel = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
GridData gdNowLabel = new GridData( GridData.FILL_HORIZONTAL );
gdNowLabel.horizontalSpan = 4;
selectionLabel.setLayoutData( gdNowLabel );
selectionLabel.setText( FORMATTER.format( dateSelection ) );
setNavigationColors( selectionLabel, bgColor, fgColor );
monthNext = new Button( composite, buttonStyle );
monthNext.setText( ">>" );
setNavigationColors( monthNext, bgColor, fgColor );
monthNext.setLayoutData( createNavigationLayoutData() );
monthNext.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( final SelectionEvent e ) {
nextMonth();
}
} );
yearNext = new Button( composite, buttonStyle );
yearNext.setText( ">" );
setNavigationColors( yearNext, bgColor, fgColor );
yearNext.setLayoutData( createNavigationLayoutData() );
yearNext.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( final SelectionEvent e ) {
nextYear();
}
} );
}
/**
*
* @param control
* @param bgColor
* @param fgColor
*/
private void setNavigationColors( final Control control,
final Color bgColor,
final Color fgColor )
{
control.setBackground( bgColor );
control.setForeground( fgColor );
}
/**
*
* @return
*/
private Object createNavigationLayoutData() {
GridData result = new GridData( GridData.FILL_HORIZONTAL );
result.heightHint = 20;
return result;
}
/**
*
*/
private void createWeekDays() {
Composite composite = new Composite( this.shell, SWT.NONE );
int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
GridData gridData = new GridData( fillBoth );
gridData.horizontalSpan = 8;
composite.setLayoutData( gridData );
GridLayout gridLayout = new GridLayout( 8, true );
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 1;
gridLayout.horizontalSpacing = 1;
composite.setLayout( gridLayout );
Color bgColor = COLOR_WEEK_DAYS_BG;
Color weekEndFgColor = COLOR_WEEK_END_FG;
monday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
monday.setText( "Mo" );
monday.setBackground( bgColor );
monday.setLayoutData( createWeekDaysLayoutData() );
tuesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
tuesday.setText( "Di" );
tuesday.setBackground( bgColor );
tuesday.setLayoutData( createWeekDaysLayoutData() );
wednesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
wednesday.setText( "Mi" );
wednesday.setBackground( bgColor );
wednesday.setLayoutData( createWeekDaysLayoutData() );
thursday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
thursday.setText( "Do" );
thursday.setBackground( bgColor );
thursday.setLayoutData( createWeekDaysLayoutData() );
friday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
friday.setText( "Fr" );
friday.setBackground( bgColor );
friday.setLayoutData( createWeekDaysLayoutData() );
saturday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
saturday.setText( "Sa" );
saturday.setForeground( weekEndFgColor );
saturday.setBackground( bgColor );
saturday.setLayoutData( createWeekDaysLayoutData() );
sunday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
sunday.setText( "So" );
sunday.setLayoutData( createWeekDaysLayoutData() );
sunday.setForeground( weekEndFgColor );
sunday.setBackground( bgColor );
kw = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
kw.setText( "KW" );
kw.setLayoutData( createWeekDaysLayoutData() );
kw.setBackground( bgColor );
}
/**
*
* @return
*/
private Object createWeekDaysLayoutData() {
GridData result = new GridData( GridData.FILL_HORIZONTAL );
result.heightHint = 20;
return result;
}
/**
*
*/
private void doClose() {
this.shell.close();
}
}
Alexandros Karypidis schrieb:
> I'm trying to use this DatePickerDialog in my application, but the
> LifeCycleLock and LifeCycleControl classes are unavailable. What plug-in
> do I need to add to my dependencies to have them included in my classpath?
>
> Frank Appel wrote:
>> Hi,
>>
>> here comes the code for the date-picker of the RAP Workbench Demo.
>> It's build only using already existing SWT stuff. I didn't have the
>> time to create a fancy javascript based custom component so I used
>> http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/SWTCalenda r.htm as
>> an inspiration. So surely the implementation could have been nicer and
>> a custom component should be faster, but it works...
>>
>>
>> /**
>> * Just a Q&D implementation of a datepicker. Should be replaced once
>> there is
>> * a component available in standard RWT.
>> */
>> public class DatePickerDialog extends Dialog implements
>> SelectionListener {
>> private final static SimpleDateFormat FORMATTER
>> = new SimpleDateFormat( "MMM yyyy" );
>> private Display display = null;
>> private Date dateSelection = null;
>> private String selectedDate = null;
>> private Shell shell = null;
>> private CLabel sunday = null;
>> private CLabel monday = null;
>> private CLabel tuesday = null;
>> private CLabel wednesday = null;
>> private CLabel thursday = null;
>> private CLabel friday = null;
>> private CLabel saturday = null;
>> private Button yearUp = null;
>> private Button yearNext = null;
>> private Button monthUp = null;
>> private Button monthNext = null;
>> private CLabel selectionLabel = null;
>> private Button[] days = new Button[ 42 ];
>> private Point location;
>> private Object lock;
>>
>>
>> public DatePickerDialog( final Shell parent, final int style ) {
>> super( parent, style );
>> }
>>
>> public DatePickerDialog( final Shell parent ) {
>> this( parent, 0 );
>> }
>>
>> public void setSelectedDate( final Date date ) {
>> this.dateSelection = date;
>> }
>>
>> public void setLocation( final Point location ) {
>> this.location = location;
>> }
>>
>> public Object open() {
>> Shell parent = getParent();
>> display = Display.getDefault();
>> shell = new Shell( parent, SWT.NO_TRIM );
>>
>> GridLayout gridLayout = new GridLayout();
>> gridLayout.marginWidth = 0;
>> gridLayout.marginHeight = 0;
>> gridLayout.horizontalSpacing = 1;
>> gridLayout.verticalSpacing = 1;
>> gridLayout.numColumns = 7;
>> gridLayout.makeColumnsEqualWidth = true;
>> shell.setLayout( gridLayout );
>>
>> Calendar now = Calendar.getInstance();
>> if( dateSelection == null ) {
>> dateSelection = new Date( now.getTimeInMillis() );
>> }
>> now.setTime( dateSelection );
>>
>> createNavigation();
>> createWeekDays();
>>
>> for( int i = 0; i < 42; i++ ) {
>> days[ i ] = new Button( shell, SWT.FLAT | SWT.CENTER );
>> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
>> GridData gridData = new GridData(fillBoth );
>> gridData.heightHint = 25;
>> gridData.widthHint = 25;
>> days[ i ].setLayoutData( gridData );
>> days[ i ].setBackground( display.getSystemColor( SWT.COLOR_WHITE
>> ) );
>> days[ i ].addSelectionListener( this );
>> }
>> setDayForDisplay( now );
>>
>>
>> if( location != null ) {
>> shell.setLocation( location );
>> }
>> shell.pack();
>> shell.addShellListener( new ShellAdapter() {
>> @Override
>> public void shellDeactivated( ShellEvent e ) {
>> doClose();
>> }
>> } );
>> shell.open();
>> shell.setActive();
>> lock = DatePickerDialogHelper.createLock();
>> DatePickerDialogHelper.block( lock, shell );
>> return selectedDate;
>> }
>>
>>
>> //////////////////////////////
>> // interface SelectionListener
>>
>> public void widgetDefaultSelected( final SelectionEvent evt ) {
>> }
>>
>> public void widgetSelected( final SelectionEvent evt ) {
>> Button day = ( Button )evt.getSource();
>> String[] split = selectionLabel.getText().split( " " );
>> this.selectedDate = split[ 0 ] + " " + day.getText() + " " +
>> split[ 1 ];
>> doClose();
>> }
>>
>>
>> //////////////////
>> // helping methods
>>
>> private int getLastDayOfMonth( final int year, final int month ) {
>> int result = 31;
>> if( month == 4 || month == 6 || month == 9 || month == 11 ) {
>> result = 30;
>> } else if( month == 2 ) {
>> if( isLeapYear( year ) ) {
>> result = 29;
>> } else {
>> result = 28;
>> }
>> }
>> return result;
>> }
>>
>> private boolean isLeapYear( final int year ) {
>> return ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 );
>> }
>>
>> private void moveTo( final int type, final int value ) {
>> Calendar now = Calendar.getInstance();
>> now.setTime( dateSelection );
>> now.add( type, value );
>> dateSelection = new Date( now.getTimeInMillis() );
>> selectionLabel.setText( FORMATTER.format( dateSelection ) );
>> setDayForDisplay( now );
>> shell.layout( true );
>> }
>>
>> private void setDayForDisplay( final Calendar now ) {
>> int currentDay = now.get( Calendar.DATE );
>> now.add( Calendar.DAY_OF_MONTH, -( now.get( Calendar.DATE ) - 1 ) );
>> int startIndex = now.get( Calendar.DAY_OF_WEEK ) - 1;
>> int year = now.get( Calendar.YEAR );
>> int month = now.get( Calendar.MONTH ) + 1;
>> int lastDay = this.getLastDayOfMonth( year, month );
>> int endIndex = startIndex + lastDay - 1;
>> int startday = 1;
>> for( int i = 0; i < 42; i++ ) {
>> Color temp = days[ i ].getBackground();
>> if( temp.equals( display.getSystemColor( SWT.COLOR_BLUE ) ) ) {
>> days[ i ].setBackground( display.getSystemColor(
>> SWT.COLOR_WHITE ) );
>> }
>> }
>> for( int i = 0; i < 42; i++ ) {
>> if( i >= startIndex && i <= endIndex ) {
>> days[ i ].setVisible( true );
>> days[ i ].setText( String.valueOf( startday ) );
>> days[ i ].setForeground( display.getSystemColor(
>> SWT.COLOR_BLACK ) );
>> days[ i ].setBackground( display.getSystemColor(
>> SWT.COLOR_WHITE ) );
>> if( startday == currentDay ) {
>> Color selectionBg =
>> DatePickerDialogHelper.getNavigationBgColor();
>> days[ i ].setBackground( selectionBg );
>> Color selectionFg =
>> DatePickerDialogHelper.getNavigationFgColor();
>> days[ i ].setForeground( selectionFg );
>> days[ i ].setFocus();
>> shell.setDefaultButton( days[ i ] );
>> }
>> startday++;
>> } else {
>> days[ i ].setVisible( false );
>> days[ i ].setForeground( display.getSystemColor(
>> SWT.COLOR_WHITE ) );
>> }
>> }
>> }
>>
>> private void previousYear() {
>> moveTo( Calendar.YEAR, -1 );
>> }
>>
>> private void nextYear() {
>> moveTo( Calendar.YEAR, 1 );
>> }
>>
>> private void nextMonth() {
>> moveTo( Calendar.MONTH, 1 );
>> }
>>
>> private void previousMonth() {
>> moveTo( Calendar.MONTH, -1 );
>> }
>>
>> private void createNavigation() {
>> Composite composite = new Composite( this.shell, SWT.NONE );
>> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
>> GridData gdComposite = new GridData( fillBoth );
>> gdComposite.horizontalSpan = 7;
>> gdComposite.heightHint = 22;
>> composite.setLayoutData( gdComposite );
>> Color bgColor = DatePickerDialogHelper.getNavigationBgColor();
>> Color fgColor = DatePickerDialogHelper.getNavigationFgColor();
>> composite.setBackground( bgColor );
>>
>> GridLayout gridLayout = new GridLayout( 7, true );
>> gridLayout.marginWidth = 0;
>> gridLayout.marginHeight = 0;
>> gridLayout.verticalSpacing = 1;
>> gridLayout.horizontalSpacing = 1;
>> composite.setLayout( gridLayout );
>>
>> int buttonStyle = SWT.PUSH;
>> yearUp = new Button( composite, buttonStyle );
>> setNavigationColors( yearUp, bgColor, fgColor );
>> yearUp.setText( "<" );
>> yearUp.setLayoutData( createNavigationLayoutData() );
>> yearUp.addSelectionListener( new SelectionAdapter() {
>> public void widgetSelected( final SelectionEvent e ) {
>> previousYear();
>> }
>> } );
>>
>> monthUp = new Button( composite, buttonStyle );
>> monthUp.setText( "<<" );
>> setNavigationColors( monthUp, bgColor, fgColor );
>> monthUp.setLayoutData( createNavigationLayoutData() );
>> monthUp.addSelectionListener( new SelectionAdapter() {
>> public void widgetSelected( final SelectionEvent e ) {
>> previousMonth();
>> }
>> } );
>>
>> selectionLabel = new CLabel( composite, SWT.CENTER |
>> SWT.SHADOW_OUT );
>> GridData gdNowLabel = new GridData( GridData.FILL_HORIZONTAL );
>> gdNowLabel.horizontalSpan = 3;
>> selectionLabel.setLayoutData( gdNowLabel );
>> selectionLabel.setText( FORMATTER.format( dateSelection ) );
>> setNavigationColors( selectionLabel, bgColor, fgColor );
>>
>> monthNext = new Button( composite, buttonStyle );
>> monthNext.setText( ">>" );
>> setNavigationColors( monthNext, bgColor, fgColor );
>> monthNext.setLayoutData( createNavigationLayoutData() );
>> monthNext.addSelectionListener( new SelectionAdapter() {
>> public void widgetSelected( final SelectionEvent e ) {
>> nextMonth();
>> }
>> } );
>>
>> yearNext = new Button( composite, buttonStyle );
>> yearNext.setText( ">" );
>> setNavigationColors( yearNext, bgColor, fgColor );
>> yearNext.setLayoutData( createNavigationLayoutData() );
>> yearNext.addSelectionListener( new SelectionAdapter() {
>> public void widgetSelected( final SelectionEvent e ) {
>> nextYear();
>> }
>> } );
>> }
>>
>> private void setNavigationColors( final Control control,
>> final Color bgColor,
>> final Color fgColor )
>> {
>> control.setBackground( bgColor );
>> control.setForeground( fgColor );
>> }
>>
>> private Object createNavigationLayoutData() {
>> GridData result = new GridData( GridData.FILL_HORIZONTAL );
>> result.heightHint = 20;
>> return result;
>> }
>>
>> private void createWeekDays() {
>> Composite composite = new Composite( this.shell, SWT.NONE );
>> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
>> GridData gridData = new GridData( fillBoth );
>> gridData.horizontalSpan = 7;
>> composite.setLayoutData( gridData );
>> GridLayout gridLayout = new GridLayout( 7, true );
>> gridLayout.marginWidth = 0;
>> gridLayout.marginHeight = 0;
>> gridLayout.verticalSpacing = 1;
>> gridLayout.horizontalSpacing = 1;
>> composite.setLayout( gridLayout );
>>
>>
>> Color bgColor = DatePickerDialogHelper.getWeekDaysBgColor();
>> Color weekEndFgColor = DatePickerDialogHelper.getWeekEndFgColor();
>> sunday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>> sunday.setText( "Sun" );
>> sunday.setLayoutData( createWeekDaysLayoutData() );
>> sunday.setForeground( weekEndFgColor );
>> sunday.setBackground( bgColor );
>> monday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>> monday.setText( "Mon" );
>> monday.setBackground( bgColor );
>> monday.setLayoutData( createWeekDaysLayoutData() );
>> tuesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>> tuesday.setText( "Tue" );
>> tuesday.setBackground( bgColor );
>> tuesday.setLayoutData( createWeekDaysLayoutData() );
>> wednesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>> wednesday.setText( "Wed" );
>> wednesday.setBackground( bgColor );
>> wednesday.setLayoutData( createWeekDaysLayoutData() );
>> thursday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>> thursday.setText( "Thu" );
>> thursday.setBackground( bgColor );
>> thursday.setLayoutData( createWeekDaysLayoutData() );
>> friday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>> friday.setText( "Fri" );
>> friday.setBackground( bgColor );
>> friday.setLayoutData( createWeekDaysLayoutData() );
>> saturday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>> saturday.setText( "Sat" );
>> saturday.setForeground( weekEndFgColor );
>> saturday.setBackground( bgColor );
>> saturday.setLayoutData( createWeekDaysLayoutData() );
>> }
>>
>> private Object createWeekDaysLayoutData() {
>> GridData result = new GridData( GridData.FILL_HORIZONTAL );
>> result.heightHint = 20;
>> return result;
>> }
>>
>>
>> private void doClose() {
>> this.shell.close();
>> DatePickerDialogHelper.resume( lock );
>> }
>> }
>>
>> public class DatePickerDialogHelper {
>>
>> private static final Color COLOR_WEEK_DAYS_BG
>> = Graphics.getColor( 211, 231, 223 );
>> private static final Color COLOR_WEEK_END_FG
>> = Graphics.getColor( 255, 0, 0 );
>> private static final Color COLOR_NAVIGATION_BG
>> = Display.getDefault().getSystemColor( SWT.COLOR_LIST_SELECTION );
>> private static final Color COLOR_NAVIGATION_FG
>> = Graphics.getColor( 255, 255, 255 );
>>
>> private DatePickerDialogHelper() {
>> // prevent instance creation
>> }
>>
>> public static Object createLock() {
>> return new LifeCycleLock();
>> }
>>
>> public static void block( final Object lock, final Shell shell ) {
>> LifeCycleControl.block( ( LifeCycleLock )lock );
>> }
>>
>> public static void resume( final Object lock ) {
>> LifeCycleControl.resume( ( LifeCycleLock )lock );
>> }
>>
>> public static Color getWeekDaysBgColor() {
>> return COLOR_WEEK_DAYS_BG;
>> }
>>
>> public static Color getWeekEndFgColor() {
>> return COLOR_WEEK_END_FG;
>> }
>>
>> public static Color getNavigationBgColor() {
>> return COLOR_NAVIGATION_BG;
>> }
>>
>> public static Color getNavigationFgColor() {
>> return COLOR_NAVIGATION_FG;
>> }
>> }
>>
>> The second class is just for single sourcing, since I also have an RCP
>> version of the demo, but that's still work in process. So if you like
>> to use the date picker you can also inline the class completely.
>>
>>
>> Ciao
>> Frank
>>
>>
>> "Ben W." <benjamin.wolff@web.de> schrieb im Newsbeitrag
>> news:19975ecc00781b8ec308a12ac492d596$1@www.eclipse.org...
>>> Hi,
>>>
>>> the date picker of the demo is very interesting, in fact i'm in
>>> desperate need of such a widget. unfortunately i'm not into
>>> javascript to create such a custom widget by myself. maybe there is a
>>> description of how to create this widget for "beginners"?? :))
>>>
>>>
>>> regards,
>>> ben
>>>
>>
>>
|
|
|
Re: RAP example, looking like a website [message #104395 is a reply to message #103059] |
Mon, 08 September 2008 19:37 |
Eclipse User |
|
|
|
Originally posted by: akarypid.yahoo.gr
This is a multi-part message in MIME format.
--------------060902090807000201040406
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Hello Ben,
Thank you for the date picker.
With your code as a basis, I have created a "widget" version so that one
can use it in dialogs and forms (I like putting these in expandable
composites / sections of forms).
I thought I'd share it in the spirit of OSS. I found several related
posts in the newsgroup while looking for a date picker and this thread
seems to be the most informative.
Thanks again,
Cheers!
Ben W. wrote:
> Hi Alexandros,
>
> i'm using this datepicker in my apps and i advanced it a little.
> i updated the code to work with the current rap release (removed
> lifecycle stuff an added readAndDispatch), moreover i added calendarweeks.
>
> in your programm you just open this datepicker like a usual dialog
> (open()). it returns a string of the selected date. here is the source
> code. hope it helps!
> feel free to ask if there are problems.
>
>
>
>
> import java.sql.Date;
> import java.text.SimpleDateFormat;
> import java.util.Calendar;
>
> import org.eclipse.rwt.graphics.Graphics;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.custom.CLabel;
> import org.eclipse.swt.events.SelectionAdapter;
> import org.eclipse.swt.events.SelectionEvent;
> import org.eclipse.swt.events.SelectionListener;
> import org.eclipse.swt.events.ShellAdapter;
> import org.eclipse.swt.events.ShellEvent;
> import org.eclipse.swt.graphics.Color;
> import org.eclipse.swt.graphics.Point;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Control;
> import org.eclipse.swt.widgets.Dialog;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
>
> /**
> * Just a Q&D implementation of a datepicker. Should be replaced once
> there is
> * a component available in standard RWT.
> */
> public class DatePickerDialog extends Dialog implements SelectionListener {
>
> private static final Color COLOR_WEEK_DAYS_BG = Graphics.getColor(
> 211, 231, 223 );
> private static final Color COLOR_WEEK_END_FG = Graphics.getColor(
> 255, 0, 0 );
> private static final Color COLOR_NAVIGATION_BG =
> Display.getDefault().getSystemColor( SWT.COLOR_LIST_SELECTION );
> private static final Color COLOR_NAVIGATION_FG = Graphics.getColor(
> 255, 255, 255 );
>
> private final SimpleDateFormat FORMATTER = new SimpleDateFormat("MMM
> yyyy");
> private final SimpleDateFormat RETURN_FORMATTER = new
> SimpleDateFormat("dd.MM.yyyy");
> private Display display = null;
> private Date dateSelection = null;
> private Calendar selectedDate = null;
> private Shell shell = null;
> private CLabel sunday = null;
> private CLabel monday = null;
> private CLabel tuesday = null;
> private CLabel wednesday = null;
> private CLabel thursday = null;
> private CLabel friday = null;
> private CLabel saturday = null;
> private CLabel kw = null;
> private Button yearUp = null;
> private Button yearNext = null;
> private Button monthUp = null;
> private Button monthNext = null;
> private CLabel selectionLabel = null;
> private Button[] days = new Button[42];
> private Button[] kws = new Button[6];
> private Point location = null;
>
> /**
> *
> * @param parent
> * @param style
> */
> public DatePickerDialog( final Shell parent, final int style ) {
> super( parent, style );
> }
>
> /**
> *
> * @param parent
> */
> public DatePickerDialog( final Shell parent ) {
> this( parent, 0 );
> }
>
> /**
> *
> * @param date
> */
> public void setSelectedDate( final Date date ) {
> this.dateSelection = date;
> }
>
> /**
> *
> * @param location
> */
> public void setLocation( final Point location ) {
> this.location = location;
> }
>
> /**
> *
> * @return
> */
> public Object open() {
> Shell parent = getParent();
> display = Display.getDefault();
> shell = new Shell( parent, SWT.NO_TRIM );
>
> GridLayout gridLayout = new GridLayout();
> gridLayout.marginWidth = 0;
> gridLayout.marginHeight = 0;
> gridLayout.horizontalSpacing = 1;
> gridLayout.verticalSpacing = 1;
> gridLayout.numColumns = 8;
> gridLayout.makeColumnsEqualWidth = true;
> shell.setLayout( gridLayout );
>
> Calendar now = Calendar.getInstance();
> if( dateSelection == null ) {
> dateSelection = new Date( now.getTimeInMillis() );
> }
> now.setTime( dateSelection );
>
> createNavigation();
> createWeekDays();
>
> int cnt1 = 0;
> int cnt2 = 0;
>
> for( int i = 0; i < 48; i++ ) {
> if( ((i + 1) % 8) == 0 ) {
> kws[ cnt2 ] = new Button( shell, SWT.FLAT | SWT.CENTER );
> int fillBoth = GridData.FILL_HORIZONTAL |
> GridData.FILL_VERTICAL;
> GridData gridData = new GridData( fillBoth );
> gridData.heightHint = 25;
> gridData.widthHint = 25;
> kws[ cnt2 ].setLayoutData( gridData );
> kws[ cnt2 ].setBackground( COLOR_NAVIGATION_BG );
> cnt2++;
> } else {
> days[ cnt1 ] = new Button( shell, SWT.FLAT | SWT.CENTER );
> int fillBoth = GridData.FILL_HORIZONTAL |
> GridData.FILL_VERTICAL;
> GridData gridData = new GridData( fillBoth );
> gridData.heightHint = 25;
> gridData.widthHint = 25;
> days[ cnt1 ].setLayoutData( gridData );
> days[ cnt1 ].setBackground( display.getSystemColor(
> SWT.COLOR_WHITE ) );
> days[ cnt1 ].addSelectionListener( this );
> cnt1++;
> }
> }
> setDayForDisplay( now );
>
>
> if( location != null ) {
> shell.setLocation( location );
> }
>
> shell.pack();
> shell.addShellListener( new ShellAdapter() {
> @Override
> public void shellDeactivated( ShellEvent e ) {
> doClose();
> }
>
> } );
>
> shell.open();
> shell.setActive();
>
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep();
> }
> }
>
> if (selectedDate != null) {
> return RETURN_FORMATTER.format(selectedDate.getTime());
> } else {
> return null;
> }
> }
>
> /**
> *
> */
> public void widgetDefaultSelected( final SelectionEvent evt ) {
>
> }
>
> /**
> *
> */
> public void widgetSelected( final SelectionEvent evt ) {
> Button day = ( Button )evt.getSource();
> selectedDate = Calendar.getInstance();
> selectedDate.setTime(dateSelection);
> selectedDate.set(Calendar.DATE, Integer.valueOf(day.getText()));
> doClose();
> }
>
> /**
> *
> * @param year
> * @param month
> * @return
> */
> private int getLastDayOfMonth( final int year, final int month ) {
> int result = 31;
> if( month == 4 || month == 6 || month == 9 || month == 11 ) {
> result = 30;
> } else if( month == 2 ) {
> if( isLeapYear( year ) ) {
> result = 29;
> } else {
> result = 28;
> }
> }
> return result;
> }
>
> /**
> *
> * @param year
> * @return
> */
> private boolean isLeapYear( final int year ) {
> return ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 );
> }
>
> /**
> *
> * @param type
> * @param value
> */
> private void moveTo( final int type, final int value ) {
> Calendar now = Calendar.getInstance();
> now.setTime( dateSelection );
> now.add( type, value );
> dateSelection = new Date( now.getTimeInMillis() );
> selectionLabel.setText( FORMATTER.format( dateSelection ) );
> setDayForDisplay( now );
> shell.layout( true );
> }
>
> /**
> *
> * @param now
> */
> private void setDayForDisplay( final Calendar now ) {
> int currentDay = now.get( Calendar.DAY_OF_MONTH );
> now.add( Calendar.DAY_OF_MONTH, -( now.get(
> Calendar.DAY_OF_MONTH ) - 1 ) );
> int startIndex = ( now.get( Calendar.DAY_OF_WEEK ) > 1 )
> ? ( now.get( Calendar.DAY_OF_WEEK ) - 2 )
> : ( Calendar.SATURDAY - 1);
> int year = now.get( Calendar.YEAR );
> int month = now.get( Calendar.MONTH ) + 1;
> int lastDay = this.getLastDayOfMonth( year, month );
> int endIndex = startIndex + lastDay - 1;
> int startday = 1;
>
> for( int i = 0; i < 42; i++ ) {
> Color temp = days[ i ].getBackground();
> if( temp.equals( display.getSystemColor( SWT.COLOR_BLUE ) ) ) {
> days[ i ].setBackground( display.getSystemColor(
> SWT.COLOR_WHITE ) );
> }
> }
>
> for( int i = 0; i < 42; i++ ) {
> if( i >= startIndex && i <= endIndex ) {
> days[ i ].setVisible( true );
> days[ i ].setText( String.valueOf( startday ) );
> days[ i ].setForeground( display.getSystemColor(
> SWT.COLOR_BLACK ) );
> days[ i ].setBackground( display.getSystemColor(
> SWT.COLOR_WHITE ) );
> if( startday == currentDay ) {
> Color selectionBg = COLOR_NAVIGATION_BG;
> days[ i ].setBackground( selectionBg );
> Color selectionFg = COLOR_NAVIGATION_FG;
> days[ i ].setForeground( selectionFg );
> days[ i ].setFocus();
> shell.setDefaultButton( days[ i ] );
> }
> startday++;
> } else {
> days[ i ].setVisible( false );
> days[ i ].setForeground( display.getSystemColor(
> SWT.COLOR_WHITE ) );
> }
> }
>
> // Letzte KW Anzeige sichtbar?
> kws[ 5 ].setVisible( false );
>
> for( int i = 35; i < 42; i++ ) {
> if( days[ i ].isVisible( ) ) {
> kws[ 5 ].setVisible( true );
> break;
> }
> }
>
> // KWs anzeigen
> Calendar cal = Calendar.getInstance();
> cal.setTimeInMillis( now.getTimeInMillis() );
>
> for( int i = 0; i < 6; i++ ) {
> kws[ i ].setText( String.valueOf(
> now.get(Calendar.WEEK_OF_YEAR) ) );
> now.add( Calendar.WEEK_OF_YEAR, 1 );
> }
> }
>
> /**
> *
> */
> private void previousYear() {
> moveTo( Calendar.YEAR, -1 );
> }
>
> /**
> *
> */
> private void nextYear() {
> moveTo( Calendar.YEAR, 1 );
> }
>
> /**
> *
> */
> private void nextMonth() {
> moveTo( Calendar.MONTH, 1 );
> }
>
> /**
> *
> */
> private void previousMonth() {
> moveTo( Calendar.MONTH, -1 );
> }
>
> /**
> *
> */
> private void createNavigation() {
> Composite composite = new Composite( this.shell, SWT.NONE );
> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
> GridData gdComposite = new GridData( fillBoth );
> gdComposite.horizontalSpan = 8;
> gdComposite.heightHint = 22;
> composite.setLayoutData( gdComposite );
> Color bgColor = COLOR_NAVIGATION_BG;
> Color fgColor = COLOR_NAVIGATION_FG;
> composite.setBackground( bgColor );
>
> GridLayout gridLayout = new GridLayout( 8, true );
> gridLayout.marginWidth = 0;
> gridLayout.marginHeight = 0;
> gridLayout.verticalSpacing = 1;
> gridLayout.horizontalSpacing = 1;
> composite.setLayout( gridLayout );
>
> int buttonStyle = SWT.PUSH;
> yearUp = new Button( composite, buttonStyle );
> setNavigationColors( yearUp, bgColor, fgColor );
> yearUp.setText( "<" );
> yearUp.setLayoutData( createNavigationLayoutData() );
> yearUp.addSelectionListener( new SelectionAdapter() {
> public void widgetSelected( final SelectionEvent e ) {
> previousYear();
> }
> } );
>
> monthUp = new Button( composite, buttonStyle );
> monthUp.setText( "<<" );
> setNavigationColors( monthUp, bgColor, fgColor );
> monthUp.setLayoutData( createNavigationLayoutData() );
> monthUp.addSelectionListener( new SelectionAdapter() {
> public void widgetSelected( final SelectionEvent e ) {
> previousMonth();
> }
> } );
>
> selectionLabel = new CLabel( composite, SWT.CENTER |
> SWT.SHADOW_OUT );
> GridData gdNowLabel = new GridData( GridData.FILL_HORIZONTAL );
> gdNowLabel.horizontalSpan = 4;
> selectionLabel.setLayoutData( gdNowLabel );
> selectionLabel.setText( FORMATTER.format( dateSelection ) );
> setNavigationColors( selectionLabel, bgColor, fgColor );
>
> monthNext = new Button( composite, buttonStyle );
> monthNext.setText( ">>" );
> setNavigationColors( monthNext, bgColor, fgColor );
> monthNext.setLayoutData( createNavigationLayoutData() );
> monthNext.addSelectionListener( new SelectionAdapter() {
> public void widgetSelected( final SelectionEvent e ) {
> nextMonth();
> }
> } );
>
> yearNext = new Button( composite, buttonStyle );
> yearNext.setText( ">" );
> setNavigationColors( yearNext, bgColor, fgColor );
> yearNext.setLayoutData( createNavigationLayoutData() );
> yearNext.addSelectionListener( new SelectionAdapter() {
> public void widgetSelected( final SelectionEvent e ) {
> nextYear();
> }
> } );
> }
>
> /**
> *
> * @param control
> * @param bgColor
> * @param fgColor
> */
> private void setNavigationColors( final Control control,
> final Color bgColor,
> final Color fgColor )
> {
> control.setBackground( bgColor );
> control.setForeground( fgColor );
> }
>
> /**
> *
> * @return
> */
> private Object createNavigationLayoutData() {
> GridData result = new GridData( GridData.FILL_HORIZONTAL );
> result.heightHint = 20;
> return result;
> }
>
> /**
> *
> */
> private void createWeekDays() {
> Composite composite = new Composite( this.shell, SWT.NONE );
> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
> GridData gridData = new GridData( fillBoth );
> gridData.horizontalSpan = 8;
> composite.setLayoutData( gridData );
> GridLayout gridLayout = new GridLayout( 8, true );
> gridLayout.marginWidth = 0;
> gridLayout.marginHeight = 0;
> gridLayout.verticalSpacing = 1;
> gridLayout.horizontalSpacing = 1;
> composite.setLayout( gridLayout );
>
>
> Color bgColor = COLOR_WEEK_DAYS_BG;
> Color weekEndFgColor = COLOR_WEEK_END_FG;
> monday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> monday.setText( "Mo" );
> monday.setBackground( bgColor );
> monday.setLayoutData( createWeekDaysLayoutData() );
> tuesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> tuesday.setText( "Di" );
> tuesday.setBackground( bgColor );
> tuesday.setLayoutData( createWeekDaysLayoutData() );
> wednesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> wednesday.setText( "Mi" );
> wednesday.setBackground( bgColor );
> wednesday.setLayoutData( createWeekDaysLayoutData() );
> thursday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> thursday.setText( "Do" );
> thursday.setBackground( bgColor );
> thursday.setLayoutData( createWeekDaysLayoutData() );
> friday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> friday.setText( "Fr" );
> friday.setBackground( bgColor );
> friday.setLayoutData( createWeekDaysLayoutData() );
> saturday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> saturday.setText( "Sa" );
> saturday.setForeground( weekEndFgColor );
> saturday.setBackground( bgColor );
> saturday.setLayoutData( createWeekDaysLayoutData() );
> sunday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> sunday.setText( "So" );
> sunday.setLayoutData( createWeekDaysLayoutData() );
> sunday.setForeground( weekEndFgColor );
> sunday.setBackground( bgColor );
> kw = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
> kw.setText( "KW" );
> kw.setLayoutData( createWeekDaysLayoutData() );
> kw.setBackground( bgColor );
> }
>
> /**
> *
> * @return
> */
> private Object createWeekDaysLayoutData() {
> GridData result = new GridData( GridData.FILL_HORIZONTAL );
> result.heightHint = 20;
> return result;
> }
>
> /**
> *
> */
> private void doClose() {
> this.shell.close();
> }
>
> }
>
>
>
> Alexandros Karypidis schrieb:
>> I'm trying to use this DatePickerDialog in my application, but the
>> LifeCycleLock and LifeCycleControl classes are unavailable. What
>> plug-in do I need to add to my dependencies to have them included in
>> my classpath?
>>
>> Frank Appel wrote:
>>> Hi,
>>>
>>> here comes the code for the date-picker of the RAP Workbench Demo.
>>> It's build only using already existing SWT stuff. I didn't have the
>>> time to create a fancy javascript based custom component so I used
>>> http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/SWTCalenda r.htm as
>>> an inspiration. So surely the implementation could have been nicer
>>> and a custom component should be faster, but it works...
>>>
>>>
>>> /**
>>> * Just a Q&D implementation of a datepicker. Should be replaced once
>>> there is
>>> * a component available in standard RWT.
>>> */
>>> public class DatePickerDialog extends Dialog implements
>>> SelectionListener {
>>> private final static SimpleDateFormat FORMATTER
>>> = new SimpleDateFormat( "MMM yyyy" );
>>> private Display display = null;
>>> private Date dateSelection = null;
>>> private String selectedDate = null;
>>> private Shell shell = null;
>>> private CLabel sunday = null;
>>> private CLabel monday = null;
>>> private CLabel tuesday = null;
>>> private CLabel wednesday = null;
>>> private CLabel thursday = null;
>>> private CLabel friday = null;
>>> private CLabel saturday = null;
>>> private Button yearUp = null;
>>> private Button yearNext = null;
>>> private Button monthUp = null;
>>> private Button monthNext = null;
>>> private CLabel selectionLabel = null;
>>> private Button[] days = new Button[ 42 ];
>>> private Point location;
>>> private Object lock;
>>>
>>>
>>> public DatePickerDialog( final Shell parent, final int style ) {
>>> super( parent, style );
>>> }
>>>
>>> public DatePickerDialog( final Shell parent ) {
>>> this( parent, 0 );
>>> }
>>>
>>> public void setSelectedDate( final Date date ) {
>>> this.dateSelection = date;
>>> }
>>>
>>> public void setLocation( final Point location ) {
>>> this.location = location;
>>> }
>>>
>>> public Object open() {
>>> Shell parent = getParent();
>>> display = Display.getDefault();
>>> shell = new Shell( parent, SWT.NO_TRIM );
>>>
>>> GridLayout gridLayout = new GridLayout();
>>> gridLayout.marginWidth = 0;
>>> gridLayout.marginHeight = 0;
>>> gridLayout.horizontalSpacing = 1;
>>> gridLayout.verticalSpacing = 1;
>>> gridLayout.numColumns = 7;
>>> gridLayout.makeColumnsEqualWidth = true;
>>> shell.setLayout( gridLayout );
>>>
>>> Calendar now = Calendar.getInstance();
>>> if( dateSelection == null ) {
>>> dateSelection = new Date( now.getTimeInMillis() );
>>> }
>>> now.setTime( dateSelection );
>>>
>>> createNavigation();
>>> createWeekDays();
>>>
>>> for( int i = 0; i < 42; i++ ) {
>>> days[ i ] = new Button( shell, SWT.FLAT | SWT.CENTER );
>>> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
>>> GridData gridData = new GridData(fillBoth );
>>> gridData.heightHint = 25;
>>> gridData.widthHint = 25;
>>> days[ i ].setLayoutData( gridData );
>>> days[ i ].setBackground( display.getSystemColor(
>>> SWT.COLOR_WHITE ) );
>>> days[ i ].addSelectionListener( this );
>>> }
>>> setDayForDisplay( now );
>>>
>>>
>>> if( location != null ) {
>>> shell.setLocation( location );
>>> }
>>> shell.pack();
>>> shell.addShellListener( new ShellAdapter() {
>>> @Override
>>> public void shellDeactivated( ShellEvent e ) {
>>> doClose();
>>> }
>>> } );
>>> shell.open();
>>> shell.setActive();
>>> lock = DatePickerDialogHelper.createLock();
>>> DatePickerDialogHelper.block( lock, shell );
>>> return selectedDate;
>>> }
>>>
>>>
>>> //////////////////////////////
>>> // interface SelectionListener
>>>
>>> public void widgetDefaultSelected( final SelectionEvent evt ) {
>>> }
>>>
>>> public void widgetSelected( final SelectionEvent evt ) {
>>> Button day = ( Button )evt.getSource();
>>> String[] split = selectionLabel.getText().split( " " );
>>> this.selectedDate = split[ 0 ] + " " + day.getText() + " " +
>>> split[ 1 ];
>>> doClose();
>>> }
>>>
>>>
>>> //////////////////
>>> // helping methods
>>>
>>> private int getLastDayOfMonth( final int year, final int month ) {
>>> int result = 31;
>>> if( month == 4 || month == 6 || month == 9 || month == 11 ) {
>>> result = 30;
>>> } else if( month == 2 ) {
>>> if( isLeapYear( year ) ) {
>>> result = 29;
>>> } else {
>>> result = 28;
>>> }
>>> }
>>> return result;
>>> }
>>>
>>> private boolean isLeapYear( final int year ) {
>>> return ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 );
>>> }
>>>
>>> private void moveTo( final int type, final int value ) {
>>> Calendar now = Calendar.getInstance();
>>> now.setTime( dateSelection );
>>> now.add( type, value );
>>> dateSelection = new Date( now.getTimeInMillis() );
>>> selectionLabel.setText( FORMATTER.format( dateSelection ) );
>>> setDayForDisplay( now );
>>> shell.layout( true );
>>> }
>>>
>>> private void setDayForDisplay( final Calendar now ) {
>>> int currentDay = now.get( Calendar.DATE );
>>> now.add( Calendar.DAY_OF_MONTH, -( now.get( Calendar.DATE ) - 1 ) );
>>> int startIndex = now.get( Calendar.DAY_OF_WEEK ) - 1;
>>> int year = now.get( Calendar.YEAR );
>>> int month = now.get( Calendar.MONTH ) + 1;
>>> int lastDay = this.getLastDayOfMonth( year, month );
>>> int endIndex = startIndex + lastDay - 1;
>>> int startday = 1;
>>> for( int i = 0; i < 42; i++ ) {
>>> Color temp = days[ i ].getBackground();
>>> if( temp.equals( display.getSystemColor( SWT.COLOR_BLUE ) ) ) {
>>> days[ i ].setBackground( display.getSystemColor(
>>> SWT.COLOR_WHITE ) );
>>> }
>>> }
>>> for( int i = 0; i < 42; i++ ) {
>>> if( i >= startIndex && i <= endIndex ) {
>>> days[ i ].setVisible( true );
>>> days[ i ].setText( String.valueOf( startday ) );
>>> days[ i ].setForeground( display.getSystemColor(
>>> SWT.COLOR_BLACK ) );
>>> days[ i ].setBackground( display.getSystemColor(
>>> SWT.COLOR_WHITE ) );
>>> if( startday == currentDay ) {
>>> Color selectionBg =
>>> DatePickerDialogHelper.getNavigationBgColor();
>>> days[ i ].setBackground( selectionBg );
>>> Color selectionFg =
>>> DatePickerDialogHelper.getNavigationFgColor();
>>> days[ i ].setForeground( selectionFg );
>>> days[ i ].setFocus();
>>> shell.setDefaultButton( days[ i ] );
>>> }
>>> startday++;
>>> } else {
>>> days[ i ].setVisible( false );
>>> days[ i ].setForeground( display.getSystemColor(
>>> SWT.COLOR_WHITE ) );
>>> }
>>> }
>>> }
>>>
>>> private void previousYear() {
>>> moveTo( Calendar.YEAR, -1 );
>>> }
>>>
>>> private void nextYear() {
>>> moveTo( Calendar.YEAR, 1 );
>>> }
>>>
>>> private void nextMonth() {
>>> moveTo( Calendar.MONTH, 1 );
>>> }
>>>
>>> private void previousMonth() {
>>> moveTo( Calendar.MONTH, -1 );
>>> }
>>>
>>> private void createNavigation() {
>>> Composite composite = new Composite( this.shell, SWT.NONE );
>>> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
>>> GridData gdComposite = new GridData( fillBoth );
>>> gdComposite.horizontalSpan = 7;
>>> gdComposite.heightHint = 22;
>>> composite.setLayoutData( gdComposite );
>>> Color bgColor = DatePickerDialogHelper.getNavigationBgColor();
>>> Color fgColor = DatePickerDialogHelper.getNavigationFgColor();
>>> composite.setBackground( bgColor );
>>>
>>> GridLayout gridLayout = new GridLayout( 7, true );
>>> gridLayout.marginWidth = 0;
>>> gridLayout.marginHeight = 0;
>>> gridLayout.verticalSpacing = 1;
>>> gridLayout.horizontalSpacing = 1;
>>> composite.setLayout( gridLayout );
>>>
>>> int buttonStyle = SWT.PUSH;
>>> yearUp = new Button( composite, buttonStyle );
>>> setNavigationColors( yearUp, bgColor, fgColor );
>>> yearUp.setText( "<" );
>>> yearUp.setLayoutData( createNavigationLayoutData() );
>>> yearUp.addSelectionListener( new SelectionAdapter() {
>>> public void widgetSelected( final SelectionEvent e ) {
>>> previousYear();
>>> }
>>> } );
>>>
>>> monthUp = new Button( composite, buttonStyle );
>>> monthUp.setText( "<<" );
>>> setNavigationColors( monthUp, bgColor, fgColor );
>>> monthUp.setLayoutData( createNavigationLayoutData() );
>>> monthUp.addSelectionListener( new SelectionAdapter() {
>>> public void widgetSelected( final SelectionEvent e ) {
>>> previousMonth();
>>> }
>>> } );
>>>
>>> selectionLabel = new CLabel( composite, SWT.CENTER |
>>> SWT.SHADOW_OUT );
>>> GridData gdNowLabel = new GridData( GridData.FILL_HORIZONTAL );
>>> gdNowLabel.horizontalSpan = 3;
>>> selectionLabel.setLayoutData( gdNowLabel );
>>> selectionLabel.setText( FORMATTER.format( dateSelection ) );
>>> setNavigationColors( selectionLabel, bgColor, fgColor );
>>>
>>> monthNext = new Button( composite, buttonStyle );
>>> monthNext.setText( ">>" );
>>> setNavigationColors( monthNext, bgColor, fgColor );
>>> monthNext.setLayoutData( createNavigationLayoutData() );
>>> monthNext.addSelectionListener( new SelectionAdapter() {
>>> public void widgetSelected( final SelectionEvent e ) {
>>> nextMonth();
>>> }
>>> } );
>>>
>>> yearNext = new Button( composite, buttonStyle );
>>> yearNext.setText( ">" );
>>> setNavigationColors( yearNext, bgColor, fgColor );
>>> yearNext.setLayoutData( createNavigationLayoutData() );
>>> yearNext.addSelectionListener( new SelectionAdapter() {
>>> public void widgetSelected( final SelectionEvent e ) {
>>> nextYear();
>>> }
>>> } );
>>> }
>>>
>>> private void setNavigationColors( final Control control,
>>> final Color bgColor,
>>> final Color fgColor )
>>> {
>>> control.setBackground( bgColor );
>>> control.setForeground( fgColor );
>>> }
>>>
>>> private Object createNavigationLayoutData() {
>>> GridData result = new GridData( GridData.FILL_HORIZONTAL );
>>> result.heightHint = 20;
>>> return result;
>>> }
>>>
>>> private void createWeekDays() {
>>> Composite composite = new Composite( this.shell, SWT.NONE );
>>> int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
>>> GridData gridData = new GridData( fillBoth );
>>> gridData.horizontalSpan = 7;
>>> composite.setLayoutData( gridData );
>>> GridLayout gridLayout = new GridLayout( 7, true );
>>> gridLayout.marginWidth = 0;
>>> gridLayout.marginHeight = 0;
>>> gridLayout.verticalSpacing = 1;
>>> gridLayout.horizontalSpacing = 1;
>>> composite.setLayout( gridLayout );
>>>
>>>
>>> Color bgColor = DatePickerDialogHelper.getWeekDaysBgColor();
>>> Color weekEndFgColor = DatePickerDialogHelper.getWeekEndFgColor();
>>> sunday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>>> sunday.setText( "Sun" );
>>> sunday.setLayoutData( createWeekDaysLayoutData() );
>>> sunday.setForeground( weekEndFgColor );
>>> sunday.setBackground( bgColor );
>>> monday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>>> monday.setText( "Mon" );
>>> monday.setBackground( bgColor );
>>> monday.setLayoutData( createWeekDaysLayoutData() );
>>> tuesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>>> tuesday.setText( "Tue" );
>>> tuesday.setBackground( bgColor );
>>> tuesday.setLayoutData( createWeekDaysLayoutData() );
>>> wednesday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>>> wednesday.setText( "Wed" );
>>> wednesday.setBackground( bgColor );
>>> wednesday.setLayoutData( createWeekDaysLayoutData() );
>>> thursday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>>> thursday.setText( "Thu" );
>>> thursday.setBackground( bgColor );
>>> thursday.setLayoutData( createWeekDaysLayoutData() );
>>> friday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>>> friday.setText( "Fri" );
>>> friday.setBackground( bgColor );
>>> friday.setLayoutData( createWeekDaysLayoutData() );
>>> saturday = new CLabel( composite, SWT.CENTER | SWT.SHADOW_OUT );
>>> saturday.setText( "Sat" );
>>> saturday.setForeground( weekEndFgColor );
>>> saturday.setBackground( bgColor );
>>> saturday.setLayoutData( createWeekDaysLayoutData() );
>>> }
>>>
>>> private Object createWeekDaysLayoutData() {
>>> GridData result = new GridData( GridData.FILL_HORIZONTAL );
>>> result.heightHint = 20;
>>> return result;
>>> }
>>>
>>>
>>> private void doClose() {
>>> this.shell.close();
>>> DatePickerDialogHelper.resume( lock );
>>> }
>>> }
>>>
>>> public class DatePickerDialogHelper {
>>>
>>> private static final Color COLOR_WEEK_DAYS_BG
>>> = Graphics.getColor( 211, 231, 223 );
>>> private static final Color COLOR_WEEK_END_FG
>>> = Graphics.getColor( 255, 0, 0 );
>>> private static final Color COLOR_NAVIGATION_BG
>>> = Display.getDefault().getSystemColor( SWT.COLOR_LIST_SELECTION );
>>> private static final Color COLOR_NAVIGATION_FG
>>> = Graphics.getColor( 255, 255, 255 );
>>>
>>> private DatePickerDialogHelper() {
>>> // prevent instance creation
>>> }
>>>
>>> public static Object createLock() {
>>> return new LifeCycleLock();
>>> }
>>>
>>> public static void block( final Object lock, final Shell shell ) {
>>> LifeCycleControl.block( ( LifeCycleLock )lock );
>>> }
>>>
>>> public static void resume( final Object lock ) {
>>> LifeCycleControl.resume( ( LifeCycleLock )lock );
>>> }
>>>
>>> public static Color getWeekDaysBgColor() {
>>> return COLOR_WEEK_DAYS_BG;
>>> }
>>>
>>> public static Color getWeekEndFgColor() {
>>> return COLOR_WEEK_END_FG;
>>> }
>>>
>>> public static Color getNavigationBgColor() {
>>> return COLOR_NAVIGATION_BG;
>>> }
>>>
>>> public static Color getNavigationFgColor() {
>>> return COLOR_NAVIGATION_FG;
>>> }
>>> }
>>>
>>> The second class is just for single sourcing, since I also have an
>>> RCP version of the demo, but that's still work in process. So if you
>>> like to use the date picker you can also inline the class completely.
>>>
>>>
>>> Ciao
>>> Frank
>>>
>>>
>>> "Ben W." <benjamin.wolff@web.de> schrieb im Newsbeitrag
>>> news:19975ecc00781b8ec308a12ac492d596$1@www.eclipse.org...
>>>> Hi,
>>>>
>>>> the date picker of the demo is very interesting, in fact i'm in
>>>> desperate need of such a widget. unfortunately i'm not into
>>>> javascript to create such a custom widget by myself. maybe there is
>>>> a description of how to create this widget for "beginners"?? :))
>>>>
>>>>
>>>> regards,
>>>> ben
>>>>
>>>
>>>
--------------060902090807000201040406
Content-Type: text/x-java;
name="DatePicker.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="DatePicker.java"
package echoes.station.daemon.widgets;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.eclipse.rwt.graphics.Graphics;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
/**
* Just a Q&D implementation of a datepicker. Should be replaced once there is a
* component available in standard RWT.
*/
public class DatePicker extends Composite implements SelectionListener {
private static final Color COLOR_WEEK_DAYS_BG = Graphics.getColor(211, 231,
223);
private static final Color COLOR_WEEK_END_FG = Graphics.getColor(255, 0, 0);
private static final Color COLOR_NAVIGATION_BG = Display.getDefault()
.getSystemColor(SWT.COLOR_LIST_SELECTION);
private static final Color COLOR_NAVIGATION_FG = Graphics.getColor(255,
255, 255);
private final SimpleDateFormat FORMATTER = new SimpleDateFormat("MMM yyyy");
private Display display = null;
private Date selectedDate = null;
private CLabel sunday = null;
private CLabel monday = null;
private CLabel tuesday = null;
private CLabel wednesday = null;
private CLabel thursday = null;
private CLabel friday = null;
private CLabel saturday = null;
private CLabel kw = null;
private Button yearUp = null;
private Button yearNext = null;
private Button monthUp = null;
private Button monthNext = null;
private CLabel selectionLabel = null;
private Button[] days = new Button[42];
private Button[] kws = new Button[6];
private Point location = null;
public DatePicker(Composite parent, int style) {
super(parent, style);
display = Display.getDefault();
GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing = 1;
gridLayout.verticalSpacing = 1;
gridLayout.numColumns = 8;
gridLayout.makeColumnsEqualWidth = true;
setLayout(gridLayout);
Calendar now = Calendar.getInstance();
if (selectedDate == null) {
selectedDate = new Date(now.getTimeInMillis());
}
now.setTime(selectedDate);
createNavigation();
createWeekDays();
int cnt1 = 0;
int cnt2 = 0;
for (int i = 0; i < 48; i++) {
if (((i + 1) % 8) == 0) {
kws[cnt2] = new Button(this, SWT.FLAT | SWT.CENTER);
int fillBoth = GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL;
GridData gridData = new GridData(fillBoth);
gridData.heightHint = 25;
gridData.widthHint = 25;
kws[cnt2].setLayoutData(gridData);
kws[cnt2].setBackground(COLOR_NAVIGATION_BG);
cnt2++;
} else {
days[cnt1] = new Button(this, SWT.FLAT | SWT.CENTER);
int fillBoth = GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL;
GridData gridData = new GridData(fillBoth);
gridData.heightHint = 25;
gridData.widthHint = 25;
days[cnt1].setLayoutData(gridData);
days[cnt1].setBackground(display
.getSystemColor(SWT.COLOR_WHITE));
days[cnt1].addSelectionListener(this);
cnt1++;
}
}
setDayForDisplay(now);
if (location != null) {
setLocation(location);
}
pack();
}
public void setSelectedDate(final Date date) {
this.selectedDate = date;
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(date);
setDayForDisplay(cal);
selectionLabel.setText(FORMATTER.format(selectedDate));
}
public java.util.Date getSelectedDate() {
return selectedDate;
}
public void setLocation(final Point location) {
this.location = location;
}
public void widgetDefaultSelected(final SelectionEvent evt) {
}
public void widgetSelected(final SelectionEvent evt) {
Button day = (Button) evt.getSource();
Calendar newSelectedDate = Calendar.getInstance();
newSelectedDate.setTime(selectedDate);
newSelectedDate.set(Calendar.DATE, Integer.valueOf(day.getText()));
selectedDate = newSelectedDate.getTime();
setDayForDisplay(newSelectedDate);
}
private int getLastDayOfMonth(final int year, final int month) {
int result = 31;
if (month == 4 || month == 6 || month == 9 || month == 11) {
result = 30;
} else if (month == 2) {
if (isLeapYear(year)) {
result = 29;
} else {
result = 28;
}
}
return result;
}
private boolean isLeapYear(final int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
private void moveTo(final int type, final int value) {
Calendar now = Calendar.getInstance();
now.setTime(selectedDate);
now.add(type, value);
selectedDate = new Date(now.getTimeInMillis());
selectionLabel.setText(FORMATTER.format(selectedDate));
setDayForDisplay(now);
layout(true);
}
private void setDayForDisplay(final Calendar now) {
int currentDay = now.get(Calendar.DAY_OF_MONTH);
now.add(Calendar.DAY_OF_MONTH, -(now.get(Calendar.DAY_OF_MONTH) - 1));
int startIndex = (now.get(Calendar.DAY_OF_WEEK) > 1) ? (now
.get(Calendar.DAY_OF_WEEK) - 2) : (Calendar.SATURDAY - 1);
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int lastDay = this.getLastDayOfMonth(year, month);
int endIndex = startIndex + lastDay - 1;
int startday = 1;
for (int i = 0; i < 42; i++) {
Color temp = days[i].getBackground();
if (temp.equals(display.getSystemColor(SWT.COLOR_BLUE))) {
days[i].setBackground(display.getSystemColor(SWT.COLOR_WHITE ));
}
}
for (int i = 0; i < 42; i++) {
if (i >= startIndex && i <= endIndex) {
days[i].setVisible(true);
days[i].setText(String.valueOf(startday));
days[i].setForeground(display.getSystemColor(SWT.COLOR_BLACK ));
days[i].setBackground(display.getSystemColor(SWT.COLOR_WHITE ));
if (startday == currentDay) {
Color selectionBg = COLOR_NAVIGATION_BG;
days[i].setBackground(selectionBg);
Color selectionFg = COLOR_NAVIGATION_FG;
days[i].setForeground(selectionFg);
days[i].setFocus();
}
startday++;
} else {
days[i].setVisible(false);
days[i].setForeground(display.getSystemColor(SWT.COLOR_WHITE ));
}
}
// Letzte KW Anzeige sichtbar?
kws[5].setVisible(false);
for (int i = 35; i < 42; i++) {
if (days[i].isVisible()) {
kws[5].setVisible(true);
break;
}
}
// KWs anzeigen
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(now.getTimeInMillis());
for (int i = 0; i < 6; i++) {
kws[i].setText(String.valueOf(now.get(Calendar.WEEK_OF_YEAR) ));
now.add(Calendar.WEEK_OF_YEAR, 1);
}
}
private void previousYear() {
moveTo(Calendar.YEAR, -1);
}
private void nextYear() {
moveTo(Calendar.YEAR, 1);
}
private void nextMonth() {
moveTo(Calendar.MONTH, 1);
}
private void previousMonth() {
moveTo(Calendar.MONTH, -1);
}
private void createNavigation() {
Composite composite = new Composite(this, SWT.NONE);
int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
GridData gdComposite = new GridData(fillBoth);
gdComposite.horizontalSpan = 8;
gdComposite.heightHint = 22;
composite.setLayoutData(gdComposite);
Color bgColor = COLOR_NAVIGATION_BG;
Color fgColor = COLOR_NAVIGATION_FG;
composite.setBackground(bgColor);
GridLayout gridLayout = new GridLayout(8, true);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 1;
gridLayout.horizontalSpacing = 1;
composite.setLayout(gridLayout);
int buttonStyle = SWT.PUSH;
yearUp = new Button(composite, buttonStyle);
setNavigationColors(yearUp, bgColor, fgColor);
yearUp.setText("<");
yearUp.setLayoutData(createNavigationLayoutData());
yearUp.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
previousYear();
}
});
monthUp = new Button(composite, buttonStyle);
monthUp.setText("<<");
setNavigationColors(monthUp, bgColor, fgColor);
monthUp.setLayoutData(createNavigationLayoutData());
monthUp.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
previousMonth();
}
});
selectionLabel = new CLabel(composite, SWT.CENTER | SWT.SHADOW_OUT);
GridData gdNowLabel = new GridData(GridData.FILL_HORIZONTAL);
gdNowLabel.horizontalSpan = 4;
selectionLabel.setLayoutData(gdNowLabel);
selectionLabel.setText(FORMATTER.format(selectedDate));
setNavigationColors(selectionLabel, bgColor, fgColor);
monthNext = new Button(composite, buttonStyle);
monthNext.setText(">>");
setNavigationColors(monthNext, bgColor, fgColor);
monthNext.setLayoutData(createNavigationLayoutData());
monthNext.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
nextMonth();
}
});
yearNext = new Button(composite, buttonStyle);
yearNext.setText(">");
setNavigationColors(yearNext, bgColor, fgColor);
yearNext.setLayoutData(createNavigationLayoutData());
yearNext.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
nextYear();
}
});
}
private void setNavigationColors(final Control control,
final Color bgColor, final Color fgColor) {
control.setBackground(bgColor);
control.setForeground(fgColor);
}
private Object createNavigationLayoutData() {
GridData result = new GridData(GridData.FILL_HORIZONTAL);
result.heightHint = 20;
return result;
}
private void createWeekDays() {
Composite composite = new Composite(this, SWT.NONE);
int fillBoth = GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL;
GridData gridData = new GridData(fillBoth);
gridData.horizontalSpan = 8;
composite.setLayoutData(gridData);
GridLayout gridLayout = new GridLayout(8, true);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 1;
gridLayout.horizontalSpacing = 1;
composite.setLayout(gridLayout);
Color bgColor = COLOR_WEEK_DAYS_BG;
Color weekEndFgColor = COLOR_WEEK_END_FG;
monday = new CLabel(composite, SWT.CENTER | SWT.SHADOW_OUT);
monday.setText("Mon");
monday.setBackground(bgColor);
monday.setLayoutData(createWeekDaysLayoutData());
tuesday = new CLabel(composite, SWT.CENTER | SWT.SHADOW_OUT);
tuesday.setText("Tue");
tuesday.setBackground(bgColor);
tuesday.setLayoutData(createWeekDaysLayoutData());
wednesday = new CLabel(composite, SWT.CENTER | SWT.SHADOW_OUT);
wednesday.setText("Wed");
wednesday.setBackground(bgColor);
wednesday.setLayoutData(createWeekDaysLayoutData());
thursday = new CLabel(composite, SWT.CENTER | SWT.SHADOW_OUT);
thursday.setText("Thu");
thursday.setBackground(bgColor);
thursday.setLayoutData(createWeekDaysLayoutData());
friday = new CLabel(composite, SWT.CENTER | SWT.SHADOW_OUT);
friday.setText("Fri");
friday.setBackground(bgColor);
friday.setLayoutData(createWeekDaysLayoutData());
saturday = new CLabel(composite, SWT.CENTER | SWT.SHADOW_OUT);
saturday.setText("Sat");
saturday.setForeground(weekEndFgColor);
saturday.setBackground(bgColor);
saturday.setLayoutData(createWeekDaysLayoutData());
sunday = new CLabel(composite, SWT.CENTER | SWT.SHADOW_OUT);
sunday.setText("Sun");
sunday.setLayoutData(createWeekDaysLayoutData());
sunday.setForeground(weekEndFgColor);
sunday.setBackground(bgColor);
kw = new CLabel(composite, SWT.CENTER | SWT.SHADOW_OUT);
kw.setText("KW");
kw.setLayoutData(createWeekDaysLayoutData());
kw.setBackground(bgColor);
}
private Object createWeekDaysLayoutData() {
GridData result = new GridData(GridData.FILL_HORIZONTAL);
result.heightHint = 20;
return result;
}
}
--------------060902090807000201040406--
|
|
|
Re: RAP example, looking like a website [message #104430 is a reply to message #104395] |
Tue, 09 September 2008 03:11 |
Austin Riddle Messages: 8 Registered: July 2009 |
Junior Member |
|
|
Hello,
For the convenience of others, I thought that I would share my simple
DatePicker Composite that uses the previously posted calendar dialog.
Thanks to all who contributed to this.
---------------------------------------------------------
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import dialogs.DatePickerDialog;
import internal.Activator;
/**
* Composite that contains a label, text field, and button for date
management.
*
*/
public class DatePicker extends Composite {
protected String lblText;
protected Text dateText;
protected Button dateChooserBtn;
protected Image icon;
protected Date inputDate;
protected Date userDate;
protected DateFormat dateFormat;
protected boolean validate;
protected boolean editable;
public DatePicker(Composite parent, int style, String labelText) {
super(parent,style);
lblText = labelText;
icon = Activator.getDefault().getImage("images/calendar.png");
createControls();
setValidationEnabled(true);
}
public void setDate(Date inputDate, DateFormat dateFormat) {
Assert.isNotNull(inputDate);
Assert.isNotNull(dateFormat);
this.inputDate = inputDate;
userDate = inputDate;
this.dateFormat = dateFormat;
dateText.setText(dateFormat.format(inputDate));
}
public Date getDate () {
return userDate;
}
public String getFormattedDate () {
return dateFormat.format(userDate);
}
public void setValidationEnabled (boolean validate) {
this.validate = validate;
}
public boolean isValidationEnabled () {
return validate;
}
public void setEditable (boolean editable) {
dateText.setEditable(editable);
}
public boolean isEditable () {
return dateText.getEditable();
}
public void addModifyListener (ModifyListener listener) {
dateText.addModifyListener(listener);
}
public void removeModifyListener (ModifyListener listener) {
dateText.removeModifyListener(listener);
}
protected void createControls () {
this.setLayout(new GridLayout(3, false));
Label lbl = new Label(this,SWT.NONE);
lbl.setText(lblText);
dateText = new Text(this, SWT.BORDER);
if (inputDate != null) {
dateText.setText(dateFormat.format(inputDate));
}
//it would be nice to use control decorations here instead
dateText.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent event) {
if (validate) {
try {
userDate = dateFormat.parse(dateText.getText());
}
catch (ParseException e) {
String msg = null;
if (dateFormat instanceof SimpleDateFormat)
msg = "The accepted format is:
"+((SimpleDateFormat)dateFormat).toPattern();
else
msg = "Please change the date to the correct format.";
MessageDialog.openError(event.widget.getDisplay().getActiveS hell(),
"Incorrect Date Format", msg);
}
}
}
});
dateChooserBtn = new Button(this, SWT.PUSH);
dateChooserBtn.setImage(icon);
dateChooserBtn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DatePickerDialog dateChooser = new
DatePickerDialog(e.widget.getDisplay().getActiveShell());
dateChooser.setSelectedDate(userDate);
Point loc =
DatePicker.this.toDisplay(dateChooserBtn.getLocation());
loc.y += dateChooserBtn.getSize().y;
dateChooser.setLocation(loc);
Date selectedDate = dateChooser.open();
if (selectedDate != null) {
userDate = selectedDate;
dateText.setText(dateFormat.format(userDate));
}
}
});
}
}
|
|
|
RAP example, looking like a website [message #107740 is a reply to message #104430] |
Thu, 02 October 2008 13:57 |
Eclipse User |
|
|
|
Originally posted by: fappel.innoopract.com
Hi all,
in the meantime there's a DataTime widget available in RWT. This should
ease the task of implementing a datepicker dialog.
Ciao
Frank
-----Ursprüngliche Nachricht-----
Von: Austin Riddle [mailto:riddle.austin@gmail.com]
Bereitgestellt: Dienstag, 9. September 2008 05:12
Bereitgestellt in: eclipse.technology.rap
Unterhaltung: RAP example, looking like a website
Betreff: Re: RAP example, looking like a website
Hello,
For the convenience of others, I thought that I would share my simple
DatePicker Composite that uses the previously posted calendar dialog.
Thanks to all who contributed to this.
---------------------------------------------------------
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import dialogs.DatePickerDialog;
import internal.Activator;
/**
* Composite that contains a label, text field, and button for date
management.
*
*/
public class DatePicker extends Composite {
protected String lblText;
protected Text dateText;
protected Button dateChooserBtn;
protected Image icon;
protected Date inputDate;
protected Date userDate;
protected DateFormat dateFormat;
protected boolean validate;
protected boolean editable;
public DatePicker(Composite parent, int style, String labelText) {
super(parent,style);
lblText = labelText;
icon = Activator.getDefault().getImage("images/calendar.png");
createControls();
setValidationEnabled(true);
}
public void setDate(Date inputDate, DateFormat dateFormat) {
Assert.isNotNull(inputDate);
Assert.isNotNull(dateFormat);
this.inputDate = inputDate;
userDate = inputDate;
this.dateFormat = dateFormat;
dateText.setText(dateFormat.format(inputDate));
}
public Date getDate () {
return userDate;
}
public String getFormattedDate () {
return dateFormat.format(userDate);
}
public void setValidationEnabled (boolean validate) {
this.validate = validate;
}
public boolean isValidationEnabled () {
return validate;
}
public void setEditable (boolean editable) {
dateText.setEditable(editable);
}
public boolean isEditable () {
return dateText.getEditable();
}
public void addModifyListener (ModifyListener listener) {
dateText.addModifyListener(listener);
}
public void removeModifyListener (ModifyListener listener) {
dateText.removeModifyListener(listener);
}
protected void createControls () {
this.setLayout(new GridLayout(3, false));
Label lbl = new Label(this,SWT.NONE);
lbl.setText(lblText);
dateText = new Text(this, SWT.BORDER);
if (inputDate != null) {
dateText.setText(dateFormat.format(inputDate));
}
//it would be nice to use control decorations here instead
dateText.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent event) {
if (validate) {
try {
userDate = dateFormat.parse(dateText.getText());
}
catch (ParseException e) {
String msg = null;
if (dateFormat instanceof SimpleDateFormat)
msg = "The accepted format is:
"+((SimpleDateFormat)dateFormat).toPattern();
else
msg = "Please change the date to the correct format.";
MessageDialog.openError(event.widget.getDisplay().getActiveS hell(),
"Incorrect Date Format", msg);
}
}
}
});
dateChooserBtn = new Button(this, SWT.PUSH);
dateChooserBtn.setImage(icon);
dateChooserBtn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DatePickerDialog dateChooser = new
DatePickerDialog(e.widget.getDisplay().getActiveShell());
dateChooser.setSelectedDate(userDate);
Point loc =
DatePicker.this.toDisplay(dateChooserBtn.getLocation());
loc.y += dateChooserBtn.getSize().y;
dateChooser.setLocation(loc);
Date selectedDate = dateChooser.open();
if (selectedDate != null) {
userDate = selectedDate;
dateText.setText(dateFormat.format(userDate));
}
}
});
}
}
|
|
|
Goto Forum:
Current Time: Mon Dec 30 14:47:13 GMT 2024
Powered by FUDForum. Page generated in 0.07031 seconds
|