Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet033CrossValidationControlDecoration.d @ 90:6086085e153d
Added databinding snippets. unmodified java sources.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:33:55 +0200 |
parents | |
children | 5d5bd660917f |
comparison
equal
deleted
inserted
replaced
89:0ca1aee7decf | 90:6086085e153d |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2009 Matthew Hall and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * Matthew Hall - initial API and implementation (bug 268472) | |
10 ******************************************************************************/ | |
11 | |
12 package org.eclipse.jface.examples.databinding.snippets; | |
13 | |
14 import java.util.Date; | |
15 | |
16 import org.eclipse.core.databinding.observable.Observables; | |
17 import org.eclipse.core.databinding.observable.Realm; | |
18 import org.eclipse.core.databinding.observable.value.IObservableValue; | |
19 import org.eclipse.core.databinding.validation.MultiValidator; | |
20 import org.eclipse.core.databinding.validation.ValidationStatus; | |
21 import org.eclipse.core.runtime.IStatus; | |
22 import org.eclipse.jface.databinding.swt.SWTObservables; | |
23 import org.eclipse.jface.databinding.swt.WidgetProperties; | |
24 import org.eclipse.jface.internal.databinding.provisional.fieldassist.ControlDecorationSupport; | |
25 import org.eclipse.jface.internal.databinding.provisional.fieldassist.ControlDecorationUpdater; | |
26 import org.eclipse.swt.SWT; | |
27 import org.eclipse.swt.graphics.Image; | |
28 import org.eclipse.swt.layout.GridData; | |
29 import org.eclipse.swt.layout.GridLayout; | |
30 import org.eclipse.swt.widgets.Composite; | |
31 import org.eclipse.swt.widgets.DateTime; | |
32 import org.eclipse.swt.widgets.Display; | |
33 import org.eclipse.swt.widgets.Label; | |
34 import org.eclipse.swt.widgets.Shell; | |
35 | |
36 /** | |
37 * @since 3.2 | |
38 * | |
39 */ | |
40 public class Snippet033CrossValidationControlDecoration { | |
41 protected Shell shell; | |
42 private DateTime startDate; | |
43 private DateTime endDate; | |
44 | |
45 /** | |
46 * Launch the application | |
47 * | |
48 * @param args | |
49 */ | |
50 public static void main(String[] args) { | |
51 try { | |
52 Snippet033CrossValidationControlDecoration window = new Snippet033CrossValidationControlDecoration(); | |
53 window.open(); | |
54 } catch (Exception e) { | |
55 e.printStackTrace(); | |
56 } | |
57 } | |
58 | |
59 /** | |
60 * Open the window | |
61 */ | |
62 public void open() { | |
63 final Display display = Display.getDefault(); | |
64 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | |
65 public void run() { | |
66 createContents(); | |
67 shell.pack(); | |
68 shell.open(); | |
69 while (!shell.isDisposed()) { | |
70 if (!display.readAndDispatch()) | |
71 display.sleep(); | |
72 } | |
73 } | |
74 }); | |
75 } | |
76 | |
77 protected void createContents() { | |
78 shell = new Shell(); | |
79 GridLayout layout = new GridLayout(); | |
80 layout.numColumns = 4; | |
81 shell.setLayout(layout); | |
82 shell.setText("Snippet033CrossValidationControlDecoration.java"); | |
83 | |
84 final Label label = new Label(shell, SWT.NONE); | |
85 label.setLayoutData(new GridData()); | |
86 label.setText("Start date"); | |
87 startDate = new DateTime(shell, SWT.CALENDAR); | |
88 final GridData gd_startDate = new GridData(); | |
89 gd_startDate.horizontalIndent = 10; | |
90 startDate.setLayoutData(gd_startDate); | |
91 | |
92 final Label startDateLabel = new Label(shell, SWT.NONE); | |
93 startDateLabel.setLayoutData(new GridData()); | |
94 startDateLabel.setText("End date"); | |
95 endDate = new DateTime(shell, SWT.CALENDAR); | |
96 final GridData gd_endDate = new GridData(); | |
97 gd_endDate.horizontalIndent = 10; | |
98 endDate.setLayoutData(gd_endDate); | |
99 | |
100 bindUI(); | |
101 } | |
102 | |
103 private void bindUI() { | |
104 IObservableValue startDateObservable = WidgetProperties.selection() | |
105 .observe(startDate); | |
106 IObservableValue endDateObservable = WidgetProperties.selection() | |
107 .observe(endDate); | |
108 | |
109 ControlDecorationSupport.create(new DateRangeValidator( | |
110 startDateObservable, endDateObservable, | |
111 "Start date must be on or before end date"), SWT.LEFT | |
112 | SWT.CENTER); | |
113 | |
114 // Customize the decoration's description text and image | |
115 ControlDecorationUpdater decorationUpdater = new ControlDecorationUpdater() { | |
116 protected String getDescriptionText(IStatus status) { | |
117 return "ERROR: " + super.getDescriptionText(status); | |
118 } | |
119 | |
120 protected Image getImage(IStatus status) { | |
121 return status.isOK() ? null : Display.getCurrent() | |
122 .getSystemImage(SWT.ICON_ERROR); | |
123 } | |
124 }; | |
125 ControlDecorationSupport.create(new DateRangeValidator(Observables | |
126 .constantObservableValue(new Date()), startDateObservable, | |
127 "Choose a starting date later than today"), SWT.LEFT | SWT.TOP, | |
128 (Composite) null, decorationUpdater); | |
129 } | |
130 | |
131 private static class DateRangeValidator extends MultiValidator { | |
132 private final IObservableValue start; | |
133 private final IObservableValue end; | |
134 private final String errorMessage; | |
135 | |
136 public DateRangeValidator(IObservableValue start, IObservableValue end, | |
137 String errorMessage) { | |
138 this.start = start; | |
139 this.end = end; | |
140 this.errorMessage = errorMessage; | |
141 } | |
142 | |
143 protected IStatus validate() { | |
144 Date startDate = (Date) start.getValue(); | |
145 Date endDate = (Date) end.getValue(); | |
146 if (startDate.compareTo(endDate) > 0) | |
147 return ValidationStatus.error(errorMessage); | |
148 return ValidationStatus.ok(); | |
149 } | |
150 } | |
151 } |