Updating a BeanItemContainer

I missing something about using BeanItemContainers. I have created a container and filled it with a number of Beans. I would like to be able to update the contents of those beans but when I try to do that, I seem to destroy the container. I tried this:

             BeanItemContainer<RawRRDData> bic = null;
	Object oid = null;
	RawRRDData rawRRD = null;
	Vector<RawRRDData> vRRD = new Vector<RawRRDData>();
	int idx = 0;
	
	long rTime = 0L;
	double rValue = 0.0;
	
	for(int i = 0; i < 10; i++){
		rTime = i * 1000;
		rValue = i * 2.0;
		rawRRD = new RawRRDData(null, rTime, rValue);
		vRRD.add(rawRRD);
	}
	
	bic = new BeanItemContainer<RawRRDData>(RawRRDData.class, vRRD);
	
	Logger.info("There are " + bic.size() + " items in BIC");
	
	idx = 0;
	for(oid = bic.firstItemId(); oid != null; oid = bic.nextItemId(oid)){
		rawRRD = bic.getItem(oid).getBean();
		Logger.info("" + idx + ")\tTime: " + rawRRD.getRTime() + "\tValue: " + rawRRD.getRValue());
		idx++;
	}
	
	idx = 0;
	Logger.info("Now negate the data");
	for(oid = bic.firstItemId(); oid != null; oid = bic.nextItemId(oid)){
		rawRRD = bic.getItem(oid).getBean();
		rValue = rawRRD.getRValue() * (-1.0);
		rawRRD.setRValue(rValue);
		Logger.info("" + idx + ")\tTime: " + rawRRD.getRTime() + "\tValue: " + rawRRD.getRValue());
		idx++;
	}
	
	Logger.info("Data has been negated");

And the output is:

There are 10 items in BIC
0) Time: 0 Value: 0.0

  1. Time: 1000 Value: 2.0
  2. Time: 2000 Value: 4.0
  3. Time: 3000 Value: 6.0
  4. Time: 4000 Value: 8.0
  5. Time: 5000 Value: 10.0
  6. Time: 6000 Value: 12.0
  7. Time: 7000 Value: 14.0
  8. Time: 8000 Value: 16.0
  9. Time: 9000 Value: 18.0
    Now negate the data
  10. Time: 0 Value: -0.0
    Data has been negated

So I’m doing something wrong in trying to access the Bean data - can someone tell me what the correct procedure would be to do this?

Thanks,

nbc

Your code (with some edits) works for me. I suspect you have some typo or problem in your code.

Below is source code for a complete example. Simply paste into the main source file for a new Vaadin project as created by the Vaadin plugin for Eclipse, running Vaadin 6.7.1.

Minor issues:
• I used different case in naming of getters/setters as automatically created by Eclipse’s Source > Generate Getters and Setters command. Ex: setrValue() rather than your setRValue().
• Your syntax for calling Logger did not work for me, giving errors related to static references. I changed by adding a ‘logger’ member to the class.
• I suggest simply using ‘System.out.println’ calls rather than Logger for this kind of simple testing.

Here’s the output.

INFO: ******* Running buttonClick method at Sun Nov 06 12:57:11 PST 2011
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: There are 10 items in BIC…
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 0)	Time: 0	Value: 0.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 1)	Time: 1000	Value: 2.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 2)	Time: 2000	Value: 4.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 3)	Time: 3000	Value: 6.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 4)	Time: 4000	Value: 8.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 5)	Time: 5000	Value: 10.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 6)	Time: 6000	Value: 12.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 7)	Time: 7000	Value: 14.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 8)	Time: 8000	Value: 16.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 9)	Time: 9000	Value: 18.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: **** Now negate the data…
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 0)	Time: 0	Value: -0.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 1)	Time: 1000	Value: -2.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 2)	Time: 2000	Value: -4.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 3)	Time: 3000	Value: -6.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 4)	Time: 4000	Value: -8.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 5)	Time: 5000	Value: -10.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 6)	Time: 6000	Value: -12.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 7)	Time: 7000	Value: -14.0[code]

Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 8) Time: 8000 Value: -16.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: 9) Time: 9000 Value: -18.0
Nov 6, 2011 12:57:11 PM com.example.trashme.TrashmeApplication buttonClick
INFO: Data has been negated.
[/code]

Here’s the complete example source code:

package com.example.trashme;

import java.util.Vector;
import java.util.logging.Logger;
import java.util.UUID;

import com.vaadin.Application;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;

/**
 * @author Basil Bourque
 *
 * For use as main application code for a Vaadin 6.7.1 app.
 * 
 * Adapted code by Neil B. Cohen to answer his post to this forum:
 * https://vaadin.com/forum/-/message_boards/view_message/875653
 *
 */
public class TrashmeApplication extends Application implements Button.ClickListener {
    // Obtain a suitable logger.
    private Logger logger = Logger.getLogger(TrashmeApplication.class.getName());
    
	@Override
	public void init() {
		Window mainWindow = new Window("Trashme Application");
		Label label = new Label("Hello Vaadin user");
		
		Button testButton = new Button("Test BeanItemContainer");
		testButton.addListener(this);
		
		mainWindow.addComponent(label);
		mainWindow.addComponent(testButton);
		setMainWindow(mainWindow);
	}

    @Override
    public void buttonClick( ClickEvent event ) {
        logger.info( "" );
        logger.info("******* Running buttonClick method at " + new java.util.Date() );
        logger.info( "" );
        
        BeanItemContainer<RawRRDData> bic = null;
        Object oid = null;
        RawRRDData rawRRD = null;
        Vector<RawRRDData> vRRD = new Vector<RawRRDData>();
        int idx = 0;

        long rTime = 0L;
        double rValue = 0.0;

        for(int i = 0; i < 10; i++){
        rTime = i * 1000;
        rValue = i * 2.0;
        rawRRD = new RawRRDData(null, rTime, rValue);
        vRRD.add(rawRRD);
        }

        bic = new BeanItemContainer<RawRRDData>(RawRRDData.class, vRRD);

        logger.info("There are " + bic.size() + " items in BIC…");

        idx = 0;
        for(oid = bic.firstItemId(); oid != null; oid = bic.nextItemId(oid)){
        rawRRD = bic.getItem(oid).getBean();
        logger.info("" + idx + ")\tTime: " + rawRRD.getrTime() + "\tValue: " + rawRRD.getrValue());
        idx++;
        }

        idx = 0;
        logger.info("");
        logger.info("**** Now negate the data…");
        for(oid = bic.firstItemId(); oid != null; oid = bic.nextItemId(oid)){
        rawRRD = bic.getItem(oid).getBean();
        rValue = rawRRD.getrValue() * (-1.0);
        rawRRD.setrValue(rValue);
        logger.info("" + idx + ")\tTime: " + rawRRD.getrTime() + "\tValue: " + rawRRD.getrValue());
        idx++;
        }

        logger.info("Data has been negated.");
    }

}

class RawRRDData {
    UUID uuid = null;
    long rTime = 0L;
    double rValue = 0.0;
    
    /**
     * Constructor 
     * 
     * @param uuid
     * @param rTime
     * @param rValue
     */
    public RawRRDData( UUID uuid, long rTime, double rValue ) {
        super();
        if(uuid == null) {
            this.uuid = UUID.randomUUID();
        }
        this.rTime = rTime;
        this.rValue = rValue;
    }
    
    // Getters & Setters for Bean
    
    /**
     * @return the uuid
     */
    public UUID getUuid() {
        return this.uuid;
    }
    /**
     * @param uuid the uuid to set
     */
    public void setUuid( UUID uuid ) {
        this.uuid = uuid;
    }
    /**
     * @return the rTime
     */
    public long getrTime() {
        return this.rTime;
    }
    /**
     * @param rTime the rTime to set
     */
    public void setrTime( long rTime ) {
        this.rTime = rTime;
    }
    /**
     * @return the rValue
     */
    public double getrValue() {
        return this.rValue;
    }
    /**
     * @param rValue the rValue to set
     */
    public void setrValue( double rValue ) {
        this.rValue = rValue;
    }
    
}

Fascinating - thanks very much… I will take a closer look and see what makes your code different from mine. One note - the Logger object in my code is a member of a library logging class we wrote in-house - it basically invokes Log4J, but it has a few other internal options…

Much obliged,

nbc