Home » Eclipse Projects » JFace » [SOLVED] [Databinding] - Bind a Text widget and a ListViewer for modification
[SOLVED] [Databinding] - Bind a Text widget and a ListViewer for modification [message #1783789] |
Sat, 17 March 2018 11:22 |
Goran M. Messages: 12 Registered: April 2012 |
Junior Member |
|
|
Hi,
I got a ListViewer showing a bunch of objects. Below the ListViewer there are several text widgets bind to corresponding valuef of the object.
When I select one element of the list, the corresponding value is shown in the text widget.
Now here comes the strange part: When I modify the value in the text widget, the element disapears in the list.
If I use a ComboViewer instead of a ListViewer, the selected element is also changed simultaneously.
Here is my test code:
package de.test;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.jface.databinding.swt.DisplayRealm;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.viewers.ViewerProperties;
import org.eclipse.jface.databinding.viewers.ViewerSupport;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ListBindingTest {
protected Shell shell;
private Text txtName;
private java.util.List<Sandwich> sandwichs;
private ListViewer listViewer;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
Realm.runWithDefault(DisplayRealm.getRealm(Display.getDefault()), new Runnable() {
public void run() {
try {
ListBindingTest window = new ListBindingTest();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new GridLayout(2, false));
Label lblNewLabel = new Label(shell, SWT.NONE);
lblNewLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
lblNewLabel.setText("New Label");
listViewer = new ListViewer(shell, SWT.BORDER | SWT.V_SCROLL);
List list = listViewer.getList();
list.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
sandwichs = new ArrayList<>();
sandwichs.add(new Sandwich("Bacon sandwich"));
sandwichs.add(new Sandwich("Bacon, egg and cheese"));
sandwichs.add(new Sandwich("Bagel toast"));
sandwichs.add(new Sandwich("Baked bean"));
sandwichs.add(new Sandwich("Bánh mì"));
sandwichs.add(new Sandwich("Barbecue"));
sandwichs.add(new Sandwich("Barros Jarpa"));
sandwichs.add(new Sandwich("Barros Luco"));
Label lblNewLabel_1 = new Label(shell, SWT.NONE);
lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblNewLabel_1.setText("New Label");
txtName = new Text(shell, SWT.BORDER);
txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
initDatabinding();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private DataBindingContext initDatabinding() {
DataBindingContext bindingContext = new DataBindingContext();
WritableList input = new WritableList(sandwichs, String.class);
ViewerSupport.bind(listViewer, input, BeanProperties.value(Sandwich.class, "name"));
bindingContext.bindValue(WidgetProperties.text(SWT.Modify).observe(txtName), BeanProperties
.value(Sandwich.class, "name").observeDetail(ViewerProperties.singleSelection().observe(listViewer)));
listViewer.setInput(input);
return bindingContext;
}
class Sandwich {
private String name;
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
public Sandwich() {
}
public Sandwich(String name) {
setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
propertyChangeSupport.firePropertyChange("name", this.name, this.name = name);
}
public void addPropertyChangeListener(PropertyChangeListener l) {
propertyChangeSupport.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l) {
propertyChangeSupport.removePropertyChangeListener(l);
}
@Override
public String toString() {
return "Sandwich [name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Sandwich other = (Sandwich) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
private ListBindingTest getOuterType() {
return ListBindingTest.this;
}
}
}
Does anybody have an idea how to get the same behaviour like when using a ComboViewer? I would like to see the modification simultaneously in the text widget and in the ListViewer.
Best regards
Goran
[Updated on: Sat, 17 March 2018 11:46] Report message to a moderator
|
|
| |
Goto Forum:
Current Time: Fri Sep 20 20:40:01 GMT 2024
Powered by FUDForum. Page generated in 0.03411 seconds
|