1 19 20 package org.netbeans.modules.web.jsf.palette.items; 21 22 import java.io.IOException ; 23 import java.text.MessageFormat ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.lang.model.element.ExecutableElement; 27 import javax.lang.model.element.TypeElement; 28 import javax.lang.model.type.TypeMirror; 29 import javax.lang.model.util.ElementFilter; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.Caret ; 32 import javax.swing.text.JTextComponent ; 33 import org.netbeans.api.java.source.CompilationController; 34 import org.netbeans.api.java.source.JavaSource; 35 import org.netbeans.modules.j2ee.common.source.AbstractTask; 36 import org.netbeans.modules.web.api.webmodule.WebModule; 37 import org.netbeans.modules.web.jsf.api.ConfigurationUtils; 38 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModel; 39 import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean; 40 import org.netbeans.modules.web.jsf.palette.JSFPaletteUtilities; 41 import org.netbeans.modules.web.jsf.wizards.JSFClientGenerator; 42 import org.openide.filesystems.FileObject; 43 import org.openide.text.ActiveEditorDrop; 44 45 49 public final class JsfTable implements ActiveEditorDrop { 50 51 private static String [] BEGIN = { 52 "<h:form>\n <h:dataTable value=\"#'{'{0}'}'\" var=\"item\">\n", 53 "<h:form>\n <h1><h:outputText value=\"List\"/></h1>\n <h:dataTable value=\"#'{'{0}'}'\" var=\"item\">\n", 54 }; 55 private static String [] END = { 56 "</h:dataTable>\n </h:form>\n", 57 "</h:dataTable>\n </h:form>\n", 58 }; 59 private static String [] ITEM = { 60 "", 61 "<h:column>\n <f:facet name=\"header\">\n <h:outputText value=\"{0}\"/>\n </f:facet>\n <h:outputText value=\"#'{'item.{2}'}'\"/>\n</h:column>\n", 62 "<h:column>\n <f:facet name=\"header\">\n <h:outputText value=\"{0}\"/>\n </f:facet>\n <h:outputText value=\"#'{'item.{2}'}'\">\n <f:convertDateTime type=\"{3}\" pattern=\"{4}\" />\n</h:outputText>\n</h:column>\n", 63 "<h:column>\n <f:facet name=\"header\">\n <h:outputText value=\"{0}\"/>\n </f:facet>\n" 64 + "<h:commandLink action=\"#'{'{4}'}'\" value=\"#'{'item.{2}'}'\"/>\n </h:column>\n", 65 }; 66 67 private String variable = ""; 68 private String bean = ""; 69 private int formType = 0; 70 private ArrayList <ManagedBean> managedBeans; 71 72 public JsfTable() { 73 } 74 75 public boolean handleTransfer(JTextComponent targetComponent) { 76 JsfTableCustomizer jsfTableCustomizer = new JsfTableCustomizer(this, targetComponent); 77 boolean accept = jsfTableCustomizer.showDialog(); 78 if (accept) { 79 try { 80 Caret caret = targetComponent.getCaret(); 81 int position0 = Math.min(caret.getDot(), caret.getMark()); 82 int position1 = Math.max(caret.getDot(), caret.getMark()); 83 int len = targetComponent.getDocument().getLength() - position1; 84 boolean containsFView = targetComponent.getText(0, position0).contains("<f:view>") 85 && targetComponent.getText(position1, len).contains("</f:view>"); 86 String body = createBody(targetComponent, !containsFView); 87 JSFPaletteUtilities.insert(body, targetComponent); 88 } catch (IOException ioe) { 89 accept = false; 90 } catch (BadLocationException ble) { 91 accept = false; 92 } 93 } 94 return accept; 95 } 96 97 private String createBody(JTextComponent target, boolean surroundWithFView) throws IOException { 98 final StringBuffer stringBuffer = new StringBuffer (); 99 if (surroundWithFView) { 100 stringBuffer.append("<f:view>\n"); 101 } 102 stringBuffer.append(MessageFormat.format(BEGIN [formType], new Object [] {variable})); 103 104 FileObject fileObject = JsfForm.getFO(target); 105 JavaSource javaSource = JavaSource.forFileObject(fileObject); 106 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 107 public void run(CompilationController controller) throws IOException { 108 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 109 TypeElement typeElement = controller.getElements().getTypeElement(bean); 110 createTable(controller, typeElement, variable, stringBuffer, "", null); 111 } 112 }, true); 113 stringBuffer.append(END [formType]); 114 if (surroundWithFView) { 115 stringBuffer.append("</f:view>\n"); 116 } 117 return stringBuffer.toString(); 118 } 119 120 123 public static void createTable(CompilationController controller, TypeElement bean, String variable, StringBuffer stringBuffer, 124 String commands, String setupDetail) { 125 int formType = 1; 126 ExecutableElement[] methods = JsfForm.getEntityMethods(bean); 127 boolean fieldAccess = JsfForm.isFieldAccess(bean); 128 TypeMirror dateTypeMirror = controller.getElements().getTypeElement("java.util.Date").asType(); 129 for (ExecutableElement method : methods) { 130 String methodName = method.getSimpleName().toString(); 131 if (methodName.startsWith("get")) { 132 int isRelationship = JsfForm.isRelationship(controller, method, fieldAccess); 133 String name = methodName.substring(3); 134 String propName = name.substring(0,1).toLowerCase() + name.substring(1); 135 if (setupDetail != null && JsfForm.isId(controller, method, fieldAccess)) { 136 String managedBeanName = JSFClientGenerator.getManagedBeanName(bean.getSimpleName().toString()); 137 stringBuffer.append(MessageFormat.format(ITEM [3], new Object [] {name, variable, propName, propName, managedBeanName + "." + setupDetail})); 138 } else if (controller.getTypes().isSameType(dateTypeMirror, method.getReturnType())) { 139 String temporal = JsfForm.getTemporal(controller, method, fieldAccess); 141 if (temporal == null) { 142 stringBuffer.append(MessageFormat.format(ITEM [formType], new Object [] {name, variable, propName})); 143 } else { 144 stringBuffer.append(MessageFormat.format(ITEM [2], new Object [] {name, variable, propName, temporal, JsfForm.getDateTimeFormat(temporal)})); 145 } 146 } else if (isRelationship == JsfForm.REL_NONE || isRelationship == JsfForm.REL_TO_ONE) { 147 stringBuffer.append(MessageFormat.format(ITEM [formType], new Object [] {name, variable, propName})); 148 } 154 } 155 } 156 stringBuffer.append(MessageFormat.format(commands, new Object [] {"item"})); 157 } 158 159 public ManagedBean[] getManagedBeanNames(JTextComponent target) { 160 managedBeans = new ArrayList <ManagedBean>(); 161 FileObject fileObject = JsfForm.getFO(target); 162 if (fileObject != null) { 163 WebModule webModule = WebModule.getWebModule(fileObject); 164 for (FileObject fo : ConfigurationUtils.getFacesConfigFiles(webModule)) { 165 JSFConfigModel model = ConfigurationUtils.getConfigModel(fo, true); 166 managedBeans.addAll(model.getRootComponent().getManagedBeans()); 167 } 168 } 169 return managedBeans.toArray(new ManagedBean[managedBeans.size()]); 170 } 171 172 public String [] getMethodsForBean(final ManagedBean bean, JTextComponent target) throws IOException { 173 final List <String > result = new ArrayList <String >(); 174 FileObject fileObject = JsfForm.getFO(target); 175 JavaSource javaSource = JavaSource.forFileObject(fileObject); 176 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 177 public void run(CompilationController controller) throws IOException { 178 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 179 TypeElement typeElement = controller.getElements().getTypeElement(bean.getManagedBeanClass()); 180 for (ExecutableElement method : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 181 result.add(method.getSimpleName().toString()); 182 } 183 } 184 }, true); 185 return result.toArray(new String [result.size()]); 186 } 187 188 public String getVariable() { 189 return variable; 190 } 191 192 public void setVariable(String variable) { 193 this.variable = variable; 194 } 195 196 public String getBean() { 197 return bean; 198 } 199 200 public void setBean(String collection) { 201 this.bean = collection; 202 } 203 204 public int getFormType() { 205 return formType; 206 } 207 208 public void setFormType(int formType) { 209 this.formType = formType; 210 } 211 212 } 213 | Popular Tags |