2010-01-12 2 views
3

SWT DateTime 구성 요소를 사용하고 있습니다. 현재 날짜가 기본 선택으로 설정됩니다. 이걸 어떻게 막을 수 있습니까?아니요 SWT DateTime의 기본 날짜

나는 ... 날짜가 전혀 선택되지 않도록

감사 패트릭

답변

1

수동으로 0 또는 적절한 어떤 null로 인스턴스의 필드를 설정해야 싶어요. 동일한 객체를 구현하기 위해 null 객체 패턴을 사용하는 NoDateTime 객체를 구현할 수도 있습니다. 나는 단지 null로 아무런 시간도 나타내지 않으려 고 유혹을 느낄 것이다. 왜 그렇게 할 수없는 이유가 있을까?

3

이것은 여전히 ​​누구에게나 유용합니다. 동일한 문제가 있습니다. 즉, UI의 필드에 날짜 또는 빈 값이 표시되어야합니다. 선택한 날짜가 유효한 입력이기 때문에 마찬가지입니다. SWT DateTime은 일종의 날짜를 표시해야하지만, 단순히 라벨과 버튼을 만들어 다른 레벨의 간접 참조를 도입해도 문제가되지 않습니다. DateTime처럼 보입니다. 그러면 버튼이 별도의 모달 창에서 DateTime을 호출합니다. 사용자가 선택을하면 응용 프로그램 창에서 레이블에 결과를 씁니다. 모달 창에 또 다른 버튼을 추가하고이를 호출합니다. 없음. 사용자가 없음을 클릭하면 응용 프로그램의 레이블 필드가 지워집니다. 모달 대화 상자에서 DateTime 컨트롤을 초기화 할 수 있도록 레이블에서 먼저 날짜의 현재 값을 긁어 낸 것을 볼 수 있습니다. 이 방법은 새로운 복합 컨트롤처럼 작동합니다. 비록 여러 번 반복해야한다면 다소 어색하다는 것을 인정합니다. 예 :

private Button buttonDeadlineDate; 
private Label labelDeadlineDate; 

// ... then define your "composite" control: 

lblNewLabel_5 = new Label(group_2, SWT.NONE); 
lblNewLabel_5.setBounds(10, 14, 50, 17); 
lblNewLabel_5.setText("Deadline:"); 

// We make our own composite date control out of a label and a button 
// and we call a modal dialog box with the SWT DateTime and 
// some buttons. 
labelDeadlineDate = new Label(group_2, SWT.BORDER | SWT.CENTER); 
labelDeadlineDate.setBounds(62, 10, 76, 20); 
// Note that I use the strange font DokChampa because this was the only way to get a margin at the top. 
labelDeadlineDate.setFont(SWTResourceManager.getFont("DokChampa", 8, SWT.NORMAL)); 
labelDeadlineDate.setBackground(SWTResourceManager.getColor(255, 255, 255)); // so it does appear editable 
buttonDeadlineDate = new Button (group_2, SWT.NONE); 
buttonDeadlineDate.setBounds(136, 11, 20, 20); // x - add 74, y - add 1 with respect to label 


// ... And later we have the call-back from the listener on the little button above: 

    //======================================== 
    // Deadline Date 
    //======================================== 

    buttonDeadlineDate.addSelectionListener(new SelectionAdapter() { 
    @Override 
    public void widgetSelected(SelectionEvent e) { 

     // Define the dialog shell. 
     // Note: DIALOG_TRIM = TITLE | CLOSE | BORDER (a typical application dialog shell) 
     final Shell dialog = new Shell (shlTaskScheduler, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 
       dialog.setText("Enter deadline date (NONE for none)"); 

     //======================================== 
     // Position and size the dialog (relative to the application). 
     // could have probably also used a single call to dialog.setBounds() 
     // instead of calling setLocation() and setSize(). 
     //======================================== 
     Point myPoint = new Point(0,0); 
     myPoint = shlTaskScheduler.getLocation(); 
     myPoint.x +=80; // myPoint.x +=30; 
     myPoint.y +=320; // myPoint.y +=350; 
     dialog.setLocation(myPoint); 
     dialog.setSize(270, 220); 

     dialog.setLayout (null); 

     //======================================== 
     // Define dialog contents 
     //======================================== 

     // Make controls final they it can be accessed from the listener. 

     final DateTime DTDeadlineDate; 
     DTDeadlineDate = new DateTime(dialog, SWT.BORDER | SWT.CALENDAR | SWT.DROP_DOWN); 
     DTDeadlineDate.setBounds(10, 10, 175, 175); 

     final Button buttonNone = new Button (dialog, SWT.PUSH); 
     buttonNone.setText ("NONE"); 
     buttonNone.setBounds(200, 35, 55, 25); 

     final Button buttonOK = new Button (dialog, SWT.PUSH); 
     buttonOK.setText ("OK"); 
     buttonOK.setBounds(200, 85, 55, 25); 

     //======================================== 
     // Initialize the DateTime control to 
     // the date displayed on the button or today's date. 
     //======================================== 

     // Get the deadline from the main application window 
     String newDeadlineDateString = (labelDeadlineDate.getText().toString()); 
     myLogger.i (className, "got deadline from main application window as " + newDeadlineDateString); 

     // If deadline date found, use it to initialize the DateTime control 
     // else the DateTime control will initialize itself to the current date automatically. 
     if ((newDeadlineDateString.length() == 10) // probably unnecessary test 
     && (isThisDateValid(newDeadlineDateString, "yyyy-MM-dd"))) { 

      // parse and extract components 
      try { 
       String tmpYearString= newDeadlineDateString.substring(0,4); 
       String tmpMoString = newDeadlineDateString.substring(5,7); 
       String tmpDayString = newDeadlineDateString.substring(8,10); 

       int tmpYearInt = Integer.parseInt(tmpYearString); 
       int tmpMoInt = Integer.parseInt(tmpMoString); 
       int tmpDayInt = Integer.parseInt(tmpDayString); 

       DTDeadlineDate.setYear(tmpYearInt); 
       DTDeadlineDate.setMonth(tmpMoInt - 1); // the control counts the months beginning with 0! - like the calendar 
       DTDeadlineDate.setDay(tmpDayInt); 

      } catch(NumberFormatException f) { 
       // this should not happen because we have a legal date 
       myScreenMessage.e(className, "Error extracting deadline date from screen <" + newDeadlineDateString + ">. Ignoring"); 
      } 
     } else if (newDeadlineDateString.length() > 0) { 
      myLogger.w (className, "Illegal current deadline date value or format <" + newDeadlineDateString + ">. Ignoring."); 
      // no need to do anything, as the control will initialize itself to the current date 
     } else { 
      // no need to do anything, as the control will initialize itself to the current date 
     } 

     //======================================== 
     // Set up the listener and assign it to the OK and None buttons. 
     // Note that the dialog has not been opened yet, but this seems OK. 
     // 
     // Note that we define a generic listener and then associate it with a control. 
     // Thus we need to check in the listener, which control we happen to be in. 
     // This is a valid way of doing it, as an alternative to using 
     //  addListener() or 
     //  addSelectionListener() 
     // for specific controls. 
     //======================================== 

     Listener listener = new Listener() { 
      public void handleEvent (Event event) { 

       if (event.widget == buttonOK) { 

        int newDeadlineDay = DTDeadlineDate.getDay(); 
        int newDeadlineMonth = DTDeadlineDate.getMonth() + 1; // the returned month will start at 0 
        int newDeadlineYear = DTDeadlineDate.getYear(); 

        String selectedDeadlineDate = String.format ("%04d-%02d-%02d", newDeadlineYear, newDeadlineMonth, newDeadlineDay); 
        if (isThisDateValid(selectedDeadlineDate, "yyyy-MM-dd")) { 
         labelDeadlineDate.setText(selectedDeadlineDate); 
        } else { 
         // This is strange as the widget should only return valid dates... 
         myScreenMessage.e(className, "Illegal deadline date selected: resetting to empty date"); 
         labelDeadlineDate.setText(""); 
        } 

       } else if (event.widget == buttonNone) { 
        // an empty date is also an important value 
        labelDeadlineDate.setText(""); 
       } else { 
        // this should not happen as there are no other buttons on the dialog 
        myLogger.e(className, "Unexpected widget state: ignoring"); 
       } 

       // once a button is pressed, we close the dialog  
       dialog.close(); 
      } 
     }; 

     // Still need to assign the listener to the buttons   
     buttonOK.addListener (SWT.Selection, listener); 
     buttonNone.addListener (SWT.Selection, listener); 

     //======================================== 
     // Display the date dialog. 
     //======================================== 
     dialog.open(); 

     //======================================== 
     // If you need to do this - you can wait for user selection before returning from this listener. 
     // Note that this wait is not necessary so that the above button listeners 
     // can capture events, but rather so that we do not continue execution and end this 
     // function call before the user has made a date selection clicked on a button. 
     // Otherwise we would just go on. 

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

     ... 

    } 
    });