当前位置: 首页 > 工具软件 > RCP100 > 使用案例 >

RCP日期控件

微生阳平
2023-12-01

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
 
 





 类似资料: