PDA

View Full Version : Instantiate a <M> Object



G.edwin
5 Aug 2009, 1:06 AM
Hi, I got a problem in a grid with a formBinding. I want to add a new Item in the grid.
Here my code :



public class ADKFormBinding<M extends ModelData> extends ADKPanel {
...
...
buttonAdd.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
M newM = (M) new BaseModelData();
formBinding.bind(newM);
for(FieldBinding f : formBinding.getBindings()){
if(f.getField().getClass().getName().contains("SimpleComboBox")){
SimpleComboBox<String> comboField = (SimpleComboBox<String>) f.getField();
newM.set(f.getProperty(),comboField.getSimpleValue());
}
else{
newM.set(f.getProperty(),f.getField().getValue());
}
}
gridCellRenderer.addNewElement(newM);
data.add(newM);
grid.getStore().insert(newM, 0);
grid.getView().refresh(false);
}
});
...
}


It works (the new item is displayed)....BUT, when i want to get data from the grid, and launch a getter (ex : myClass.getName()) I have a cast error :
java.lang.ClassCastException: com.extjs.gxt.ui.client.data.BaseModelData

This error only appeared when I use the "add" button before.

I think it's due to this line :

M newM = (M) new BaseModelData();

Do you have an idea to instantiate the M object (like a "M newM = new M();"), in order to have all methods an attribute when i will recover it ?

Thanks a lot !

rdopico
5 Aug 2009, 3:41 AM
Why are you using this generic declaration? If it is only to ensure your class is only going to work with ModelData instances, then, why do you need to work with M instances. M is unknown: you don't know any details of it (no methods, ...) so, why don't you work with ModelData in your code, instead of using M?

Hope this helps...

G.edwin
5 Aug 2009, 3:51 AM
Hi and thanks rdopico, in fact i can't use a specific ModelData because I don't know the class used.
I develop a kind of internal framework, so this class (public class ADKFormBinding<M extends ModelData> extends ADKPanel) could receive any ModelData.

That's why I can't directly specify the ModelData, and that's why I have to work with a generic M.

Update works well because i directly get the object from the grid :
M currentM = (M) be.getSelection().get(0);

So i have not to instantiate it and i don't loose any methods or attributes. But with the add feature i have to create a new M and this is the problem.
Maybe it's not possible...:(.

rdopico
5 Aug 2009, 4:07 AM
Sorry, I think I didn't explain very well...

As long as you don't know a thing about M, you can only rely in that M is subclass of ModelData. So, in your code, your M objects could only call to ModelData methods. In this situation, you would be able to work this way:


public class ADKFormBinding<M extends ModelData> extends ADKPanel {
...
...
buttonAdd.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
ModelData newM = new BaseModelData();
formBinding.bind(newM);
for(FieldBinding f : formBinding.getBindings()){
if(f.getField().getClass().getName().contains("SimpleComboBox")){
SimpleComboBox<String> comboField = (SimpleComboBox<String>) f.getField();
newM.set(f.getProperty(),comboField.getSimpleValue());
}
else{
newM.set(f.getProperty(),f.getField().getValue());
}
}
gridCellRenderer.addNewElement(newM);
data.add(newM);
grid.getStore().insert(newM, 0);
grid.getView().refresh(false);
}
});
...
}

G.edwin
5 Aug 2009, 4:40 AM
With :

ModelData newM = new BaseModelData();

I have the same problem, because it is not instantiate with all attributes and methods of the M object defined.

rdopico
5 Aug 2009, 5:01 AM
With :

ModelData newM = new BaseModelData();I have the same problem, because it is not instantiate with all attributes and methods of the M object defined.

Then, the generic declaration is not enough accurate. With

ADKFormBinding<M extends ModelData>you are saying ADKFormBinding would work with any subclass of ModelData. Maybe you need to change it to


ADKFormBinding<M extends YourModelData> where


public interface YourModelData extends ModelData { (...) }

Hope this helps B)

G.edwin
5 Aug 2009, 5:32 AM
Yes your solutions should work, but I can't do :

ADKFormBinding<M extends MyModelData>

This project does not have any sources of the MyModelData.
I have not explain very well my case, this project is a framework for others applications. So it does not contains class used by applications, that's why i have no reference to "MyModelData".
Thanks again for your help

rdopico
5 Aug 2009, 5:46 AM
(...) this project is a framework for others applications. So it does not contains class used by applications, that's why i have no reference to "MyModelData".
Thanks again for your help

But there must be a list of requirements that must meet each ModelData to work with your framework. Just create an interface/class that must implement each concrete instance of ModelData passed to your framework. This forces the framework users to add their ModelDatas the needed functionality to meet the requirements to work with your classes.