SwingTree extends AbstractSwingContainer implements XulTree { public <T> void setElements( Collection<T> elements ) { suppressEvents = true; this.elements = elements; this.getRootChildren().removeAll(); if ( table != null ) { CellEditor ce = table.getCellEditor(); if ( ce != null ) { ce.stopCellEditing(); } } if ( elements == null ) { if ( table != null ) { table.updateUI(); } else { tree.updateUI(); } changeSupport.firePropertyChange( "selectedRows", null, getSelectedRows() ); changeSupport.firePropertyChange( "absoluteSelectedRows", null, getAbsoluteSelectedRows() ); return; } try { if ( table != null ) { for ( T o : elements ) { XulTreeRow row = this.getRootChildren().addNewRow(); for ( int x = 0; x < this.getColumns().getChildNodes().size(); x++ ) { XulComponent col = this.getColumns().getColumn( x ); final XulTreeCell cell = (XulTreeCell) getDocument().createElement( "treecell" ); XulTreeCol column = (XulTreeCol) col; for ( InlineBindingExpression exp : ( (XulTreeCol) col ).getBindingExpressions() ) { logger .debug( "applying binding expression [" + exp + "] to xul tree cell [" + cell + "] and model [" + o + "]" ); String colType = column.getType(); if ( StringUtils.isEmpty( colType ) == false && colType.equalsIgnoreCase( "dynamic" ) ) { colType = extractDynamicColType( o, x ); } if ( colType == null ) { if ( StringUtils.isNotEmpty( exp.getModelAttr() ) ) { Binding binding = createBinding( (XulEventSource) o, exp.getModelAttr(), cell, exp.getXulCompAttr() ); if ( !this.editable ) { binding.setBindingType( Binding.Type.ONE_WAY ); } domContainer.addBinding( binding ); binding.fireSourceChanged(); } } else if ( ( colType.equalsIgnoreCase( "combobox" ) || colType.equalsIgnoreCase( "editablecombobox" ) ) && column.getCombobinding() != null ) { Binding binding = createBinding( (XulEventSource) o, column.getCombobinding(), cell, "value" ); binding.setBindingType( Binding.Type.ONE_WAY ); domContainer.addBinding( binding ); binding.fireSourceChanged(); binding = createBinding( (XulEventSource) o, ( (XulTreeCol) col ).getBinding(), cell, "selectedIndex" ); binding.setConversion( new BindingConvertor<Object, Integer>() { @Override public Integer sourceToTarget( Object value ) { int index = ( (Vector) cell.getValue() ).indexOf( value ); return index > -1 ? index : 0; } @Override public Object targetToSource( Integer value ) { return ( (Vector) cell.getValue() ).get( value ); } } ); domContainer.addBinding( binding ); binding.fireSourceChanged(); if ( colType.equalsIgnoreCase( "editablecombobox" ) ) { binding = createBinding( (XulEventSource) o, exp.getModelAttr(), cell, exp.getXulCompAttr() ); if ( !this.editable ) { binding.setBindingType( Binding.Type.ONE_WAY ); } else { binding.setBindingType( Binding.Type.BI_DIRECTIONAL ); } domContainer.addBinding( binding ); } } else if ( colType.equalsIgnoreCase( "checkbox" ) ) { if ( StringUtils.isNotEmpty( exp.getModelAttr() ) ) { Binding binding = createBinding( (XulEventSource) o, exp.getModelAttr(), cell, "value" ); if ( !this.editable ) { binding.setBindingType( Binding.Type.ONE_WAY ); } domContainer.addBinding( binding ); binding.fireSourceChanged(); } } else if ( colType != null && this.customEditors.containsKey( colType ) ) { Binding binding = createBinding( (XulEventSource) o, exp.getModelAttr(), cell, "value" ); binding.setBindingType( Binding.Type.BI_DIRECTIONAL ); domContainer.addBinding( binding ); binding.fireSourceChanged(); } else { if ( StringUtils.isNotEmpty( exp.getModelAttr() ) ) { Binding binding = createBinding( (XulEventSource) o, exp.getModelAttr(), cell, exp.getXulCompAttr() ); if ( !this.editable ) { binding.setBindingType( Binding.Type.ONE_WAY ); } domContainer.addBinding( binding ); binding.fireSourceChanged(); } } } if ( column.getDisabledbinding() != null ) { String prop = column.getDisabledbinding(); Binding bind = createBinding( (XulEventSource) o, column.getDisabledbinding(), cell, "disabled" ); bind.setBindingType( Binding.Type.ONE_WAY ); domContainer.addBinding( bind ); bind.fireSourceChanged(); } row.addCell( cell ); } } if ( popupListener == null ) { if ( StringUtils.isNotEmpty( newItemBinding ) ) { if ( isCollectionManaged() ) { popupListener = new PopupListener(); table.addMouseListener( popupListener ); table.getTableHeader().addMouseListener( popupListener ); } else { logger .error( "Operations associated with newitembinding attribute not allowed on an unbound collection. Refactor your model " + "to use a managed collection (for an example, see AbstractModelList)." ); } } } } else { for ( T o : elements ) { XulTreeRow row = this.getRootChildren().addNewRow(); addTreeChild( o, row ); } } if ( table != null ) { table.updateUI(); } else { setupTree(); tree.updateUI(); } suppressEvents = false; changeSupport.firePropertyChange( "selectedRows", null, getSelectedRows() ); changeSupport.firePropertyChange( "absoluteSelectedRows", null, getAbsoluteSelectedRows() ); } catch ( XulException e ) { logger.error( "error adding elements", e ); } catch ( Exception e ) { logger.error( "error adding elements", e ); } } SwingTree( Element self, XulComponent parent, XulDomContainer domContainer, String tagName ); JTable getTable(); JTree getTree(); int[] getActiveCellCoordinates(); XulTreeCols getColumns(); String getOnselect(); XulTreeChildren getRootChildren(); int getRows(); String getSeltype(); Object[][] getValues(); int getWidth(); void setWidth( int width ); boolean isDisabled(); boolean isEditable(); boolean isEnableColumnDrag(); boolean isHierarchical(); void setActiveCellCoordinates( int row, int column ); void setDisabled( boolean dis ); void setEditable( boolean edit ); void setEnableColumnDrag( boolean drag ); void setOnselect( final String select ); void setColumns( XulTreeCols columns ); void setRootChildren( final XulTreeChildren rootChildren ); void addTreeRow( XulTreeRow row ); void removeTreeRows( int[] rows ); void setRows( int rows ); void setSeltype( String type ); void setTableModel( TableModel model ); @Override void layout(); @Override void replaceChild( XulComponent oldElement, XulComponent newElement ); @Override Object getManagedObject(); int[] getSelectedRows(); int[] getAbsoluteSelectedRows(); void addRow( XulTreeRow row ); Object getData(); void setData( Object data ); void update(); void clearSelection(); void setSelectedRows( int[] rows ); String getOnedit(); void setOnedit( String onedit ); void setElements( Collection<T> elements ); Collection<T> getElements(); Object getSelectedItem(); void setExpanded( boolean expanded ); boolean getExpanded(); void registerCellEditor( String key, org.pentaho.ui.xul.util.TreeCellEditor editor ); void registerCellRenderer( String key, TreeCellRenderer renderer ); void setBoundObjectExpanded( Object o, boolean expanded ); void setTreeItemExpanded( XulTreeItem item, boolean expanded ); void collapseAll(); void expandAll(); Collection<T> getSelectedItems(); void setSelectedItems( Collection<T> items ); int findIndexOfItem( Object o ); boolean isHiddenrootnode(); void setHiddenrootnode( boolean hidden ); String getCommand(); void setCommand( String command ); boolean isPreserveexpandedstate(); void setPreserveexpandedstate( boolean preserve ); boolean isSortable(); void setSortable( boolean sort ); boolean isTreeLines(); void setTreeLines( boolean visible ); void setNewitembinding( String binding ); String getNewitembinding(); void setAutocreatenewrows( boolean auto ); boolean getAutocreatenewrows(); boolean isPreserveselection(); void setPreserveselection( boolean preserve );  }
@Test public void testSetElements() { List<Person> peeps = new ArrayList<Person>(); Person perp = new Person(); perp.enabled = true; perp.name = "Wil"; perp.city = "Narcossee"; perp.titles = new Vector(); perp.titles.addAll( Arrays.asList( new String[] { "Blogger", "Actor", "Static coder" } ) ); perp.disposition = "Feeling it"; peeps.add( perp ); perp = new Person(); perp.enabled = false; perp.name = "Mike"; perp.city = "St Cloud"; perp.titles = new Vector(); perp.titles.addAll( Arrays.asList( new String[] { "Add clicker", "Prettiest man in OSS", "GWT ninja" } ) ); perp.disposition = "Weary"; peeps.add( perp ); tree3.setElements( peeps ); Object[][] sampleData = new Object[2][5]; sampleData[0] = new Object[] { Boolean.TRUE, "Wil", "Narcossee", "Blogger", "Feeling it" }; sampleData[1] = new Object[] { Boolean.FALSE, "Mike", "St Cloud", "Add clicker", "Weary" }; Object[][] vals = tree3.getValues(); for ( int i = 0; i < sampleData.length; i++ ) { for ( int y = 0; y < sampleData[i].length; y++ ) { assertTrue( sampleData[i][y].equals( vals[i][y] ) ); } } }