Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2006, 2009 IBM Corporation 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 |
* IBM Corporation - initial API and implementation |
10 |
******************************************************************************/ |
11 |
|
12 |
package org.eclipse.jface.examples.databinding.snippets; |
13 |
|
14 |
import java.util.Date; |
15 |
|
16 |
import org.eclipse.core.databinding.Binding; |
17 |
import org.eclipse.core.databinding.DataBindingContext; |
18 |
import org.eclipse.core.databinding.editing.DateEditing; |
19 |
import org.eclipse.core.databinding.editing.Editing; |
20 |
import org.eclipse.core.databinding.editing.IntegerEditing; |
21 |
import org.eclipse.core.databinding.observable.Realm; |
22 |
import org.eclipse.core.databinding.observable.list.WritableList; |
23 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
24 |
import org.eclipse.core.databinding.observable.value.WritableValue; |
25 |
import org.eclipse.core.runtime.IStatus; |
26 |
import org.eclipse.jface.databinding.swt.SWTObservables; |
27 |
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider; |
28 |
import org.eclipse.jface.internal.databinding.provisional.fieldassist.ControlDecorationSupport; |
29 |
import org.eclipse.jface.layout.GridDataFactory; |
30 |
import org.eclipse.jface.layout.GridLayoutFactory; |
31 |
import org.eclipse.jface.viewers.LabelProvider; |
32 |
import org.eclipse.jface.viewers.ListViewer; |
33 |
import org.eclipse.swt.SWT; |
34 |
import org.eclipse.swt.events.SelectionAdapter; |
35 |
import org.eclipse.swt.events.SelectionEvent; |
36 |
import org.eclipse.swt.layout.GridLayout; |
37 |
import org.eclipse.swt.widgets.Composite; |
38 |
import org.eclipse.swt.widgets.Display; |
39 |
import org.eclipse.swt.widgets.Group; |
40 |
import org.eclipse.swt.widgets.Label; |
41 |
import org.eclipse.swt.widgets.Shell; |
42 |
import org.eclipse.swt.widgets.Text; |
43 |
|
44 |
import com.ibm.icu.text.DateFormat; |
45 |
import com.ibm.icu.text.SimpleDateFormat; |
46 |
|
47 |
public class Snippet035Editing { |
48 |
|
49 |
public static void main(String[] args) { |
50 |
Display display = new Display(); |
51 |
|
52 |
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { |
53 |
public void run() { |
54 |
final Shell shell = createShell(); |
55 |
Display display = Display.getCurrent(); |
56 |
while (!shell.isDisposed()) { |
57 |
if (!display.readAndDispatch()) { |
58 |
display.sleep(); |
59 |
} |
60 |
} |
61 |
} |
62 |
}); |
63 |
} |
64 |
|
65 |
private static Shell createShell() { |
66 |
Display display = Display.getCurrent(); |
67 |
Shell shell = new Shell(display); |
68 |
shell.setLayout(new GridLayout(1, false)); |
69 |
|
70 |
createValueSection(shell); |
71 |
createListSection(shell); |
72 |
|
73 |
shell.pack(); |
74 |
shell.open(); |
75 |
|
76 |
return shell; |
77 |
} |
78 |
|
79 |
private static void createValueSection(Composite parent) { |
80 |
Group section = createSectionGroup(parent, "Value bindings", false); |
81 |
|
82 |
DataBindingContext dbc = new DataBindingContext(); |
83 |
|
84 |
// Edit a required, positive integer. |
85 |
Text positiveText = createTextField(section, "Positive value*"); |
86 |
IntegerEditing positiveEditing = EditingFactory.forInteger().required().positive(); |
87 |
bindTextField(dbc, positiveText, new WritableValue(), positiveEditing); |
88 |
|
89 |
// Edit an integer within the range [1, 100] by using the default validation message. |
90 |
Text range1Text = createTextField(section, "Value in [1, 100]"); |
91 |
IntegerEditing range1Editing = EditingFactory.forInteger().range(1, 100); |
92 |
bindTextField(dbc, range1Text, new WritableValue(new Integer(0), null), range1Editing); |
93 |
|
94 |
// Edit an integer within the range [1, 100] by customizing the validation message. |
95 |
Text range2Text = createTextField(section, "Value in [1, 100]"); |
96 |
IntegerEditing range2Editing = EditingFactory.forInteger().range(1, 100, "Value lies outside [{0}, {1}]."); |
97 |
bindTextField(dbc, range2Text, new WritableValue(new Integer(0), null), range2Editing); |
98 |
} |
99 |
|
100 |
private static void createListSection(Composite parent) { |
101 |
Group section = createSectionGroup(parent, "List bindings", true); |
102 |
|
103 |
DataBindingContext dbc = new DataBindingContext(); |
104 |
|
105 |
// Edit a date supporting the default input/display patterns. |
106 |
Text dateText = createTextField(section, "Date"); |
107 |
DateEditing dateEditing = EditingFactory.forDate(); |
108 |
final WritableValue dateObservable = new WritableValue(); |
109 |
final Binding dateBinding = bindTextField(dbc, dateText, dateObservable, dateEditing); |
110 |
|
111 |
// Create a list to which the dates are added when the user hits ENTER. |
112 |
new Label(section, SWT.LEFT); |
113 |
ListViewer dateListViewer = new ListViewer(section); |
114 |
GridDataFactory.fillDefaults().grab(true, true).hint(150, 200).applyTo(dateListViewer.getList()); |
115 |
|
116 |
dateListViewer.setContentProvider(new ObservableListContentProvider()); |
117 |
dateListViewer.setLabelProvider(new LabelProvider()); |
118 |
|
119 |
// We use the same DateEditing object as for the text field above to |
120 |
// create a list binding which maps the entered dates to their string |
121 |
// representation which is then set as input on the ListViewer. |
122 |
final WritableList targetDateList = new WritableList(); |
123 |
final WritableList modelDateList = new WritableList(); |
124 |
dbc.bindList( |
125 |
targetDateList, |
126 |
modelDateList, |
127 |
dateEditing.createTargetListStrategy(), |
128 |
dateEditing.createModelListStrategy()); |
129 |
|
130 |
// Set the list containing the string representation of the dates as input. |
131 |
dateListViewer.setInput(targetDateList); |
132 |
|
133 |
// Add the current date in the text field when the user hits ENTER. |
134 |
dateText.addSelectionListener(new SelectionAdapter() { |
135 |
public void widgetDefaultSelected(SelectionEvent e) { |
136 |
IStatus dateValidationStatus = (IStatus) dateBinding.getValidationStatus().getValue(); |
137 |
Date date = (Date) dateObservable.getValue(); |
138 |
if (dateValidationStatus.isOK() && date != null) { |
139 |
modelDateList.add(date); |
140 |
} |
141 |
} |
142 |
}); |
143 |
} |
144 |
|
145 |
private static Binding bindTextField( |
146 |
DataBindingContext dbc, |
147 |
Text text, |
148 |
IObservableValue modelValue, |
149 |
Editing editing) { |
150 |
// Create the binding using the editing object. |
151 |
Binding binding = dbc.bindValue( |
152 |
SWTObservables.observeText(text, SWT.Modify), |
153 |
modelValue, |
154 |
editing.createTargetValueStrategy(), |
155 |
editing.createModelValueStrategy()); |
156 |
|
157 |
// Decorate the control with the validation status. |
158 |
ControlDecorationSupport.create(binding, SWT.TOP); |
159 |
|
160 |
return binding; |
161 |
} |
162 |
|
163 |
private static Group createSectionGroup(Composite parent, String groupText, boolean grabVertical) { |
164 |
Group section = new Group(parent, SWT.DEFAULT); |
165 |
section.setText(groupText); |
166 |
GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).margins(5, 5).spacing(15, 5).applyTo(section); |
167 |
GridDataFactory.fillDefaults().grab(true, grabVertical).applyTo(section); |
168 |
return section; |
169 |
} |
170 |
|
171 |
private static Text createTextField(Composite parent, String labelText) { |
172 |
Label label = new Label(parent, SWT.LEFT); |
173 |
label.setText(labelText); |
174 |
GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(label); |
175 |
|
176 |
Text text = new Text(parent, SWT.BORDER); |
177 |
GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).applyTo(text); |
178 |
|
179 |
return text; |
180 |
} |
181 |
|
182 |
private static final class EditingFactory { |
183 |
|
184 |
private static final String REQUIRED_MESSAGE = "Please specify a value."; |
185 |
|
186 |
private static final String INTEGER_PARSE_ERROR_MESSAGE = "The input is invalid."; |
187 |
|
188 |
private static final String INTEGER_OUT_OF_RANGE_MESSAGE = "The value lies outside the supported range."; |
189 |
|
190 |
private static final String INTEGER_RANGE_MESSAGE = "The value must lie between {0} and {1}."; |
191 |
|
192 |
private static final String[] DATE_INPUT_PATTERNS = new String[] { |
193 |
"dd.MM.yy", |
194 |
"dd/MM/yy", |
195 |
"dd.MM.yyyy", |
196 |
"dd/MM/yyyy" |
197 |
}; |
198 |
|
199 |
private static final String DATE_DISPLAY_PATTERN = "dd.MM.yyyy"; |
200 |
|
201 |
private static final String DATE_PARSE_ERROR_MESSAGE = createDateParseErrorMessage(DATE_INPUT_PATTERNS); |
202 |
|
203 |
private EditingFactory() { |
204 |
// prevent instantiation |
205 |
} |
206 |
|
207 |
public static IntegerEditing forInteger() { |
208 |
return IntegerEditing |
209 |
.withDefaults(INTEGER_PARSE_ERROR_MESSAGE, INTEGER_OUT_OF_RANGE_MESSAGE) |
210 |
.requiredMessage(REQUIRED_MESSAGE) |
211 |
.rangeMessage(INTEGER_RANGE_MESSAGE); |
212 |
} |
213 |
|
214 |
public static DateEditing forDate() { |
215 |
return DateEditing |
216 |
.forFormats( |
217 |
createDateFormats(DATE_INPUT_PATTERNS), |
218 |
DATE_PARSE_ERROR_MESSAGE, |
219 |
new SimpleDateFormat(DATE_DISPLAY_PATTERN)) |
220 |
.requiredMessage(REQUIRED_MESSAGE); |
221 |
} |
222 |
|
223 |
private static DateFormat[] createDateFormats(String[] datePatterns) { |
224 |
DateFormat[] dateFormats = new DateFormat[datePatterns.length]; |
225 |
for (int i = 0; i < dateFormats.length; i++) { |
226 |
dateFormats[i] = new SimpleDateFormat(datePatterns[i]); |
227 |
} |
228 |
return dateFormats; |
229 |
} |
230 |
|
231 |
private static String createDateParseErrorMessage(String[] datePatterns) { |
232 |
StringBuffer messageSb = new StringBuffer(); |
233 |
messageSb.append("Supported formats: "); |
234 |
for (int i = 0; i < datePatterns.length; i++) { |
235 |
if (i > 0) { |
236 |
messageSb.append(", "); |
237 |
} |
238 |
messageSb.append(datePatterns[i]); |
239 |
} |
240 |
return messageSb.toString(); |
241 |
} |
242 |
} |
243 |
} |