001: package org.osbl.client.wings.form.editor;
002:
003: import org.osbl.client.wings.form.editor.OneToManyCompositionEditor;
004: import org.osbl.client.wings.form.GenericObjectTableModel;
005: import org.osbl.client.wings.shell.Client;
006: import org.wings.SComponent;
007: import org.wings.SListSelectionModel;
008: import org.wings.table.STableColumnModel;
009: import org.conform.*;
010: import org.conform.wings.ComponentFactory;
011: import org.conform.wings.PropertyEditableCellRenderer;
012: import org.wingx.table.XTableColumnModel;
013: import org.wingx.table.EditableTableCellRenderer;
014:
015: import javax.swing.*;
016: import java.util.*;
017: import java.awt.event.ActionEvent;
018:
019: public class OneToManyInlineEditor extends OneToManyCompositionEditor {
020: public SComponent createComponent(PropertyMeta propertyMeta) {
021: TablePanel tablePanel = new InlineTablePanel(propertyMeta);
022: configureComponent(propertyMeta, tablePanel, false);
023: return tablePanel;
024: }
025:
026: class InlineTablePanel extends TablePanel {
027: InlineTablePanel(PropertyMeta propertyMeta) {
028: super (propertyMeta);
029: }
030:
031: public void setPropertyData(PropertyData propertyData) {
032: this .propertyData = propertyData;
033:
034: PropertyMeta propertyMeta = propertyData.getPropertyMeta();
035: BeanMeta relationBeanMeta = propertyMeta.getRelationBean();
036: if (relationBeanMeta == null)
037: relationBeanMeta = Client.getInstance()
038: .getBeanMetaProvider().getBeanMeta(
039: propertyMeta.getType());
040:
041: List<String> readableProperties = new ArrayList<String>();
042: for (PropertyMeta property : relationBeanMeta
043: .getProperties()) {
044: if (property.isReadable())
045: readableProperties.add(property.getName());
046: }
047: InlineTablePanel.InlineTableModel inlineTableModel = new InlineTablePanel.InlineTableModel(
048: relationBeanMeta, readableProperties
049: .toArray(new String[readableProperties
050: .size()]));
051: inlineTableModel.setList((List) propertyData.getValue());
052: setTableModel(inlineTableModel);
053: setTableColumnModel(inlineTableModel.getColumnModel());
054: table
055: .setSelectionMode(SListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
056: }
057:
058: protected List<? extends EditorAction> createActions() {
059: return Arrays.asList(new AddInlineAction(),
060: new DeleteInlineAction());
061: }
062:
063: public void setTableColumnModel(STableColumnModel columnModel) {
064: table.setColumnModel(columnModel);
065: }
066:
067: private class InlineTableModel extends GenericObjectTableModel {
068: public InlineTableModel(BeanMeta beanMeta,
069: String... propertyNames) {
070: super (beanMeta, propertyNames);
071: XTableColumnModel columnModel = getColumnModel();
072: for (String propertyName : propertyNames) {
073: PropertyMeta propertyMeta = beanMeta
074: .getProperty(propertyName);
075: if (propertyMeta.isWritable()) {
076: org.conform.wings.Editor editor = ComponentFactory.INSTANCE
077: .getEditor(propertyMeta);
078: EditableTableCellRenderer editableCellRenderer = new PropertyEditableCellRenderer(
079: editor, propertyMeta);
080: columnModel.getColumn(propertyName)
081: .setCellRenderer(editableCellRenderer);
082: }
083: }
084: }
085:
086: public void setValueAt(Object value, int rowIndex,
087: int columnIndex) {
088: super .setValueAt(value, rowIndex, columnIndex);
089: propertyData.getBeanData().revalidate();
090: }
091:
092: public void fireTableDataChanged() {
093: super .fireTableDataChanged();
094: propertyData.getBeanData().revalidate();
095: }
096: }
097: }
098:
099: static private class AddInlineAction extends AbstractEditorAction {
100: public AddInlineAction() {
101: putValue(Action.ACTION_COMMAND_KEY,
102: "businessobject.buttons.add");
103: }
104:
105: public void actionPerformed(ActionEvent event) {
106: GenericObjectTableModel model = (GenericObjectTableModel) ((InlineTablePanel) getComponent())
107: .getTableModel();
108: List list = model.getList();
109: list.add(newInstance());
110: model.fireTableRowsInserted(list.size() - 1,
111: list.size() - 1);
112: }
113:
114: protected Object newInstance() {
115: GenericObjectTableModel model = (GenericObjectTableModel) ((InlineTablePanel) getComponent())
116: .getTableModel();
117: try {
118: return model.getBeanMeta().getType().newInstance();
119: } catch (Exception e) {
120: throw new RuntimeException(e);
121: }
122: }
123: }
124:
125: static private class DeleteInlineAction extends
126: AbstractEditorAction {
127: public DeleteInlineAction() {
128: putValue(Action.ACTION_COMMAND_KEY,
129: "businessobject.buttons.delete");
130: }
131:
132: public void actionPerformed(ActionEvent event) {
133: GenericObjectTableModel model = (GenericObjectTableModel) ((InlineTablePanel) getComponent())
134: .getTableModel();
135: List list = model.getList();
136: int[] indices = ((InlineTablePanel) getComponent()).table
137: .getSelectedRows();
138: for (int i = indices.length - 1; i > -1; i--) {
139: int index = indices[i];
140: list.remove(index);
141: }
142: model.fireTableDataChanged();
143: }
144: }
145: }
|