KickJava   Java API By Example, From Geeks To Geeks.

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


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.CmpField;
23 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EntityMethodController;
24 import org.openide.DialogDisplayer;
25 import org.openide.NotifyDescriptor;
26 import org.openide.util.NbBundle;
27
28 import java.io.IOException JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  * @author pfiala
34  */

35 public class CmpFieldHelper {
36
37     private EntityHelper entityHelper;
38     private CmpField field;
39     public static final String JavaDoc PROPERTY_FIELD_ROW_CHANGED = "FIELD_ROW_CHANGED"; // NOI18N
40

41     public CmpFieldHelper(EntityHelper entityHelper, CmpField field) {
42         this.entityHelper = entityHelper;
43         this.field = field;
44     }
45
46 // public String getTypeString() {
47
// Type type = getType();
48
// return type == null ? null : type.getName();
49
// }
50

51 // public Type getType() {
52
// Method getterMethod = entityHelper.getGetterMethod(getFieldName());
53
// return getterMethod == null ? null : getterMethod.getType();
54
// }
55

56 // public void reloadType() {
57
// setType(getTypeString());
58
// }
59

60 // public void setType(String typeString) {
61
// setType(JMIUtils.resolveType(typeString));
62
// }
63

64 // public void setType(Type newType) {
65
// EntityMethodController entityMethodController = entityHelper.getEntityMethodController();
66
// entityMethodController.beginWriteJmiTransaction();
67
// boolean rollback = true;
68
// try {
69
// if (newType instanceof PrimitiveType && isPrimary()) {
70
// newType = JMIUtils.getPrimitiveTypeWrapper((PrimitiveType) newType);
71
// }
72
// String fieldName = getFieldName();
73
// Method getterMethod = entityHelper.getGetterMethod(fieldName);
74
// Method setterMethod = entityHelper.getSetterMethod(fieldName, getterMethod);
75
// boolean primary = isPrimary();
76
// JavaClass localBusinessInterfaceClass = entityHelper.getLocalBusinessInterfaceClass();
77
// entityMethodController.registerClassForSave(localBusinessInterfaceClass);
78
// JavaClass remoteBusinessInterfaceClass = entityHelper.getRemoteBusinessInterfaceClass();
79
// entityMethodController.registerClassForSave(remoteBusinessInterfaceClass);
80
// changeReturnType(Utils.getMethod(localBusinessInterfaceClass, getterMethod), newType);
81
// Utils.changeParameterType(Utils.getMethod(localBusinessInterfaceClass, setterMethod), newType);
82
// changeReturnType(Utils.getMethod(remoteBusinessInterfaceClass, getterMethod), newType);
83
// Utils.changeParameterType(Utils.getMethod(remoteBusinessInterfaceClass, setterMethod), newType);
84
// changeReturnType(getterMethod, newType);
85
// Utils.changeParameterType(setterMethod, newType);
86
// newType = JMIUtils.resolveType(getTypeString());
87
// if (primary) {
88
// entityHelper.setPrimKeyClass(newType);
89
// }
90
// JavaClass beanClass = entityHelper.getBeanClass();
91
// if (beanClass != null) {
92
// entityMethodController.registerClassForSave(beanClass);
93
// Method[] methods = JMIUtils.getMethods(beanClass);
94
// for (int i = 0; i < methods.length; i++) {
95
// Method method = methods[i];
96
// String name = method.getName();
97
// boolean isCreate = "ejbCreate".equals(name);
98
// boolean isPostCreate = "ejbPostCreate".equals(name);
99
// if (isCreate && primary) {
100
// changeReturnType(method, newType);
101
// }
102
// if (isCreate || isPostCreate) {
103
// List parameters = method.getParameters();
104
// for (Iterator it1 = parameters.iterator(); it1.hasNext();) {
105
// Parameter parameter = (Parameter) it1.next();
106
// if (fieldName.equals(parameter.getName())) {
107
// parameter.setType(newType);
108
// break;
109
// }
110
// }
111
// }
112
// }
113
// }
114
// rollback = false;
115
// } finally {
116
// entityMethodController.endWriteJmiTransaction(rollback);
117
// }
118
// entityHelper.cmpFields.firePropertyChange(null);
119
// modelUpdatedFromUI();
120
// }
121

122     private void modelUpdatedFromUI() {
123         entityHelper.modelUpdatedFromUI();
124     }
125
126 // private void changeReturnType(Method method, Type type) {
127
// if (method != null) {
128
// method.setType(type);
129
// }
130
// }
131

132 // public boolean hasLocalGetter() {
133
// return getLocalGetter() != null;
134
// }
135
//
136
// private Method getLocalGetter() {
137
// return entityHelper.getEntityMethodController().getGetterMethod(getFieldName(), true);
138
// }
139
//
140
// public boolean hasLocalSetter() {
141
// return getLocalSetter() != null;
142
// }
143
//
144
// private Method getLocalSetter() {
145
// return entityHelper.getEntityMethodController().getSetterMethod(getFieldName(), true);
146
// }
147
//
148
// public boolean hasRemoteGetter() {
149
// return getRemoteGetter() != null;
150
// }
151
//
152
// private Method getRemoteGetter() {
153
// return entityHelper.getEntityMethodController().getGetterMethod(getFieldName(), false);
154
// }
155
//
156
// public boolean hasRemoteSetter() {
157
// return getRemoteSetter() != null;
158
// }
159
//
160
// private Method getRemoteSetter() {
161
// return entityHelper.getEntityMethodController().getSetterMethod(getFieldName(), false);
162
// }
163

164     public void setLocalGetter(boolean create) {
165         entityHelper.updateFieldAccessor(getFieldName(), true, true, create);
166     }
167
168     public void setLocalSetter(boolean create) {
169         entityHelper.updateFieldAccessor(getFieldName(), false, true, create);
170     }
171
172     public void setRemoteGetter(boolean create) {
173         entityHelper.updateFieldAccessor(getFieldName(), true, false, create);
174     }
175
176     public void setRemoteSetter(boolean create) {
177         entityHelper.updateFieldAccessor(getFieldName(), false, false, create);
178     }
179
180     public boolean deleteCmpField() {
181         String JavaDoc message = NbBundle.getMessage(CmpFieldHelper.class, "MSG_ConfirmDeleteField", field.getFieldName());
182         String JavaDoc title = NbBundle.getMessage(CmpFieldHelper.class, "MSG_ConfirmDeleteFieldTitle");
183         NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message, title, NotifyDescriptor.YES_NO_OPTION);
184         if (NotifyDescriptor.YES_OPTION.equals(DialogDisplayer.getDefault().notify(desc))) {
185             EntityMethodController entityMethodController = entityHelper.getEntityMethodController();
186             try {
187                 entityMethodController.deleteField(field, entityHelper.ejbJarFile);
188                 modelUpdatedFromUI();
189                 return true;
190             } catch (IOException JavaDoc e) {
191                 Utils.notifyError(e);
192             }
193         }
194         return false;
195     }
196
197
198 // private static void removeMethod(JavaClass interfaceClass, Method method) {
199
// if (Utils.getMethod(interfaceClass, method) != null) {
200
// Utils.removeMethod(interfaceClass, method);
201
// }
202
// }
203

204     public void setFieldName(String JavaDoc newName) {
205 // final IllegalArgumentException ex = FieldCustomizer.validateFieldName(newName);
206
// if (ex != null) {
207
// ErrorManager.getDefault().notify(ex);
208
// return;
209
// }
210
// String fieldName = getFieldName();
211
// boolean primary = isPrimary();
212
// final int oldFieldRow = entityHelper.cmpFields. getFieldRow(field);
213
// RefactoringSession refactoringSession = RefactoringSession.create("Rename");
214
// Method[] methods = JMIUtils.getMethods(entityHelper.getBeanClass());
215
// Method ejbPostCreateMethod = (Method) findNamedElement(methods, "ejbPostCreate");
216
// Method ejbCreateMethod = (Method) findNamedElement(methods, "ejbCreate");
217
// if (ejbCreateMethod != null) {
218
// Parameter[] parameters = (Parameter[]) ejbCreateMethod.getParameters().toArray(new Parameter[0]);
219
// NamedElement parameter = findNamedElement(parameters, fieldName);
220
// if (parameter != null) {
221
// prepareRename(refactoringSession, parameter, newName);
222
// }
223
// }
224
// if (ejbPostCreateMethod != null) {
225
// Parameter[] parameters = (Parameter[]) ejbPostCreateMethod.getParameters().toArray(new Parameter[0]);
226
// NamedElement parameter = findNamedElement(parameters, fieldName);
227
// if (parameter != null) {
228
// prepareRename(refactoringSession, parameter, newName);
229
// }
230
// }
231
// Method getterMethod = entityHelper.getGetterMethod(fieldName);
232
// Method setterMethod = entityHelper.getSetterMethod(fieldName, getterMethod);
233
// String getterName = EntityMethodController.getMethodName(newName, true);
234
// String setterName = EntityMethodController.getMethodName(newName, false);
235
// prepareRename(refactoringSession, getterMethod, getterName);
236
// prepareRename(refactoringSession, setterMethod, setterName);
237
// prepareRename(refactoringSession, getLocalGetter(), getterName);
238
// prepareRename(refactoringSession, getLocalSetter(), setterName);
239
// prepareRename(refactoringSession, getRemoteGetter(), getterName);
240
// prepareRename(refactoringSession, getRemoteSetter(), setterName);
241
// refactoringSession.doRefactoring(true);
242
// field.setFieldName(newName);
243
// if (primary) {
244
// entityHelper.setPrimkeyFieldName(newName);
245
// }
246
// final int newFieldRow = entityHelper.cmpFields.getFieldRow(field);
247
// if (oldFieldRow != newFieldRow) {
248
// entityHelper.cmpFields.firePropertyChange(new PropertyChangeEvent(entityHelper.cmpFields,
249
// PROPERTY_FIELD_ROW_CHANGED, new Integer(oldFieldRow), new Integer(newFieldRow)));
250
// }
251
// modelUpdatedFromUI();
252
}
253
254 // private static void prepareRename(RefactoringSession refactoringSession, NamedElement element, String newName) {
255
// if (element != null) {
256
// RenameRefactoring refactoring = new RenameRefactoring(element);
257
// refactoring.setNewName(newName);
258
// refactoring.prepare(refactoringSession);
259
// }
260
// }
261

262 // private static NamedElement findNamedElement(NamedElement[] elements, String name) {
263
// for (int i = 0; i < elements.length; i++) {
264
// NamedElement element = elements[i];
265
// if (name.equals(element.getName())) {
266
// return element;
267
// }
268
// }
269
// return null;
270
// }
271

272     public void setDescription(String JavaDoc s) {
273         field.setDescription(s);
274         modelUpdatedFromUI();
275     }
276
277     public String JavaDoc getDefaultDescription() {
278         return field.getDefaultDescription();
279     }
280
281     public String JavaDoc getFieldName() {
282         return field.getFieldName();
283     }
284
285     public boolean isPrimary() {
286         return getFieldName().equals(entityHelper.getPrimkeyField());
287     }
288
289     public boolean edit() {
290 // Field field = JavaModel.getDefaultExtent().getField().createField();
291
// String fieldName = getFieldName();
292
// field.setName(fieldName);
293
// field.setType(JMIUtils.resolveType(getTypeString()));
294
// FieldCustomizer customizer = new FieldCustomizer(field, getDefaultDescription(),
295
// entityHelper.hasLocalInterface(), entityHelper.hasRemoteInterface(), hasLocalGetter(),
296
// hasLocalSetter(), hasRemoteGetter(), hasRemoteSetter());
297
// while (openEditCmpFieldDialog(customizer)) {
298
// customizer.isOK(); // apply possible changes in dialog fields
299
// String newFieldName = field.getName();
300
// if (!fieldName.equals(newFieldName)) {
301
// try {
302
// entityHelper.getEntityMethodController().validateNewCmpFieldName(newFieldName);
303
// } catch (IllegalArgumentException ex) {
304
// Utils.notifyError(ex);
305
// continue;
306
// }
307
// }
308
// Utils.beginJmiTransaction(true);
309
// boolean rollback = true;
310
// try {
311
// setFieldName(newFieldName);
312
// setType(field.getType());
313
// setDescription(customizer.getDescription());
314
// setLocalGetter(customizer.isLocalGetter());
315
// setLocalSetter(customizer.isLocalSetter());
316
// setRemoteGetter(customizer.isRemoteGetter());
317
// setRemoteSetter(customizer.isRemoteSetter());
318
// rollback = false;
319
// } finally {
320
// Utils.endJmiTransaction(rollback);
321
// }
322
// modelUpdatedFromUI();
323
// return true;
324
// }
325
return false;
326     }
327
328 // private boolean openEditCmpFieldDialog(FieldCustomizer customizer) {
329
// String title = Utils.getBundleMessage("LBL_EditCmpField");
330
// NotifyDescriptor nd = new NotifyDescriptor(customizer, title, NotifyDescriptor.OK_CANCEL_OPTION,
331
// NotifyDescriptor.PLAIN_MESSAGE, null, null);
332
// return DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.OK_OPTION;
333
// }
334

335 }
336
Popular Tags