Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » popup-calendar
popup-calendar [message #454664] Tue, 26 April 2005 21:07 Go to next message
Michael Permana is currently offline Michael PermanaFriend
Messages: 23
Registered: July 2009
Junior Member
Hi,

I'm trying to write a calendar that popups when you click a button. It sort
of working. But now I want to hide my popup calendar when the user click on
other places. I put my calendar in a composite. I tried addFocusListener on
the top composite, but it's not working.
Can anyone help me?


import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormatSymbols;
import java.util.Date;
import java.util.GregorianCalendar;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

/**
* This composite displays either a "Button" or the "Calendar". To save
space it
* display only the button. When the button is clicked, it hides the
* compositeButton and show the compositeCalendar.
*
* @author mpermana@hotmail.com
*/
public class DatePicker extends Composite {

Composite compositeButton;

Composite compositeCalendar;

GregorianCalendar calendar = new GregorianCalendar();

PropertyChangeListener listener;

static Color black = new Color(null, 0, 0, 0);

static Color gray = new Color(null, 127, 127, 127);

static Color selectionBackground = new Color(null, 200, 200, 200);

static Image buttonImage =
ImageDescriptor.createFromFile(DatePicker.class,
"picker.ico").createImage();

DateFormatSymbols dfs;

Button buttonShow;

Combo comboMonth;

Combo comboYear;

Label[][] labels = new Label[7][7];

boolean showCalendar = false;

/**
*
*/
public DatePicker(Composite parent) {
super(parent, SWT.NONE);
FormLayout formLayout = new FormLayout();
setLayout(formLayout);
/*
addFocusListener(new FocusListener() {

public void focusGained(FocusEvent e) {
}

public void focusLost(FocusEvent e) {
setShown(false);
System.out.println("focusLost");

}});
*/
compositeButton = new Composite(this, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
compositeButton.setLayout(gridLayout);
compositeButton.setVisible(true);

compositeCalendar = new Composite(this, SWT.BORDER);
gridLayout = new GridLayout();
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
gridLayout.numColumns = 7;
compositeCalendar.setLayout(gridLayout);
compositeCalendar.setVisible(false);

dfs = new DateFormatSymbols();

buttonShow = new Button(compositeButton, SWT.ARROW);
buttonShow.setToolTipText("tooltip.calendar");
buttonShow.setImage(buttonImage);
buttonShow.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent e) {
setShown(!showCalendar);
}

public void widgetDefaultSelected(SelectionEvent e) {
}
});

GridData gridData = new GridData();
gridData.horizontalSpan = 7;
buttonShow.setLayoutData(gridData);

SelectionListener selectionListener = new SelectionListener() {

public void widgetSelected(SelectionEvent e) {
int month = comboMonth.getSelectionIndex();
calendar.set(GregorianCalendar.MONTH, month);
while (calendar.get(GregorianCalendar.MONTH) != month)
calendar.add(GregorianCalendar.DATE, -1);
int year = calendar.get(GregorianCalendar.YEAR);
try {
year = Integer.parseInt(comboYear.getText());
} catch (Exception e2) {
}
calendar.set(GregorianCalendar.YEAR, year);
while (calendar.get(GregorianCalendar.MONTH) != month)
calendar.add(GregorianCalendar.DATE, -1);
updateDates();
}

public void widgetDefaultSelected(SelectionEvent e) {

}
};

comboMonth = new Combo(compositeCalendar, SWT.READ_ONLY);
comboMonth.setItems(dfs.getMonths());
comboMonth.remove(12);
comboMonth.addSelectionListener(selectionListener);

gridData = new GridData();
gridData.horizontalSpan = 5;
comboMonth.setLayoutData(gridData);

comboYear = new Combo(compositeCalendar, SWT.READ_ONLY);
String[] years = new String[25];
int year = calendar.get(GregorianCalendar.YEAR);
for (int i = 0; i < years.length; i++)
years[i] = String.valueOf(year + i);
comboYear.setItems(years);
comboYear.addSelectionListener(selectionListener);

gridData = new GridData();
gridData.horizontalSpan = 2;
comboYear.setLayoutData(gridData);

for (int week = 0; week < 7; week++) {
for (int i = 0; i < 7; i++) {
final Label fLabel = new Label(compositeCalendar, SWT.NONE);
gridData = new GridData();
gridData.widthHint = 23;
fLabel.setLayoutData(gridData);
labels[week][i] = fLabel;
if (week > 0) {

labels[week][i].addMouseListener(new MouseListener() {

public void mouseDoubleClick(MouseEvent e) {
}

public void mouseDown(MouseEvent e) {
setSelection(fLabel);
setShown(false);
}

public void mouseUp(MouseEvent e) {
}
});
}
}
}

// create week day label
for (int i = 0; i < 7; i++) {
labels[0][i]
.setText(dfs.getShortWeekdays()[1 + (new
GregorianCalendar()
.getFirstDayOfWeek()
+ i - 1) % 7]);
}

updateDates();
}

/**
* @param b
*/
protected void setShown(boolean shown) {
this.showCalendar = shown;
compositeButton.setVisible(!shown);
compositeCalendar.setVisible(shown);
setSize(computeSize(-1, -1));
}

public void updateDates() {
GregorianCalendar firstDay = (GregorianCalendar) calendar.clone();
firstDay.set(GregorianCalendar.DATE, 1);

int month = calendar.get(GregorianCalendar.MONTH);
int year = calendar.get(GregorianCalendar.YEAR);

GregorianCalendar calendarDay = (GregorianCalendar)
firstDay.clone();
calendarDay.add(GregorianCalendar.DAY_OF_MONTH, -firstDay
.get(GregorianCalendar.DAY_OF_WEEK) + 1);
comboMonth.select(month);
comboYear.setText(String.valueOf(year));
int week = 1;
int endMonth = (month + 1) % 12;
while (true) {
for (int i = 0; i < 7; i++) {
int iterateDay = calendarDay
.get(GregorianCalendar.DAY_OF_MONTH);
int iterateMonth = calendarDay.get(GregorianCalendar.MONTH);
labels[week][i].setForeground((month == iterateMonth) ?
black
: gray);
labels[week][i]
.setBackground(calendarDay.equals(calendar) ?
selectionBackground
: getBackground());
if (calendarDay.equals(calendar))
this.selection = labels[week][i];
labels[week][i].setText(String.valueOf(iterateDay));
labels[week][i].setData(calendarDay.getTime());
calendarDay.add(GregorianCalendar.DAY_OF_MONTH, 1);
}
if (endMonth == calendarDay.get(GregorianCalendar.MONTH))
break;
week++;
}
for (int i = 0; i < 7; i++) {
labels[6][i].setVisible(5 < week);
}
}

Label selection;

public void setSelection(Label selection) {
this.selection.setBackground(getBackground());
this.selection = selection;
selection.setBackground(selectionBackground);
calendar.setTime((Date) selection.getData());
if (null != listener) {
PropertyChangeEvent event = new PropertyChangeEvent(this,
"calendar",
null, calendar);
listener.propertyChange(event);
}
}

/**
*
*/
public Point computeSize(int wHint, int hHint, boolean changed) {
if (showCalendar)
return compositeCalendar.computeSize(wHint, hHint, changed);
else
return compositeButton.computeSize(wHint, hHint, changed);
}

/**
* @param listener
* The listener to set.
*/
public void setListener(PropertyChangeListener listener) {
this.listener = listener;
}

/**
* @return Returns the calendar.
*/
public GregorianCalendar getCalendar() {
return calendar;
}

/**
* unit test code
*/
public static void main(String[] args) {
Locale locale = Locale.US;
locale = Locale.CHINESE;
//locale = Locale.FRENCH; // lundi=monday,
Locale.setDefault(locale);

final Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;

shell.setLayout(gridLayout);

new Label(shell, SWT.BORDER).setText("date");
new Label(shell, SWT.BORDER).setText("date");
new Label(shell, SWT.BORDER).setText("date");

new Label(shell, SWT.BORDER).setText("date");
new DatePicker(shell);
new Label(shell, SWT.BORDER).setText("date");

new Label(shell, SWT.BORDER).setText("date");
new Label(shell, SWT.BORDER).setText("date");
new Label(shell, SWT.BORDER).setText("date");

new Label(shell, SWT.BORDER).setText("date");
new DatePicker(shell);
new Label(shell, SWT.BORDER).setText("date");

shell.setSize(640, 480);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();

}

}
Re: popup-calendar [message #454666 is a reply to message #454664] Tue, 26 April 2005 21:28 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: micasim.gmx.de

On Tue, 26 Apr 2005 23:07:10 +0200, Michael Permana <mpermana@hotmail.com>
wrote:

> Hi,
>
> I'm trying to write a calendar that popups when you click a button. It
> sort
> of working. But now I want to hide my popup calendar when the user click
> on
> other places. I put my calendar in a composite. I tried addFocusListener
> on
> the top composite, but it's not working.
> Can anyone help me?

Hi Michael,

you might take a look at my effort on the same subject at
http://micasim.de/warehouse/mgswtsrc.zip

Cheers,
Michael
Re: popup-calendar [message #454703 is a reply to message #454664] Wed, 27 April 2005 16:19 Go to previous messageGo to next message
Max Rotvel is currently offline Max RotvelFriend
Messages: 21
Registered: July 2009
Junior Member
On Tue, 26 Apr 2005 23:07:10 +0200, Michael Permana <mpermana@hotmail.com>
wrote:

> I'm trying to write a calendar that popups when you click a button. It
> sort
> of working. But now I want to hide my popup calendar when the user click
> on
> other places. I put my calendar in a composite. I tried addFocusListener
> on
> the top composite, but it's not working.

What happens? I think you should be able to use a mechanism similar to
this:

shell.addFocusListener(new FocusAdapter()
{
public void focusLost(FocusEvent e)
{
display.asyncExec(new Runnable()
{
public void run()
{
shell.dispose();
}
});
}
});

Regards
--
Max - rotvel AT bolignet-aarhus DOT dk
Re: popup-calendar [message #454707 is a reply to message #454703] Wed, 27 April 2005 18:10 Go to previous messageGo to next message
Michael Permana is currently offline Michael PermanaFriend
Messages: 23
Registered: July 2009
Junior Member
FocusListener doesn't work as intended because I added the listener on my
top composite.
Mr. Simons' solution worked for me, it displays my composite in a Shell.
Using Shell is generic enough, that it can be used to "popup" anyting.
So here's my final code, thanks everyone, it works nicely.

public class PopupCalendar {

public PopupCalendar(Shell parent,Point location,GregorianCalendar
calendar,PropertyChangeListener listener) {
final Shell shell = new Shell(parent,SWT.NO_TRIM);
shell.setLayout(new GridLayout());
CalendarPicker picker = new CalendarPicker(shell, SWT.BORDER);
picker.setCalendar(calendar);
picker.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
shell.close();
}
});
picker.addPropertyChangeListener(listener);
shell.pack();
shell.setLocation(location);
shell.addShellListener(new ShellAdapter() {
public void shellDeactivated(ShellEvent e) {
shell.close();
}
});
shell.open();
}

}

public class CalendarPicker extends Composite {

PropertyChangeSupport propertyChangeSupport = new
PropertyChangeSupport(this);

GregorianCalendar calendar = new GregorianCalendar();

DateFormatSymbols dfs;

Combo comboMonth;

Combo comboYear;

Label[][] labels = new Label[7][7];

Color foreground;

Color grayForeground;

Color sundayForeground;

Color selectionBackground;

Color selectionForeground;

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);

SelectionListener selectionListener;

/**
* @param parent
* @param style
*/
public CalendarPicker(Composite parent, int style) {

super(parent, style);
selectionBackground = parent.getShell().getDisplay().getSystemColor(
SWT.COLOR_LIST_SELECTION);
selectionForeground = parent.getShell().getDisplay().getSystemColor(
SWT.COLOR_LIST_SELECTION_TEXT);
grayForeground = parent.getShell().getDisplay().getSystemColor(
SWT.COLOR_GRAY);
sundayForeground = parent.getShell().getDisplay().getSystemColor(
SWT.COLOR_RED);

setBackground(parent.getShell().getDisplay().getSystemColor(
SWT.COLOR_LIST_BACKGROUND));
setForeground(parent.getShell().getDisplay().getSystemColor(
SWT.COLOR_LIST_FOREGROUND));

GridLayout gridLayout = new GridLayout();
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
gridLayout.numColumns = 7;
setLayout(gridLayout);

dfs = new DateFormatSymbols();

selectionListener = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int month = comboMonth.getSelectionIndex();
calendar.set(GregorianCalendar.MONTH, month);
while (calendar.get(GregorianCalendar.MONTH) != month)
calendar.add(GregorianCalendar.DATE, -1);
int year = calendar.get(GregorianCalendar.YEAR);
try {
year = Integer.parseInt(comboYear.getText());
} catch (Exception e2) {
}
calendar.set(GregorianCalendar.YEAR, year);
while (calendar.get(GregorianCalendar.MONTH) != month)
calendar.add(GregorianCalendar.DATE, -1);
updateDates();
}
};

Button buttonPrevious = new Button(this, SWT.ARROW | SWT.LEFT);
buttonPrevious.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
comboMonth.select((comboMonth.getSelectionIndex()+11)%12);
if (11 == comboMonth.getSelectionIndex() && 0 <
comboYear.getSelectionIndex()) {
comboYear.select(comboYear.getSelectionIndex()-1);
}
selectionListener.widgetSelected(e);
}
});

comboMonth = new Combo(this, SWT.READ_ONLY);
comboMonth.setItems(dfs.getMonths());
comboMonth.remove(12);
comboMonth.addSelectionListener(selectionListener);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 3;
comboMonth.setLayoutData(gridData);

comboYear = new Combo(this, SWT.READ_ONLY);
String[] years = new String[25];
int year = calendar.get(GregorianCalendar.YEAR);
for (int i = 0; i < years.length; i++)
years[i] = String.valueOf(year + i);
comboYear.setItems(years);
comboYear.addSelectionListener(selectionListener);
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
comboYear.setLayoutData(gridData);

Button buttonNext = new Button(this, SWT.ARROW | SWT.RIGHT);
buttonNext.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
comboMonth.select((comboMonth.getSelectionIndex()+1)%12);
if (0 == comboMonth.getSelectionIndex()) {

comboYear.setText(String.valueOf(1+Integer.parseInt(comboYea r.getText())));
}
selectionListener.widgetSelected(e);
}
});

for (int week = 0; week < 7; week++) {
for (int i = 0; i < 7; i++) {
final Label fLabel = new Label(CalendarPicker.this,
SWT.NONE);
fLabel.setBackground(getBackground());
gridData = new GridData(GridData.FILL_BOTH);
fLabel.setLayoutData(gridData);
labels[week][i] = fLabel;
if (week > 0) {
labels[week][i].addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
setSelection(fLabel);
}
});
}
}
}

// create week day label Sunday Monday Tuesday ...
for (int i = 0; i < 7; i++) {
labels[0][i].setText(dfs.getShortWeekdays()[1 + i]);
}

updateDates();
}

public void updateDates() {
GregorianCalendar firstDay = (GregorianCalendar) calendar.clone();
firstDay.set(GregorianCalendar.DATE, 1);

int month = calendar.get(GregorianCalendar.MONTH);
int year = calendar.get(GregorianCalendar.YEAR);

GregorianCalendar calendarDay = (GregorianCalendar)
firstDay.clone();
calendarDay.add(GregorianCalendar.DAY_OF_MONTH, -firstDay
.get(GregorianCalendar.DAY_OF_WEEK) + 1);
comboMonth.select(month);
comboYear.setText(String.valueOf(year));
int week = 1;
int endMonth = (month + 1) % 12;
while (true) {
for (int i = 0; i < 7; i++) {
int iterateDay = calendarDay
.get(GregorianCalendar.DAY_OF_MONTH);
int iterateMonth = calendarDay.get(GregorianCalendar.MONTH);

// set colors
if (calendarDay.equals(calendar)) {
this.selection = labels[week][i];
labels[week][i].setBackground(selectionBackground);
labels[week][i].setForeground(selectionForeground);
} else {
Color dayForeground = (iterateMonth == month) ?
foreground
: grayForeground;
if (calendarDay.get(GregorianCalendar.DAY_OF_WEEK) ==
GregorianCalendar.SUNDAY)
dayForeground = sundayForeground;
labels[week][i].setForeground(dayForeground);
labels[week][i].setBackground(getBackground());
}
// set tool tip
labels[week][i]
.setToolTipText(df.format(calendarDay.getTime()));
// set day number 1..31
labels[week][i].setText(String.valueOf(iterateDay));
labels[week][i].setData(calendarDay.getTime());
calendarDay.add(GregorianCalendar.DAY_OF_MONTH, 1);
}
if (endMonth == calendarDay.get(GregorianCalendar.MONTH))
break;
week++;
}
for (int i = 0; i < 7; i++) {
labels[6][i].setVisible(5 < week);
}
}

Label selection;

public void setSelection(Label selection) {
this.selection.setBackground(getBackground());
this.selection.setForeground(foreground);
this.selection = selection;
selection.setBackground(selectionBackground);
selection.setForeground(selectionForeground);
calendar.setTime((Date) selection.getData());
propertyChangeSupport.firePropertyChange("calendar",null,calendar);
}

public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}

public GregorianCalendar getCalendar() {
return calendar;
}

public void setCalendar(GregorianCalendar calendar) {
this.calendar = calendar;
updateDates();
}

}


"Max Rotvel" <rotvel@bolignet-aarhus.dk> wrote in message
news:op.spwflzy094ko3h@trixie.katlan...
> On Tue, 26 Apr 2005 23:07:10 +0200, Michael Permana <mpermana@hotmail.com>
> wrote:
>
> > I'm trying to write a calendar that popups when you click a button. It
> > sort
> > of working. But now I want to hide my popup calendar when the user click
> > on
> > other places. I put my calendar in a composite. I tried addFocusListener
> > on
> > the top composite, but it's not working.
>
> What happens? I think you should be able to use a mechanism similar to
> this:
>
> shell.addFocusListener(new FocusAdapter()
> {
> public void focusLost(FocusEvent e)
> {
> display.asyncExec(new Runnable()
> {
> public void run()
> {
> shell.dispose();
> }
> });
> }
> });
>
> Regards
> --
> Max - rotvel AT bolignet-aarhus DOT dk
Re: popup-calendar [message #463505 is a reply to message #454707] Tue, 08 November 2005 14:47 Go to previous message
J. Michael Dean, M.D. is currently offline J. Michael Dean, M.D.Friend
Messages: 218
Registered: July 2009
Senior Member
Glad to see your code as I searched for same problem solution. But I must
be dense - can you provide a description of how to implement your code? I
can create a demonstration of the CalendarPicker itself but I do not
understand how you implement the popup behavior and retrieve the date value
from the popup. Thanks in advance.

- Mike Dean


On 4/27/05 11:10 AM, in article d4okpq$n9j$1@news.eclipse.org, "Michael
Permana" <mpermana@hotmail.com> wrote:

> FocusListener doesn't work as intended because I added the listener on my
> top composite.
> Mr. Simons' solution worked for me, it displays my composite in a Shell.
> Using Shell is generic enough, that it can be used to "popup" anyting.
> So here's my final code, thanks everyone, it works nicely.
>
> public class PopupCalendar {
>
> public PopupCalendar(Shell parent,Point location,GregorianCalendar
> calendar,PropertyChangeListener listener) {
> final Shell shell = new Shell(parent,SWT.NO_TRIM);
> shell.setLayout(new GridLayout());
> CalendarPicker picker = new CalendarPicker(shell, SWT.BORDER);
> picker.setCalendar(calendar);
> picker.addPropertyChangeListener(new PropertyChangeListener() {
> public void propertyChange(PropertyChangeEvent evt) {
> shell.close();
> }
> });
> picker.addPropertyChangeListener(listener);
> shell.pack();
> shell.setLocation(location);
> shell.addShellListener(new ShellAdapter() {
> public void shellDeactivated(ShellEvent e) {
> shell.close();
> }
> });
> shell.open();
> }
>
> }
>
> public class CalendarPicker extends Composite {
>
> PropertyChangeSupport propertyChangeSupport = new
> PropertyChangeSupport(this);
>
> GregorianCalendar calendar = new GregorianCalendar();
>
> DateFormatSymbols dfs;
>
> Combo comboMonth;
>
> Combo comboYear;
>
> Label[][] labels = new Label[7][7];
>
> Color foreground;
>
> Color grayForeground;
>
> Color sundayForeground;
>
> Color selectionBackground;
>
> Color selectionForeground;
>
> DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
>
> SelectionListener selectionListener;
>
> /**
> * @param parent
> * @param style
> */
> public CalendarPicker(Composite parent, int style) {
>
> super(parent, style);
> selectionBackground = parent.getShell().getDisplay().getSystemColor(
> SWT.COLOR_LIST_SELECTION);
> selectionForeground = parent.getShell().getDisplay().getSystemColor(
> SWT.COLOR_LIST_SELECTION_TEXT);
> grayForeground = parent.getShell().getDisplay().getSystemColor(
> SWT.COLOR_GRAY);
> sundayForeground = parent.getShell().getDisplay().getSystemColor(
> SWT.COLOR_RED);
>
> setBackground(parent.getShell().getDisplay().getSystemColor(
> SWT.COLOR_LIST_BACKGROUND));
> setForeground(parent.getShell().getDisplay().getSystemColor(
> SWT.COLOR_LIST_FOREGROUND));
>
> GridLayout gridLayout = new GridLayout();
> gridLayout.marginHeight = 0;
> gridLayout.marginWidth = 0;
> gridLayout.numColumns = 7;
> setLayout(gridLayout);
>
> dfs = new DateFormatSymbols();
>
> selectionListener = new SelectionAdapter() {
> public void widgetSelected(SelectionEvent e) {
> int month = comboMonth.getSelectionIndex();
> calendar.set(GregorianCalendar.MONTH, month);
> while (calendar.get(GregorianCalendar.MONTH) != month)
> calendar.add(GregorianCalendar.DATE, -1);
> int year = calendar.get(GregorianCalendar.YEAR);
> try {
> year = Integer.parseInt(comboYear.getText());
> } catch (Exception e2) {
> }
> calendar.set(GregorianCalendar.YEAR, year);
> while (calendar.get(GregorianCalendar.MONTH) != month)
> calendar.add(GregorianCalendar.DATE, -1);
> updateDates();
> }
> };
>
> Button buttonPrevious = new Button(this, SWT.ARROW | SWT.LEFT);
> buttonPrevious.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent e) {
> comboMonth.select((comboMonth.getSelectionIndex()+11)%12);
> if (11 == comboMonth.getSelectionIndex() && 0 <
> comboYear.getSelectionIndex()) {
> comboYear.select(comboYear.getSelectionIndex()-1);
> }
> selectionListener.widgetSelected(e);
> }
> });
>
> comboMonth = new Combo(this, SWT.READ_ONLY);
> comboMonth.setItems(dfs.getMonths());
> comboMonth.remove(12);
> comboMonth.addSelectionListener(selectionListener);
> GridData gridData = new GridData(GridData.FILL_BOTH);
> gridData.horizontalSpan = 3;
> comboMonth.setLayoutData(gridData);
>
> comboYear = new Combo(this, SWT.READ_ONLY);
> String[] years = new String[25];
> int year = calendar.get(GregorianCalendar.YEAR);
> for (int i = 0; i < years.length; i++)
> years[i] = String.valueOf(year + i);
> comboYear.setItems(years);
> comboYear.addSelectionListener(selectionListener);
> gridData = new GridData(GridData.FILL_BOTH);
> gridData.horizontalSpan = 2;
> comboYear.setLayoutData(gridData);
>
> Button buttonNext = new Button(this, SWT.ARROW | SWT.RIGHT);
> buttonNext.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent e) {
> comboMonth.select((comboMonth.getSelectionIndex()+1)%12);
> if (0 == comboMonth.getSelectionIndex()) {
>
> comboYear.setText(String.valueOf(1+Integer.parseInt(comboYea r.getText())));
> }
> selectionListener.widgetSelected(e);
> }
> });
>
> for (int week = 0; week < 7; week++) {
> for (int i = 0; i < 7; i++) {
> final Label fLabel = new Label(CalendarPicker.this,
> SWT.NONE);
> fLabel.setBackground(getBackground());
> gridData = new GridData(GridData.FILL_BOTH);
> fLabel.setLayoutData(gridData);
> labels[week][i] = fLabel;
> if (week > 0) {
> labels[week][i].addMouseListener(new MouseAdapter() {
> public void mouseDown(MouseEvent e) {
> setSelection(fLabel);
> }
> });
> }
> }
> }
>
> // create week day label Sunday Monday Tuesday ...
> for (int i = 0; i < 7; i++) {
> labels[0][i].setText(dfs.getShortWeekdays()[1 + i]);
> }
>
> updateDates();
> }
>
> public void updateDates() {
> GregorianCalendar firstDay = (GregorianCalendar) calendar.clone();
> firstDay.set(GregorianCalendar.DATE, 1);
>
> int month = calendar.get(GregorianCalendar.MONTH);
> int year = calendar.get(GregorianCalendar.YEAR);
>
> GregorianCalendar calendarDay = (GregorianCalendar)
> firstDay.clone();
> calendarDay.add(GregorianCalendar.DAY_OF_MONTH, -firstDay
> .get(GregorianCalendar.DAY_OF_WEEK) + 1);
> comboMonth.select(month);
> comboYear.setText(String.valueOf(year));
> int week = 1;
> int endMonth = (month + 1) % 12;
> while (true) {
> for (int i = 0; i < 7; i++) {
> int iterateDay = calendarDay
> .get(GregorianCalendar.DAY_OF_MONTH);
> int iterateMonth = calendarDay.get(GregorianCalendar.MONTH);
>
> // set colors
> if (calendarDay.equals(calendar)) {
> this.selection = labels[week][i];
> labels[week][i].setBackground(selectionBackground);
> labels[week][i].setForeground(selectionForeground);
> } else {
> Color dayForeground = (iterateMonth == month) ?
> foreground
> : grayForeground;
> if (calendarDay.get(GregorianCalendar.DAY_OF_WEEK) ==
> GregorianCalendar.SUNDAY)
> dayForeground = sundayForeground;
> labels[week][i].setForeground(dayForeground);
> labels[week][i].setBackground(getBackground());
> }
> // set tool tip
> labels[week][i]
> .setToolTipText(df.format(calendarDay.getTime()));
> // set day number 1..31
> labels[week][i].setText(String.valueOf(iterateDay));
> labels[week][i].setData(calendarDay.getTime());
> calendarDay.add(GregorianCalendar.DAY_OF_MONTH, 1);
> }
> if (endMonth == calendarDay.get(GregorianCalendar.MONTH))
> break;
> week++;
> }
> for (int i = 0; i < 7; i++) {
> labels[6][i].setVisible(5 < week);
> }
> }
>
> Label selection;
>
> public void setSelection(Label selection) {
> this.selection.setBackground(getBackground());
> this.selection.setForeground(foreground);
> this.selection = selection;
> selection.setBackground(selectionBackground);
> selection.setForeground(selectionForeground);
> calendar.setTime((Date) selection.getData());
> propertyChangeSupport.firePropertyChange("calendar",null,calendar);
> }
>
> public void addPropertyChangeListener(PropertyChangeListener listener) {
> propertyChangeSupport.addPropertyChangeListener(listener);
> }
>
> public GregorianCalendar getCalendar() {
> return calendar;
> }
>
> public void setCalendar(GregorianCalendar calendar) {
> this.calendar = calendar;
> updateDates();
> }
>
> }
>
>
> "Max Rotvel" <rotvel@bolignet-aarhus.dk> wrote in message
> news:op.spwflzy094ko3h@trixie.katlan...
>> On Tue, 26 Apr 2005 23:07:10 +0200, Michael Permana <mpermana@hotmail.com>
>> wrote:
>>
>>> I'm trying to write a calendar that popups when you click a button. It
>>> sort
>>> of working. But now I want to hide my popup calendar when the user click
>>> on
>>> other places. I put my calendar in a composite. I tried addFocusListener
>>> on
>>> the top composite, but it's not working.
>>
>> What happens? I think you should be able to use a mechanism similar to
>> this:
>>
>> shell.addFocusListener(new FocusAdapter()
>> {
>> public void focusLost(FocusEvent e)
>> {
>> display.asyncExec(new Runnable()
>> {
>> public void run()
>> {
>> shell.dispose();
>> }
>> });
>> }
>> });
>>
>> Regards
>> --
>> Max - rotvel AT bolignet-aarhus DOT dk
>
>
Previous Topic:FileTransfer drag& drop and special characters?
Next Topic:get DOM node without javascript?
Goto Forum:
  


Current Time: Thu Apr 25 15:44:03 GMT 2024

Powered by FUDForum. Page generated in 0.04035 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top