RCP日期控件

1、版面調用

text_17 = new Text(composite_2, SWT.BORDER );

FormData fd_text_17 = new FormData();
fd_text_17.top = new FormAttachment(combo_4,0,SWT.TOP);
fd_text_17.bottom = new FormAttachment(combo_4,0,SWT.BOTTOM);
fd_text_17.left = new FormAttachment(label_23, 2);
fd_text_17.right = new FormAttachment(label_23, 146,SWT.RIGHT);
text_17.setLayoutData(fd_text_17);
//text_17.setEnabled(false);
text_17.addMouseListener(new MouseAdapter(){


@Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseUp(e);
OpenDateToText.getDateToText(e, shell);
}

});


2、公共代碼

public class OpenDateToText {

public static void getDateToText(MouseEvent e,Shell shell){
Text bt = (Text) e.widget;
Point er = bt.getLocation();
DateDialog dl = new DateDialog(shell);
int x = er.x+20;
int y = er.y+177; //定位
dl.open(x,y,bt.getText());
bt.setText(dl.selectedDate);
}

 }


3、日期控件代碼

public class DateDialog extends Dialog implements MouseListener {


private static String ID = "com.ss.sfm.util.DateDialog";

   private Display display = null;


   private Date nowDate = null; // current date


   public String selectedDate = ""; // selected date


   public Shell shell = null;
   
   public Shell parent = 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 DateDialog(Shell parent, int style) {
       super(parent, style);
   }


   public DateDialog(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,String sDate){
    int currentDay =now.get(Calendar.DATE);
   
    //System.out.println("currentDay="+currentDay);
   
    if(!DealStrNull.judgeStrNull(sDate)){
    if(sDate.length()>10){
   
    }else if(sDate.length()==10){
    currentDay = Integer.parseInt(sDate.substring(8,sDate.length()));
    }else if(sDate.length()==9){
    currentDay = Integer.parseInt(sDate.substring(8,sDate.length()));
    }
    }
   // System.out.println("currentDay==="+currentDay);
       
       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("")) {
        String str = day.getText().length()==2?day.getText():"0"+day.getText();
           this.selectedDate = nowLabel.getText() + "-"+str;
          // System.out.println("日期====="+this.selectedDate);
           this.shell.close();
       }
       hasChanged = true;
   }


   public void mouseUp(MouseEvent e) {
   }


   



   public void open(int x, int y,String date) {


       parent = getParent();
       display = Display.getDefault();
       shell = new Shell(parent,SWT.TITLE|SWT.RESIZE);
       shell.setBounds(x, y, 230, 220);
       
       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("<");
       gridData.widthHint = 30;
       gridData.heightHint = 24;
       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("<<");
       gridData.widthHint = 30;
       gridData.heightHint = 24;
       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;
       gridData.widthHint = 90;
       gridData.heightHint = 24;
       nowLabel.setLayoutData(gridData);


       gridData = new GridData(GridData.FILL_HORIZONTAL);
       monthNext = new Button(shell, SWT.PUSH | SWT.FLAT);
       monthNext.setText(">>");
       gridData.widthHint = 30;
       gridData.heightHint = 24;
       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(">");
       gridData.widthHint = 32;
       gridData.heightHint = 24;
       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 = 24;
       sunday.setLayoutData(gridData);
       sunday.setText("Su");


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


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


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


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


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


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


       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);
       }


       
       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
       if(DealStrNull.judgeStrNull(date)){
        nowLabel.setText(formatter.format(new Date()));
       
        Calendar now = Calendar.getInstance(); //
         nowDate = new Date(now.getTimeInMillis());
         setDayForDisplay(now,"");
       }else{
        nowLabel.setText(formatter.format(java.sql.Date.valueOf(date)));
       
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
       
        Date date2 = null;
try {
date2 = sdf.parse(date);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date2);
       
        nowDate = new Date(calendar.getTimeInMillis());
     setDayForDisplay(calendar,"");
       }
           shell.open();
           disposed();
   }
   
   
   public void disposed(){
   
       Display display = parent.getDisplay();
       while (!shell.isDisposed()) {
           if (!display.readAndDispatch()){
               display.sleep();
           }
       }
   }


   public boolean isChanged() {
       return hasChanged;
   }


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




public static void  main(String[] args)  {
DateDialog dl = new DateDialog(new Shell());
dl.open(80, 90,"2016-10-19");
System.out.println("finish==="+dl.selectedDate); 
}
    }


// WT.BORDER //建立一個有邊框但沒有標題欄的窗口 
// SWT.CLOSE //建立一個只有關閉按鈕的窗口 
// SWT.MIN  //建立一個不能最大化的窗口 
// SWT.MAX, //建立一個可以最大化最小化的窗口 
// SWT.NO_TRIM //建立一個沒有任何邊界和標題欄的窗口 
// SWT.RESIZE //建立一個可以改變大小的窗口 
// SWT.TITLE //建立一個沒有標題欄圖標,沒有關閉按鈕的窗口 
// SWT.ON_TOP //建立一個總是在上的窗口,注意:此屬性最好與CLOSE、MIN、MAX一起使用。 
// SWT.TOOL  //建立一個類似工具欄的窗口 
// SWT.APPLICATION_MODAL //建立一個APPLICATION模態窗口 
// SWT.MODELESS //建立一個非模態窗口 
// SWT.PRIMARY_MODAL //建立一個PRIMARY模態窗口 
// SWT.SYSTEM_MODAL  //建立一個SYSTEM模態窗口      還有兩個快捷屬性來建立窗口 
// SHELL_TRIM //建立一個標準模式的窗口,相當於屬性設置爲CLOSE | TITLE | MIN | MAX | RESIZE 
// DIALOG_TRIM //建立一個對話框模式的窗口,相當於屬性設置爲TITLE | CLOSE | BORDER
 
 





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章