In order to make GenericTableView work in BXML at all, you'd need to move the Class<T> argument from the constructor to a property. You could do something like:
public Class<?> getBeanType(); public void setBeanType(Class<?> beanType); public void setBeanType(String beanType); The String overload of setBeanType() would simply call class.forName() internally, so you could do this in your BXML: <GenericTableView beanType="com.foo.MyBean"/> Alternatively, since it is really just being used to initialize the columns, perhaps this would be more appropriate: public void setColumns(Class<?> beanType); public void setColumns(String beanType); Then you could potentially attach annotations to each property (via the getter method) to return things like default column width, etc. G On Jan 4, 2011, at 12:47 PM, Gerrick Bivins wrote: > +1 for this example! > I had been thinking about something like this as well to simplify creating > table views for java Beans, ie, dynamically creating the table columns based > on properties. This addresses part of what I was trying to do. > What would really be slick is if that Class parameter in the constructor > could be made Bindable so it could dynamically specified via BXML ala: > > ... > <bxml:define> > <Class bxml:id="beanClass"> > </bxml:define> > ... > <GenericTableView class="$beanClass" ...> > ... > > > Gerrick > > > On Tue, Jan 4, 2011 at 11:11 AM, calathus <[email protected]> wrote: > > > On Mon, Dec 20, 2010 at 5:54 PM, Greg Brown <[email protected]> wrote: > > My goal was to create a generic class to support CRUD GUI from a given Java > > Beans class. Although it is not generic version, Vaadin provides such > > sample implementation which may be immediately used for real projects. > > I'd be interested in learning more about how you envision something like this > might work. > > Greg, > > I created a sample generic TableView class which can take a parameter bean > class and using the reflection library, it defines table view's column > fields. This class is modified from tutorials tableviews sample. > This is only for demonstration of the generic class approach in Pivot using > builder class approach. > > In general, if we introduce new annotations for entity beans class to define > more presentation related information (like width of column) , we can fine > tune the look and feels. > And the same entity beans can have another JPA annotation from which DB > scheme can be generated and also some utility (like in netbeans) will allow > to generate restful API from the entity beans. > > So if we define more GUI feature to support CRUD operation as generic class > library, it become very simple to develop DB based web application. > There is an open source project called openxava which has similar approach, > but its GUI is based on JSP and not so impressive compared to other GUI(web) > projects. > The degree of usability of such generic library may not be so general, but if > there are a lot of entity classes, this type of approach would be quite > useful. > Also the pattern of these class may be used as starting point for other type > of CRUD GUI. > > Following is the sample code: > > public static class GenericTableView<T> extends TableView { > > private final Class<T> cls; > > public GenericTableView(final Class<T> cls, final Map<String, Object> > namespace) throws Exception { > this.cls = cls; > > for (final Field field: cls.getFields()) { > final String fname = field.getName(); > getColumns().add(new TableView.Column() {{ > setName(fname); > setWidth(3, true); > if (fname.equals("flag")) { > setCellRenderer(new TableViewImageCellRenderer() {{ > }}); // INSTANCE, name: > <content:TableViewImageCellRenderer> > } else { > setHeaderData(getCapitalName(fname)); > } > System.out.println(">>> fname: "+fname); > }}); > } > getTableViewSortListeners().add(new > TableViewSortListener.Adapter() { > @Override > @SuppressWarnings("unchecked") > public void sortChanged(TableView tableView) { > tableView.getTableData().setComparator(new > org.apache.pivot.wtk.content.TableViewRowComparator(tableView)); > } > }); > } > > public void add(T... ts) { > for (T t: ts) { > getTableData().add(t); > } > } > > static String getCapitalName(String name) { > if (name.length() == 0) return name; > return Character.toUpperCase(name.charAt(0))+name.substring(1); > } > } > > static java.util.List<OlympicStanding> createOlympicStandings() throws > Exception { > java.util.List<OlympicStanding> ts = new > java.util.ArrayList<OlympicStanding>(); > > // tableViewSortListeners(): LISTENER_LIST_PROPERTY > ts.add(new OlympicStanding() {{ > setNation("China"); > setGold(51); > setSilver(21); > setBronze(28); > setFlag(new > URL("file:/share/workspace/pivot/tutorials/src/org/apache/pivot/tutorials/tableviews/cn.png")); > }}); // INSTANCE, name: <tableviews:OlympicStanding> > ts.add(new OlympicStanding() {{ > setNation("United States"); > setGold(36); > setSilver(38); > setBronze(36); > setFlag(new > URL("file:/share/workspace/pivot/tutorials/src/org/apache/pivot/tutorials/tableviews/us.png")); > }}); // INSTANCE, name: <tableviews:OlympicStanding> > return ts; > } > > static Window create() throws Exception { > return new Window() {{ > final Map<String, Object> namespace = new HashMap<String, > Object>(); > setTitle("Table Views"); > setMaximized(true); > setContent(new Border() {{ > setContent(new ScrollPane() {{ > > setHorizontalScrollBarPolicy(ScrollPane.ScrollBarPolicy.FILL); > setView(new > GenericTableView<OlympicStanding>(OlympicStanding.class, namespace){{ > namespace.put("tableView", this); > > for (OlympicStanding os: createOlympicStandings()) { > add(os); > } > }}); // INSTANCE, name: <TableView> > // columnHeader(): WRITABLE_PROPERTY > setColumnHeader(new TableViewHeader() {{ > setTableView((TableView)namespace.get("tableView")); > setSortMode(TableViewHeader.SortMode.MULTI_COLUMN); > }}); // INSTANCE, name: <TableViewHeader> > }}); // INSTANCE, name: <ScrollPane> > }}); // INSTANCE, name: <Border> > CodeEmitterRuntime.initialize(this, namespace); > }}; > } > > > > > I wondered if Pivot is really designed to support normal Java class based > > GUI implementation. > > It most certainly is. :-) BXML is just a shortcut to coding your UI by > hand. Anything you can do in BXML, you can do in Java (though, in many cases, > not quite as conveniently). > > > Regarding to the translator, since there is no detail sample how to use > > these Java API directly, it would be helpful if we have such bxml to Java > > translator. > > But ideally, Pivot site should include more detailed sample/explanation for > > Java Pivot API based approach(without bxml). Then we would not need such a > > tool. > > If you read the BXML Primer, you should have a good understanding of how BXML > maps to Java. There's no magic to it - it is very straightforward. > > > Also if we may really have declarative GUI design, using Scala may be more > > attractive way. Scala would allow declaring GUI in equivalent code side as > > BXML. > > I'm not sure how this would work. Scala is conceptually more akin to Java > than markup. Could you elaborate? > > > BTW, I still wonder where the following code went wrong. I would > > appreciate your suggestion for the following code(java verson of > > custom_table_view.bxml). > > When it is run, it opens the applet window, but it does not show anything. > > Two errors: > > - You need to call border.setContent(scrollPane), not border.add(scrollPane). > > - You need to call scrollPane.setView(tableView). Otherwise, the scroll pane > won't know what it's content is. > > I made these changes and the app worked fine. > > G > > > > > > -- > Cheers, > calathus > > > >
