RCP Date Control

1. Layout call

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 code

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. Date control code

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 //Create a window with a border but no title bar 
//SWT.CLOSE //Create a window with only a close button 
//SWT.MIN / /Create a window that cannot be maximized 
//SWT.MAX, //Create a window that can be maximized and minimized 
//SWT.NO_TRIM //Create a window without any border and title bar 
//SWT.RESIZE //Create a window that can be resized 
//SWT.TITLE //Create a window without a title bar icon and no close button 
//SWT.ON_TOP //Create a window that is always on, Note: This property is best used with CLOSE, MIN, MAX. 
//SWT.TOOL //Create a toolbar-like window 
//SWT.APPLICATION_MODAL //Create an APPLICATION modal window 
//SWT.MODELESS //Create a non-modal window 
//SWT.PRIMARY_MODAL //Create a PRIMARY Modal window 
//SWT.SYSTEM_MODAL //Create a SYSTEM modal window and two shortcut properties to create a window 
//SHELL_TRIM //Create a standard mode window, which is equivalent to setting the property to CLOSE | TITLE | MIN | MAX | RESIZE 
//DIALOG_TRIM //Create a window in dialog mode, which is equivalent to setting the property to TITLE | CLOSE | BORDER
 
 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327081248&siteId=291194637