1 56 57 package org.objectstyle.cayenne.modeler.editor; 58 59 import java.awt.BorderLayout ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.util.ArrayList ; 63 import java.util.Collection ; 64 import java.util.EventObject ; 65 import java.util.Iterator ; 66 67 import javax.swing.DefaultComboBoxModel ; 68 import javax.swing.InputVerifier ; 69 import javax.swing.JButton ; 70 import javax.swing.JCheckBox ; 71 import javax.swing.JComboBox ; 72 import javax.swing.JComponent ; 73 import javax.swing.JLabel ; 74 import javax.swing.JPanel ; 75 import javax.swing.JTextField ; 76 import javax.swing.JToolBar ; 77 78 import org.objectstyle.cayenne.access.DataDomain; 79 import org.objectstyle.cayenne.dba.JdbcPkGenerator; 80 import org.objectstyle.cayenne.map.DataMap; 81 import org.objectstyle.cayenne.map.DbEntity; 82 import org.objectstyle.cayenne.map.DbKeyGenerator; 83 import org.objectstyle.cayenne.map.DerivedDbEntity; 84 import org.objectstyle.cayenne.map.event.EntityEvent; 85 import org.objectstyle.cayenne.modeler.Application; 86 import org.objectstyle.cayenne.modeler.ProjectController; 87 import org.objectstyle.cayenne.modeler.action.CreateAttributeAction; 88 import org.objectstyle.cayenne.modeler.action.CreateObjEntityAction; 89 import org.objectstyle.cayenne.modeler.action.CreateRelationshipAction; 90 import org.objectstyle.cayenne.modeler.action.DbEntitySyncAction; 91 import org.objectstyle.cayenne.modeler.event.DbEntityDisplayListener; 92 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent; 93 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 94 import org.objectstyle.cayenne.modeler.util.CellRenderers; 95 import org.objectstyle.cayenne.modeler.util.ProjectUtil; 96 import org.objectstyle.cayenne.modeler.util.TextAdapter; 97 import org.objectstyle.cayenne.util.Util; 98 import org.objectstyle.cayenne.validation.ValidationException; 99 100 import com.jgoodies.forms.builder.DefaultFormBuilder; 101 import com.jgoodies.forms.layout.FormLayout; 102 103 109 public class DbEntityTab extends JPanel implements ExistingSelectionProcessor, 110 DbEntityDisplayListener, ActionListener { 111 112 protected ProjectController mediator; 113 114 protected TextAdapter name; 115 protected JTextField schema; 116 protected JComboBox parentEntities; 117 protected JButton parentLabel; 118 protected JLabel schemaLabel; 119 120 protected JCheckBox customPKGenerator; 121 protected JLabel customPKGeneratorLabel; 122 protected JLabel customPKGeneratorNote; 123 protected JLabel customPKGeneratorNameLabel; 124 protected JLabel customPKSizeLabel; 125 protected JTextField customPKName; 126 protected JTextField customPKSize; 127 128 public DbEntityTab(ProjectController mediator) { 129 super(); 130 this.mediator = mediator; 131 132 initView(); 133 initController(); 134 } 135 136 private void initView() { 137 this.setLayout(new BorderLayout ()); 138 139 JToolBar toolBar = new JToolBar (); 140 Application app = Application.getInstance(); 141 toolBar.add(app.getAction(CreateObjEntityAction.getActionName()).buildButton()); 142 toolBar.add(app.getAction(DbEntitySyncAction.getActionName()).buildButton()); 143 toolBar.addSeparator(); 144 145 toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton()); 146 toolBar.add(app.getAction(CreateRelationshipAction.getActionName()).buildButton()); 147 add(toolBar, BorderLayout.NORTH); 148 149 name = new TextAdapter(new JTextField ()) { 151 152 protected void updateModel(String text) { 153 setEntityName(text); 154 } 155 }; 156 schemaLabel = CayenneWidgetFactory.createLabel("Schema:"); 157 schema = CayenneWidgetFactory.createTextField(); 158 159 parentLabel = CayenneWidgetFactory.createLabelButton("Parent DbEntity:"); 160 parentLabel.setEnabled(false); 161 162 parentEntities = CayenneWidgetFactory.createComboBox(); 163 parentEntities.setEditable(false); 164 parentEntities.setEnabled(false); 165 166 customPKGenerator = new JCheckBox (); 167 customPKGeneratorLabel = CayenneWidgetFactory 168 .createLabel("Customize primary key generation"); 169 customPKGeneratorNote = CayenneWidgetFactory 170 .createLabel("(currently ignored by all adapters except Oracle)"); 171 customPKGeneratorNote.setFont(customPKGeneratorNote.getFont().deriveFont(10)); 172 customPKGeneratorNameLabel = CayenneWidgetFactory 173 .createLabel("Database object name: "); 174 customPKSizeLabel = CayenneWidgetFactory.createLabel("Cached PK Size: "); 175 176 customPKName = CayenneWidgetFactory.createTextField(); 177 customPKSize = CayenneWidgetFactory.createTextField(); 178 179 FormLayout layout = new FormLayout( 181 "right:max(50dlu;pref), 3dlu, fill:max(200dlu;pref)", 182 ""); 183 DefaultFormBuilder builder = new DefaultFormBuilder(layout); 184 builder.setDefaultDialogBorder(); 185 186 builder.appendSeparator("DbEntity Configuration"); 187 builder.append("DbEntity name:", name.getComponent()); 188 builder.append(schemaLabel, schema); 189 builder.append(parentLabel, parentEntities); 190 191 builder.appendSeparator("PK Generation"); 192 builder.append(customPKGenerator, customPKGeneratorLabel); 193 builder.append("", customPKGeneratorNote); 194 builder.append(customPKGeneratorNameLabel, customPKName); 195 builder.append(customPKSizeLabel, customPKSize); 196 197 add(builder.getPanel(), BorderLayout.CENTER); 198 } 199 200 private void initController() { 201 mediator.addDbEntityDisplayListener(this); 202 InputVerifier inputCheck = new FieldVerifier(); 203 schema.setInputVerifier(inputCheck); 204 customPKName.setInputVerifier(inputCheck); 205 customPKSize.setInputVerifier(inputCheck); 206 207 parentEntities.addActionListener(new ActionListener () { 208 209 public void actionPerformed(ActionEvent e) { 210 DbEntity current = mediator.getCurrentDbEntity(); 211 212 if (current instanceof DerivedDbEntity) { 213 DerivedDbEntity derived = (DerivedDbEntity) current; 214 DbEntity parent = (DbEntity) parentEntities.getSelectedItem(); 215 216 if (parent != derived.getParentEntity()) { 217 derived.setParentEntity(parent); 218 derived.resetToParentView(); 219 ProjectUtil.cleanObjMappings(mediator.getCurrentDataMap()); 220 221 EntityEvent event = new EntityEvent(this, current); 222 mediator.fireDbEntityEvent(event); 223 } 224 } 225 } 226 }); 227 228 parentLabel.addActionListener(this); 229 customPKGenerator.addActionListener(this); 230 } 231 232 public void processExistingSelection(EventObject e) { 233 EntityDisplayEvent ede = new EntityDisplayEvent( 234 this, 235 mediator.getCurrentDbEntity(), 236 mediator.getCurrentDataMap(), 237 mediator.getCurrentDataDomain()); 238 mediator.fireDbEntityDisplayEvent(ede); 239 } 240 241 public void currentDbEntityChanged(EntityDisplayEvent e) { 242 DbEntity entity = (DbEntity) e.getEntity(); 243 if (null == entity || !e.isEntityChanged()) { 244 return; 245 } 246 247 name.setText(entity.getName()); 248 schema.setText(entity.getSchema()); 249 250 if (entity instanceof DerivedDbEntity) { 251 updateState(true); 252 253 255 DataMap map = mediator.getCurrentDataMap(); 256 Collection allEntities = map.getNamespace().getDbEntities(); 257 java.util.List entities = new ArrayList (allEntities.size()); 258 Iterator it = allEntities.iterator(); 259 260 while (it.hasNext()) { 261 DbEntity parentEntity = (DbEntity) it.next(); 262 if (!(parentEntity instanceof DerivedDbEntity)) { 263 entities.add(parentEntity); 264 } 265 } 266 267 DefaultComboBoxModel model = new DefaultComboBoxModel (entities.toArray()); 268 model.setSelectedItem(((DerivedDbEntity) entity).getParentEntity()); 269 parentEntities.setRenderer(CellRenderers.entityListRendererWithIcons(map)); 270 parentEntities.setModel(model); 271 } 272 else { 273 updateState(false); 274 parentEntities.setSelectedIndex(-1); 275 } 276 } 277 278 281 protected void updateState(boolean isDerivedEntity) { 282 schemaLabel.setEnabled(!isDerivedEntity); 283 schema.setEnabled(!isDerivedEntity); 284 285 parentLabel.setEnabled(isDerivedEntity); 286 parentEntities.setEnabled(isDerivedEntity); 287 parentLabel.setVisible(isDerivedEntity); 288 parentEntities.setVisible(isDerivedEntity); 289 290 DbEntity entity = mediator.getCurrentDbEntity(); 291 updatePrimaryKeyGeneratorView(entity); 292 } 293 294 protected void updatePrimaryKeyGeneratorView(DbEntity entity) { 295 DbKeyGenerator generator = entity.getPrimaryKeyGenerator(); 296 boolean isPKGeneratorCustomized = generator != null; 297 298 customPKGenerator.setSelected(isPKGeneratorCustomized); 299 300 customPKGeneratorNameLabel.setEnabled(isPKGeneratorCustomized); 301 customPKSizeLabel.setEnabled(isPKGeneratorCustomized); 302 303 customPKName.setEnabled(isPKGeneratorCustomized); 304 customPKSize.setEnabled(isPKGeneratorCustomized); 305 306 if (isPKGeneratorCustomized) { 307 customPKName.setText(generator.getGeneratorName()); 308 customPKSize.setText(generator.getKeyCacheSize() != null ? generator 309 .getKeyCacheSize() 310 .toString() : "0"); 311 } 312 else { 313 customPKName.setText(""); 314 customPKSize.setText(""); 315 } 316 } 317 318 public void actionPerformed(ActionEvent e) { 319 if (parentLabel == e.getSource()) { 320 DbEntity current = mediator.getCurrentDbEntity(); 321 322 if (current instanceof DerivedDbEntity) { 323 DbEntity parent = ((DerivedDbEntity) current).getParentEntity(); 324 if (parent != null) { 325 DataDomain dom = mediator.getCurrentDataDomain(); 326 mediator.fireDbEntityDisplayEvent(new EntityDisplayEvent( 327 this, 328 parent, 329 parent.getDataMap(), 330 dom)); 331 } 332 } 333 } 334 else if (customPKGenerator == e.getSource()) { 335 DbEntity entity = mediator.getCurrentDbEntity(); 336 if (entity == null) { 337 return; 338 } 339 340 if (customPKGenerator.isSelected()) { 341 if (entity.getPrimaryKeyGenerator() == null) { 342 DbKeyGenerator generator = new DbKeyGenerator(); 343 generator.setGeneratorType(DbKeyGenerator.ORACLE_TYPE); 344 generator.setKeyCacheSize(new Integer ( 345 JdbcPkGenerator.DEFAULT_PK_CACHE_SIZE)); 346 entity.setPrimaryKeyGenerator(generator); 347 } 348 349 updatePrimaryKeyGeneratorView(entity); 350 } 351 else { 352 entity.setPrimaryKeyGenerator(null); 353 updatePrimaryKeyGeneratorView(entity); 354 } 355 } 356 } 357 358 void setEntityName(String newName) { 359 if (newName != null && newName.trim().length() == 0) { 360 newName = null; 361 } 362 363 DbEntity entity = mediator.getCurrentDbEntity(); 364 365 if (entity == null || Util.nullSafeEquals(newName, entity.getName())) { 366 return; 367 } 368 369 if (newName == null) { 370 throw new ValidationException("Entity name is required."); 371 } 372 else if (entity.getDataMap().getDbEntity(newName) == null) { 373 EntityEvent e = new EntityEvent(this, entity, entity.getName()); 375 entity.setName(newName); 376 mediator.fireDbEntityEvent(e); 378 } 379 else { 380 throw new ValidationException("There is another entity with name '" 382 + newName 383 + "'."); 384 } 385 } 386 387 class FieldVerifier extends InputVerifier { 388 389 public boolean verify(JComponent input) { 390 if (input == schema) { 391 return verifySchema(); 392 } 393 else if (input == customPKSize) { 394 return verifyCustomPKSize(); 395 } 396 else if (input == customPKName) { 397 return verifyCustomPKName(); 398 } 399 else { 400 return true; 401 } 402 } 403 404 protected boolean verifySchema() { 405 String text = schema.getText(); 406 if (text != null && text.trim().length() == 0) { 407 text = null; 408 } 409 410 DbEntity ent = mediator.getCurrentDbEntity(); 411 412 if (ent != null && !Util.nullSafeEquals(ent.getSchema(), text)) { 413 ent.setSchema(text); 414 mediator.fireDbEntityEvent(new EntityEvent(this, ent)); 415 } 416 417 return true; 418 } 419 420 protected boolean verifyCustomPKSize() { 421 String text = customPKSize.getText(); 422 int cacheSize = 0; 423 424 if (text != null && text.trim().length() > 0) { 425 try { 426 cacheSize = Integer.parseInt(text); 427 } 428 catch (NumberFormatException nfex) { 429 } 430 } 431 432 if (cacheSize == 0) { 434 customPKSize.setText("0"); 435 } 436 437 DbEntity entity = mediator.getCurrentDbEntity(); 438 DbKeyGenerator generator = entity.getPrimaryKeyGenerator(); 439 440 if (generator != null 441 && (generator.getKeyCacheSize() == null || generator 442 .getKeyCacheSize() 443 .intValue() != cacheSize)) { 444 generator.setKeyCacheSize(new Integer (cacheSize)); 445 mediator.fireDbEntityEvent(new EntityEvent(this, entity)); 446 } 447 448 return true; 449 } 450 451 protected boolean verifyCustomPKName() { 452 String text = customPKName.getText(); 453 if (text != null && text.trim().length() == 0) { 454 text = null; 455 } 456 457 DbEntity entity = mediator.getCurrentDbEntity(); 458 DbKeyGenerator generator = entity.getPrimaryKeyGenerator(); 459 460 if (generator != null && (!Util.nullSafeEquals(text, generator.getName()))) { 461 generator.setGeneratorName(text); 462 mediator.fireDbEntityEvent(new EntityEvent(this, entity)); 463 } 464 return true; 465 } 466 } 467 } | Popular Tags |