Alejandro,
One step further than hard-coding the formatting in is to use a string template as a property on your renderer or use an easy form of templates. a) If you want to customize the string shown, your renderer can use a string template library and your renderer, when created, can have a property on it for the format string to use. This way you can change the displayed content using the format string in BXML instead of code. Spring has a string "template" library you can use and does not require all of spring to use. Others libs exist. b) Another neat trick is to have your renderer load a BXML file itself and add the pojo to the namespace after loading the BXML. The BXML can bind to the pojo using standard BXML scripting. This is a "template" and allows you to configure the rendered object externally in BXML then attach it using the renderer syntax below. Your item renderer could have a property called "templateLocation" on it for the location of the BXML resource to load and your renderer would place the object to be rendered in the namespace using a published "name" e.g. "myObject' or "content" so its easy to find in your BXML script. This is more advanced but you would not have to write another From: Alejandro Vilar [mailto:[email protected]] Sent: Monday, August 02, 2010 1:11 PM To: [email protected] Subject: RE: ListButton - Is it the correct way? Hi again, Related to all renderers in pivot bxml files, it will possible to use a some kind of reflection to render some data? (specially with text type data) For example: <ListButton render="${name} - ${address}"/> And use reflection to get fields inside curly brackets, also it will be helpful with resources files to customize rendered data by a custom localization: <ListButton render="%renderers.customer"/> In my current project we have about 15 POJOs with list, data and table renderers each one. <ListButton> <dataRenderer> <renderers:CustomerButtonRenderer/> </dataRenderer> <itemRenderer> < renderers:CustomerItemRenderer/> </itemRenderer> </ListButton> It's a little verbose. Regards, Alejandro From: Alejandro Vilar [mailto:[email protected]] Sent: lunes, 02 de agosto de 2010 12:47 p.m. To: [email protected] Subject: RE: ListButton - Is it the correct way? Hi Stefano, Your approach works well because "toString" method only be called when a customer is painted, but it doesn't avoids some possible side effects in the rest of your code(i.e. logging). Another way is to use renderers as follows: import org.apache.pivot.wtk.Button; import org.apache.pivot.wtk.content.ButtonDataRenderer; public class CustomerDataRenderer extends ButtonDataRenderer { @Override public void render(Object data, Button button, boolean highlighted) { super.render(data, button, highlighted); if (data instanceof lbtCustomer) { lbtCustomer customer = (lbtCustomer) data; super.label.setText(customer.getName() + "-" + customer.getAddress()); } } } Also you can keep the computed name inside a variable in your lbtCustomer instance to compute it just once. To setup this renderer in your ListButton: ListButton listButton = new ListButton(customers); listButton.setDataRenderer(new CustomerDataRenderer()); listButton.setItemRenderer(new CustomerItemRenderer()); //<-- same as data renderer, but extending from ListViewItemRenderer If you want get the chosen record: lbtCutomer selectedCustomer = (lbtCutomer)listButton.getSelectedItem(); I'll suggest override equals method in your lbtCustomer class, some list abilities are based on it. Hope this helps, Alejandro From: Dr. Stefano Sancese [mailto:[email protected]] Sent: lunes, 02 de agosto de 2010 11:20 a.m. To: [email protected] Subject: ListButton - Is it the correct way? Hi to all, in a form, I have a ListButton that I need to populate with 1000 customers data (Id, Name, Address). I wrote this class: class lbtCustomer { private String id; private String name; private String address; lbtCustomer(String c1, String c2, String c3) { id = c1; name = c2; address = c3; } public String getKey() { return id; } @Override public String toString() { return name + " - " + address; } } and I populated the ArrayList of the ListButton with: ArrayList lbtValues = new ArrayList(); lbtCustomer r1 = new lbtCustomer("1","John Doe","New York");; lbtCustomer r2 = new lbtCustomer("8","Charlie Brown","Los Angeles"); lbtCustomer r3 = new lbtCustomer("2","Donald Duck","Orlando"); lbtCustomer r4 = new lbtCustomer("9","Snoopy","Los Angeles"); lbtValues.add(r1); lbtValues.add(r2); lbtValues.add(r3); lbtValues.add(r4); listButtonTest.setListData(lbtValues); With the toString method I can format the information showed to the user and with the getKey method I can retrieve the Id of the chosen record. The test case works, but I wonder if there is a better way. I'm concerned about the cost (CPU and memory) associated with the instantiation of the 1000 objects that I need for the real case. Perhaps two array lists: one with the data to show to the user and the other to store the id of the corresponding Customer? Ciao Stefano P.s. I'm realy green to java and - YES - I'm Reading the F...... Manuals. There are simply too many of them ;-) -- Dr. Stefano Sancese WatchGuard Certified System Professional - http://www.watchguard.com Socio Clusit - Associazione Italiana per la Sicurezza Informatica ************************************************************************ In God we trust, all others we monitor (National Security Agency) ************************************************************************
