KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > CmpRelationshipsDialogHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
34 import javax.swing.event.DocumentListener JavaDoc;
35 import java.awt.*;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.ActionListener JavaDoc;
38 import java.util.Vector JavaDoc;
39
40 /**
41  * @author pfiala
42  */

43 public class CmpRelationshipsDialogHelper {
44
45     private static final String JavaDoc CLASS_COLLECTION = "java.util.Collection"; //NOI18N
46
private static final String JavaDoc CLASS_SET = "java.util.Set"; //NOI18N
47
private static final String JavaDoc[] FILED_TYPE_ITEMS = new String JavaDoc[]{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 JavaDoc 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 JavaDoc origEjbName;
71         private String JavaDoc origFieldName;
72         private String JavaDoc origFieldType;
73         private EntityHelper origEntityHelper;
74 // protected Method origGetterMethod;
75
// protected Method origSetterMethod;
76
protected boolean origGetter;
77         protected boolean origSetter;
78
79         private String JavaDoc opositeEjbName;
80
81         private String JavaDoc lastFieldName;
82         private String JavaDoc 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 JavaDoc() {
93                 public void actionPerformed(ActionEvent JavaDoc e) {
94                     createCmrFieldChanged = true;
95                     listener.validate();
96                 }
97             });
98         }
99
100         private void processResult(RelationshipHelper.RelationshipRoleHelper helper) {
101             String JavaDoc ejbName = getEjbName();
102             String JavaDoc roleName = getRoleName();
103             helper.setEjbName(ejbName);
104             helper.setRoleName(roleName);
105             helper.setMultiple(isMultiple());
106             helper.setCascadeDelete(isCascadeDelete());
107             String JavaDoc fieldName;
108             String JavaDoc 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 // if (getterChanged) {
127
// Utils.removeMethod(origEntityHelper.getLocalBusinessInterfaceClass(), origGetterMethod);
128
// }
129
// if (setterChanged) {
130
// Utils.removeMethod(origEntityHelper.getLocalBusinessInterfaceClass(), origSetterMethod);
131
// }
132
// if (fieldChanged) {
133
// JavaClass beanClass = origEntityHelper.getBeanClass();
134
// Utils.removeMethod(beanClass, origGetterMethod);
135
// Utils.removeMethod(beanClass, origSetterMethod);
136
// }
137
}
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 // entityMethodController.beginWriteJmiTransaction();
149
// boolean rollback = true;
150
// try {
151
// String typeName = fieldType == null ? getEntity(opositeEjbName).getLocal() : fieldType;
152
// Type type = JMIUtils.resolveType(typeName);
153
// Method getterMethod = entityHelper.getGetterMethod(fieldName);
154
// if (getterMethod == null) {
155
// getterMethod = entityHelper.createAccessMethod(fieldName, type, true);
156
// }
157
// Method setterMethod = entityHelper.getSetterMethod(fieldName, getterMethod);
158
// if (setterMethod == null) {
159
// setterMethod = entityHelper.createAccessMethod(fieldName, type, false);
160
// }
161
// if (getter && getterChanged) {
162
// entityMethodController.addMethod(getterMethod, true, true);
163
// }
164
// if (setter && setterChanged) {
165
// entityMethodController.addMethod(setterMethod, true, true);
166
// }
167
// rollback = false;
168
// } finally {
169
// entityMethodController.endWriteJmiTransaction(rollback);
170
// }
171
}
172                 }
173             }
174         }
175
176         private boolean equal(String JavaDoc s1, String JavaDoc 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 JavaDoc getFieldType() {
213             return (String JavaDoc) fieldTypeComboBox.getSelectedItem();
214         }
215
216         private void setFieldType(String JavaDoc fieldType) {
217             if (fieldType != null) {
218                 lastFieldType = fieldType;
219             }
220             fieldTypeComboBox.setSelectedItem(fieldType);
221         }
222
223         private String JavaDoc getRoleName() {
224             String JavaDoc roleName = roleNameTextField.getText().trim();
225             return roleName.length() == 0 ? null : roleName;
226         }
227
228         private void setRoleName(String JavaDoc roleName) {
229             roleNameTextField.setText(roleName);
230         }
231
232         private String JavaDoc getEjbName() {
233             return (String JavaDoc) ejbComboBox.getSelectedItem();
234         }
235
236         private void setEjbName(String JavaDoc 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 //// origGetterMethod = origEntityHelper.getGetterMethod(origFieldName);
261
//// origSetterMethod = origEntityHelper.getSetterMethod(origFieldName, origGetterMethod);
262
//// JavaClass localBusinessInterfaceClass = origEntityHelper.getLocalBusinessInterfaceClass();
263
// origGetter = Utils.getMethod(localBusinessInterfaceClass, origGetterMethod) != null;
264
// origSetter = Utils.getMethod(localBusinessInterfaceClass, origSetterMethod) != null;
265
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 JavaDoc getFieldName() {
289             return fieldNameTextField.getText().trim();
290         }
291
292         private void setFieldName(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc validateFieldName() {
345             if(isCreateCmrField()) {
346                 String JavaDoc 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 JavaDoc 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 // listener.validate();
396
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 JavaDoc getEntities() {
412         Vector JavaDoc entityNames = new Vector JavaDoc();
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 JavaDoc relationName = relationshipNameTextField.getText().trim();
425         if (relationName.length() == 0) {
426             relationName = roleA.getEjbName() + "-" + roleB.getEjbName(); //NOI18N
427
}
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 JavaDoc 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 JavaDoc {
490
491         public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc, ActionListener JavaDoc {
502
503         private final JLabel errorLabel;
504         private final DialogDescriptor dialogDescriptor;
505 // protected JMIUtils.ClassPathScanHelper classPathScanHelper;
506

507         public DialogListener(JLabel errorLabel, DialogDescriptor dialogDescriptor) {
508             this.errorLabel = errorLabel;
509             this.dialogDescriptor = dialogDescriptor;
510 // classPathScanHelper = new JMIUtils.ClassPathScanHelper() {
511
// public void scanFinished() {
512
// validateFields();
513
// }
514
// };
515
}
516
517         public void changedUpdate(DocumentEvent JavaDoc e) {
518             validateFields();
519         }
520
521         public void insertUpdate(DocumentEvent JavaDoc e) {
522             validateFields();
523         }
524
525         public void removeUpdate(DocumentEvent JavaDoc e) {
526             validateFields();
527         }
528
529         public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc roleNameA = roleA.getRoleName();
542             final String JavaDoc roleNameB = roleB.getRoleName();
543             if (false /*classPathScanHelper.isScanInProgress()*/) {
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 JavaDoc s1 = roleA.validateFieldName();
559                 if (s1 != null) {
560                     errorLabel.setText(s1);
561                     dialogDescriptor.setValid(false);
562                 } else {
563                     String JavaDoc 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 JavaDoc str){
576             return null == str || "".equals(str.trim());
577         }
578     }
579 }
580
Popular Tags