How to bind a list of objects to ListBox in GWT? - gwt

I want to bind my services to ListBox but I can't bind it.
//Service class
public class Service {
private String serviceName;
public Service(String serviceName) {
this.serviceName = serviceName;
}
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
}
// SignUpBean class
public class SignUpBean {
private List<Service> services;
public List<Service> getServices() {
return services;
}
public void setServices(List<Service> services) {
this.services = services;
}
}
The following is my Main Editor
public class SignUpEditor extends SimplePanelimplements Editor<SignUpBean> {
public ListBox services;
public void SignUpEditor (){
services.addItem("Service1");
services.addItem("Service2");
setWidget(services);
}
}
I am not getting any error but I think I have to use ListEditor or CompositeEditor. But i don't know about it. Can anyone suggest how to bind Service to ListBox

ListBox suppose to be a LeafValueEditor. Conceptually we don't edit the list in the ListBox. Rather We select a value from the list which will be displayed in the selection. So, ListBox holds only one value and thus ListBox must be leaf value Editor.
Currently there is no GWT support to directly bind the list to ListBox. We have to write an adapter which extends ListEditor ( Refer HasDataEditor class for more detail ). This approach is strange.
Simple approach is to write an adapter which implements LeafValueEditor< List < String > >. In adapter's setValue method we should iterate over the list and call listBox.addItem for each value in the list.
I don't recommend either approaches simply because ListBox's LIST is NON EDITABLE and only VALUE is EDITABLE. I recommend doing addItems manually without using Editors.
UPDATED : HasDataAdapter is an example ListEditor. Refer that implementation. You may get some idea.

Related

How to handle java.util.Properties when using eclipse data binding

I have a model class, it contains a java.util.Properties.
public class Model{
private Properties properties;
}
This properties can contains arbitrary key-value pairs. Now I want to bind "properties" to a JFace TableViewer. How to do this?
Some example code will be good, I have googled for this, but found nothing useful.
To access your model from a TableViewer you have to provide a LabelProvider and a ContentProvider as a translation to the TableViewer and of course provide the model itself.
In short the ContentProvider wraps your models content to the structure of the TableViewer (basically a breakdown of your model to the line by line approach of the TableViewer).
The LabelProvider lets you control the actual rendering in the TableCells.
To provide your model the TableViewer supplies a setInput method.
This works best however when your model is modeled as a JavaBean.
If you look at the snippets you will find something like this in your implementation of the ContentProvider like here:
public Object[] getElements(Object inputElement) {
return (MyModel[]) inputElement;
}
So, if your model as I understand right now, is just a wrapper around your properties, one way to achieve your goal would be to introduce the notion of a PropertyLine that gets populated either by startup, or dynamically via JFace Databinding (that might be a different topic).
public class Model{
...
private PropertyLine[] propertyLineArray;
private PropertyLine[] initializeProperties(){
//initialize your PropertyLines
}
public Model(){
...
propertyLineArray=initializeProperties();
...
}
public PropertyLine[] getPropertyLines(){
return propertyLineArray;
}
}
public class ProperyLine{
...
private String propertyKey;
private String propertyText;
//getter, setter
...
}
Your ContentProvider would look something like this:
private class PropertyContentProvider implements IStructuredContentProvider {
...
public Object[] getElements(Object inputElement) {
return (PropertyLine[]) inputElement;
}
...
}
Your LabelProvider seems quite straightforward then:
public class PropertyLabelProvider extends LabelProvider implements
ITableLabelProvider {
...
public String getColumnText(Object element, int columnIndex) {
PropertyLine pl=(PropertyLine) element
return pl.getPropertyText();
}
...
}
You wire everything up somewhere by:
final TableViewer v = new TableViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION);
v.setLabelProvider(new PropertyLabelProvider());
v.setContentProvider(new PropertyContentProvider ());
v.setInput(myModel.getPropertyLines());

How to edit a Set<? extends EntityProxy> with GWT Editor framework?

for sake of simplicity:
public class Person
{
String name;
Set<Address> addresses;
}
public class Address
{
String city;
String street;
}
with and matching
public interface PersonProxy extends EntityProxy
{
public String getName();
public Set<AdressProxy> getAddresses();
}
and
public interface AdressProxy extends EntityProxy
{
public String getCity();
public String getStreet();
}
I got UiBuinder classes to edit AddressProxy
and it clear to me how to use ListEditor in case if I got List but data is Set in the Person class
how do I use Editor Framework to edit them?
Or may be how do I convert Set to List when it becomes PersonProxy?
I did an attempt to put a kind of adapter Editor class that would implement
LeafValueEditor<Set<AddressProxy>>
and then inside of the LeafValueEditor.setValue() move to a List and start a new driver.edit() on a separate Editor hierarchy that takes care of List editing but with now luck.
You should create a CompositeEditor<Set<AddressProxy>, AddressProxy, AddressEditor>, similar to a ListEditor but handling a Set instead of a List.
I suppose you could somehow delegate to a ListEditor though I'm really not sure.
I've done it with Points and Routes (one Route contains N Points):
Route (Composite):
#UiField
TextBox name;
#Ignore
#UiField
FlexTable listPoints;
PointsEditor pointsEditor = new PointsEditor();
....
pointsEditor.add(String id);
PointsEditor:
public class PointsEditor implements HasRequestContext<List<PointProxy>>, ValueAwareEditor<List<PointProxy>> {
List<PointProxy> points = new ArrayList<PointProxy>();
public void add(String id) {
PointProxy point = ctx.create(PointProxy.class);
point.setId(id);
points.add(point);
}
Route (server side):
#Embedded
private List<Point> points = new ArrayList<Point>();
RouteProxy
public interface RouteProxy extends EntityProxy {
abstract List<PointProxy> getPoints();
abstract void setPoints(List<PointProxy> points);
PointProxy
public interface PointProxy extends ValueProxy {
...
}

How to get access to underlying POJO from GWT Editor class

I have a HashMap inside my POJO that I am using the Editor framework in GWT to edit. While I have access to the standard member variables bound through thier getters/setters, I don't know how to access the values inside the HashMap. How do I get access to the underlying POJO that is being edited through my editor that is using the SimpleBeanEditorDriver?
My POJO:
#Entity(noClassnameStored=true)
public class ProfileConfig extends BaseEntity {
#Indexed(unique=true)
private String name;
private boolean isDefault;
private HashMap<ProfileID, ProfileInfo> profiles= new HashMap<ProfileID, ProfileInfo>();
public ProfileInfo getProfile(ProfileID id) {
return profiles.get(id);
}
public void setProfile(ProfileID id, ProfileInfo p) {
profiles.put(id, p);
}
My Editor:
public class ProfileConfigEditor extends Composite implements ManagedObjectEditor<ProfileConfig> {
private static ProfileConfigEditorUiBinder uiBinder = GWT.create(ProfileConfigEditorUiBinder.class);
interface ProfileConfigEditorUiBinder extends UiBinder<Widget, ProfileConfigEditor> {
}
private UserManager userManager;
#UiField
CellList Profiles;
#UiField
TextBox name;
#UiField
CheckBox isDefault;
So given that I have a list of valid Profile ids from the userManager, how do I go about calling the getProfile method from my POJO from within my Editor?
What you need is a ValueAwareEditor.
public class ProfileConfigEditor extends Composite implements ManagedObjectEditor<ProfileConfig>, ValueAwareEditor<ProfileConfig> {
void setValue(ProfileConfig value){
// TODO: Call ProfileConfig.getProfile()
}
void flush(){
// TODO: Call ProfileConfig.setProfile()
}
// ... Other methods here
Alternatively, if you want more of a challenge, you can look at rolling your own CompositeEditor, for example see the source code for ListEditor. In your case, you would implement a CompositeEditor<ProfileConfig, ProfileInfo, MyNewProfileInfoEditor>. You can this of this as "This editor will take a ProfileConfig object, extract one or more ProfileInfo objects and edit it with one or more MyNewProfileInfoEditor editors"

Converting a bean path to a simple string property in Ext GWT grid

I am working on an Ext GWT 3 (beta) application.
I am trying to display a simple value of a dependent bean in a Grid.
My data beans look like this:
public class MyBean {
private String content;
private MyOtherBean otherBean;
// getters and setters here...
}
public class MyOtherBean {
private String otherBeanContent;
// getter and setter here...
}
The PropertyAccess looks like this:
interface MyBeanProperties extends PropertyAccess<MyBean> {
ModelKeyProvider<MyBean> key();
ValueProvider<MyBean, String> content();
ValueProvider<MyBean, MyOtherBean> otherBean();
}
With the corresponding ColumnConfiguration, my grid now displays one column with content of MyBean and one column with MyOtherBean's toString().
But I want to display MyOtherBean.otherBeanContent instead (without changing MyOtherBean's toString()).
I think I need some kind of value converter and register it for the column? Or am I taking the wrong approach here?
This did the trick:
columnConfig.setCell(new PropertyDisplayCell<MyOtherBean>(new PropertyEditor<MyOtherBean>() {
#Override
public MyOtherBean parse(CharSequence text) throws ParseException {
return otherBean.setOtherBeanContentFromText(text);
}
#Override
public String render(MyOtherBean otherBean) {
return otherBean == null ? "" : otherBean.getOtherBeanContent();
}
}));

GWT CellTree Selection of Parent nodes

I'm trying to use the GWT CellTree to display a heterogeneous, hierarchical data model. I need to be able to a single selection, but be able to select Parent nodes as well as child nodes. For example, if you look at GWT's own example, you'll see that they only provide one selection model for the leave nodes.
I tried to extend their example by providing one selection model for all nodes. However, that seems impossible. So what I ended up with where 3 SelectionModels one for each node type (Composer, PlayList, Song).
What am I missing?
Thanks in advance.
In the getNodeInfo function of your TreeViewModel you have to pass the selectionModel to each new DefaultNodeInfo instance at each level.
return new DefaultNodeInfo<MyDTO>(dataProvider,new MyDTOCell(),selectionModel,null);
and then in the SelectionChangeEventHandler you have do something like this:
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Object object = selectionModel.getSelectedObject();
if (object instanceof MyRootDTO)
{
// DO SOMETHING with root level selected node
}
else if (object instanceof MySecondLevelDTO) {
// DO SOMETHING WITH 2. level selected node
}
// additional levels
});
Update:
In order to get around the typing problem, you can define an abstract base class which is extended by all your DTO's.
public abstract class BaseModel {
public static final ProvidesKey<BaseModel> KEY_PROVIDER = new ProvidesKey<BaseModel>() {
public Object getKey(BaseModel item) {
return item == null ? null : item.getId();
}
};
public abstract Object getId();
}
In your DTO's you extend the BaseModel and implement the abstract getId() method:
public class MyDTO extends BaseModel {
#Override
public Object getId() {
//return unique ID (i.e. MyDTO_1)
}
}