1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.awt.BorderLayout ; 59 import java.awt.Color ; 60 import java.awt.Component ; 61 import java.util.EventObject ; 62 63 import javax.swing.DefaultCellEditor ; 64 import javax.swing.JComboBox ; 65 import javax.swing.JPanel ; 66 import javax.swing.JTable ; 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.DefaultTableCellRenderer ; 72 import javax.swing.table.TableColumn ; 73 74 import org.objectstyle.cayenne.map.ObjAttribute; 75 import org.objectstyle.cayenne.map.ObjEntity; 76 import org.objectstyle.cayenne.map.event.AttributeEvent; 77 import org.objectstyle.cayenne.map.event.EntityEvent; 78 import org.objectstyle.cayenne.map.event.ObjAttributeListener; 79 import org.objectstyle.cayenne.map.event.ObjEntityListener; 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.ObjEntitySyncAction; 84 import org.objectstyle.cayenne.modeler.action.RemoveAttributeAction; 85 import org.objectstyle.cayenne.modeler.event.AttributeDisplayEvent; 86 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent; 87 import org.objectstyle.cayenne.modeler.event.ObjEntityDisplayListener; 88 import org.objectstyle.cayenne.modeler.util.CayenneTable; 89 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 90 import org.objectstyle.cayenne.modeler.util.ModelerUtil; 91 import org.objectstyle.cayenne.modeler.util.PanelFactory; 92 import org.objectstyle.cayenne.modeler.util.UIUtil; 93 94 100 public class ObjEntityAttributeTab extends JPanel implements ObjEntityDisplayListener, 101 ObjEntityListener, ObjAttributeListener, ExistingSelectionProcessor { 102 103 protected ProjectController mediator; 104 protected CayenneTable table; 105 106 public ObjEntityAttributeTab(ProjectController mediator) { 107 this.mediator = mediator; 108 109 init(); 110 initController(); 111 } 112 113 private void init() { 114 this.setLayout(new BorderLayout ()); 115 116 JToolBar toolBar = new JToolBar (); 117 Application app = Application.getInstance(); 118 toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton()); 119 toolBar.add(app.getAction(ObjEntitySyncAction.getActionName()).buildButton()); 120 toolBar.addSeparator(); 121 toolBar.add(app.getAction(RemoveAttributeAction.getActionName()).buildButton()); 122 add(toolBar, BorderLayout.NORTH); 123 124 table = new CayenneTable(); 125 table.setDefaultRenderer(String .class, new CellRenderer ()); 126 127 add(PanelFactory.createTablePanel(table, null) , BorderLayout.CENTER); 128 } 129 130 private void initController() { 131 mediator.addObjEntityDisplayListener(this); 132 mediator.addObjEntityListener(this); 133 mediator.addObjAttributeListener(this); 134 135 table.getSelectionModel().addListSelectionListener(new ListSelectionListener () { 136 137 public void valueChanged(ListSelectionEvent e) { 138 processExistingSelection(e); 139 } 140 }); 141 } 142 143 146 public void selectAttribute(ObjAttribute attr) { 147 if (attr == null) { 148 Application.getInstance().getAction(RemoveAttributeAction.getActionName()).setEnabled(false); 149 return; 150 } 151 Application.getInstance().getAction(RemoveAttributeAction.getActionName()).setEnabled(true); 153 154 ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel(); 155 java.util.List attrs = model.getObjectList(); 156 int attrPos = attrs.indexOf(attr); 157 if (attrPos >= 0) { 158 table.select(attrPos); 159 } 160 } 161 162 public void processExistingSelection(EventObject e) { 163 if (e instanceof ChangeEvent ) { 164 table.clearSelection(); 165 } 166 ObjAttribute attribute = null; 167 if (table.getSelectedRow() >= 0) { 168 ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel(); 169 attribute = model.getAttribute(table.getSelectedRow()); 170 171 UIUtil.scrollToSelectedRow(table); 173 } 174 175 AttributeDisplayEvent ev = new AttributeDisplayEvent(this, attribute, mediator 176 .getCurrentObjEntity(), mediator.getCurrentDataMap(), mediator 177 .getCurrentDataDomain()); 178 179 mediator.fireObjAttributeDisplayEvent(ev); 180 } 181 182 public void objAttributeChanged(AttributeEvent e) { 183 table.select(e.getAttribute()); 184 } 185 186 public void objAttributeAdded(AttributeEvent e) { 187 rebuildTable((ObjEntity) e.getEntity()); 188 table.select(e.getAttribute()); 189 } 190 191 public void objAttributeRemoved(AttributeEvent e) { 192 ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel(); 193 int ind = model.getObjectList().indexOf(e.getAttribute()); 194 model.removeRow(e.getAttribute()); 195 table.select(ind); 196 } 197 198 public void currentObjEntityChanged(EntityDisplayEvent e) { 199 if (e.getSource() == this) { 200 return; 201 } 202 203 ObjEntity entity = (ObjEntity) e.getEntity(); 204 205 if (entity != null) { 208 rebuildTable(entity); 209 } 210 211 if (e.isUnselectAttributes()) { 214 table.clearSelection(); 215 } 216 } 217 218 protected void rebuildTable(ObjEntity entity) { 219 ObjAttributeTableModel model = new ObjAttributeTableModel(entity, mediator, this); 220 table.setModel(model); 221 table.setRowHeight(25); 222 table.setRowMargin(3); 223 setUpTableStructure(model); 224 } 225 226 protected void setUpTableStructure(ObjAttributeTableModel model) { 227 TableColumn nameColumn = table.getColumnModel().getColumn( 228 ObjAttributeTableModel.OBJ_ATTRIBUTE); 229 nameColumn.setMinWidth(150); 230 231 TableColumn typeColumn = table.getColumnModel().getColumn( 232 ObjAttributeTableModel.OBJ_ATTRIBUTE_TYPE); 233 typeColumn.setMinWidth(150); 234 235 JComboBox javaTypesCombo = CayenneWidgetFactory.createComboBox(ModelerUtil 236 .getRegisteredTypeNames(), false); 237 javaTypesCombo.setEditable(true); 238 typeColumn.setCellEditor(new DefaultCellEditor (javaTypesCombo)); 239 240 TableColumn lockColumn = table.getColumnModel().getColumn( 241 ObjAttributeTableModel.LOCKING); 242 lockColumn.setMinWidth(100); 243 244 TableColumn dbTypeColumn = table.getColumnModel().getColumn( 245 ObjAttributeTableModel.DB_ATTRIBUTE_TYPE); 246 dbTypeColumn.setMinWidth(120); 247 248 TableColumn dbNameColumn = table.getColumnModel().getColumn( 249 ObjAttributeTableModel.DB_ATTRIBUTE); 250 dbNameColumn.setMinWidth(150); 251 252 if (model.getEntity().getDbEntity() != null) { 253 JComboBox dbAttributesCombo = CayenneWidgetFactory.createComboBox(ModelerUtil 254 .getDbAttributeNames(mediator, mediator 255 .getCurrentObjEntity() 256 .getDbEntity()), true); 257 258 dbAttributesCombo.setEditable(false); 259 dbNameColumn.setCellEditor(new DefaultCellEditor (dbAttributesCombo)); 260 } 261 } 262 263 266 public void objEntityChanged(EntityEvent e) { 267 if (e.getSource() == this) { 268 return; 269 } 270 271 ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel(); 272 if (model.getDbEntity() != ((ObjEntity) e.getEntity()).getDbEntity()) { 273 model.resetDbEntity(); 274 setUpTableStructure(model); 275 } 276 } 277 278 public void objEntityAdded(EntityEvent e) { 279 } 280 281 public void objEntityRemoved(EntityEvent e) { 282 } 283 284 final class CellRenderer extends DefaultTableCellRenderer { 286 287 public Component getTableCellRendererComponent( 288 JTable table, 289 Object value, 290 boolean isSelected, 291 boolean hasFocus, 292 int row, 293 int column) { 294 295 super.getTableCellRendererComponent( 296 table, 297 value, 298 isSelected, 299 hasFocus, 300 row, 301 column); 302 303 ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel(); 304 ObjAttribute attribute = model.getAttribute(row); 305 306 if (attribute != null && attribute.getEntity() != model.getEntity()) { 307 setForeground(Color.GRAY); 308 } 309 else { 310 setForeground(isSelected && !hasFocus 311 ? table.getSelectionForeground() 312 : table.getForeground()); 313 } 314 315 return this; 316 } 317 } 318 } | Popular Tags |