Те, кто сталкивался с необходимостью использовать радиобаттоны в woodstockовских таблицах (да и не только), знают как это делается через TableSelectPhaseListener. Примерно так: Код | private TableSelectPhaseListener tablePhaseListener = new TableSelectPhaseListener();
public void setSelected(Object object) { RowKey rowKey = (RowKey)getValue("#{currentRow.tableRow}"); if (rowKey != null) { tablePhaseListener.setSelected(rowKey, object); } }
public Object getSelected(){ RowKey rowKey = (RowKey)getValue("#{currentRow.tableRow}"); return tablePhaseListener.getSelected(rowKey);
}
public Object getSelectedValue() { RowKey rowKey = (RowKey)getValue("#{currentRow.tableRow}"); return (rowKey != null) ? rowKey.getRowId() : null;
}
public boolean getSelectedState() { RowKey rowKey = (RowKey)getValue("#{currentRow.tableRow}"); return tablePhaseListener.isSelected(rowKey); }
|
И затем у таблицы прописываем что-то вроде этого: Код | <webuijsf:radioButton binding="#{Page1.radioButton1}" id="radioButton1" label="" name="#{Page1.radioButton1.id}"selected="#{Page1.selected}" selectedValue="#{Page1.selectedValue}"/> <webuijsf:tableColumn binding="#{Page1.tableColumn5}" id="tableColumn5" onClick="setTimeout('initAllRows()', 0)" selectId="#{Page1.radioButton1.id}"> <webuijsf:tableRowGroup binding="#{Page1.tableRowGroup1}" id="tableRowGroup1" rows="10" selected="#{Page1.selectedState}" sourceData="#{SessionBean1.users}" sourceVar="currentRow">
|
Такой подход работает как надо. А как быть если таблиц больше одной? На страницах forum.java.sun.com предлагают статично писать для каждой таблицы свой обработчик. А если у меня 20 таблиц? Писать 20 обработчиков? Я решил реализовать Factory Method по этому случаю. Код | public interface SelectionHandler { public RowSHandler getSelectionHandler(); }
|
Код | public interface RowSHandler { public RowSelectionHandler getRSH(String i, TableSelectPhaseListener tablePhaseListener); }
|
Код | public class RowHandler implements SelectionHandler, Serializable { public RowSHandler getSelectionHandler() { return new RowSelectionManager(); } private class RowSelectionManager extends AbstractSessionBean implements RowSHandler, Serializable { private transient RowSelectionHandler rsh; public RowSelectionHandler getRSH(String i, TableSelectPhaseListener tablePhaseListener) { rsh = new RowSelectionHandler(i, tablePhaseListener); return rsh; } } }
|
Код | public class RowSelectionHandler extends AbstractSessionBean { String i = null; private TableSelectPhaseListener tablePhaseListener; public RowSelectionHandler(String i, TableSelectPhaseListener tablePhaseListener) { this.i = i; this.tablePhaseListener = tablePhaseListener; } public void setSelected(Object object) { RowKey rowKey = (RowKey) getValue("#{currentRow".concat(i) + ".tableRow}"); if (rowKey != null) { tablePhaseListener.setSelected(rowKey, object); } } public Object getSelected() { System.out.println(i); RowKey rowKey = (RowKey) getValue("#{currentRow".concat(i) + ".tableRow}"); return tablePhaseListener.getSelected(rowKey); } public Object getSelectedValue() { RowKey rowKey = (RowKey) getValue("#{currentRow".concat(i) + ".tableRow}"); return (rowKey != null) ? rowKey.getRowId() : null; } public boolean getSelectedState() { RowKey rowKey = (RowKey) getValue("#{currentRow".concat(i) + ".tableRow}"); return tablePhaseListener.isSelected(rowKey); } }
|
Испльзую так: Код | RowSelectionHandler rowSelectionHandler;
public RowSelectionHandler getRowSelectionHandler() { RowHandler rh = new RowHandler(); rowSelectionHandler = rh.getSelectionHandler().getRSH(String.valueOf(getTableIndexCount() - 1), new TableSelectPhaseListener()); return rowSelectionHandler; }
public Table getTable(){
................................ TableRowGroup rowGroup = new TableRowGroup(); rowGroup.setSourceVar("currentRow".concat(/*индекс*/)); rowGroup.setValueExpression("selected", createValueExpression("#{DynamicTableCreator.selectedState}", Boolean.class)); table.getChildren().add(rowGroup); ...................................
RadioButton rb = new RadioButton(); rb.setValueExpression("selected", createValueExpression("#{DynamicTableCreator.selected}", Object.class)); rb.setValueExpression("selectedValue", createValueExpression("#{DynamicTableCreator.selectedValue}", Object.class)); .................................. }
|
При попытке выполнить: Код | public void deleteRow(ActionEvent e) { Table t = (Table) e.getComponent().getParent().getParent(); int selectedRows = t.getTableRowGroupChild().getSelectedRowsCount(); RowKey[] selectedRowKeys = t.getTableRowGroupChild().getSelectedRowKeys(); int rowId = Integer.parseInt(selectedRowKeys[0].getRowId()) + 1;
System.out.println("Row " + rowId + " is selected"); }
|
Вылетает java.lang.ArrayIndexOutOfBoundsException: 0, т.е. данные не биндятся . Что делаю не так?
--------------------
Thank you opensource.
|