1 19 20 package org.netbeans.modules.web.jsf.wizards; 21 22 import java.io.BufferedWriter ; 23 import java.io.IOException ; 24 import java.io.OutputStreamWriter ; 25 import java.lang.reflect.Modifier ; 26 import java.text.MessageFormat ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import javax.swing.JEditorPane ; 34 import javax.swing.text.BadLocationException ; 35 import org.netbeans.api.project.Project; 36 import org.netbeans.api.project.SourceGroup; 37 import org.netbeans.api.project.Sources; 38 import org.netbeans.editor.BaseDocument; 39 import org.netbeans.modules.web.api.webmodule.WebModule; 40 import org.netbeans.modules.web.api.webmodule.WebProjectConstants; 41 import org.netbeans.modules.web.jsf.JSFConfigDataObject; 42 import org.netbeans.modules.web.jsf.JSFConfigUtilities; 43 import org.netbeans.modules.web.jsf.JSFFrameworkProvider; 44 import org.netbeans.modules.web.jsf.palette.items.JsfForm; 45 import org.netbeans.modules.web.jsf.palette.items.JsfTable; 46 import org.openide.ErrorManager; 47 import org.openide.filesystems.FileLock; 48 import org.openide.filesystems.FileObject; 49 import org.openide.filesystems.FileSystem; 50 import org.openide.filesystems.FileUtil; 51 import org.openide.loaders.DataObject; 52 import org.openide.util.Utilities; 53 54 58 public class JSFClientGenerator { 59 60 private static String INDEX_PAGE = "index.jsp"; 61 public static void generateJSFPages(Project project, String entityClass, String jsfFolder, String controllerClass, FileObject pkg) throws IOException { 63 65 String simpleControllerName = simpleClassName(controllerClass); 66 String simpleEntityName = simpleClassName(entityClass); 67 if (jsfFolder.startsWith("/")) { 68 jsfFolder = jsfFolder.substring(1); 69 } 70 71 Sources srcs = (Sources) project.getLookup().lookup(Sources.class); 72 String pkgName = controllerClass.substring(0, controllerClass.lastIndexOf('.')); 73 74 String persistenceUnit = null; 75 492 } 493 494 1140 private static HashSet <String > CONVERTED_TYPES = new HashSet <String >(); 1141 static { 1142 CONVERTED_TYPES.add("Boolean"); 1143 CONVERTED_TYPES.add("Byte"); 1144 CONVERTED_TYPES.add("Double"); 1145 CONVERTED_TYPES.add("Float"); 1146 CONVERTED_TYPES.add("Integer"); 1147 CONVERTED_TYPES.add("Long"); 1148 CONVERTED_TYPES.add("Short"); 1149 CONVERTED_TYPES.add("StringBuffer"); 1150 } 1151 private static HashMap <String ,String > PRIMITIVE_TYPES = new HashMap <String , String >(); 1152 static { 1153 PRIMITIVE_TYPES.put("boolean", "Boolean"); 1154 PRIMITIVE_TYPES.put("byte", "Byte"); 1155 PRIMITIVE_TYPES.put("double", "Double"); 1156 PRIMITIVE_TYPES.put("float", "Float"); 1157 PRIMITIVE_TYPES.put("int", "Integer"); 1158 PRIMITIVE_TYPES.put("long", "Long"); 1159 PRIMITIVE_TYPES.put("short", "Short"); 1160 } 1161 1162 1163 private static String createIdFieldDeclaration(String idPropertyType, String valueVar) { 1164 String idField; 1165 if (idPropertyType.startsWith("java.lang.")) { 1166 String shortName = idPropertyType.substring(10); 1167 idField = shortName + " id = " + createIdFieldInitialization(idPropertyType, valueVar) + ";"; 1168 } else if (idPropertyType.equals("java.math.BigInteger") || "BigInteger".equals(idPropertyType)) { 1169 idField = "java.math.BigInteger id = " + createIdFieldInitialization(idPropertyType, valueVar) + ";"; 1170 } else if (idPropertyType.equals("java.math.BigDecimal") || "BigDecimal".equals(idPropertyType)) { 1171 idField = "java.math.BigDecimal id = " + createIdFieldInitialization(idPropertyType, valueVar) + ";"; 1172 } else { 1173 idField = idPropertyType + " id = " + createIdFieldInitialization(idPropertyType, valueVar) + ";"; 1174 } 1175 return idField; 1176 } 1177 1178 1179 private static String createIdFieldInitialization(String idPropertyType, String valueVar) { 1180 String idField; 1181 if ("char".equals(idPropertyType)) { 1183 idField = valueVar + ".charAt(0);"; 1184 } else if (PRIMITIVE_TYPES.containsKey(idPropertyType)) { 1185 String objectType = PRIMITIVE_TYPES.get(idPropertyType); 1186 String methodName = "parse" + idPropertyType.substring(0,1).toUpperCase() + idPropertyType.substring(1); 1187 idField = objectType + "." + methodName + "(" + valueVar + ")"; 1188 } else if (idPropertyType.equals("java.math.BigInteger") || "BigInteger".equals(idPropertyType)) { 1189 idField = "new java.math.BigInteger(" + valueVar + ")"; 1190 } else if (idPropertyType.equals("java.math.BigDecimal") || "BigDecimal".equals(idPropertyType)) { 1191 idField = "new java.math.BigDecimal(" + valueVar + ")"; 1192 } else if (idPropertyType.equals("java.lang.String") || "String".equals(idPropertyType)) { 1193 idField = valueVar; 1194 } else if (idPropertyType.startsWith("java.lang.")) { 1195 String shortName = idPropertyType.substring(10); 1196 idField = "new " + shortName + "(" + valueVar + ")"; 1197 } else if (CONVERTED_TYPES.contains(idPropertyType)) { 1198 idField = "new " + idPropertyType + "(" + valueVar + ")"; 1199 } else { 1200 idField = "(" + idPropertyType + ") FacesContext.getCurrentInstance().getApplication().\n" 1201 + "createConverter(" + idPropertyType + ".class).getAsObject(FacesContext.\n" 1202 + "getCurrentInstance(), null, " + valueVar + ")"; 1203 } 1204 return idField; 1205 } 1206 1207 public static String simpleClassName(String fqn) { 1208 int lastDot = fqn.lastIndexOf('.'); 1209 return lastDot > 0 ? fqn.substring(lastDot + 1) : fqn; 1210 } 1211 1212 public static String fieldFromClassName(String className) { 1213 boolean makeFirstLower = className.length() == 1 || (!Character.isUpperCase(className.charAt(1))); 1214 String candidate = makeFirstLower ? className.substring(0,1).toLowerCase() + className.substring(1) : className; 1215 if (!Utilities.isJavaIdentifier(candidate)) { 1216 candidate += "1"; } 1218 return candidate; 1219 } 1220 1221 public static String getManagedBeanName(String simpleEntityName) { 1222 int len = simpleEntityName.length(); 1223 return len > 1 ? simpleEntityName.substring(0,1).toLowerCase() + simpleEntityName.substring(1) : simpleEntityName.toLowerCase(); 1224 } 1225 1226 public static String getPropNameFromMethod(String name) { 1227 boolean makeFirstLower = name.length() < 5 || (!Character.isUpperCase(name.charAt(4))); 1230 return makeFirstLower ? name.substring(3,4).toLowerCase() + name.substring(4) : name.substring(3); 1231 } 1232} 1233 | Popular Tags |