1 19 20 package org.netbeans.modules.j2ee.common.method; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 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 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 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 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 { 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 arg0) { 98 validate(); 99 } 100 101 private boolean validate() { 102 String 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 String returnType = panel.getReturnType(); 118 if ("".equals(returnType)) { 119 setError(NbBundle.getMessage(FieldCustomizer.class, "ERROR_returnTypeInvalid")); 120 return false; 121 } 122 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 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 |