How to add and modify bean in BeanItemContainer - java

I'm developing a Vaadin application. Now, I have a trouble with a BeanItemContainer. I have a few items inside my container.
private void populateTable() {
tableContainer.removeAllItems();
for(MyBean myBean : beans){
tableContainer.addItem(myBean);
}
}
When I select the item in the table, I bind the item selected with the binder and I fill the form automatically
table.addItemClickListener(new ItemClickListener() {
public void itemClick(ItemClickEvent event) {
myBean = ((BeanItem<MyBean>) event.getItem()).getBean();
//BeanFieldGroup<MyBean>
binder.setItemDataSource(myBean);
}
});
private Component makeForm() {
formLayout = new FormLayout();
binder.bind(comboBoxModPag,"modPagamento");
binder.bind(fieldInizioVal, "id.dInizioVal");
formLayout.addComponent(comboBoxModPag);
formLayout.addComponent(fieldInizioVal);
formLayout.addComponent(binder.buildAndBind(getI18NMessage("dValidoAl"), "dValidoAl", DateField.class));
return formLayout;
}
Now, I have to manage the user interactions in a different way. For example, if the user modify the value inside the combobox, I have to add a new Bean in the container, while if the users modify the value of the field fieldInizioVal I have to update the current Bean.
insertOrUpdateButton.addClickListener(new ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
tableContainer.addItem(myBean));
}
});
But, when add a new Item, the container adds the new item correctly but modify also the old item selected.
How can I do?

I solved in this way
comboBoxModPag.addValueChangeListener(new ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
MyBean oldValue = (MyBean) comboBoxModPag.getOldValue();
MyBean newValue = (MyBean) comboBoxModPag.getValue();
if( oldValue!=null && newValue!=null && !oldValue.equals(newValue) ){
insertMode = true;
}
else{
insertMode = false;
}
}
}
});
protected void saveOrUpdateModPagContrattoSito() {
if(insertMode){
MyBean newMyBean = new MyBean(myBean);
//Do somethings to restore myBean statuse
//....
//....
tableContainer.addBean(newMyBean);
}
else{
tableContainer.addBean(myBean);
}
table.refreshRowCache();
}
But I don't know if this is the correct way.

Related

How to listen even choose item from combobox in ZK 7?

I am use MVC design pattern. In file FmCompress.zul, I have:
<combobox id="cboFmCompress" model="${$composer.listTypeOfProcess}" mold="rounded" hflex="1">
<attribute name="onCreate">self.setSelectedIndex(1);</attribute>
<template name="model">
<comboitem label="${each.typeOfCompress}" value="${each.typeOfCompressId}"></comboitem>
</template>
</combobox>
Model for combo box: TypeOfCompressDTO.java
public class TypeOfCompressDTO {
private String typeOfCompressId;
private String typeOfCompress;
public TypeOfCompressDTO() {
}
public TypeOfCompressDTO(String typeOfCompressId, String typeOfCompress) {
this.typeOfCompressId = typeOfCompressId;
this.typeOfCompress = typeOfCompress;
}
public String getTypeOfCompressId() {
return typeOfCompressId;
}
public void setTypeOfCompressId(String typeOfCompressId) {
this.typeOfCompressId = typeOfCompressId;
}
public String getTypeOfCompress() {
return typeOfCompress;
}
public void setTypeOfCompress(String typeOfCompress) {
this.typeOfCompress = typeOfCompress;
}
}
In file controller: FmCompressComposer.java , I try something like this (my idea):
public class FmCompressComposer extends BaseCustomComposer<FmCompressService, FmCompressDTO> {
//....
#Wire
private Combobox cboToggleZipUnzip;
//....
// initialize value for combo box.
public ListModel<TypeOfCompressDTO> getListTypeOfProcess() {
lstTypeOfCompress = new ArrayList<TypeOfCompressDTO>();
TypeOfCompressDTO t1 = new TypeOfCompressDTO("1", "Zip file");
TypeOfCompressDTO t2 = new TypeOfCompressDTO("2", "Unzip file");
lstTypeOfCompress.add(t1);
lstTypeOfCompress.add(t2);
listTypeOfProcess = new ListModelList(lstTypeOfCompress, true);
return listTypeOfProcess;
}
// Listen even select item in combo box.
public void onSelect$cboZipUnzip(){
searchDTO = new FmCompressDTO();
searchDTO.setType("1");
// my problem focus at this method, and at this line, get value what user choosen. searchDTO.setType(cboToggleZipUnzip.getSelectedItem().getValue().toString());
List<FmCompressDTO> listDTO = fmCompressService.search(searchDTO);
if (listDTO != null && !listDTO.isEmpty()) {
ListModelList model = new ListModelList(listDTO);
model.setMultiple(true);
gridDataFmCompress.setModel(model);
refreshGridData(null);
}
}
//...
}
Please help me: In combo box, when user has event selecting, call method. (In method, get value what user choosen from combobox).
I assume your BaseCustomComposer is extending a GenericForwardComposer.
If so, you are strictly bound to the naming conventions.
As your id of the combobox is cboFmCompress your wired variable should be
// no need for #Wire
private Combobox cboFmCompress;
and the event listener method should be
public void onSelect$cboFmCompress(Event event) {}
Here you can find a minimized zkfiddle: http://zkfiddle.org/sample/3hnhc92/2-SO-33120026

Eclipse DatabindingContext valuechange Listener => dirty

i have a dialog to show some values. Now I need to know if the user has changed something.
All fields are wrapped in an eclipse DatabindingContext.
...
bindingContext.bindValue(process_observable_milage, process_bean_mileage, new UpdateValueStrategy(), null);
...
If I change some fields, the propertyChangeSupport listener inside the model gets fired. (Setter of the property is called).
this.firePropertyChange(SignalEntity.SIGNALNAME, this.signalname, this.signalname = name);
Now I need a global Listener to detect if any value was changed.
I tried it with the following without success:
IObservableValue globalValidity = new WritableValue();
globalValidity.addChangeListener(new IChangeListener() {
#Override
public void handleChange(ChangeEvent event) {
dirty = true;
}
});
bindingContext.bindValue(globalValidity, new AggregateValidationStatus(bindingContext.getBindings(), AggregateValidationStatus.MAX_SEVERITY), null, null);
This is from another class with some afterConvertValidators added.
So I thought I have to use "addValueChangeListener" instead but even this is not working.
IObservableValue globalValidity = new WritableValue();
globalValidity.addValueChangeListener(new IValueChangeListener() {
#Override
public void handleValueChange(ValueChangeEvent event) {
dirty = true;
}
});
bindingContext.bindValue(globalValidity, new AggregateValidationStatus(bindingContext.getBindings(), AggregateValidationStatus.MAX_SEVERITY), null, null);
Any ideas how to achieve this?
Best regards
I use:
IObservableValue setError = PojoProperties.value("error").observe(new SetError());
bindingContext.bindValue(setError, new AggregateValidationStatus(bindingContext.getBindings(), AggregateValidationStatus.MAX_SEVERITY));
private class SetError
{
public IStatus getError()
{
return ValidationStatus.ok();
}
public void setError(final IStatus status)
{
final String msg = status.isOK() ? null : status.getMessage();
// TODO deal with message / status
}
}

JavaFX8 TableView with custom control

I want to use a custom control (ClientControl in the code) in my TableView. Therefore I created a class ClientCell:
public class NewClientCell extends TableCell<Client, Client> {
private final ClientControl cc;
public NewClientCell(ObservableList<Client> suggestions) {
cc = new ClientControl(this.getItem(), suggestions);
this.setAlignment(Pos.CENTER_LEFT);
this.setGraphic(cc);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
#Override
protected void updateItem(Client c, boolean empty) {
super.updateItem(c, empty);
if(!empty){
setGraphic(cc);
}
}
}
In the main program I use the following code to fill the table:
TableColumn<Client, Client> clmClients = new TableColumn<>("Klient");
clmClients.setCellFactory(new Callback<TableColumn<Client, Client>, TableCell<Client, Client>>() {
#Override
public TableCell<Client, Client> call(TableColumn<Client, Client> p) {
return new NewClientCell(suggestions);
};
});
clmClients.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Client, Client>, ObservableValue<Client>>() {
#Override
public ObservableValue<Client> call(TableColumn.CellDataFeatures<Client, Client> p) {
return new SimpleObjectProperty<Client>(p.getValue());
}
});
getColumns().add(clmClients);
The data in the table comes from an ObservableList and is initialized correct.
My problem now is that the custom control needs an Client-Object which it should get out of the ObservableList, but "this.getItem()" always returns null.
How do I get the Client objects correctly into the custom control?
Thanks!
EDIT
Here's the constructor of ClientControl:
public ClientControl(Client client, ObservableList<Client> suggestions) {
setClient(client);
setSuggestions(suggestions);
FXMLLoader loader = new FXMLLoader(getClass().getResource("ClientControl.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException e) {
throw new RuntimeException(e);
}
initTextField();
setLabelText(client.toString());
}
The method setClient is a simple setter method (this.client = client;). The variables client and suggestions are this simple defined:
private ObservableList<Client> suggestions;
private Client client;
AFAIK, you should instantiate any controls in the constructor as you did, so that they are only created once (remember that cells get reused for different locations).
But then you need to override one or more of the other methods such as updateItem to get the data from the current item to render.
EDIT
Well, you're assigning the same control without changing it over and over again. Rather than setting the graphics in the updateItem method, set the item property of the client control:
#Override
protected void updateItem(Client c, boolean empty) {
super.updateItem(c, empty);
if(!empty){
cc.setClient(c);
} else {
cc.setClient(null);
}
}
Edit 2
The ClientControl should provide the client item as a property instead of a constructor argument and set it in the updateItem method, not in the constructor.
E.g. something like this (untested!):
private final ObjectProperty<Client> client = new SimpleObjectProperty<>(this, "client");
public final Client getClient(){
return clientProperty().get();
}
public final void setClient(Client client){
clientProperty().set(client);
}
public ObjectProperty<Client> clientProperty(){
return client;
}
And in the constructor: listen for changes of this property to set the labelText etc.
You also might want to provide a constructor without a client argument, as it is not available when you instantiate it in the TableCell constructor.
So I found the solution for my problem. Thank you so much Puce for your help! :-)
Now I set Client via the property like that:
private ObjectProperty<Client> clientProperty = new SimpleObjectProperty<Client>();
Additionally I added a ChangeListener in the constructor of ClientControl:
public ClientControl(ObservableList<Client> suggestions) {
clientProperty.addListener(new ChangeListener<Client>() {
#Override
public void changed(ObservableValue<? extends Client> observable, Client oldValue,
ClientnewValue) {
if(newValue != null) {
setLabelText(newValue.toString());
}
}
});
setSuggestions(suggestions);
FXMLLoader loader = new FXMLLoader(getClass().getResource("ClientControl.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException e) {
throw new RuntimeException(e);
}
initTextField();
}
My ClientCell class needed only some simple changes because of the changes in ClientControl:
public class NewClientCell extends TableCell<Client, Client> {
private final ClientControl cc;
public NewClientCell(ObservableList<Client> suggestions) {
cc = new ClientControl(suggestions);
this.setAlignment(Pos.CENTER_LEFT);
this.setGraphic(cc);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
#Override
protected void updateItem(Client c, boolean empty) {
super.updateItem(c, empty);
if(!empty){
cc.setClient(c);
}
}
}
In the main program nothing changed.
In conclusion I would like to thank Puce one more time, I stuck at this problem for many days...

JavaFX 8 Custom TableCell implementation is rendering three times

I've developed a simple custom TableCell to enable the edition of the values in a table. The behaviour of the component is show a BigDecimalTextField no matter if the user is editing or not that cell, it should enable the edition all the time. The component is working fine, there is only a strange problem: when the table is rendered, instead of show only a single line, three lines are shown:
The code of the component is this:
public class BigDecimalEditingCell extends TableCell {
private BigDecimalField spinner = new BigDecimalField(new BigDecimal("0.00"));
private ObjectProperty<BigDecimal> campoLigado = null;
public BigDecimalEditingCell() {
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
createField();
}
private void createField() {
spinner.setStepwidth(new BigDecimal("0.01"));
spinner.setMinValue(new BigDecimal("0.00"));
spinner.setFormat(NumberFormat.getInstance());
spinner.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
}
#Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
criarBind();
setGraphic(spinner);
setText(null);
}
private void criarBind() {
ObservableValue<BigDecimal> valor = getTableColumn().getCellObservableValue(getIndex());
if (valor != null) {
ObjectProperty<BigDecimal> propriedade = (ObjectProperty<BigDecimal>) valor;
if (campoLigado == null) {
spinner.numberProperty().bindBidirectional(propriedade);
campoLigado = propriedade;
} else if (campoLigado != propriedade) {
spinner.numberProperty().unbindBidirectional(campoLigado);
spinner.numberProperty().bindBidirectional(propriedade);
campoLigado = propriedade;
}
}
}
}
If I use the default TextFieldTableCell, the table is rendered correctly. I have another component (like this) that uses JavaFX's DatePicker and the same problem happens.
What I'm doing wrong?
ADDED
Here is the usage of this component:
public class ControladorPainelFormaPagamento extends ControladorPainelSubmeter {
#FXML
private TableView<ParcelaBean> tabela;
#FXML
private TableColumn<ParcelaBean, LocalDate> colunaVencimento;
#FXML
private TableColumn<ParcelaBean, BigDecimal> colunaValor;
private FormaPagamentoBean bean;
.
.
.
private void configurarColunaValor() {
colunaValor.setCellValueFactory(new PropertyValueFactory<ParcelaBean, BigDecimal>("valor"));
colunaValor.setCellFactory(new Callback<TableColumn<ParcelaBean, BigDecimal>, TableCell<ParcelaBean, BigDecimal>>() {
#Override
public TableCell<ParcelaBean, BigDecimal> call(TableColumn<ParcelaBean, BigDecimal> parcelaBeanStringTableColumn) {
return new BigDecimalEditingCell();
}
});
}
private void configurarColunaVencimento() {
colunaVencimento.setCellValueFactory(new PropertyValueFactory<ParcelaBean, LocalDate>("dataVencimento"));
}
public void carregar(ModoExibicao modoExibicao, FormaPagamentoBean formaPagamento) {
this.bean=formaPagamento;
tabela.setItems(bean.getParcelas());
.
.
.
}
.
.
.
}
I've checked, inclusive using debug, if there was more than one bean in the list used by the table. Every time only one was there.
I was looking at another table in the same system and noticed this: when there is no item in the table, no one row is rendered.
but when you add just one row, the table renders empty row until reach the height of the table (repair the light grey rows):
So, it was obvious that the extra rows were added by TableView, this way a simple null check solve the problem:
public class BigDecimalEditingCell extends TableCell {
private BigDecimalField element = new BigDecimalField(new BigDecimal("0.00"));
private ObjectProperty<BigDecimal> campoLigado = null;
#Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item,empty);
if (getIndex() < getTableView().getItems().size() && !empty) {
createField();
createBind();
setGraphic(element);
setText(null);
} else {
removeBind();
setGraphic(null);
}
}
.
.
.
}

Add table column with BeanItemContainer

I should add a column to a table that has a BeanItemContainer datasource.
This is my situation:
I hava an entity bean
#Entity
public class MyBean implements {
#Id
private Long id;
//other properties
}
Then in my vaadin panel i have this method
private Table makeTable(){
Table table = new Table();
tableContainer = new BeanItemContainer<MyBean>(MyBean.class);
table.setContainerDataSource(tableContainer);
table.setHeight("100px");
table.setSelectable(true);
return table;
}
Now, I want to add a column that should give me the ability to delete an item in this container.
How can i do?
You could create a ColumnGenerator which creates the button for you.
Have a look here.
Example:
Let's say we have a MyBean class:
public class MyBean {
private String sDesignation;
private int iValue;
public MyBean() {
}
public MyBean(String sDesignation, int iValue) {
this.sDesignation = sDesignation;
this.iValue = iValue;
}
public String getDesignation() {
return sDesignation;
}
public int getValue() {
return iValue;
}
}
We then can create a table with a generated column giving a button to delete the current item.
Table table = new Table();
BeanItemContainer<MyBean> itemContainer = new BeanItemContainer<MyBean>(MyBean.class);
table.setContainerDataSource(itemContainer);
table.addItem(new MyBean("A", 1));
table.addItem(new MyBean("B", 2));
table.addGeneratedColumn("Action", new ColumnGenerator() { // or instead of "Action" you can add ""
#Override
public Object generateCell(final Table source, final Object itemId, Object columnId) {
Button btn = new Button("Delete");
btn.addClickListener(new ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
source.removeItem(itemId);
}
});
return btn;
}
});
table.setVisibleColumns(new Object[]{"designation", "value", "Action"}); // if you added "" instead of "Action" replace it by ""
I would recommend using shourtcut instead:
table.addShortcutListener(new ShortcutListener("Delete", KeyCode.DELETE, null) {
#Override
public void handleAction(final Object sender,
final Object target) {
if (table.getValue() != null) {
// here send event to your presenter to remove it physically in database
// and then refresh the table
// or just call tableContainer.removeItem(itemId)
}
}
});
if you don't want shourtcuts you would need add the column, eg:
table.addContainerProperty("Delete", Button.class, null);
and then put there the button that would do the same action.

Categories