KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > palette > items > JsfTable


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.web.jsf.palette.items;
21
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
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 JavaDoc;
31 import javax.swing.text.Caret JavaDoc;
32 import javax.swing.text.JTextComponent JavaDoc;
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 /**
46  *
47  * @author Pavel Buzek
48  */

49 public final class JsfTable implements ActiveEditorDrop {
50     
51     private static String JavaDoc [] 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 JavaDoc [] END = {
56         "</h:dataTable>\n </h:form>\n",
57         "</h:dataTable>\n </h:form>\n",
58     };
59     private static String JavaDoc [] 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 JavaDoc variable = "";
68     private String JavaDoc bean = "";
69     private int formType = 0;
70     private ArrayList JavaDoc<ManagedBean> managedBeans;
71     
72     public JsfTable() {
73     }
74     
75     public boolean handleTransfer(JTextComponent JavaDoc targetComponent) {
76         JsfTableCustomizer jsfTableCustomizer = new JsfTableCustomizer(this, targetComponent);
77         boolean accept = jsfTableCustomizer.showDialog();
78         if (accept) {
79             try {
80                 Caret JavaDoc 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 JavaDoc body = createBody(targetComponent, !containsFView);
87                 JSFPaletteUtilities.insert(body, targetComponent);
88             } catch (IOException JavaDoc ioe) {
89                 accept = false;
90             } catch (BadLocationException JavaDoc ble) {
91                 accept = false;
92             }
93         }
94         return accept;
95     }
96     
97     private String JavaDoc createBody(JTextComponent JavaDoc target, boolean surroundWithFView) throws IOException JavaDoc {
98         final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
99         if (surroundWithFView) {
100             stringBuffer.append("<f:view>\n");
101         }
102         stringBuffer.append(MessageFormat.format(BEGIN [formType], new Object JavaDoc [] {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 JavaDoc {
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     /** @param commands a message that will be added to the end of each line in table,
121      * it will be formated using {0} = iterator variable
122      */

123     public static void createTable(CompilationController controller, TypeElement bean, String JavaDoc variable, StringBuffer JavaDoc stringBuffer,
124             String JavaDoc commands, String JavaDoc 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 JavaDoc methodName = method.getSimpleName().toString();
131             if (methodName.startsWith("get")) {
132                 int isRelationship = JsfForm.isRelationship(controller, method, fieldAccess);
133                 String JavaDoc name = methodName.substring(3);
134                 String JavaDoc propName = name.substring(0,1).toLowerCase() + name.substring(1);
135                 if (setupDetail != null && JsfForm.isId(controller, method, fieldAccess)) {
136                     String JavaDoc managedBeanName = JSFClientGenerator.getManagedBeanName(bean.getSimpleName().toString());
137                     stringBuffer.append(MessageFormat.format(ITEM [3], new Object JavaDoc [] {name, variable, propName, propName, managedBeanName + "." + setupDetail}));
138                 } else if (controller.getTypes().isSameType(dateTypeMirror, method.getReturnType())) {
139                     //param 3 - temporal, param 4 - date/time format
140
String JavaDoc temporal = JsfForm.getTemporal(controller, method, fieldAccess);
141                     if (temporal == null) {
142                         stringBuffer.append(MessageFormat.format(ITEM [formType], new Object JavaDoc [] {name, variable, propName}));
143                     } else {
144                         stringBuffer.append(MessageFormat.format(ITEM [2], new Object JavaDoc [] {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 JavaDoc [] {name, variable, propName}));
148                     //links to related objects -- does not work correctly for composite IDs
149
// } else if (isRelationship == JsfForm.REL_TO_ONE) {
150
// String managedBeanName = JSFClientGenerator.getManagedBeanName(JSFClientGenerator.simpleClassName(methods[i].getType().getName()));
151
// String relatedIdProp = JSFClientGenerator.getPropNameFromMethod(JsfForm.getIdGetter(fieldAccess, (JavaClass) methods[i].getType()).getName());
152
// sb.append(MessageFormat.format(ITEM [3], new Object [] {name, variable, propName + "." + relatedIdProp , relatedIdProp, managedBeanName + "." + setupDetail}));
153
}
154             }
155         }
156         stringBuffer.append(MessageFormat.format(commands, new Object JavaDoc [] {"item"}));
157     }
158     
159     public ManagedBean[] getManagedBeanNames(JTextComponent JavaDoc target) {
160         managedBeans = new ArrayList JavaDoc<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 JavaDoc[] getMethodsForBean(final ManagedBean bean, JTextComponent JavaDoc target) throws IOException JavaDoc {
173         final List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
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 JavaDoc {
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 JavaDoc[result.size()]);
186     }
187     
188     public String JavaDoc getVariable() {
189         return variable;
190     }
191     
192     public void setVariable(String JavaDoc variable) {
193         this.variable = variable;
194     }
195     
196     public String JavaDoc getBean() {
197         return bean;
198     }
199     
200     public void setBean(String JavaDoc 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