11package de .vogella .databinding .example ;
22
3+ import java .util .Arrays ;
4+
35import org .eclipse .core .databinding .AggregateValidationStatus ;
46import org .eclipse .core .databinding .Binding ;
57import org .eclipse .core .databinding .DataBindingContext ;
1012import org .eclipse .core .databinding .validation .ValidationStatus ;
1113import org .eclipse .core .runtime .IStatus ;
1214import org .eclipse .jface .databinding .fieldassist .ControlDecorationSupport ;
15+ import org .eclipse .jface .databinding .swt .ISWTObservableValue ;
1316import org .eclipse .jface .databinding .swt .WidgetProperties ;
1417import org .eclipse .swt .SWT ;
1518import org .eclipse .swt .events .SelectionAdapter ;
2326import org .eclipse .swt .widgets .Text ;
2427import org .eclipse .ui .part .ViewPart ;
2528
29+ import de .vogella .databinding .example .converter .CommaSeparatedStringToStringArrayConverter ;
30+ import de .vogella .databinding .example .converter .StringArrayToCommaSeparatedStringConverter ;
2631import de .vogella .databinding .example .model .Address ;
2732import de .vogella .databinding .example .model .Person ;
2833import de .vogella .databinding .example .validators .StringLongerThenTwo ;
@@ -37,6 +42,7 @@ public class View extends ViewPart {
3742 private Combo genderCombo ;
3843 private Text countryText ;
3944 private Label errorLabel ;
45+ private Text programmingSkillsText ;
4046
4147 @ Override
4248 public void createPartControl (Composite parent ) {
@@ -78,6 +84,14 @@ public void createPartControl(Composite parent) {
7884 Label countryLabel = new Label (parent , SWT .NONE );
7985 countryLabel .setText ("Country" );
8086 countryText = new Text (parent , SWT .BORDER );
87+
88+ Label programmingSkillsLabel = new Label (parent , SWT .NONE );
89+ programmingSkillsLabel .setText ("Programming Skills" );
90+ programmingSkillsText = new Text (parent , SWT .BORDER );
91+ gridData = new GridData ();
92+ gridData .horizontalAlignment = SWT .FILL ;
93+ gridData .grabExcessHorizontalSpace = true ;
94+ programmingSkillsText .setLayoutData (gridData );
8195
8296 Button button1 = new Button (parent , SWT .PUSH );
8397 button1 .setText ("Write model" );
@@ -89,8 +103,8 @@ public void widgetSelected(SelectionEvent e) {
89103 System .out .println ("Age " + person .getAge ());
90104 System .out .println ("Married: " + person .isMarried ());
91105 System .out .println ("Gender: " + person .getGender ());
92- System .out .println ("Country: "
93- + person . getAddress (). getCountry ( ));
106+ System .out .println ("Country: " + person . getAddress (). getCountry ());
107+ System . out . println ( "Programming Skills: " + Arrays . toString ( person . getProgrammingSkills () ));
94108 }
95109 });
96110
@@ -139,6 +153,7 @@ private Person createPerson() {
139153 person .setGender ("Male" );
140154 person .setAge (12 );
141155 person .setMarried (true );
156+ person .setProgrammingSkills (new String [] { "Java" , "JavaScript" , "Groovy" });
142157 return person ;
143158 }
144159
@@ -150,10 +165,8 @@ private void bindValues() {
150165 // The DataBindingContext object will manage the databindings
151166 // Lets bind it
152167 DataBindingContext ctx = new DataBindingContext ();
153- IObservableValue widgetValue = WidgetProperties .text (SWT .Modify )
154- .observe (firstName );
155- IObservableValue modelValue = BeanProperties .value (Person .class ,
156- "firstName" ).observe (person );
168+ IObservableValue widgetValue = WidgetProperties .text (SWT .Modify ).observe (firstName );
169+ IObservableValue modelValue = BeanProperties .value (Person .class , "firstName" ).observe (person );
157170 // Here we define the UpdateValueStrategy
158171 UpdateValueStrategy update = new UpdateValueStrategy ();
159172 update .setAfterConvertValidator (new StringLongerThenTwo ());
@@ -179,14 +192,12 @@ public IStatus validate(Object value) {
179192 UpdateValueStrategy strategy = new UpdateValueStrategy ();
180193 strategy .setBeforeSetValidator (validator );
181194
182- Binding bindValue = ctx .bindValue (widgetValue , modelValue , strategy ,
183- null );
195+ Binding bindValue = ctx .bindValue (widgetValue , modelValue , strategy , null );
184196 // Add some decorations
185197 ControlDecorationSupport .create (bindValue , SWT .TOP | SWT .LEFT );
186198
187199 widgetValue = WidgetProperties .selection ().observe (marriedButton );
188- modelValue = BeanProperties .value (Person .class , "married" ).observe (
189- person );
200+ modelValue = BeanProperties .value (Person .class , "married" ).observe (person );
190201 ctx .bindValue (widgetValue , modelValue );
191202
192203 widgetValue = WidgetProperties .selection ().observe (genderCombo );
@@ -197,19 +208,27 @@ public IStatus validate(Object value) {
197208 // Address field is bound to the Ui
198209 widgetValue = WidgetProperties .text (SWT .Modify ).observe (countryText );
199210
200- modelValue = BeanProperties .value (Person .class , "address.country" )
201- .observe (person );
211+ modelValue = BeanProperties .value (Person .class , "address.country" ).observe (person );
202212 ctx .bindValue (widgetValue , modelValue );
203213
214+ UpdateValueStrategy programmingSkillsTargetStrategy = new UpdateValueStrategy ();
215+ programmingSkillsTargetStrategy .setConverter (new CommaSeparatedStringToStringArrayConverter ());
216+ UpdateValueStrategy programmingSkillsModelStrategy = new UpdateValueStrategy ();
217+ programmingSkillsModelStrategy .setConverter (new StringArrayToCommaSeparatedStringConverter ());
218+
219+ ISWTObservableValue programmingSkillsTarget = WidgetProperties .text (SWT .Modify ).observe (programmingSkillsText );
220+ IObservableValue programmingSkillsModel = BeanProperties .value ("programmingSkills" ).observe (person );
221+
222+ ctx .bindValue (programmingSkillsTarget , programmingSkillsModel , programmingSkillsTargetStrategy ,
223+ programmingSkillsModelStrategy );
224+
204225 // We listen to all errors via this binding
205226 // We don't need to listen to any SWT event on this label as it never
206227 // changes independently
207- final IObservableValue errorObservable = WidgetProperties .text ()
208- .observe (errorLabel );
228+ final IObservableValue errorObservable = WidgetProperties .text ().observe (errorLabel );
209229 // This one listenes to all changes
210230 ctx .bindValue (errorObservable ,
211- new AggregateValidationStatus (ctx .getBindings (),
212- AggregateValidationStatus .MAX_SEVERITY ), null , null );
231+ new AggregateValidationStatus (ctx .getBindings (), AggregateValidationStatus .MAX_SEVERITY ), null , null );
213232
214233 }
215234}
0 commit comments