X Tutup
Skip to content

Commit bbac253

Browse files
committed
Adds databinding example
1 parent d4dc6d3 commit bbac253

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Manifest-Version: 1.0
2+
Bundle-ManifestVersion: 2
3+
Bundle-Name: Databinding
4+
Bundle-SymbolicName: com.vogella.rcp.databinding;singleton:=true
5+
Bundle-Version: 1.0.0.qualifier
6+
Bundle-Vendor: VOGELLA
7+
Require-Bundle: javax.inject;bundle-version="0.0.0",
8+
org.eclipse.core.runtime;bundle-version="0.0.0",
9+
org.eclipse.swt;bundle-version="0.0.0",
10+
org.eclipse.e4.ui.model.workbench;bundle-version="0.0.0",
11+
org.eclipse.jface;bundle-version="0.0.0",
12+
org.eclipse.e4.ui.services;bundle-version="0.0.0",
13+
org.eclipse.e4.ui.workbench;bundle-version="0.0.0",
14+
org.eclipse.e4.core.di;bundle-version="0.0.0",
15+
org.eclipse.e4.ui.di;bundle-version="0.0.0",
16+
org.eclipse.e4.core.contexts;bundle-version="0.0.0",
17+
org.eclipse.core.databinding;bundle-version="1.6.0",
18+
org.eclipse.core.databinding.beans;bundle-version="1.3.0",
19+
org.eclipse.core.databinding.property;bundle-version="1.6.0",
20+
org.eclipse.jface.databinding;bundle-version="1.8.0"
21+
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<?eclipse version="3.4"?>
3+
<plugin>
4+
5+
<extension
6+
id="product"
7+
point="org.eclipse.core.runtime.products">
8+
<product
9+
name="com.vogella.rcp.databinding"
10+
application="org.eclipse.e4.ui.workbench.swt.E4Application">
11+
<property
12+
name="applicationCSS"
13+
value="platform:/plugin/com.vogella.rcp.databinding/css/default.css">
14+
</property>
15+
<property
16+
name="appName"
17+
value="com.vogella.rcp.databinding">
18+
</property>
19+
</product>
20+
</extension>
21+
22+
</plugin>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.vogella.rcp.databinding.parts;
2+
3+
import java.beans.PropertyChangeListener;
4+
import java.beans.PropertyChangeSupport;
5+
6+
public class ModelObject {
7+
private final PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
8+
9+
public void addPropertyChangeListener(PropertyChangeListener listener) {
10+
changeSupport.addPropertyChangeListener(listener);
11+
}
12+
13+
public void removePropertyChangeListener(PropertyChangeListener listener) {
14+
changeSupport.removePropertyChangeListener(listener);
15+
}
16+
17+
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
18+
changeSupport.firePropertyChange(propertyName, oldValue, newValue);
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.vogella.rcp.databinding.parts;
2+
public class Person extends ModelObject {
3+
private String name;
4+
private String[] programmingSkills;
5+
6+
public String getName() {
7+
return name;
8+
}
9+
10+
public void setName(String name) {
11+
firePropertyChange("name", this.name, this.name = name);
12+
}
13+
14+
public String[] getProgrammingSkills() {
15+
return programmingSkills;
16+
}
17+
18+
public void setProgrammingSkills(String[] programmingSkills) {
19+
firePropertyChange("programmingSkills", this.programmingSkills,
20+
this.programmingSkills = programmingSkills);
21+
}
22+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.vogella.rcp.databinding.parts;
2+
3+
import javax.annotation.PostConstruct;
4+
5+
import org.eclipse.core.databinding.DataBindingContext;
6+
import org.eclipse.core.databinding.UpdateValueStrategy;
7+
import org.eclipse.core.databinding.beans.BeanProperties;
8+
import org.eclipse.core.databinding.conversion.IConverter;
9+
import org.eclipse.core.databinding.observable.value.IObservableValue;
10+
import org.eclipse.jface.databinding.swt.WidgetProperties;
11+
import org.eclipse.jface.layout.GridDataFactory;
12+
import org.eclipse.jface.layout.GridLayoutFactory;
13+
import org.eclipse.swt.SWT;
14+
import org.eclipse.swt.events.SelectionAdapter;
15+
import org.eclipse.swt.events.SelectionEvent;
16+
import org.eclipse.swt.layout.GridData;
17+
import org.eclipse.swt.widgets.Button;
18+
import org.eclipse.swt.widgets.Composite;
19+
import org.eclipse.swt.widgets.Label;
20+
import org.eclipse.swt.widgets.Text;
21+
22+
public class SamplePart {
23+
24+
@PostConstruct
25+
@SuppressWarnings("unchecked")
26+
public void createPartControl(Composite parent) {
27+
// create the Person model with programming skills
28+
Person person = new Person();
29+
person.setName("John");
30+
person.setProgrammingSkills(new String[] { "Java", "JavaScript", "Groovy" });
31+
GridLayoutFactory.swtDefaults().numColumns(2).applyTo(parent);
32+
Label programmingSkillsLabel = new Label(parent, SWT.NONE);
33+
programmingSkillsLabel.setText("Programming Skills");
34+
GridDataFactory.swtDefaults().applyTo(programmingSkillsLabel);
35+
Text programmingSkillsText = new Text(parent, SWT.BORDER);
36+
GridDataFactory.fillDefaults().grab(true, false).applyTo(programmingSkillsText);
37+
// Do the actual binding and conversion
38+
DataBindingContext dbc = new DataBindingContext();
39+
40+
// define converters
41+
IConverter convertToStringArray =
42+
IConverter.create(String.class, String[].class, (o1) -> ((String) o1).split(","));
43+
IConverter convertToString =
44+
IConverter.create(String[].class, String.class, (o1) -> convert((String[])o1));;
45+
46+
// create the observables, which should be bound
47+
IObservableValue<Text> programmingSkillsTarget = WidgetProperties.text(SWT.Modify).observe(programmingSkillsText);
48+
IObservableValue<Person> programmingSkillsModel = BeanProperties.value("programmingSkills").observe(person);
49+
50+
// bind observables together with the appropriate UpdateValueStrategies
51+
dbc.bindValue(programmingSkillsTarget, programmingSkillsModel,
52+
UpdateValueStrategy.create(convertToStringArray),
53+
UpdateValueStrategy.create(convertToString));
54+
55+
// button to check the data model
56+
Button button = new Button(parent, SWT.PUSH);
57+
button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
58+
button.setText("Show data model");
59+
button.addSelectionListener(new SelectionAdapter() {
60+
@Override
61+
public void widgetSelected(SelectionEvent e) {
62+
for (String string : person.getProgrammingSkills()) {
63+
System.out.println(string);
64+
65+
}
66+
}
67+
});
68+
69+
}
70+
71+
private static String convert(String[] fromObject) {
72+
String[] stringArray = fromObject;
73+
StringBuilder sb = new StringBuilder();
74+
int length = stringArray.length;
75+
for (int i = 0; i < length; i++) {
76+
String string = stringArray[i];
77+
sb.append(string);
78+
if (i + 1 < length) {
79+
sb.append(",");
80+
}
81+
}
82+
return sb.toString();
83+
}
84+
}

0 commit comments

Comments
 (0)
X Tutup