1 56 package org.objectstyle.cayenne.modeler.dialog; 57 58 import java.awt.BorderLayout ; 59 import java.awt.Container ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.util.ArrayList ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 66 import javax.swing.DefaultCellEditor ; 67 import javax.swing.JButton ; 68 import javax.swing.JComboBox ; 69 import javax.swing.JPanel ; 70 import javax.swing.JTable ; 71 import javax.swing.table.TableColumn ; 72 73 import org.objectstyle.cayenne.map.DbAttribute; 74 import org.objectstyle.cayenne.map.DbEntity; 75 import org.objectstyle.cayenne.map.DerivedDbAttribute; 76 import org.objectstyle.cayenne.map.DerivedDbEntity; 77 import org.objectstyle.cayenne.map.event.AttributeEvent; 78 import org.objectstyle.cayenne.modeler.Application; 79 import org.objectstyle.cayenne.modeler.editor.DerivedAttributeParamsTableModel; 80 import org.objectstyle.cayenne.modeler.util.CayenneDialog; 81 import org.objectstyle.cayenne.modeler.util.CayenneTable; 82 import org.objectstyle.cayenne.modeler.util.CayenneTableModel; 83 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 84 import org.objectstyle.cayenne.modeler.util.PanelFactory; 85 86 92 public class EditDerivedParamsDialog extends CayenneDialog implements ActionListener { 93 94 protected DerivedDbAttribute attr; 95 96 protected JTable table = new CayenneTable(); 97 protected JButton add = new JButton ("Add"); 98 protected JButton remove = new JButton ("Remove"); 99 protected JButton save = new JButton ("Save"); 100 protected JButton cancel = new JButton ("Cancel"); 101 102 105 public EditDerivedParamsDialog(DerivedDbAttribute attr) { 106 super(Application.getFrame(), "Edit Derived Attribute Parameters", true); 107 108 this.attr = attr; 109 110 init(); 111 pack(); 112 centerWindow(); 113 } 114 115 protected void init() { 116 Container pane = getContentPane(); 117 pane.setLayout(new BorderLayout ()); 118 119 buildTable(); 120 121 JPanel panel = 122 PanelFactory.createTablePanel( 123 table, 124 new JButton [] { add, remove, save, cancel }); 125 pane.add(panel, BorderLayout.CENTER); 126 127 add.addActionListener(this); 128 remove.addActionListener(this); 129 save.addActionListener(this); 130 cancel.addActionListener(this); 131 } 132 133 protected void buildTable() { 134 DerivedAttributeParamsTableModel model = 135 new DerivedAttributeParamsTableModel(attr, getMediator(), this); 136 table.setModel(model); 137 table.setRowHeight(25); 138 table.setRowMargin(3); 139 TableColumn nameCol = table.getColumnModel().getColumn(model.nameColumnInd()); 140 nameCol.setMinWidth(150); 141 142 TableColumn typeCol = table.getColumnModel().getColumn(model.typeColumnInd()); 143 typeCol.setMinWidth(90); 144 145 DbEntity parent = ((DerivedDbEntity) attr.getEntity()).getParentEntity(); 146 147 List list = new ArrayList (32); 148 list.add(""); 149 list.addAll(parent.getAttributeMap().keySet()); 150 String [] names = (String []) (list.toArray(new String [list.size()])); 151 152 JComboBox comboBox = CayenneWidgetFactory.createComboBox(names, true); 153 comboBox.setEditable(false); 154 nameCol.setCellEditor(new DefaultCellEditor (comboBox)); 155 } 156 157 160 public void actionPerformed(ActionEvent e) { 161 Object src = e.getSource(); 162 if (src == add) { 163 addRow(); 164 } 165 else if (src == remove) { 166 removeRow(); 167 } 168 else if (src == save) { 169 save(); 170 } 171 else if (src == cancel) { 172 cancel(); 173 } 174 } 175 176 protected void removeRow() { 177 DerivedAttributeParamsTableModel model = 178 (DerivedAttributeParamsTableModel) table.getModel(); 179 model.removeRow(model.getAttribute(table.getSelectedRow())); 180 } 181 182 protected void addRow() { 183 ((CayenneTableModel) table.getModel()).addRow(null); 184 } 185 186 protected void save() { 187 attr.clearParams(); 189 Iterator it = ((CayenneTableModel) table.getModel()).getObjectList().iterator(); 190 while (it.hasNext()) { 191 DbAttribute at = (DbAttribute) it.next(); 192 attr.addParam(at); 193 } 194 195 getMediator().fireDbAttributeEvent( 197 new AttributeEvent( 198 this, 199 attr, 200 attr.getEntity(), 201 AttributeEvent.CHANGE)); 202 203 setVisible(false); 204 } 205 206 protected void cancel() { 207 setVisible(false); 208 } 209 } 210 | Popular Tags |