1 19 20 package org.netbeans.modules.web.jsf.palette.items; 21 import java.awt.Dialog ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.io.IOException ; 25 import javax.lang.model.element.TypeElement; 26 import javax.swing.UIManager ; 27 import javax.swing.event.DocumentEvent ; 28 import javax.swing.event.DocumentListener ; 29 import javax.swing.text.JTextComponent ; 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 42 public final class JsfFormCustomizer extends javax.swing.JPanel implements DocumentListener { 43 44 private Dialog dialog = null; 45 private DialogDescriptor descriptor = null; 46 private boolean dialogOK = false; 47 48 private final boolean hasModuleJsf; 49 JsfForm jsfTable; 50 JTextComponent target; 51 52 public JsfFormCustomizer(JsfForm jsfTable, JTextComponent target) { 53 this.jsfTable = jsfTable; 54 this.target = target; 55 56 initComponents(); 57 hasModuleJsf = JsfForm.hasModuleJsf(target); 58 errorField.setForeground(UIManager.getColor("nb.errorForeground")); classTextField.getDocument().addDocumentListener(this); 60 } 61 62 public boolean showDialog() { 63 64 dialogOK = false; 65 66 String 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 () { 72 public void actionPerformed(ActionEvent 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 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 105 private void initComponents() { 107 jFileChooser1 = new javax.swing.JFileChooser (); 108 populate = new javax.swing.ButtonGroup (); 109 viewType = new javax.swing.ButtonGroup (); 110 empty = new javax.swing.JRadioButton (); 111 fromBean = new javax.swing.JRadioButton (); 112 jPanel2 = new javax.swing.JPanel (); 113 jLabel1 = new javax.swing.JLabel (); 114 classTextField = new javax.swing.JTextField (); 115 jButton1 = new javax.swing.JButton (); 116 detail = new javax.swing.JRadioButton (); 117 edit = new javax.swing.JRadioButton (); 118 jLabel2 = new javax.swing.JLabel (); 119 errorField = new javax.swing.JLabel (); 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 (0, 0, 0, 0)); 128 empty.addItemListener(new java.awt.event.ItemListener () { 129 public void itemStateChanged(java.awt.event.ItemEvent 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 (0, 0, 0, 0)); 138 fromBean.addItemListener(new java.awt.event.ItemListener () { 139 public void itemStateChanged(java.awt.event.ItemEvent 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 () { 149 public void actionPerformed(java.awt.event.ActionEvent 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 (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 (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 } 238 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { } 243 private void emptyItemStateChanged(java.awt.event.ItemEvent evt) { checkStatus(); 245 } 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 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 documentEvent) { 273 checkStatus(); 274 } 275 276 public void removeUpdate(DocumentEvent documentEvent) { 277 checkStatus(); 278 } 279 280 public void changedUpdate(DocumentEvent documentEvent) { 281 checkStatus(); 282 } 283 284 private javax.swing.JTextField classTextField; 286 private javax.swing.JRadioButton detail; 287 private javax.swing.JRadioButton edit; 288 private javax.swing.JRadioButton empty; 289 private javax.swing.JLabel errorField; 290 private javax.swing.JRadioButton fromBean; 291 private javax.swing.JButton jButton1; 292 private javax.swing.JFileChooser jFileChooser1; 293 private javax.swing.JLabel jLabel1; 294 private javax.swing.JLabel jLabel2; 295 private javax.swing.JPanel jPanel2; 296 private javax.swing.ButtonGroup populate; 297 private javax.swing.ButtonGroup viewType; 298 300 protected static boolean classExists(final JTextComponent jTextComponent) throws IOException { 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 { 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 |