package com.infragistics.jsf.v81.webgrid.functionality; import java.util.Date; import java.util.List; import javax.faces.component.UICommand; import javax.faces.context.FacesContext; import javax.faces.event.ActionEvent; import com.infragistics.data.ColumnMetaData; import com.infragistics.data.DAO; import com.infragistics.data.DataSource; import com.infragistics.data.Person; import com.infragistics.faces.grid.component.Column; import com.infragistics.faces.grid.component.GridView; import com.infragistics.faces.shared.BaseWebGridPage; public class DynamicChangingBean extends BaseGridPage { private final static short ADD_DATA_SOURCE = 1; private final static short REMOVE_DATA_SOURCE = 2; private final static short CHANGE_DATA_SOURCE = 3; private static int addingId = 8000; private static int changedCount = 0; private static int removedCount = 0; private short actionType = 0; private DataSource dataSource; private UICommand addButton = null; private UICommand removeButton = null; private UICommand changeButton = null; public void onDataSourceChange(ActionEvent event) { String value = (String) ((UICommand) event.getSource()).getValue(); if ("Add Data".equals(value)) { actionType = ADD_DATA_SOURCE; } else if ("Remove Data".equals(value)) { actionType = REMOVE_DATA_SOURCE; } else if ("Change Data".equals(value)) { actionType = CHANGE_DATA_SOURCE; } // initGrid(); } public void setGrid(GridView grid) { this.grid = grid; // if the grid is empty, it probably means that it hasn't been // initialized yet if (grid.getChildren().isEmpty()) { initGrid(); } } private DataSource getDataSource() { if (dataSource == null) { dataSource = DAO.getDataSourceEmployees(); } switch (actionType) { case 1: { addToDataSource(dataSource); break; } case 2: { removeFromDataSource(dataSource); break; } case 3: { changeDataSource(dataSource); break; } } actionType = 0; return dataSource; } private void addToDataSource(DataSource dSource) { Person employee = new Person(addingId, "Added" + addingId, "Added" + addingId, "Added" + addingId, "Added" + addingId, "Added" + addingId, "Added" + addingId, new Date(), true, addingId); addingId++; ((List) dSource.getRows()).add(employee); } private void removeFromDataSource(DataSource dSource) { ((List) dSource.getRows()).remove(0); removedCount++; } private void changeDataSource(DataSource dSource) { changedCount++; Person employee = (Person) ((List) dSource.getRows()).get(0); employee.setAge(changedCount); employee.setCity("Changed" + changedCount); employee.setCountry("Changed" + changedCount); employee.setEmail("Changed" + changedCount); } public Object getRows() { return getDataSource().getRows(); } /** * Clear a grid to rebind it later-on * */ private void clearGrid() { // clear the grid grid.setFooter(null); grid.setHeader(null); grid.getChildren().clear(); grid.getTemplateItems().clear(); grid.setColumnPositions(null); } private void initGrid() { // Thread.dumpStack(); FacesContext context = FacesContext.getCurrentInstance(); // clear the grid clearGrid(); grid.setAllowFixedColumns(true); grid.setFixedColumnCount(2); // loop through the meta-data defining the columns in the dataModel // and create a column in the grid for basic data type dataSource = getDataSource(); if (dataSource != null) { ColumnMetaData[] columns = dataSource.getColumMetaData(); for (int i = 0; i < columns.length; i++) { ColumnMetaData columnMetaData = columns[i]; if (columnMetaData != null) { if (columnMetaData.getDatatype() != List.class) { // create a column Column columnName = createColumn(context, columnMetaData.getDisplayName(), "#{DATA_ROW." + columnMetaData.getName() + "}", columnMetaData.getName()); grid.getTemplateItems().add(columnName); } } } } // ask the grid to rebind itself to its datasource // grid.dataBind(); } public UICommand getAddButton() { return addButton; } public void setAddButton(UICommand addButton) { this.addButton = addButton; } public UICommand getRemoveButton() { return removeButton; } public void setRemoveButton(UICommand removeButton) { this.removeButton = removeButton; } public UICommand getChangeButton() { return changeButton; } public void setChangeButton(UICommand changeButton) { this.changeButton = changeButton; } }