comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet033CrossValidationControlDecoration.d @ 99:5d5bd660917f

build some databind snippets
author Frank Benoit <benoit@tionex.de>
date Wed, 22 Apr 2009 18:59:26 +0200
parents 6086085e153d
children e884642ad36e
comparison
equal deleted inserted replaced
98:48d4ee626868 99:5d5bd660917f
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * Matthew Hall - initial API and implementation (bug 268472) 9 * Matthew Hall - initial API and implementation (bug 268472)
10 ******************************************************************************/ 10 ******************************************************************************/
11 11
12 package org.eclipse.jface.examples.databinding.snippets; 12 module org.eclipse.jface.examples.databinding.snippets.Snippet033CrossValidationControlDecoration;
13
14 import java.lang.all;
13 15
14 import java.util.Date; 16 import java.util.Date;
15 17
16 import org.eclipse.core.databinding.observable.Observables; 18 import org.eclipse.core.databinding.observable.Observables;
17 import org.eclipse.core.databinding.observable.Realm; 19 import org.eclipse.core.databinding.observable.Realm;
36 /** 38 /**
37 * @since 3.2 39 * @since 3.2
38 * 40 *
39 */ 41 */
40 public class Snippet033CrossValidationControlDecoration { 42 public class Snippet033CrossValidationControlDecoration {
41 protected Shell shell; 43 protected Shell shell;
42 private DateTime startDate; 44 private DateTime startDate;
43 private DateTime endDate; 45 private DateTime endDate;
44 46
45 /** 47 /**
46 * Launch the application 48 * Launch the application
47 * 49 *
48 * @param args 50 * @param args
49 */ 51 */
50 public static void main(String[] args) { 52 public static void main(String[] args) {
51 try { 53 try {
52 Snippet033CrossValidationControlDecoration window = new Snippet033CrossValidationControlDecoration(); 54 Snippet033CrossValidationControlDecoration window = new Snippet033CrossValidationControlDecoration();
53 window.open(); 55 window.open();
54 } catch (Exception e) { 56 } catch (Exception e) {
55 e.printStackTrace(); 57 e.printStackTrace();
56 } 58 }
57 } 59 }
58 60
59 /** 61 /**
60 * Open the window 62 * Open the window
61 */ 63 */
62 public void open() { 64 public void open() {
63 final Display display = Display.getDefault(); 65 final Display display = Display.getDefault();
64 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 66 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
65 public void run() { 67 public void run() {
66 createContents(); 68 createContents();
67 shell.pack(); 69 shell.pack();
68 shell.open(); 70 shell.open();
69 while (!shell.isDisposed()) { 71 while (!shell.isDisposed()) {
70 if (!display.readAndDispatch()) 72 if (!display.readAndDispatch())
71 display.sleep(); 73 display.sleep();
72 } 74 }
73 } 75 }
74 }); 76 });
75 } 77 }
76 78
77 protected void createContents() { 79 protected void createContents() {
78 shell = new Shell(); 80 shell = new Shell();
79 GridLayout layout = new GridLayout(); 81 GridLayout layout = new GridLayout();
80 layout.numColumns = 4; 82 layout.numColumns = 4;
81 shell.setLayout(layout); 83 shell.setLayout(layout);
82 shell.setText("Snippet033CrossValidationControlDecoration.java"); 84 shell.setText("Snippet033CrossValidationControlDecoration.java");
83 85
84 final Label label = new Label(shell, SWT.NONE); 86 final Label label = new Label(shell, SWT.NONE);
85 label.setLayoutData(new GridData()); 87 label.setLayoutData(new GridData());
86 label.setText("Start date"); 88 label.setText("Start date");
87 startDate = new DateTime(shell, SWT.CALENDAR); 89 startDate = new DateTime(shell, SWT.CALENDAR);
88 final GridData gd_startDate = new GridData(); 90 final GridData gd_startDate = new GridData();
89 gd_startDate.horizontalIndent = 10; 91 gd_startDate.horizontalIndent = 10;
90 startDate.setLayoutData(gd_startDate); 92 startDate.setLayoutData(gd_startDate);
91 93
92 final Label startDateLabel = new Label(shell, SWT.NONE); 94 final Label startDateLabel = new Label(shell, SWT.NONE);
93 startDateLabel.setLayoutData(new GridData()); 95 startDateLabel.setLayoutData(new GridData());
94 startDateLabel.setText("End date"); 96 startDateLabel.setText("End date");
95 endDate = new DateTime(shell, SWT.CALENDAR); 97 endDate = new DateTime(shell, SWT.CALENDAR);
96 final GridData gd_endDate = new GridData(); 98 final GridData gd_endDate = new GridData();
97 gd_endDate.horizontalIndent = 10; 99 gd_endDate.horizontalIndent = 10;
98 endDate.setLayoutData(gd_endDate); 100 endDate.setLayoutData(gd_endDate);
99 101
100 bindUI(); 102 bindUI();
101 } 103 }
102 104
103 private void bindUI() { 105 private void bindUI() {
104 IObservableValue startDateObservable = WidgetProperties.selection() 106 IObservableValue startDateObservable = WidgetProperties.selection()
105 .observe(startDate); 107 .observe(startDate);
106 IObservableValue endDateObservable = WidgetProperties.selection() 108 IObservableValue endDateObservable = WidgetProperties.selection()
107 .observe(endDate); 109 .observe(endDate);
108 110
109 ControlDecorationSupport.create(new DateRangeValidator( 111 ControlDecorationSupport.create(new DateRangeValidator(
110 startDateObservable, endDateObservable, 112 startDateObservable, endDateObservable,
111 "Start date must be on or before end date"), SWT.LEFT 113 "Start date must be on or before end date"), SWT.LEFT
112 | SWT.CENTER); 114 | SWT.CENTER);
113 115
114 // Customize the decoration's description text and image 116 // Customize the decoration's description text and image
115 ControlDecorationUpdater decorationUpdater = new ControlDecorationUpdater() { 117 ControlDecorationUpdater decorationUpdater = new ControlDecorationUpdater() {
116 protected String getDescriptionText(IStatus status) { 118 protected String getDescriptionText(IStatus status) {
117 return "ERROR: " + super.getDescriptionText(status); 119 return "ERROR: " + super.getDescriptionText(status);
118 } 120 }
119 121
120 protected Image getImage(IStatus status) { 122 protected Image getImage(IStatus status) {
121 return status.isOK() ? null : Display.getCurrent() 123 return status.isOK() ? null : Display.getCurrent()
122 .getSystemImage(SWT.ICON_ERROR); 124 .getSystemImage(SWT.ICON_ERROR);
123 } 125 }
124 }; 126 };
125 ControlDecorationSupport.create(new DateRangeValidator(Observables 127 ControlDecorationSupport.create(new DateRangeValidator(Observables
126 .constantObservableValue(new Date()), startDateObservable, 128 .constantObservableValue(new Date()), startDateObservable,
127 "Choose a starting date later than today"), SWT.LEFT | SWT.TOP, 129 "Choose a starting date later than today"), SWT.LEFT | SWT.TOP,
128 (Composite) null, decorationUpdater); 130 (Composite) null, decorationUpdater);
129 } 131 }
130 132
131 private static class DateRangeValidator extends MultiValidator { 133 private static class DateRangeValidator extends MultiValidator {
132 private final IObservableValue start; 134 private final IObservableValue start;
133 private final IObservableValue end; 135 private final IObservableValue end;
134 private final String errorMessage; 136 private final String errorMessage;
135 137
136 public DateRangeValidator(IObservableValue start, IObservableValue end, 138 public DateRangeValidator(IObservableValue start, IObservableValue end,
137 String errorMessage) { 139 String errorMessage) {
138 this.start = start; 140 this.start = start;
139 this.end = end; 141 this.end = end;
140 this.errorMessage = errorMessage; 142 this.errorMessage = errorMessage;
141 } 143 }
142 144
143 protected IStatus validate() { 145 protected IStatus validate() {
144 Date startDate = (Date) start.getValue(); 146 Date startDate = (Date) start.getValue();
145 Date endDate = (Date) end.getValue(); 147 Date endDate = (Date) end.getValue();
146 if (startDate.compareTo(endDate) > 0) 148 if (startDate.compareTo(endDate) > 0)
147 return ValidationStatus.error(errorMessage); 149 return ValidationStatus.error(errorMessage);
148 return ValidationStatus.ok(); 150 return ValidationStatus.ok();
149 } 151 }
150 } 152 }
151 } 153 }
154 void main( String[] args ){
155 Snippet033CrossValidationControlDecoration.main(args);
156 }