KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.Dialog JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import javax.lang.model.element.TypeElement;
26 import javax.swing.UIManager JavaDoc;
27 import javax.swing.event.DocumentEvent JavaDoc;
28 import javax.swing.event.DocumentListener JavaDoc;
29 import javax.swing.text.JTextComponent JavaDoc;
30 import org.netbeans.api.java.source.CompilationController;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.modules.j2ee.common.source.AbstractTask;
33 import org.openide.DialogDescriptor;
34 import org.openide.filesystems.FileObject;
35 import org.openide.DialogDisplayer;
36 import org.openide.ErrorManager;
37 import org.openide.util.NbBundle;
38
39 /**
40  * @author Pavel Buzek
41  */

42 public final class JsfFormCustomizer extends javax.swing.JPanel JavaDoc implements DocumentListener JavaDoc {
43     
44     private Dialog JavaDoc dialog = null;
45     private DialogDescriptor descriptor = null;
46     private boolean dialogOK = false;
47
48     private final boolean hasModuleJsf;
49     JsfForm jsfTable;
50     JTextComponent JavaDoc target;
51             
52     public JsfFormCustomizer(JsfForm jsfTable, JTextComponent JavaDoc target) {
53         this.jsfTable = jsfTable;
54         this.target = target;
55         
56         initComponents();
57         hasModuleJsf = JsfForm.hasModuleJsf(target);
58         errorField.setForeground(UIManager.getColor("nb.errorForeground")); //NOI18N
59
classTextField.getDocument().addDocumentListener(this);
60     }
61     
62     public boolean showDialog() {
63         
64         dialogOK = false;
65         
66         String JavaDoc displayName = NbBundle.getMessage(JsfFormCustomizer.class, "NAME_jsp-JsfForm");
67         
68         descriptor = new DialogDescriptor
69                 (this, NbBundle.getMessage(JsfFormCustomizer.class, "LBL_Customizer_InsertPrefix") + " " + displayName, true,
70                  DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION,
71                  new ActionListener JavaDoc() {
72                      public void actionPerformed(ActionEvent JavaDoc actionEvent) {
73                         if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) {
74                             evaluateInput();
75                             dialogOK = true;
76                         }
77                         dialog.dispose();
78              }
79          }
80                 );
81         checkStatus();
82         dialog = DialogDisplayer.getDefault().createDialog(descriptor);
83         dialog.setVisible(true);
84         repaint();
85         
86         return dialogOK;
87     }
88     
89     private void evaluateInput() {
90         
91         String JavaDoc entityClass = classTextField.getText();
92         jsfTable.setBean(entityClass);
93         
94         jsfTable.setVariable("anInstanceOf" + entityClass);
95         
96         int formType = empty.isSelected() ? 0 : detail.isSelected() ? 1 : 2;
97         jsfTable.setFormType(formType);
98     }
99     
100     /** This method is called from within the constructor to
101      * initialize the form.
102      * WARNING: Do NOT modify this code. The content of this method is
103      * always regenerated by the Form Editor.
104      */

105     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
106
private void initComponents() {
107         jFileChooser1 = new javax.swing.JFileChooser JavaDoc();
108         populate = new javax.swing.ButtonGroup JavaDoc();
109         viewType = new javax.swing.ButtonGroup JavaDoc();
110         empty = new javax.swing.JRadioButton JavaDoc();
111         fromBean = new javax.swing.JRadioButton JavaDoc();
112         jPanel2 = new javax.swing.JPanel JavaDoc();
113         jLabel1 = new javax.swing.JLabel JavaDoc();
114         classTextField = new javax.swing.JTextField JavaDoc();
115         jButton1 = new javax.swing.JButton JavaDoc();
116         detail = new javax.swing.JRadioButton JavaDoc();
117         edit = new javax.swing.JRadioButton JavaDoc();
118         jLabel2 = new javax.swing.JLabel JavaDoc();
119         errorField = new javax.swing.JLabel JavaDoc();
120
121         jFileChooser1.setCurrentDirectory(null);
122
123         populate.add(empty);
124         empty.setSelected(true);
125         org.openide.awt.Mnemonics.setLocalizedText(empty, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_Empty_Form"));
126         empty.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
127         empty.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
128         empty.addItemListener(new java.awt.event.ItemListener JavaDoc() {
129             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
130                 emptyItemStateChanged(evt);
131             }
132         });
133
134         populate.add(fromBean);
135         org.openide.awt.Mnemonics.setLocalizedText(fromBean, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_Form_From_Entity"));
136         fromBean.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
137         fromBean.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
138         fromBean.addItemListener(new java.awt.event.ItemListener JavaDoc() {
139             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
140                 emptyItemStateChanged(evt);
141             }
142         });
143
144         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_GetProperty_Bean"));
145
146         org.openide.awt.Mnemonics.setLocalizedText(jButton1, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_Browse"));
147         jButton1.setEnabled(false);
148         jButton1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
149             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
150                 jButton1ActionPerformed(evt);
151             }
152         });
153
154         viewType.add(detail);
155         detail.setSelected(true);
156         org.openide.awt.Mnemonics.setLocalizedText(detail, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_View_Detail"));
157         detail.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
158         detail.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
159
160         viewType.add(edit);
161         org.openide.awt.Mnemonics.setLocalizedText(edit, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_View_Edit"));
162         edit.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
163         edit.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
164
165         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_From_Fields"));
166
167         org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
168         jPanel2.setLayout(jPanel2Layout);
169         jPanel2Layout.setHorizontalGroup(
170             jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
171             .add(jPanel2Layout.createSequentialGroup()
172                 .addContainerGap()
173                 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
174                     .add(jLabel2)
175                     .add(jLabel1))
176                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
177                 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
178                     .add(jPanel2Layout.createSequentialGroup()
179                         .add(edit)
180                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
181                         .add(detail))
182                     .add(classTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE))
183                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
184                 .add(jButton1))
185         );
186         jPanel2Layout.setVerticalGroup(
187             jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
188             .add(jPanel2Layout.createSequentialGroup()
189                 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
190                     .add(jLabel1)
191                     .add(jButton1)
192                     .add(classTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
193                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
194                 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
195                     .add(jLabel2)
196                     .add(edit)
197                     .add(detail)))
198         );
199
200         org.openide.awt.Mnemonics.setLocalizedText(errorField, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("MSG_No_Managed_Beans"));
201
202         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
203         this.setLayout(layout);
204         layout.setHorizontalGroup(
205             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
206             .add(layout.createSequentialGroup()
207                 .addContainerGap()
208                 .add(empty)
209                 .add(289, 289, 289))
210             .add(layout.createSequentialGroup()
211                 .addContainerGap()
212                 .add(fromBean)
213                 .addContainerGap(430, Short.MAX_VALUE))
214             .add(layout.createSequentialGroup()
215                 .add(27, 27, 27)
216                 .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
217                 .addContainerGap())
218             .add(layout.createSequentialGroup()
219                 .addContainerGap()
220                 .add(errorField)
221                 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
222         );
223         layout.setVerticalGroup(
224             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
225             .add(layout.createSequentialGroup()
226                 .addContainerGap()
227                 .add(empty)
228                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
229                 .add(fromBean)
230                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
231                 .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
232                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
233                 .add(errorField)
234                 .addContainerGap())
235         );
236     }// </editor-fold>//GEN-END:initComponents
237

238     private void jButton1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton1ActionPerformed
239
//TODO: RETOUCHE FQN search
240
// FQNSearch.showFastOpen(classTextField);
241
}//GEN-LAST:event_jButton1ActionPerformed
242

243     private void emptyItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_emptyItemStateChanged
244
checkStatus();
245     }//GEN-LAST:event_emptyItemStateChanged
246

247     private void checkStatus() {
248         if (empty.isSelected()) {
249             classTextField.setEnabled(false);
250             jButton1.setEnabled(false);
251             detail.setEnabled(false);
252             edit.setEnabled(false);
253         }
254         if (fromBean.isSelected()) {
255             classTextField.setEnabled(true);
256             jButton1.setEnabled(true);
257             detail.setEnabled(true);
258             edit.setEnabled(true);
259         }
260         boolean validClassName = false;
261         try {
262             validClassName = empty.isSelected() || classExists(classTextField);
263         } catch (IOException JavaDoc ioe) {
264             ErrorManager.getDefault().notify(ioe);
265         }
266         descriptor.setValid(hasModuleJsf && validClassName);
267         errorField.setText(hasModuleJsf ?
268                 (validClassName ? "" : java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("MSG_InvalidClassName")) :
269                 java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("MSG_NoJSF"));
270     }
271
272     public void insertUpdate(DocumentEvent JavaDoc documentEvent) {
273         checkStatus();
274     }
275
276     public void removeUpdate(DocumentEvent JavaDoc documentEvent) {
277         checkStatus();
278     }
279
280     public void changedUpdate(DocumentEvent JavaDoc documentEvent) {
281         checkStatus();
282     }
283     
284     // Variables declaration - do not modify//GEN-BEGIN:variables
285
private javax.swing.JTextField JavaDoc classTextField;
286     private javax.swing.JRadioButton JavaDoc detail;
287     private javax.swing.JRadioButton JavaDoc edit;
288     private javax.swing.JRadioButton JavaDoc empty;
289     private javax.swing.JLabel JavaDoc errorField;
290     private javax.swing.JRadioButton JavaDoc fromBean;
291     private javax.swing.JButton JavaDoc jButton1;
292     private javax.swing.JFileChooser JavaDoc jFileChooser1;
293     private javax.swing.JLabel JavaDoc jLabel1;
294     private javax.swing.JLabel JavaDoc jLabel2;
295     private javax.swing.JPanel JavaDoc jPanel2;
296     private javax.swing.ButtonGroup JavaDoc populate;
297     private javax.swing.ButtonGroup JavaDoc viewType;
298     // End of variables declaration//GEN-END:variables
299

300     protected static boolean classExists(final JTextComponent JavaDoc jTextComponent) throws IOException JavaDoc {
301         final boolean[] result = new boolean[] { false };
302         FileObject fileObject = JsfForm.getFO(jTextComponent);
303         JavaSource javaSource = JavaSource.forFileObject(fileObject);
304         javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
305             public void run(CompilationController controller) throws IOException JavaDoc {
306                 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
307                 TypeElement typeElement = controller.getElements().getTypeElement(jTextComponent.getText());
308                 result[0] = typeElement != null;
309             }
310         }, true);
311         return result[0];
312     }
313     
314 }
315
Popular Tags