KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > method > FieldCustomizer


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.common.method;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import org.netbeans.modules.j2ee.common.method.impl.FieldCustomizerPanel;
25 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
26 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
27 import org.openide.DialogDescriptor;
28 import org.openide.DialogDisplayer;
29 import org.openide.NotifyDescriptor;
30 import org.openide.util.NbBundle;
31 import org.openide.util.Utilities;
32
33 /**
34  *
35  * @author Martin Adamek
36  */

37 public class FieldCustomizer {
38     
39     private final Entity entity;
40     private final FieldCustomizerPanel panel;
41     
42     public FieldCustomizer(Entity entity, MethodModel.Variable element, String JavaDoc description, boolean localEnabled, boolean remoteEnabled,
43             boolean localGetter, boolean localSetter, boolean remoteGetter, boolean remoteSetter) {
44         this.entity = entity;
45         this.panel = new FieldCustomizerPanel(element, description, localEnabled, remoteEnabled, localGetter, localSetter, remoteGetter, remoteSetter);
46     }
47     
48     public boolean customizeField() {
49         DialogDescriptor notifyDescriptor = new DialogDescriptor(
50                 panel,
51                 NbBundle.getMessage(FieldCustomizer.class, "LBL_AddCmpField"),
52                 true,
53                 DialogDescriptor.OK_CANCEL_OPTION,
54                 DialogDescriptor.PLAIN_MESSAGE,
55                 null
56                 );
57         panel.addPropertyChangeListener(new Validator(panel, notifyDescriptor, entity));
58         return DialogDisplayer.getDefault().notify(notifyDescriptor) == NotifyDescriptor.OK_OPTION;
59     }
60     
61     public MethodModel.Variable getField() {
62         return MethodModel.Variable.create(panel.getReturnType(), panel.getMethodName());
63     }
64     
65     public String JavaDoc getDescription() {
66         return panel.getDescription();
67     }
68
69     public boolean isLocalGetter() {
70         return panel.isLocalGetter();
71     }
72
73     public boolean isLocalSetter() {
74         return panel.isLocalSetter();
75     }
76
77     public boolean isRemoteGetter() {
78         return panel.isRemoteGetter();
79     }
80
81     public boolean isRemoteSetter() {
82         return panel.isRemoteSetter();
83     }
84
85     private static class Validator implements PropertyChangeListener JavaDoc {
86         
87         private final FieldCustomizerPanel panel;
88         private final NotifyDescriptor notifyDescriptor;
89         private final Entity entity;
90         
91         public Validator(FieldCustomizerPanel panel, NotifyDescriptor notifyDescriptor, Entity entity) {
92             this.panel = panel;
93             this.notifyDescriptor = notifyDescriptor;
94             this.entity = entity;
95         }
96         
97         public void propertyChange(PropertyChangeEvent JavaDoc arg0) {
98             validate();
99         }
100         
101         private boolean validate() {
102             // method name
103
String JavaDoc name = panel.getMethodName();
104             if (!Utilities.isJavaIdentifier(name)) {
105                 setError(NbBundle.getMessage(FieldCustomizer.class, "ERROR_nameNonJavaIdentifier"));
106                 return false;
107             }
108             CmpField[] cmpField = entity.getCmpField();
109             for (int i = 0; i < cmpField.length; i++) {
110                 CmpField field = cmpField[i];
111                 if (name.equals(field.getFieldName())) {
112                     setError(NbBundle.getMessage(FieldCustomizer.class, "MSG_Duplicate_Field_Name", name));
113                     return false;
114                 }
115             }
116             // return type
117
String JavaDoc returnType = panel.getReturnType();
118             if ("".equals(returnType)) {
119                 setError(NbBundle.getMessage(FieldCustomizer.class, "ERROR_returnTypeInvalid"));
120                 return false;
121             }
122             // interfaces
123
if (!panel.isLocalGetter() && !panel.isLocalSetter() && !panel.isRemoteGetter() && !panel.isRemoteSetter()) {
124                 setError(NbBundle.getMessage(FieldCustomizer.class, "ERROR_selectSomeInterface"));
125                 return false;
126             }
127             unsetError();
128             return true;
129         }
130         
131         private void setError(String JavaDoc message) {
132             notifyDescriptor.setValid(false);
133             panel.setError(message);
134         }
135         
136         private void unsetError() {
137             notifyDescriptor.setValid(true);
138             panel.setError("");
139         }
140
141     }
142     
143 }
144
Popular Tags