Hexise's Blog

业精于勤荒于嬉 行成于思毁于随
posts - 13, comments - 12, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

SWT中的日期选择控件

Posted on 2006-12-29 11:11 Hexise 阅读(9810) 评论(3)  编辑  收藏 所属分类: SWT/JFace

类似于Java Swing 中的JDateChooser,SWT里有没有相类似的日期选择控件呢?

目前有几种方式提供SWT的时间控件:

1.eclipse 3.3自带的org.eclipse.swt.widgets.DateTime控件.
   eclipse 3.3版本增加了对日期选择控件的支持,下面是官方提供的示例代码:

import  org.eclipse.swt. * ;
import  org.eclipse.swt.events. * ;
import  org.eclipse.swt.layout. * ;
import  org.eclipse.swt.widgets. * ;

public   class  Snippet251  {

    
public   static   void  main(String[] args)  {
        Display display 
=   new  Display();
        
final  Shell shell  =   new  Shell(display);
        shell.setLayout(
new  FillLayout());

        Button open 
=   new  Button(shell, SWT.PUSH);
        open.setText(
" Open Dialog " );
        open.addSelectionListener(
new  SelectionAdapter()  {
            
public   void  widgetSelected(SelectionEvent e)  {
                
final  Shell dialog  =   new  Shell(shell, SWT.DIALOG_TRIM);
                dialog.setLayout(
new  GridLayout( 3 false ));

                
final  DateTime calendar  =   new  DateTime(dialog, SWT.CALENDAR
                        
|  SWT.BORDER);
                
final  DateTime date  =   new  DateTime(dialog, SWT.DATE  |  SWT.SHORT);
                
final  DateTime time  =   new  DateTime(dialog, SWT.TIME  |  SWT.SHORT);

                
new  Label(dialog, SWT.NONE);
                
new  Label(dialog, SWT.NONE);
                Button ok 
=   new  Button(dialog, SWT.PUSH);
                ok.setText(
" OK " );
                ok.setLayoutData(
new  GridData(SWT.FILL, SWT.CENTER,  false ,
                        
false ));
                ok.addSelectionListener(
new  SelectionAdapter()  {
                    
public   void  widgetSelected(SelectionEvent e)  {
                        System.out
                                .println(
" Calendar date selected (MM/DD/YYYY) =  "
                                        
+  (calendar.getMonth()  +   1 )
                                        
+   " / "
                                        
+  calendar.getDay()
                                        
+   " / "
                                        
+  calendar.getYear());
                        System.out.println(
" Date selected (MM/YYYY) =  "
                                
+  (date.getMonth()  +   1 +   " / "   +  date.getYear());
                        System.out.println(
" Time selected (HH:MM) =  "
                                
+  time.getHours()  +   " : "   +  time.getMinutes());
                        dialog.close();
                    }

                }
);
                dialog.setDefaultButton(ok);
                dialog.pack();
                dialog.open();
            }

        }
);
        shell.pack();
        shell.open();

        
while  ( ! shell.isDisposed())  {
            
if  ( ! display.readAndDispatch())
                display.sleep();
        }

        display.dispose();
    }

}

2.第三方提供的免费日期选择控件,例如DatePicker, 它是一个下拉列表框, 提供了日期选择的功能.它在sourceforge上的主页是 http://sourceforge.net/projects/swt-datepicker/

3.也可以自己实现日期选择控件,下面是一个实现的例子:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class CalendarDialog extends Dialog implements MouseListener {

    
private Display display = null;

    
private Date nowDate = null// current date

    
private String selectedDate = ""// selected date

    
private Shell shell = null;

    
private GridLayout gridLayout = null;

    
private GridData gridData = 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 nowLabel = null;

    
private CLabel[] days = new CLabel[42];

    
private boolean hasChanged = false;

    
public CalendarDialog(Shell parent, int style) {
        
super(parent, style);
    }


    
public CalendarDialog(Shell parent) {
        
this(parent, 0);
    }


    
private int getLastDayOfMonth(int year, int month) {
        
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                
|| month == 10 || month == 12{
            
return 31;
        }

        
if (month == 4 || month == 6 || month == 9 || month == 11{
            
return 30;
        }

        
if (month == 2{
            
if (isLeapYear(year)) {
                
return 29;
            }
 else {
                
return 28;
            }

        }

        
return 0;
    }


    
public boolean isLeapYear(int year) {
        
return (year % 4 == 0 && year % 100 != 0|| (year % 400 == 0);
    }


    
private void moveTo(int type, int value) {
        Calendar now 
= Calendar.getInstance(); // get current Calendar object
        now.setTime(nowDate); // set current date
        now.add(type, value); // add to spec time.
        nowDate = new Date(now.getTimeInMillis()); // result
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");// format
        
// date
        nowLabel.setText(formatter.format(nowDate)); // set to label
        setDayForDisplay(now);
    }


    
private void setDayForDisplay(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].setText(
"" + startday);
                
if (startday == currentDay) {
                    days[i].setBackground(display
                            .getSystemColor(SWT.COLOR_BLUE)); 
//
                }

                startday
++;
            }
 else {
                days[i].setText(
"");
            }

        }


    }


    
public void previousYear() {
        moveTo(Calendar.YEAR, 
-1);
    }


    
public void nextYear() {
        moveTo(Calendar.YEAR, 
1);
    }


    
public void nextMonth() {
        moveTo(Calendar.MONTH, 
1);
    }


    
public void previousMonth() {
        moveTo(Calendar.MONTH, 
-1);
    }


    
public void mouseDoubleClick(MouseEvent e) {
    }


    
public void mouseDown(MouseEvent e) {

        CLabel day 
= (CLabel) e.getSource();

        
if (!day.getText().equals("")) {
            
this.selectedDate = nowLabel.getText() + "-" + day.getText();
            
this.shell.close();
        }

        hasChanged 
= true;
    }


    
public void mouseUp(MouseEvent e) {
    }


    
public void open(int x, int y) {

        Shell parent 
= getParent();
        display 
= Display.getDefault();
        shell 
= new Shell(parent);
        shell.setBounds(x, y, 
230220);

        hasChanged 
= false;

        gridLayout 
= new GridLayout();
        gridLayout.numColumns 
= 7;
        shell.setLayout(gridLayout);

        gridData 
= new GridData(GridData.FILL_HORIZONTAL);
        yearUp 
= new Button(shell, SWT.PUSH | SWT.FLAT);
        yearUp.setText(
"<");
        yearUp.setLayoutData(gridData);
        yearUp.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                previousYear();
            }

        }
);

        gridData 
= new GridData(GridData.FILL_HORIZONTAL);
        monthUp 
= new Button(shell, SWT.PUSH | SWT.FLAT);
        monthUp.setText(
"<<");
        monthUp.setLayoutData(gridData);
        monthUp.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                previousMonth();
            }

        }
);

        nowLabel 
= new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData 
= new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan 
= 3;
        nowLabel.setLayoutData(gridData);
        SimpleDateFormat formatter 
= new SimpleDateFormat("yyyy-MM");
        nowLabel.setText(formatter.format(
new Date()));

        gridData 
= new GridData(GridData.FILL_HORIZONTAL);
        monthNext 
= new Button(shell, SWT.PUSH | SWT.FLAT);
        monthNext.setText(
">>");
        monthNext.setLayoutData(gridData);
        monthNext.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                nextMonth();
            }

        }
);

        gridData 
= new GridData(GridData.FILL_HORIZONTAL);
        yearNext 
= new Button(shell, SWT.PUSH | SWT.FLAT);
        yearNext.setText(
">");
        yearNext.setLayoutData(gridData);
        yearNext.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                nextYear();
            }

        }
);

        sunday 
= new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData 
= new GridData(GridData.FILL_HORIZONTAL
                
| GridData.FILL_VERTICAL);
        gridData.widthHint 
= 20;
        gridData.heightHint 
= 20;
        sunday.setLayoutData(gridData);
        sunday.setText(
"Sun");

        monday 
= new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData 
= new GridData(GridData.FILL_HORIZONTAL
                
| GridData.FILL_VERTICAL);
        gridData.widthHint 
= 20;
        gridData.heightHint 
= 20;
        monday.setLayoutData(gridData);
        monday.setText(
"Mon");

        tuesday 
= new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData 
= new GridData(GridData.FILL_HORIZONTAL
                
| GridData.FILL_VERTICAL);
        gridData.widthHint 
= 20;
        gridData.heightHint 
= 20;
        tuesday.setLayoutData(gridData);
        tuesday.setText(
"Tue");

        wednesday 
= new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData 
= new GridData(GridData.FILL_HORIZONTAL
                
| GridData.FILL_VERTICAL);
        gridData.widthHint 
= 20;
        gridData.heightHint 
= 20;
        wednesday.setLayoutData(gridData);
        wednesday.setText(
"Wed");

        thursday 
= new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData 
= new GridData(GridData.FILL_HORIZONTAL
                
| GridData.FILL_VERTICAL);
        gridData.widthHint 
= 20;
        gridData.heightHint 
= 20;
        thursday.setLayoutData(gridData);
        thursday.setText(
"Thu");

        friday 
= new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData 
= new GridData(GridData.FILL_HORIZONTAL
                
| GridData.FILL_VERTICAL);
        gridData.widthHint 
= 20;
        gridData.heightHint 
= 20;
        friday.setLayoutData(gridData);
        friday.setText(
"Fri");

        saturday 
= new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData 
= new GridData(GridData.FILL_HORIZONTAL
                
| GridData.FILL_VERTICAL);
        gridData.widthHint 
= 20;
        gridData.heightHint 
= 20;
        saturday.setLayoutData(gridData);
        saturday.setText(
"Sat");

        
for (int i = 0; i < 42; i++{
            days[i] 
= new CLabel(shell, SWT.FLAT | SWT.CENTER);
            gridData 
= new GridData(GridData.FILL_HORIZONTAL
                    
| GridData.FILL_VERTICAL);
            days[i].setLayoutData(gridData);
            days[i].setBackground(display.getSystemColor(SWT.COLOR_WHITE));
            days[i].addMouseListener(
this);
        }


        Calendar now 
= Calendar.getInstance(); //
        nowDate = new Date(now.getTimeInMillis());
        setDayForDisplay(now);

        shell.open();
        Display display 
= parent.getDisplay();
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch()) {
                display.sleep();
            }

        }

    }


    
public boolean isChanged() {
        
return hasChanged;
    }


    
public String getDateText() {
        
return selectedDate.toString();
    }


}

评论

# re: SWT中的时间控件  回复  更多评论   

2006-12-29 11:23 by 交口称赞

拆台的啊
看看偶写的
http://www.blogjava.net/vip01/archive/2006/12/27/90385.html

# re: SWT中的时间控件  回复  更多评论   

2006-12-29 12:12 by Hexise
@交口称赞
呵呵,巧合巧合

# re: SWT中的日期选择控件  回复  更多评论   

2007-07-12 12:08 by wanglin
这个是我在你的基础上改的
public class CalendarDialog implements MouseListener
{
private SimpleDateFormat stdDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
private SimpleDateFormat stdDate = new SimpleDateFormat("yyyy-MM-dd");
private String[][] week =
{
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
{ "日", "一", "二", "三", "四", "五", "六" } };
private String[] hours =
{ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23" };
private String[] mins =
{ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36",
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
"55", "56", "57", "58", "59" };
private static Map widgetMap = new HashMap();
private Display display = null;

private Locale locale = Locale.CHINESE;

private Date nowDate = null; // current date

private Calendar now = null;

private Text selectedDate = null; // selected date

private boolean selectTime = false;

private Button hasTime;
private Text time;
private Shell shell = null;

private GridLayout gridLayout = null;

private GridData gridData = 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 nowLabel = null;

private CLabel[] days = new CLabel[35];

private boolean hasChanged = false;
public CalendarDialog(Locale locale,boolean hasTime)
{
this.locale = locale;
this.selectTime = hasTime;
}

public CalendarDialog(Locale locale)
{
this(locale,false);
}
public CalendarDialog(boolean hasTime)
{
this(Locale.CHINESE,hasTime);
}

public CalendarDialog()
{
this(Locale.CHINESE,false);
}

private int getLastDayOfMonth(int year, int month)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
return 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11)
{
return 30;
}
if (month == 2)
{
if (isLeapYear(year))
{
return 29;
} else
{
return 28;
}
}
return 0;
}

public boolean isLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

private void moveTo(int type, int value)
{
Calendar now = Calendar.getInstance(); // get current Calendar object
now.setTime(nowDate); // set current date
now.add(type, value); // add to spec time.
nowDate = new Date(now.getTimeInMillis()); // result
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");// format
// date
nowLabel.setText(formatter.format(nowDate)); // set to label
setDayForDisplay(now);
}

private void setDayForDisplay(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 hourInt = now.get(Calendar.HOUR_OF_DAY);
int minInt = now.get(Calendar.MINUTE);
int lastDay = this.getLastDayOfMonth(year, month); //
int endIndex = startIndex + lastDay - 1; //
int startday = 1;
for (int i = 0; i < 35; 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 < 35; i++)
{
if (i >= startIndex && i <= endIndex)
{
days[i].setText("" + startday);
if (startday == currentDay)
{
days[i].setBackground(display.getSystemColor(SWT.COLOR_BLUE)); //
}
startday++;
} else
{
days[i].setText("");
}
}
if(StringUtil.isStdDateTime(selectedDate.getText())){
hasTime.setSelection(true);
time.setText(hours[hourInt] + ":" + mins[minInt]);
}else{
hasTime.setSelection(false);
int hi = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int mi = Calendar.getInstance().get(Calendar.MINUTE);
time.setText(hours[hi] + ":" + mins[mi]);
}
}

public void previousYear()
{
moveTo(Calendar.YEAR, -1);
}

public void nextYear()
{
moveTo(Calendar.YEAR, 1);
}

public void nextMonth()
{
moveTo(Calendar.MONTH, 1);
}

public void previousMonth()
{
moveTo(Calendar.MONTH, -1);
}

public void mouseDoubleClick(MouseEvent e)
{
}

public void mouseDown(MouseEvent e)
{
if (widgetMap.get(e.getSource()) != null)
{
return;
}
widgetMap.put(e.getSource(), e.getSource());
selectedDate = (Text) e.getSource();
open(getX(), getY());

hasChanged = true;
}

private int getX()
{
int x = 0;
x += selectedDate.getBounds().x;
Scrollable par;
par = selectedDate.getParent();
while (par instanceof Scrollable && par != null)
{
x += par.getBounds().x;
par = par.getParent();
}
return x;

}

private int getY()
{
int y = 50;
y += selectedDate.getBounds().y;
y += selectedDate.getBounds().height;
Scrollable par;
par = selectedDate.getParent();
while (par instanceof Scrollable && par != null)
{
y += par.getBounds().y;
par = par.getParent();
}

return y;

}

public void mouseUp(MouseEvent e)
{
}

public void dayMouseDown(MouseEvent e)
{

CLabel day = (CLabel) e.getSource();

if (!day.getText().equals("") && !day.getText().equals("×"))
{
if (selectedDate == null)
{
widgetMap.remove(selectedDate);
this.shell.close();
}
this.selectedDate.setText(nowLabel.getText() + "-" + day.getText());
}
if (hasTime.getSelection())
{
isValidaTime("");
if (!day.getText().equals("") && !day.getText().equals("×"))
{
if (selectedDate == null)
{
widgetMap.remove(selectedDate);
this.shell.close();
}
this.selectedDate.setText(nowLabel.getText() + "-" + day.getText() + " " + time.getText());
}
}

widgetMap.remove(selectedDate);
this.shell.close();
hasChanged = true;
}

public void open(int x, int y)
{

display = Display.getDefault();
display.setWarnings(true);

shell = new Shell(display, SWT.ON_TOP);

hasChanged = false;

gridLayout = new GridLayout();
gridLayout.numColumns = 7;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
shell.setBounds(x, y, 210, 220);

gridData = new GridData(GridData.FILL_HORIZONTAL);
yearUp = new Button(shell, SWT.PUSH | SWT.FLAT);
yearUp.setText("<");
yearUp.setLayoutData(gridData);
yearUp.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
previousYear();
}
});

gridData = new GridData(GridData.FILL_HORIZONTAL);
monthUp = new Button(shell, SWT.PUSH | SWT.FLAT);
monthUp.setText("<<");
monthUp.setLayoutData(gridData);
monthUp.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
previousMonth();
}
});

nowLabel = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
nowLabel.setLayoutData(gridData);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
nowLabel.setText(formatter.format(new Date()));

gridData = new GridData(GridData.FILL_HORIZONTAL);
monthNext = new Button(shell, SWT.PUSH | SWT.FLAT);
monthNext.setText(">>");
monthNext.setLayoutData(gridData);
monthNext.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
nextMonth();
}
});

gridData = new GridData(GridData.FILL_HORIZONTAL);
yearNext = new Button(shell, SWT.PUSH | SWT.FLAT);
yearNext.setText(">");
yearNext.setLayoutData(gridData);
yearNext.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
nextYear();
}
});

sunday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
sunday.setLayoutData(gridData);
sunday.setText(getWeekName(0));

monday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
monday.setLayoutData(gridData);
monday.setText(getWeekName(1));

tuesday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
tuesday.setLayoutData(gridData);
tuesday.setText(getWeekName(2));

wednesday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
wednesday.setLayoutData(gridData);
wednesday.setText(getWeekName(3));

thursday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
thursday.setLayoutData(gridData);
thursday.setText(getWeekName(4));

friday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
friday.setLayoutData(gridData);
friday.setText(getWeekName(5));

saturday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
saturday.setLayoutData(gridData);
saturday.setText(getWeekName(6));

for (int i = 0; i < 35; i++)
{
days[i] = new CLabel(shell, SWT.FLAT | SWT.CENTER);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
days[i].setLayoutData(gridData);
days[i].addMouseListener(new MouseAdapter()
{
public void mouseDown(MouseEvent e)
{
dayMouseDown(e);
}
});
}

hasTime = new Button(shell, SWT.CHECK);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
gridData.horizontalAlignment = GridData.CENTER;
hasTime.setLayoutData(gridData);
hasTime.setSelection(selectTime);

Composite timeCom = new Composite(shell, SWT.NONE);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
gridData.horizontalSpan = 5;
timeCom.setLayoutData(gridData);
{
GridLayout timeLayout = new GridLayout();
timeLayout.numColumns = 5;
gridLayout.makeColumnsEqualWidth = true;
timeCom.setLayout(timeLayout);

Composite lCom = new Composite(timeCom, SWT.NONE);
timeLayout = new GridLayout();
timeLayout.numColumns = 1;
lCom.setLayout(timeLayout);
{
CLabel hourUp = new CLabel(lCom, SWT.NONE);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
hourUp.setLayoutData(gridData);
hourUp.setText("∧");
hourUp.addMouseListener(new MouseAdapter()
{
public void mouseDown(MouseEvent e)
{
nextHour();
}
});

CLabel hourDown = new CLabel(lCom, SWT.CENTER | SWT.EMBEDDED);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
hourDown.setLayoutData(gridData);
hourDown.setText("∨");
hourDown.addMouseListener(new MouseAdapter()
{
public void mouseDown(MouseEvent e)
{
preHour();
}
});
}

{
time = new Text(timeCom, SWT.SINGLE | SWT.CENTER | SWT.BORDER);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
time.setLayoutData(gridData);
new ControlCheck().setTextTimeCheck1(time);
}

Composite rCom = new Composite(timeCom, SWT.NONE);
timeLayout = new GridLayout();
timeLayout.numColumns = 1;
rCom.setLayout(timeLayout);
{
CLabel minUp = new CLabel(rCom, SWT.FLAT | SWT.CENTER);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
minUp.setLayoutData(gridData);
minUp.setText("∧");
minUp.addMouseListener(new MouseAdapter()
{
public void mouseDown(MouseEvent e)
{
nextMinute();
}
});

CLabel minDown = new CLabel(rCom, SWT.FLAT | SWT.CENTER);
GridData gridData = new GridData();
gridData.horizontalSpan = 3;
minDown.setLayoutData(gridData);
minDown.setText("∨");
minDown.addMouseListener(new MouseAdapter()
{
public void mouseDown(MouseEvent e)
{
preMinute();
}
});
}
}

CLabel cls = new CLabel(shell, SWT.FLAT | SWT.CENTER);
GridData gridData = new GridData();
cls.setLayoutData(gridData);
cls.setText("×");
cls.addMouseListener(new MouseAdapter()
{
public void mouseDown(MouseEvent e)
{
dayMouseDown(e);
}
});

now = getCalendar();
nowDate = new Date(now.getTimeInMillis());
setDayForDisplay(now);

shell.open();
Display display = shell.getDisplay();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
}

public boolean isChanged()
{
return hasChanged;
}

public String getDateText()
{
return selectedDate.toString();
}

private String getWeekName(int weekIndex)
{
if (locale.equals(Locale.CHINESE))
{
return week[1][weekIndex];
} else
{
return week[0][weekIndex];
}
}

private boolean isValidaTime(String time)
{
return true;
}

private void nextHour()
{
int textHour = getHourInText();
int textMinute = getMinuteInText();
if (textHour < 23)
{
time.setText(hours[textHour + 1] + ":" + mins[textMinute]);
} else
{
time.setText(hours[0]+ ":" + mins[textMinute]);
}
}

private void preHour()
{
int textHour = getHourInText();
int textMinute = getMinuteInText();
if (textHour > 0)
{
time.setText(hours[textHour - 1] + ":" + mins[textMinute]);
} else
{
time.setText(hours[23] + ":" + mins[textMinute]);
}

}

private void nextMinute()
{
int textHour = getHourInText();
int textMinute = getMinuteInText();
if (textMinute < 59)
{
time.setText(hours[textHour] + ":" + mins[textMinute + 1]);
} else
{
time.setText(hours[textHour] + ":" + mins[0]);
}

}

private void preMinute()
{
int textHour = getHourInText();
int textMinute = getMinuteInText();
if (textMinute > 0)
{
time.setText(hours[textHour] + ":" + mins[textMinute - 1]);
} else
{
time.setText(hours[textHour] + ":" + mins[59]);
}

}

private int getHourInText()
{
return new Integer(time.getText().split(":")[0]).intValue();
}

private int getMinuteInText()
{
return new Integer(time.getText().split(":")[1]).intValue();
}

private Calendar getCalendar()
{
Date d;
String timeStr = this.selectedDate.getText();
if (timeStr == null || timeStr.equals(""))
{
return Calendar.getInstance();
}
now = Calendar.getInstance();
if (StringUtil.isStdDateTime(timeStr))
{
try
{
d = this.stdDateTime.parse(timeStr);
now.setTime(d);
} catch (ParseException e)
{
e.printStackTrace();
}
}
if (StringUtil.isStdDate(timeStr))
{
try
{
d = this.stdDate.parse(timeStr);
now.setTime(d);
} catch (ParseException e)
{
e.printStackTrace();
}
}
return now;
}

}

只有注册用户登录后才能发表评论。


网站导航: