1 19 20 package org.netbeans.modules.j2ee.ddloaders.multiview; 21 22 import org.netbeans.modules.j2ee.dd.api.ejb.CmrField; 23 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 24 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelation; 25 import org.netbeans.modules.j2ee.dd.api.ejb.Entity; 26 import org.netbeans.modules.j2ee.dd.api.ejb.Relationships; 27 import org.netbeans.modules.j2ee.ddloaders.multiview.ui.CmpRelationshipsForm; 28 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EntityMethodController; 29 import org.openide.DialogDescriptor; 30 import org.openide.DialogDisplayer; 31 32 import javax.swing.*; 33 import javax.swing.event.DocumentEvent ; 34 import javax.swing.event.DocumentListener ; 35 import java.awt.*; 36 import java.awt.event.ActionEvent ; 37 import java.awt.event.ActionListener ; 38 import java.util.Vector ; 39 40 43 public class CmpRelationshipsDialogHelper { 44 45 private static final String CLASS_COLLECTION = "java.util.Collection"; private static final String CLASS_SET = "java.util.Set"; private static final String [] FILED_TYPE_ITEMS = new String []{CLASS_COLLECTION, CLASS_SET}; 48 49 private final EjbJarMultiViewDataObject dataObject; 50 private final EjbJar ejbJar; 51 52 private JTextField relationshipNameTextField; 53 private JTextArea descriptionTextArea; 54 private Vector entityNames; 55 private RelationshipDialogActionListener listener; 56 57 private class FormRoleHelper { 58 59 private JTextField roleNameTextField; 60 private JComboBox ejbComboBox; 61 private JRadioButton multiplicityManyRadioButton; 62 private JRadioButton multiplicityOneRadioButton; 63 private JCheckBox cascadeDeleteCheckBox; 64 private JCheckBox createCmrFieldCheckBox; 65 private JTextField fieldNameTextField; 66 private JComboBox fieldTypeComboBox; 67 private JCheckBox getterCheckBox; 68 private JCheckBox setterCheckBox; 69 70 private String origEjbName; 71 private String origFieldName; 72 private String origFieldType; 73 private EntityHelper origEntityHelper; 74 protected boolean origGetter; 77 protected boolean origSetter; 78 79 private String opositeEjbName; 80 81 private String lastFieldName; 82 private String lastFieldType = CLASS_COLLECTION; 83 private boolean lastGetter = true; 84 private boolean lastSetter = true; 85 private boolean createCmrFieldChanged = true; 86 87 private void init() { 88 ejbComboBox.setModel(new DefaultComboBoxModel(entityNames)); 89 fieldTypeComboBox.setModel(new DefaultComboBoxModel(FILED_TYPE_ITEMS)); 90 multiplicityOneRadioButton.addActionListener(listener); 91 multiplicityManyRadioButton.addActionListener(listener); 92 createCmrFieldCheckBox.addActionListener(new ActionListener () { 93 public void actionPerformed(ActionEvent e) { 94 createCmrFieldChanged = true; 95 listener.validate(); 96 } 97 }); 98 } 99 100 private void processResult(RelationshipHelper.RelationshipRoleHelper helper) { 101 String ejbName = getEjbName(); 102 String roleName = getRoleName(); 103 helper.setEjbName(ejbName); 104 helper.setRoleName(roleName); 105 helper.setMultiple(isMultiple()); 106 helper.setCascadeDelete(isCascadeDelete()); 107 String fieldName; 108 String fieldType; 109 if (isCreateCmrField()) { 110 fieldName = getFieldName(); 111 fieldType = getFieldType(); 112 helper.setCmrField(fieldName, fieldType); 113 } else { 114 fieldName = null; 115 fieldType = null; 116 helper.setCmrField(null); 117 } 118 boolean getter = hasGetter(); 119 boolean setter = hasSetter(); 120 boolean ejbNameChanged = !equal(origEjbName, ejbName); 121 boolean fieldChanged = !equal(origFieldName, fieldName) || !equal(origFieldType, fieldType); 122 boolean getterChanged = origGetter != getter || fieldChanged; 123 boolean setterChanged = origSetter != setter || fieldChanged; 124 if (ejbNameChanged || fieldChanged || getterChanged || setterChanged) { 125 if (origEntityHelper != null) { 126 } 138 if (fieldName != null) { 139 EntityHelper entityHelper; 140 if (ejbNameChanged) { 141 Entity entity = getEntity(ejbName); 142 entityHelper = entity == null ? null : dataObject.getEntityHelper(entity); 143 } else { 144 entityHelper = origEntityHelper; 145 } 146 if (entityHelper != null) { 147 EntityMethodController entityMethodController = entityHelper.getEntityMethodController(); 148 } 172 } 173 } 174 } 175 176 private boolean equal(String s1, String s2) { 177 return s1 == null ? s2 == null : s1.equals(s2); 178 } 179 180 private boolean isCascadeDelete() { 181 return cascadeDeleteCheckBox.isSelected(); 182 } 183 184 private void setCascadeDelete(boolean cascadeDelete) { 185 cascadeDeleteCheckBox.setSelected(cascadeDelete); 186 } 187 188 private boolean isMultiple() { 189 return multiplicityManyRadioButton.isSelected(); 190 } 191 192 private void setMultiple(boolean multiple) { 193 if (multiple) { 194 multiplicityManyRadioButton.setSelected(true); 195 } else { 196 multiplicityOneRadioButton.setSelected(true); 197 } 198 } 199 200 private boolean isCreateCmrField() { 201 return createCmrFieldCheckBox.isSelected(); 202 } 203 204 private boolean hasSetter() { 205 return setterCheckBox.isSelected(); 206 } 207 208 private boolean hasGetter() { 209 return getterCheckBox.isSelected(); 210 } 211 212 private String getFieldType() { 213 return (String ) fieldTypeComboBox.getSelectedItem(); 214 } 215 216 private void setFieldType(String fieldType) { 217 if (fieldType != null) { 218 lastFieldType = fieldType; 219 } 220 fieldTypeComboBox.setSelectedItem(fieldType); 221 } 222 223 private String getRoleName() { 224 String roleName = roleNameTextField.getText().trim(); 225 return roleName.length() == 0 ? null : roleName; 226 } 227 228 private void setRoleName(String roleName) { 229 roleNameTextField.setText(roleName); 230 } 231 232 private String getEjbName() { 233 return (String ) ejbComboBox.getSelectedItem(); 234 } 235 236 private void setEjbName(String ejbName) { 237 ejbComboBox.setSelectedItem(ejbName); 238 } 239 240 private void populateFormFields(RelationshipHelper.RelationshipRoleHelper helper) { 241 setRoleName(helper.getRoleName()); 242 origEjbName = helper.getEjbName(); 243 Entity entity = getEntity(origEjbName); 244 origEntityHelper = entity == null ? null : dataObject.getEntityHelper(entity); 245 setEjbName(origEjbName); 246 setMultiple(helper.isMultiple()); 247 setCascadeDelete(helper.isCascadeDelete()); 248 249 CmrField field = helper.getCmrField(); 250 if (field == null) { 251 origFieldName = null; 252 origFieldType = null; 253 setCreateCmrField(false); 254 setFieldName(null); 255 setFieldType(null); 256 } else { 257 origFieldName = field.getCmrFieldName(); 258 origFieldType = field.getCmrFieldType(); 259 if (origEntityHelper != null) { 260 lastGetter = origGetter; 266 lastSetter = origSetter; 267 setLocalGetter(origGetter); 268 setLocalSetter(origSetter); 269 } 270 setCreateCmrField(true); 271 setFieldName(origFieldName); 272 setFieldType(origFieldType); 273 } 274 } 275 276 private void setLocalSetter(boolean setter) { 277 setterCheckBox.setSelected(setter); 278 } 279 280 private void setLocalGetter(boolean getter) { 281 getterCheckBox.setSelected(getter); 282 } 283 284 private void setCreateCmrField(boolean selected) { 285 createCmrFieldCheckBox.setSelected(selected); 286 } 287 288 public String getFieldName() { 289 return fieldNameTextField.getText().trim(); 290 } 291 292 private void setFieldName(String fieldName) { 293 if (fieldName != null && fieldName.length() > 0) { 294 lastFieldName = fieldName; 295 } 296 fieldNameTextField.setText(fieldName); 297 } 298 299 public void setFieldStates(FormRoleHelper opositeRole) { 300 boolean createCmrField = isCreateCmrField(); 301 String fieldName = getFieldName(); 302 if (createCmrFieldChanged) { 303 createCmrFieldChanged = false; 304 if (createCmrField) { 305 if (fieldName.length() == 0) { 306 setFieldName(lastFieldName); 307 } 308 fieldNameTextField.setEnabled(true); 309 setLocalGetter(lastGetter); 310 getterCheckBox.setEnabled(true); 311 setLocalSetter(lastSetter); 312 setterCheckBox.setEnabled(true); 313 } else { 314 lastGetter = getterCheckBox.isSelected(); 315 lastSetter = setterCheckBox.isSelected(); 316 if (fieldName.length() > 0) { 317 lastFieldName = fieldName; 318 } 319 setFieldName(null); 320 fieldNameTextField.setEnabled(false); 321 setLocalGetter(false); 322 getterCheckBox.setEnabled(false); 323 setLocalSetter(false); 324 setterCheckBox.setEnabled(false); 325 } 326 } 327 boolean opositeMultiple = opositeRole.isMultiple(); 328 String fieldType = getFieldType(); 329 if (createCmrField && opositeMultiple) { 330 if (fieldType == null) { 331 setFieldType(lastFieldType); 332 } 333 fieldTypeComboBox.setEnabled(true); 334 } else { 335 if (fieldType != null) { 336 lastFieldType = fieldType; 337 } 338 setFieldType(null); 339 fieldTypeComboBox.setEnabled(false); 340 } 341 opositeEjbName = opositeRole.getEjbName(); 342 } 343 344 private String validateFieldName() { 345 if(isCreateCmrField()) { 346 String fieldName = getFieldName(); 347 if (!Utils.isJavaIdentifier(fieldName)) { 348 return Utils.getBundleMessage("MSG_InvalidFieldName"); 349 } 350 } 351 return null; 352 } 353 } 354 355 FormRoleHelper roleA = new FormRoleHelper(); 356 FormRoleHelper roleB = new FormRoleHelper(); 357 358 public CmpRelationshipsDialogHelper(EjbJarMultiViewDataObject dataObject, EjbJar ejbJar) { 359 this.dataObject = dataObject; 360 this.ejbJar = ejbJar; 361 } 362 363 public boolean showCmpRelationshipsDialog(String title, EjbRelation relation) { 364 final CmpRelationshipsForm form = initForm(); 365 final JLabel errorLabel = form.getErrorLabel(); 366 367 RelationshipHelper helper; 368 if (relation != null) { 369 helper = new RelationshipHelper(relation); 370 populateFormFields(helper); 371 } else { 372 helper = null; 373 } 374 375 listener.validate(); 376 377 DialogDescriptor dialogDescriptor = new DialogDescriptor(form, title); 378 dialogDescriptor.setOptionType(DialogDescriptor.OK_CANCEL_OPTION); 379 Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 380 dialog.setFocusTraversalPolicy(form.createFocusTraversalPolicy()); 381 DialogListener dialogListener = new DialogListener(errorLabel, dialogDescriptor); 382 form.getFieldNameTextField().getDocument().addDocumentListener(dialogListener); 383 form.getFieldNameTextField2().getDocument().addDocumentListener(dialogListener); 384 form.getCreateCmrFieldCheckBox().addActionListener(dialogListener); 385 form.getCreateCmrFieldCheckBox2().addActionListener(dialogListener); 386 form.getRoleNameTextField().getDocument().addDocumentListener(dialogListener); 387 form.getRoleNameTextField2().getDocument().addDocumentListener(dialogListener); 388 form.getEjbComboBox().addActionListener(dialogListener); 389 form.getEjbComboBox2().addActionListener(dialogListener); 390 dialogListener.validateFields(); 391 if (dataObject.getEjbJar().getEnterpriseBeans().getEntity().length==0) 392 dialogDescriptor.setValid(false); 393 dialog.setVisible(true); 394 if (dialogDescriptor.getValue() == DialogDescriptor.OK_OPTION) { 395 if (helper == null) { 397 Relationships relationships = ejbJar.getSingleRelationships(); 398 if (relationships == null) { 399 relationships = ejbJar.newRelationships(); 400 ejbJar.setRelationships(relationships); 401 } 402 helper = new RelationshipHelper(relationships); 403 } 404 processResult(helper); 405 return true; 406 } else { 407 return false; 408 } 409 } 410 411 private Vector getEntities() { 412 Vector entityNames = new Vector (); 413 Entity[] entities = ejbJar.getEnterpriseBeans().getEntity(); 414 for (int i = 0; i < entities.length; i++) { 415 Entity entity = entities[i]; 416 if (entity.getLocal() != null) { 417 entityNames.add(entity.getEjbName()); 418 } 419 } 420 return entityNames; 421 } 422 423 private void processResult(RelationshipHelper helper) { 424 String relationName = relationshipNameTextField.getText().trim(); 425 if (relationName.length() == 0) { 426 relationName = roleA.getEjbName() + "-" + roleB.getEjbName(); } 428 helper.setRelationName(relationName); 429 helper.setDescription(descriptionTextArea.getText().trim()); 430 roleA.processResult(helper.roleA); 431 roleB.processResult(helper.roleB); 432 433 } 434 435 private void populateFormFields(RelationshipHelper helper) { 436 relationshipNameTextField.setText(helper.getRelationName()); 437 descriptionTextArea.setText(helper.getDescription()); 438 roleA.populateFormFields(helper.roleA); 439 roleB.populateFormFields(helper.roleB); 440 } 441 442 private Entity getEntity(String entityName) { 443 if (entityName == null){ 444 return null; 445 } 446 Entity[] entities = ejbJar.getEnterpriseBeans().getEntity(); 447 for (int i = 0; i < entities.length; i++) { 448 Entity entity = entities[i]; 449 if (entityName.equals(entity.getEjbName())) { 450 return entity; 451 } 452 } 453 return null; 454 } 455 456 private CmpRelationshipsForm initForm() { 457 listener = new RelationshipDialogActionListener(); 458 entityNames = getEntities(); 459 CmpRelationshipsForm form = new CmpRelationshipsForm(); 460 relationshipNameTextField = form.getRelationshipNameTextField(); 461 descriptionTextArea = form.getDescriptionTextArea(); 462 463 roleA.roleNameTextField = form.getRoleNameTextField(); 464 roleA.ejbComboBox = form.getEjbComboBox(); 465 roleA.multiplicityManyRadioButton = form.getMultiplicityManyRadioButton(); 466 roleA.multiplicityOneRadioButton = form.getMultiplicityOneRadioButton(); 467 roleA.cascadeDeleteCheckBox = form.getCascadeDeleteCheckBox(); 468 roleA.createCmrFieldCheckBox = form.getCreateCmrFieldCheckBox(); 469 roleA.fieldNameTextField = form.getFieldNameTextField(); 470 roleA.fieldTypeComboBox = form.getFieldTypeComboBox(); 471 roleA.getterCheckBox = form.getGetterCheckBox(); 472 roleA.setterCheckBox = form.getSetterCheckBox(); 473 roleA.init(); 474 475 roleB.roleNameTextField = form.getRoleNameTextField2(); 476 roleB.ejbComboBox = form.getEjbComboBox2(); 477 roleB.multiplicityManyRadioButton = form.getMultiplicityManyRadioButton2(); 478 roleB.multiplicityOneRadioButton = form.getMultiplicityOneRadioButton2(); 479 roleB.cascadeDeleteCheckBox = form.getCascadeDeleteCheckBox2(); 480 roleB.createCmrFieldCheckBox = form.getCreateCmrFieldCheckBox2(); 481 roleB.fieldNameTextField = form.getFieldNameTextField2(); 482 roleB.fieldTypeComboBox = form.getFieldTypeComboBox2(); 483 roleB.getterCheckBox = form.getGetterCheckBox2(); 484 roleB.setterCheckBox = form.getSetterCheckBox2(); 485 roleB.init(); 486 return form; 487 } 488 489 private class RelationshipDialogActionListener implements ActionListener { 490 491 public void actionPerformed(ActionEvent e) { 492 validate(); 493 } 494 495 public void validate() { 496 roleA.setFieldStates(roleB); 497 roleB.setFieldStates(roleA); 498 } 499 } 500 501 private class DialogListener implements DocumentListener , ActionListener { 502 503 private final JLabel errorLabel; 504 private final DialogDescriptor dialogDescriptor; 505 507 public DialogListener(JLabel errorLabel, DialogDescriptor dialogDescriptor) { 508 this.errorLabel = errorLabel; 509 this.dialogDescriptor = dialogDescriptor; 510 } 516 517 public void changedUpdate(DocumentEvent e) { 518 validateFields(); 519 } 520 521 public void insertUpdate(DocumentEvent e) { 522 validateFields(); 523 } 524 525 public void removeUpdate(DocumentEvent e) { 526 validateFields(); 527 } 528 529 public void actionPerformed(ActionEvent e) { 530 validateFields(); 531 } 532 533 private boolean isCmrFieldSpecified() { 534 boolean a = roleA.createCmrFieldCheckBox.isSelected() && !roleA.fieldNameTextField.getText().trim().equals(""); 535 boolean b = roleB.createCmrFieldCheckBox.isSelected() && !roleB.fieldNameTextField.getText().trim().equals(""); 536 return a || b; 537 } 538 539 private void validateFields() { 540 541 final String roleNameA = roleA.getRoleName(); 542 final String roleNameB = roleB.getRoleName(); 543 if (false ) { 544 errorLabel.setText(Utils.getBundleMessage("LBL_ScanningInProgress")); 545 } else if (roleNameA != null && roleNameA.equals(roleNameB)) { 546 errorLabel.setText(Utils.getBundleMessage("MSG_SameRoleNames")); 547 dialogDescriptor.setValid(false); 548 } else if (!isCmrFieldSpecified()) { 549 errorLabel.setText(Utils.getBundleMessage("MSG_NoCmrDefined")); 550 dialogDescriptor.setValid(false); 551 } else if (ejbJar.getEnterpriseBeans().getEntity() == null || ejbJar.getEnterpriseBeans().getEntity().length == 0){ 552 errorLabel.setText(Utils.getBundleMessage("MSG_NoEntitiesFound")); 553 dialogDescriptor.setValid(false); 554 } else if (isEmpty(roleA.getEjbName()) || isEmpty(roleB.getEjbName())){ 555 errorLabel.setText(Utils.getBundleMessage("MSG_NoEJbNameSpecified")); 556 dialogDescriptor.setValid(false); 557 } else { 558 String s1 = roleA.validateFieldName(); 559 if (s1 != null) { 560 errorLabel.setText(s1); 561 dialogDescriptor.setValid(false); 562 } else { 563 String s2 = roleB.validateFieldName(); 564 if (s2 != null) { 565 errorLabel.setText(s2); 566 dialogDescriptor.setValid(false); 567 } else { 568 errorLabel.setText(" "); 569 dialogDescriptor.setValid(true); 570 } 571 } 572 } 573 } 574 575 private boolean isEmpty(String str){ 576 return null == str || "".equals(str.trim()); 577 } 578 } 579 } 580 | Popular Tags |