Guest User

Untitled

a guest
May 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. private Button buttonDeadlineDate;
  2. private Label labelDeadlineDate;
  3.  
  4. // ... then define your "composite" control:
  5.  
  6. lblNewLabel_5 = new Label(group_2, SWT.NONE);
  7. lblNewLabel_5.setBounds(10, 14, 50, 17);
  8. lblNewLabel_5.setText("Deadline:");
  9.  
  10. // We make our own composite date control out of a label and a button
  11. // and we call a modal dialog box with the SWT DateTime and
  12. // some buttons.
  13. labelDeadlineDate = new Label(group_2, SWT.BORDER | SWT.CENTER);
  14. labelDeadlineDate.setBounds(62, 10, 76, 20);
  15. // Note that I use the strange font DokChampa because this was the only way to get a margin at the top.
  16. labelDeadlineDate.setFont(SWTResourceManager.getFont("DokChampa", 8, SWT.NORMAL));
  17. labelDeadlineDate.setBackground(SWTResourceManager.getColor(255, 255, 255)); // so it does appear editable
  18. buttonDeadlineDate = new Button (group_2, SWT.NONE);
  19. buttonDeadlineDate.setBounds(136, 11, 20, 20); // x - add 74, y - add 1 with respect to label
  20.  
  21.  
  22. // ... And later we have the call-back from the listener on the little button above:
  23.  
  24. //========================================
  25. // Deadline Date
  26. //========================================
  27.  
  28. buttonDeadlineDate.addSelectionListener(new SelectionAdapter() {
  29. @Override
  30. public void widgetSelected(SelectionEvent e) {
  31.  
  32. // Define the dialog shell.
  33. // Note: DIALOG_TRIM = TITLE | CLOSE | BORDER (a typical application dialog shell)
  34. final Shell dialog = new Shell (shlTaskScheduler, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
  35. dialog.setText("Enter deadline date (NONE for none)");
  36.  
  37. //========================================
  38. // Position and size the dialog (relative to the application).
  39. // could have probably also used a single call to dialog.setBounds()
  40. // instead of calling setLocation() and setSize().
  41. //========================================
  42. Point myPoint = new Point(0,0);
  43. myPoint = shlTaskScheduler.getLocation();
  44. myPoint.x +=80; // myPoint.x +=30;
  45. myPoint.y +=320; // myPoint.y +=350;
  46. dialog.setLocation(myPoint);
  47. dialog.setSize(270, 220);
  48.  
  49. dialog.setLayout (null);
  50.  
  51. //========================================
  52. // Define dialog contents
  53. //========================================
  54.  
  55. // Make controls final they it can be accessed from the listener.
  56.  
  57. final DateTime DTDeadlineDate;
  58. DTDeadlineDate = new DateTime(dialog, SWT.BORDER | SWT.CALENDAR | SWT.DROP_DOWN);
  59. DTDeadlineDate.setBounds(10, 10, 175, 175);
  60.  
  61. final Button buttonNone = new Button (dialog, SWT.PUSH);
  62. buttonNone.setText ("NONE");
  63. buttonNone.setBounds(200, 35, 55, 25);
  64.  
  65. final Button buttonOK = new Button (dialog, SWT.PUSH);
  66. buttonOK.setText ("OK");
  67. buttonOK.setBounds(200, 85, 55, 25);
  68.  
  69. //========================================
  70. // Initialize the DateTime control to
  71. // the date displayed on the button or today's date.
  72. //========================================
  73.  
  74. // Get the deadline from the main application window
  75. String newDeadlineDateString = (labelDeadlineDate.getText().toString());
  76. myLogger.i (className, "got deadline from main application window as " + newDeadlineDateString);
  77.  
  78. // If deadline date found, use it to initialize the DateTime control
  79. // else the DateTime control will initialize itself to the current date automatically.
  80. if ((newDeadlineDateString.length() == 10) // probably unnecessary test
  81. && (isThisDateValid(newDeadlineDateString, "yyyy-MM-dd"))) {
  82.  
  83. // parse and extract components
  84. try {
  85. String tmpYearString= newDeadlineDateString.substring(0,4);
  86. String tmpMoString = newDeadlineDateString.substring(5,7);
  87. String tmpDayString = newDeadlineDateString.substring(8,10);
  88.  
  89. int tmpYearInt = Integer.parseInt(tmpYearString);
  90. int tmpMoInt = Integer.parseInt(tmpMoString);
  91. int tmpDayInt = Integer.parseInt(tmpDayString);
  92.  
  93. DTDeadlineDate.setYear(tmpYearInt);
  94. DTDeadlineDate.setMonth(tmpMoInt - 1); // the control counts the months beginning with 0! - like the calendar
  95. DTDeadlineDate.setDay(tmpDayInt);
  96.  
  97. } catch(NumberFormatException f) {
  98. // this should not happen because we have a legal date
  99. myScreenMessage.e(className, "Error extracting deadline date from screen <" + newDeadlineDateString + ">. Ignoring");
  100. }
  101. } else if (newDeadlineDateString.length() > 0) {
  102. myLogger.w (className, "Illegal current deadline date value or format <" + newDeadlineDateString + ">. Ignoring.");
  103. // no need to do anything, as the control will initialize itself to the current date
  104. } else {
  105. // no need to do anything, as the control will initialize itself to the current date
  106. }
  107.  
  108. //========================================
  109. // Set up the listener and assign it to the OK and None buttons.
  110. // Note that the dialog has not been opened yet, but this seems OK.
  111. //
  112. // Note that we define a generic listener and then associate it with a control.
  113. // Thus we need to check in the listener, which control we happen to be in.
  114. // This is a valid way of doing it, as an alternative to using
  115. // addListener() or
  116. // addSelectionListener()
  117. // for specific controls.
  118. //========================================
  119.  
  120. Listener listener = new Listener () {
  121. public void handleEvent (Event event) {
  122.  
  123. if (event.widget == buttonOK) {
  124.  
  125. int newDeadlineDay = DTDeadlineDate.getDay();
  126. int newDeadlineMonth = DTDeadlineDate.getMonth() + 1; // the returned month will start at 0
  127. int newDeadlineYear = DTDeadlineDate.getYear();
  128.  
  129. String selectedDeadlineDate = String.format ("%04d-%02d-%02d", newDeadlineYear, newDeadlineMonth, newDeadlineDay);
  130. if (isThisDateValid(selectedDeadlineDate, "yyyy-MM-dd")) {
  131. labelDeadlineDate.setText(selectedDeadlineDate);
  132. } else {
  133. // This is strange as the widget should only return valid dates...
  134. myScreenMessage.e(className, "Illegal deadline date selected: resetting to empty date");
  135. labelDeadlineDate.setText("");
  136. }
  137.  
  138. } else if (event.widget == buttonNone) {
  139. // an empty date is also an important value
  140. labelDeadlineDate.setText("");
  141. } else {
  142. // this should not happen as there are no other buttons on the dialog
  143. myLogger.e(className, "Unexpected widget state: ignoring");
  144. }
  145.  
  146. // once a button is pressed, we close the dialog
  147. dialog.close ();
  148. }
  149. };
  150.  
  151. // Still need to assign the listener to the buttons
  152. buttonOK.addListener (SWT.Selection, listener);
  153. buttonNone.addListener (SWT.Selection, listener);
  154.  
  155. //========================================
  156. // Display the date dialog.
  157. //========================================
  158. dialog.open ();
  159.  
  160. //========================================
  161. // If you need to do this - you can wait for user selection before returning from this listener.
  162. // Note that this wait is not necessary so that the above button listeners
  163. // can capture events, but rather so that we do not continue execution and end this
  164. // function call before the user has made a date selection clicked on a button.
  165. // Otherwise we would just go on.
  166.  
  167. while (!dialog.isDisposed()) {
  168. if (!display.readAndDispatch()) {
  169. display.sleep();
  170. }
  171. }
  172.  
  173. ...
  174.  
  175. }
  176. });
  177.  
  178. private boolean isModified = false;
  179.  
  180. selectDate = new DateTime(this, SWT.DATE | SWT.DROP_DOWN);
  181.  
  182. SelectionListener selListener = new SelectionAdapter() {
  183. @Override
  184. public void widgetSelected(SelectionEvent e) {
  185. isModified = true;
  186. }
  187. };
  188.  
  189. selectDate.addSelectionListener(selListener);
Add Comment
Please, Sign In to add comment