1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.awt.BorderLayout ; 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.ActionListener ; 61 import java.util.EventObject ; 62 63 import javax.swing.DefaultCellEditor ; 64 import javax.swing.JButton ; 65 import javax.swing.JComboBox ; 66 import javax.swing.JPanel ; 67 import javax.swing.JToolBar ; 68 import javax.swing.event.ChangeEvent ; 69 import javax.swing.event.ListSelectionEvent ; 70 import javax.swing.event.ListSelectionListener ; 71 import javax.swing.table.TableColumn ; 72 73 import org.objectstyle.cayenne.dba.TypesMapping; 74 import org.objectstyle.cayenne.map.DbAttribute; 75 import org.objectstyle.cayenne.map.DbEntity; 76 import org.objectstyle.cayenne.map.DerivedDbAttribute; 77 import org.objectstyle.cayenne.map.DerivedDbEntity; 78 import org.objectstyle.cayenne.map.event.AttributeEvent; 79 import org.objectstyle.cayenne.map.event.DbAttributeListener; 80 import org.objectstyle.cayenne.modeler.Application; 81 import org.objectstyle.cayenne.modeler.ProjectController; 82 import org.objectstyle.cayenne.modeler.action.CreateAttributeAction; 83 import org.objectstyle.cayenne.modeler.action.DbEntitySyncAction; 84 import org.objectstyle.cayenne.modeler.action.RemoveAttributeAction; 85 import org.objectstyle.cayenne.modeler.dialog.EditDerivedParamsDialog; 86 import org.objectstyle.cayenne.modeler.event.AttributeDisplayEvent; 87 import org.objectstyle.cayenne.modeler.event.DbEntityDisplayListener; 88 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent; 89 import org.objectstyle.cayenne.modeler.util.CayenneTable; 90 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 91 import org.objectstyle.cayenne.modeler.util.ModelerUtil; 92 import org.objectstyle.cayenne.modeler.util.PanelFactory; 93 import org.objectstyle.cayenne.modeler.util.UIUtil; 94 95 101 public class DbEntityAttributeTab 102 extends JPanel 103 implements 104 DbEntityDisplayListener, 105 ListSelectionListener , 106 DbAttributeListener, 107 ExistingSelectionProcessor, 108 ActionListener { 109 110 protected ProjectController mediator; 111 protected CayenneTable table; 112 protected JButton editParams; 113 114 public DbEntityAttributeTab(ProjectController temp_mediator) { 115 super(); 116 mediator = temp_mediator; 117 mediator.addDbEntityDisplayListener(this); 118 mediator.addDbAttributeListener(this); 119 120 init(); 122 123 editParams.addActionListener(this); 124 } 125 126 private void init() { 127 this.setLayout(new BorderLayout ()); 128 129 JToolBar toolBar = new JToolBar (); 130 Application app = Application.getInstance(); 131 toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton()); 132 toolBar.add(app.getAction(DbEntitySyncAction.getActionName()).buildButton()); 133 134 toolBar.addSeparator(); 135 136 139 editParams = new JButton (); 140 editParams.setIcon(ModelerUtil.buildIcon("icon-info.gif")); 141 editParams.setToolTipText("Edit Parameters"); 142 toolBar.add(editParams); 143 144 toolBar.addSeparator(); 145 146 toolBar.add(app.getAction(RemoveAttributeAction.getActionName()).buildButton()); 147 148 add(toolBar, BorderLayout.NORTH); 149 150 table = new CayenneTable(); 152 add(PanelFactory.createTablePanel(table, null), BorderLayout.CENTER); 153 } 154 155 public void actionPerformed(ActionEvent e) { 156 if (e.getSource() == editParams) { 157 int row = table.getSelectedRow(); 158 if (row >= 0) { 159 DbAttribute attr = 160 ((DbAttributeTableModel) table.getModel()).getAttribute(row); 161 162 EditDerivedParamsDialog dialog = 163 new EditDerivedParamsDialog((DerivedDbAttribute) attr); 164 dialog.setVisible(true); 165 dialog.dispose(); 166 } 167 } 168 } 169 170 public void valueChanged(ListSelectionEvent e) { 171 processExistingSelection(e); 172 } 173 174 177 public void selectAttribute(DbAttribute attr) { 178 if (attr == null) { 179 Application.getInstance().getAction(RemoveAttributeAction.getActionName()).setEnabled(false); 180 return; 181 } 182 Application.getInstance().getAction(RemoveAttributeAction.getActionName()).setEnabled(true); 184 185 DbAttributeTableModel model = (DbAttributeTableModel) table.getModel(); 186 java.util.List attrs = model.getObjectList(); 187 int attrPos = attrs.indexOf(attr); 188 if (attrPos >= 0) { 189 table.select(attrPos); 190 } 191 } 192 193 public void processExistingSelection(EventObject e) { 194 if (e instanceof ChangeEvent ) { 195 table.clearSelection(); 196 } 197 DbAttribute att = null; 198 if (table.getSelectedRow() >= 0) { 199 DbAttributeTableModel model = (DbAttributeTableModel) table.getModel(); 200 att = model.getAttribute(table.getSelectedRow()); 201 editParams.setEnabled(att instanceof DerivedDbAttribute); 202 203 UIUtil.scrollToSelectedRow(table); 205 } 206 207 mediator.fireDbAttributeDisplayEvent( 208 new AttributeDisplayEvent( 209 this, 210 att, 211 mediator.getCurrentDbEntity(), 212 mediator.getCurrentDataMap(), 213 mediator.getCurrentDataDomain())); 214 } 215 216 public void dbAttributeChanged(AttributeEvent e) { 217 table.select(e.getAttribute()); 218 } 219 220 public void dbAttributeAdded(AttributeEvent e) { 221 rebuildTable((DbEntity) e.getEntity()); 222 table.select(e.getAttribute()); 223 } 224 225 public void dbAttributeRemoved(AttributeEvent e) { 226 DbAttributeTableModel model = (DbAttributeTableModel) table.getModel(); 227 int ind = model.getObjectList().indexOf(e.getAttribute()); 228 model.removeRow(e.getAttribute()); 229 table.select(ind); 230 } 231 232 public void currentDbEntityChanged(EntityDisplayEvent e) { 233 DbEntity entity = (DbEntity) e.getEntity(); 234 if (entity != null && e.isEntityChanged()) { 235 rebuildTable(entity); 236 } 237 238 if (e.isUnselectAttributes()) { 241 table.clearSelection(); 242 } 243 } 244 245 protected void rebuildTable(DbEntity ent) { 246 editParams.setVisible(ent instanceof DerivedDbEntity); 247 editParams.setEnabled(false); 248 249 DbAttributeTableModel model = 250 (ent instanceof DerivedDbEntity) 251 ? new DerivedDbAttributeTableModel(ent, mediator, this) 252 : new DbAttributeTableModel(ent, mediator, this); 253 table.setModel(model); 254 table.setRowHeight(25); 255 table.setRowMargin(3); 256 TableColumn col = table.getColumnModel().getColumn(model.nameColumnInd()); 257 col.setMinWidth(150); 258 259 col = table.getColumnModel().getColumn(model.typeColumnInd()); 260 col.setMinWidth(90); 261 262 String [] types = TypesMapping.getDatabaseTypes(); 263 JComboBox comboBox = CayenneWidgetFactory.createComboBox(types, true); 264 comboBox.setEditable(true); 265 col.setCellEditor(new DefaultCellEditor (comboBox)); 266 267 table.getSelectionModel().addListSelectionListener(this); 268 } 269 } 270 | Popular Tags |