001: /* $Id: OneToManyEditor.java 832 2007-02-05 08:49:01Z hengels $ */
002: package org.conform.wings.editor;
003:
004: import org.conform.*;
005: import org.conform.wings.Editor;
006: import org.conform.wings.PropertyAdapter;
007: import org.conform.wings.Table;
008: import org.wings.*;
009: import org.apache.commons.logging.LogFactory;
010:
011: import javax.swing.table.TableModel;
012: import java.awt.*;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ActionListener;
015: import java.util.Collection;
016: import java.util.List;
017:
018: public class OneToManyEditor implements Editor {
019: private static org.apache.commons.logging.Log LOG = LogFactory
020: .getLog(Editor.class);
021:
022: public SComponent createComponent(PropertyMeta propertyMeta) {
023: TablePanel tablePanel = new TablePanel();
024: tablePanel.setTableData(new TableModelPropertyAdapter(
025: propertyMeta));
026: configureComponent(propertyMeta, tablePanel, false);
027: return tablePanel;
028: }
029:
030: public void configureComponent(PropertyMeta propertyMeta,
031: SComponent component, boolean erroneous) {
032: TablePanel tablePanel = (TablePanel) component;
033: tablePanel.setEnabled(propertyMeta.isWritable());
034: tablePanel.setVisible(propertyMeta.isReadable());
035: }
036:
037: public void setPropertyData(SComponent component,
038: PropertyData propertyData) {
039: BeanData beanData = propertyData.getBeanData();
040: if (beanData != null)
041: beanData.addPropertyChangeListener(propertyData
042: .getPropertyMeta().getName(),
043: new ComponentInvalidator(component));
044: if (component instanceof TablePanel) {
045: TablePanel tablePanel = (TablePanel) component;
046: ((TableModelPropertyAdapter) tablePanel.getModel())
047: .setPropertyData(propertyData);
048: }
049: }
050:
051: public PropertyData getPropertyData(SComponent component) {
052: if (component instanceof TablePanel) {
053: TablePanel tablePanel = (TablePanel) component;
054: return ((TableModelPropertyAdapter) tablePanel.getModel())
055: .getPropertyData();
056: }
057:
058: return null;
059: }
060:
061: static class TableModelPropertyAdapter extends AbstractTableData
062: implements PropertyAdapter {
063: private PropertyData propertyData;
064: private PropertyMeta property;
065:
066: public TableModelPropertyAdapter(PropertyMeta property) {
067: this .property = property;
068:
069: BeanMeta relationBean = property.getRelationBean();
070: if (property.getRelationProperty() != null) {
071: String relationPropertyName = property
072: .getRelationProperty().getName();
073: LOG.debug(property + " possible backlink: "
074: + relationPropertyName);
075: relationBean = (BeanMeta) relationBean.clone();
076: PropertyMeta Property = (PropertyMeta) relationBean
077: .getProperty(relationPropertyName);
078: Property.setReadable(false);
079: }
080: setBeanMeta(relationBean);
081: }
082:
083: public PropertyData getPropertyData() {
084: return propertyData;
085: }
086:
087: public void setPropertyData(PropertyData propertyData) {
088: this .propertyData = propertyData;
089: }
090:
091: private TableData getTableData(PropertyData propertyData) {
092: Collection collection = (Collection) propertyData
093: .getValue();
094:
095: // make sure parent has collection
096: /*
097: if (collection == null) {
098: if (SortedSet.class.isAssignableFrom(property.getType()))
099: collection = new TreeSet();
100: else if (Set.class.isAssignableFrom(property.getType()))
101: collection = new HashSet();
102: else if (Collection.class.isAssignableFrom(property.getType()))
103: collection = new ArrayList();
104: else
105: logger.finest("na so was " + property.getType());
106: propertyData.setValue(property, collection);
107: }
108: */
109:
110: TableData tableData = (TableData) propertyData
111: .getRelationData();
112:
113: // make shure table data uses the actual collection if there is one
114: if (collection != null
115: && collection != tableData.getValue())
116: tableData.setValue(collection);
117:
118: return tableData;
119: }
120:
121: public void setValueAt(Object aValue, int rowIndex,
122: int columnIndex) {
123: getTableData(propertyData).getCellData(rowIndex,
124: columnIndex).setValue(aValue);
125: }
126:
127: public Object getValueAt(int rowIndex, int columnIndex) {
128: return getTableData(propertyData).getCellData(rowIndex,
129: columnIndex).getValue();
130: }
131:
132: public BeanData getRowData(int rowIndex) {
133: return getTableData(propertyData).getRowData(rowIndex);
134: }
135:
136: public int getRowCount() {
137: return getTableData(propertyData).getRowCount();
138: }
139:
140: public Object newRow() {
141: return getTableData(propertyData).newRow();
142: }
143:
144: public void addRow(Object object) {
145: getTableData(propertyData).addRow(object);
146: }
147:
148: public Object removeRow(int index) {
149: return getTableData(propertyData).removeRow(index);
150: }
151:
152: public List getRows() {
153: return getTableData(propertyData).getRows();
154: }
155:
156: public void setRows(List rows) {
157: getTableData(propertyData).setRows(rows);
158: }
159: }
160:
161: static class TablePanel extends SContainer implements
162: ActionListener {
163: private Table table;
164: private SButton add;
165: private SButton remove;
166:
167: public TablePanel() {
168: super (new SGridBagLayout());
169: GridBagConstraints constraints = new GridBagConstraints();
170:
171: add = new SButton(new SResourceIcon("actions/include.png"));
172: add.addActionListener(TablePanel.this );
173: constraints.gridwidth = GridBagConstraints.RELATIVE;
174: add(add, constraints);
175:
176: table = new Table();
177: constraints.gridwidth = GridBagConstraints.REMAINDER;
178: constraints.gridheight = 2;
179: add(table, constraints);
180:
181: remove = new SButton(new SResourceIcon(
182: "actions/exclude.png"));
183: remove.addActionListener(TablePanel.this );
184: constraints.gridwidth = GridBagConstraints.RELATIVE;
185: constraints.gridheight = 1;
186: add(remove, constraints);
187: }
188:
189: public void setTableData(AbstractTableData tableData) {
190: table.setTableData(tableData);
191: }
192:
193: public TableModel getModel() {
194: return table.getModel();
195: }
196:
197: public void setEnabled(boolean b) {
198: table.setEnabled(b);
199: add.setEnabled(b);
200: remove.setEnabled(b);
201: }
202:
203: public void actionPerformed(ActionEvent e) {
204: TableData tableData = (TableData) table.getModel();
205: if (add.equals(e.getSource())) {
206: tableData.newRow();
207: } else if (remove.equals(e.getSource())) {
208: int[] rows = table.getSelectedRows();
209: for (int i = 0; i < rows.length; i++) {
210: int row = rows[i];
211: tableData.removeRow(row);
212: }
213: if (tableData.getRowCount() == 0)
214: table.clearSelection();
215: }
216: }
217: }
218: }
|