Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » [Databinding]Handling List,Set
[Databinding]Handling List,Set [message #324165] Fri, 18 January 2008 03:15 Go to next message
Eclipse UserFriend
Originally posted by: gaurav.v.bagga.gmail.com

Hi all,
I am fairly new to databinding. I have a FruitList class which gives me
list of fruits that I want to display in the list(widget).This I am able
to do. When I select a fruit from the list it should get populated in
person objects fruits arraylist. I have tried few things but invain. I
have posted the code below. I only have to use swt.

I'll be really thankful if someone helps me with this.

I have seen authors blog he said I can use "observeMultiSelection" but I
dint find this method in ViewersObservables. Which version should I use.Is
it public yet?I am using eclipse-SDK-3.3.1.1-win32.

Thanks and Regards
Gaurav

The code follows
What I have done till now is marked as "///////////"

class Fruit extends AbstractModelObject{
private String fruit;

public Fruit(String fruit) {
// TODO Auto-generated constructor stub
this.fruit=fruit;
}

public void setFruit(String fruit) {
this.fruit = fruit;
System.out.println("setfruit called");
}

public String getFruit() {
System.out.println("getfruit called");
return fruit;
}

@Override
public String toString() {
// TODO Auto-generated method stub
System.out.println("tostring fruit called");
return this.fruit;
}
}
class Person extends AbstractModelObject{
// A property...
String name = "HelloWorld";
private int id = 0;
private ArrayList<Fruit> fruits = new ArrayList<Fruit>();

public Person() {

}

public String getName() {
return name;
}

public void setName(String name) {
System.out.println("set name called");
this.name = name;
}

public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}

public void setFruits(ArrayList<Fruit> fruits) {
System.out.println("called");
this.fruits = fruits;
}

public ArrayList<Fruit> getFruits() {
return fruits;
}
}



class ViewModel {
// The model to bind
private Person person = new Person();

public Person getPerson() {
return person;
}
}

//The GUI view
class View {

private ViewModel viewModel;
private Text name;
private Text id;
private org.eclipse.swt.widgets.List fruits;

public View(ViewModel viewModel) {
this.viewModel = viewModel;
}

public Shell createShell() {
// Build a UI
DataBindingContext bindingContext = new DataBindingContext();
Display display = Display.getDefault();
final Person person = viewModel.getPerson();
final Shell shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.VERTICAL));
name = new Text(shell, SWT.BORDER);
name.setData("name");
id = new Text(shell, SWT.BORDER);
id.setData("id");
fruits = new org.eclipse.swt.widgets.List(shell, SWT.SINGLE);
fruits.setData("fruits");


//System.out.println(text);
Button msg = new Button(shell, SWT.PUSH);
msg.setText("Message");
msg.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {
MessageBox messageBox = new MessageBox(shell);
messageBox.setMessage("First name is " + person.getName()
+ " Id is: " + person.getId());
messageBox.open();
}

});


// Bind it

for (Control child : shell.getChildren()) {
System.out.println(child.getClass());
if(child.getClass()==org.eclipse.swt.widgets.Text.class){
//System.out.println(child);
bindVisualToBean(person,
bindingContext,child,child.getData().toString());
}else if(child.getClass()==org.eclipse.swt.widgets.List.class){
//System.out.println(child);
bindVisualToBeanList(person,
bindingContext,child,child.getData().toString());
}
}





// Open and return the Shell
shell.pack();
shell.open();

return shell;
}

private void bindVisualToBean(final Person person, DataBindingContext
bindingContext,Control text,String property) {
bindingContext.bindValue(SWTObservables.observeText(text,SWT .Modify),BeansObservables.observeValue(person,
property),null, null);
}
private class StringToFruitConverter extends Converter {
public StringToFruitConverter() {
super( String.class, ArrayList.class );
}
public Object convert( final Object fromObject ) {
System.out.println("string to fruit convet called");
System.out.println("from object => " + fromObject);
return new ArrayList<Fruit>().add(new Fruit( ( String )fromObject
));
}


}

/*private class FruitToStringConverter extends Converter {
public FruitToStringConverter() {
super( List.class, String.class );
}
public Object convert( final Object fromObject ) {
List fruit = ( List )fromObject;
System.out.println("fruit to string convert called");
return fruit.toString();
}
}*/
//////////////////////////////////////////////////
private void bindVisualToBeanList(final Person person, DataBindingContext
dbc,Control combo,String property) {
IObservableList observedModel
= BeansObservables.observeList( Realm.getDefault(), new
FruitList(), "fruitlist", Fruit.class );
IObservableList observedModel2
= BeansObservables.observeList( Realm.getDefault(), person,
"fruits", Fruit.class );

UpdateValueStrategy targetToModel = new UpdateValueStrategy();
targetToModel.setConverter( new StringToFruitConverter() );
/* UpdateValueStrategy modelToTarget = new UpdateValueStrategy();
modelToTarget.setConverter( new FruitToStringConverter() );*/
dbc.bindValue(SWTObservables.observeSelection(combo),BeansOb servables.observeValue(person,
property),null, null);
dbc.bindList( SWTObservables.observeItems( combo ),
observedModel2,null,null );
dbc.bindList( SWTObservables.observeItems( combo ),
observedModel,null,null );
}
//////////////////////////////////////////////////////////// /////////
class FruitList{
private List<Fruit> fruitlist;
public FruitList() {
// TODO Auto-generated constructor stub
setFruitlist(new ArrayList<Fruit>());
getFruitlist().add(new Fruit("Mango"));
getFruitlist().add(new Fruit("Orange"));
getFruitlist().add(new Fruit("Banana"));
getFruitlist().add(new Fruit("Grapes"));
}
public void setFruitlist(List<Fruit> fruitlist) {
this.fruitlist = fruitlist;
}
public List<Fruit> getFruitlist() {
return fruitlist;
}

}


}


public class SnippetHelloWorld {
public static void main(String[] args) {
Display display = new Display();
final ViewModel viewModel = new ViewModel();

Realm.runWithDefault(SWTObservables.getRealm(display),
new Runnable() {
public void run() {
final Shell shell = new View(viewModel).createShell();
// The SWT event loop
Display display = Display.getCurrent();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
});
// Print the results
System.out.println("person.getName() = " +
viewModel.getPerson().getName());
System.out.println("person.getId() = " + viewModel.getPerson().getId());
System.out.println("person.getFruits() = " +
viewModel.getPerson().getFruits());
}

}
Re: [Databinding]Handling List,Set [message #324174 is a reply to message #324165] Fri, 18 January 2008 07:38 Go to previous message
Eclipse UserFriend
This is a multi-part message in MIME format.
--------------080805000400050601010200
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I'm not sure I got your request right but if you want to have
multi-selection support you'll need to use Databinding from CVS-HEAD.

One small sidenote it would be nice the next time to provide a snippet
concentrating on the specific problem (see my attached snippet). I would
also prefer a snippet without Java5 syntax because then I don't have to
setup a project but can use the Databinding-Example project.

I'll also file a bug and ask if this snippet could be integrated in the
collection.

Tom

--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------

--------------080805000400050601010200
Content-Type: text/plain;
name="Snippet.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="Snippet.java"

/*********************************************************** ********************
* Copyright (c) 2008 Tom Schindl and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tom Schindl - initial API and implementation
************************************************************ ******************/
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList ;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* Snippet to demonstrate multi-selection support added in 3.4
*/
public class Snippet {
class Person {
private PropertyChangeSupport support = new PropertyChangeSupport(this);
private ArrayList fruits = new ArrayList();

public Person() {

}

public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
support.removePropertyChangeListener(listener);
}

public ArrayList getFruits() {
return fruits;
}

public void setFruits(ArrayList fruits) {
this.fruits = fruits;
}
}

class Fruit {
private String name;

public Fruit(String name) {
this.name = name;
}

public String toString() {
return name;
}

}

private Snippet(Shell shell) {
shell.setLayout(new GridLayout(1, false));

ArrayList fruits = new ArrayList();
fruits.add(new Fruit("Mango"));
fruits.add(new Fruit("Orange"));
fruits.add(new Fruit("Banana"));
fruits.add(new Fruit("Grapes"));

final Person p = new Person();

ListViewer viewer = new ListViewer(shell, SWT.BORDER | SWT.MULTI);
viewer.setLabelProvider(new LabelProvider());
viewer.setContentProvider(new ArrayContentProvider());
viewer.setInput(fruits);
viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));

IObservableList mList = BeansObservables.observeList(
Realm.getDefault(), p, "fruits");
IObservableList uList = ViewersObservables
.observeMultiSelection(viewer);

DataBindingContext ctx = new DataBindingContext();
ctx.bindList(uList, mList, null, null);

Button button = new Button(shell, SWT.PUSH);
button.setText("Read content");
button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
button.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {
System.err.println(p.getFruits());
}

});
}

public static void main(String[] args) {
final Display display = new Display();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
final Shell shell = new Shell(display);
new Snippet(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
});
}
}

--------------080805000400050601010200--
Previous Topic:Mapping left/right to old/new in Compare API two-way diffs
Next Topic:BasicNewProjectResourceWizard - disabling Finish Button
Goto Forum:
  


Current Time: Fri Jul 18 00:42:20 EDT 2025

Powered by FUDForum. Page generated in 0.07069 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top