1 19 20 package org.netbeans.modules.web.jsf.palette.items; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.io.IOException ; 27 import javax.swing.JLabel ; 28 import javax.swing.JList ; 29 import javax.swing.ListCellRenderer ; 30 import javax.swing.UIManager ; 31 import javax.swing.event.DocumentEvent ; 32 import javax.swing.event.DocumentListener ; 33 import javax.swing.text.JTextComponent ; 34 import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean; 35 import org.openide.DialogDescriptor; 36 import org.openide.DialogDisplayer; 37 import org.openide.ErrorManager; 38 import org.openide.util.NbBundle; 39 40 43 public class JsfTableCustomizer extends javax.swing.JPanel implements DocumentListener { 44 45 private Dialog dialog = null; 46 private DialogDescriptor descriptor = null; 47 private boolean dialogOK = false; 48 49 private final boolean hasModuleJsf; 50 JsfTable jsfTable; 51 JTextComponent target; 52 53 public JsfTableCustomizer(JsfTable jsfTable, JTextComponent target) { 54 this.jsfTable = jsfTable; 55 this.target = target; 56 57 initComponents(); 58 errorField.setForeground(UIManager.getColor("nb.errorForeground")); hasModuleJsf = JsfForm.hasModuleJsf(target); 60 classTextField.getDocument().addDocumentListener(this); 61 } 62 63 public boolean showDialog() { 64 65 dialogOK = false; 66 67 String displayName = NbBundle.getMessage(JsfTableCustomizer.class, "NAME_jsp-JsfTable"); 69 descriptor = new DialogDescriptor 70 (this, NbBundle.getMessage(JsfTableCustomizer.class, "LBL_Customizer_InsertPrefix") + " " + displayName, true, 71 DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, 72 new ActionListener () { 73 public void actionPerformed(ActionEvent event) { 74 if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) { 75 evaluateInput(); 76 dialogOK = true; 77 } 78 dialog.dispose(); 79 } 80 } 81 ); 82 83 checkStatus(); 84 dialog = DialogDisplayer.getDefault().createDialog(descriptor); 85 dialog.setVisible(true); 86 repaint(); 87 88 return dialogOK; 89 } 90 91 private void evaluateInput() { 92 93 String entityClass = classTextField.getText(); 94 jsfTable.setBean(entityClass); 95 96 jsfTable.setVariable("arrayOrCollectionOf" + entityClass); 97 int formType = empty.isSelected() ? 0 : 1; 98 jsfTable.setFormType(formType); 99 } 100 101 106 private void initComponents() { 108 jFileChooser1 = new javax.swing.JFileChooser (); 109 populate = new javax.swing.ButtonGroup (); 110 jLabel1 = new javax.swing.JLabel (); 111 classTextField = new javax.swing.JTextField (); 112 jButton1 = new javax.swing.JButton (); 113 empty = new javax.swing.JRadioButton (); 114 fromBean = new javax.swing.JRadioButton (); 115 errorField = new javax.swing.JLabel (); 116 117 jFileChooser1.setCurrentDirectory(null); 118 119 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_GetProperty_Bean")); 120 121 org.openide.awt.Mnemonics.setLocalizedText(jButton1, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_Browse")); 122 jButton1.setEnabled(false); 123 jButton1.addActionListener(new java.awt.event.ActionListener () { 124 public void actionPerformed(java.awt.event.ActionEvent evt) { 125 jButton1ActionPerformed(evt); 126 } 127 }); 128 129 populate.add(empty); 130 empty.setSelected(true); 131 org.openide.awt.Mnemonics.setLocalizedText(empty, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_Empty_Table")); 132 empty.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 133 empty.setMargin(new java.awt.Insets (0, 0, 0, 0)); 134 empty.addItemListener(new java.awt.event.ItemListener () { 135 public void itemStateChanged(java.awt.event.ItemEvent evt) { 136 emptyItemStateChanged(evt); 137 } 138 }); 139 140 populate.add(fromBean); 141 org.openide.awt.Mnemonics.setLocalizedText(fromBean, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("LBL_Table_From_Entity")); 142 fromBean.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 143 fromBean.setMargin(new java.awt.Insets (0, 0, 0, 0)); 144 fromBean.addItemListener(new java.awt.event.ItemListener () { 145 public void itemStateChanged(java.awt.event.ItemEvent evt) { 146 emptyItemStateChanged(evt); 147 } 148 }); 149 150 org.openide.awt.Mnemonics.setLocalizedText(errorField, java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("MSG_No_Managed_Beans")); 151 152 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 153 this.setLayout(layout); 154 layout.setHorizontalGroup( 155 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 156 .add(layout.createSequentialGroup() 157 .addContainerGap() 158 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 159 .add(empty) 160 .add(fromBean) 161 .add(layout.createSequentialGroup() 162 .add(17, 17, 17) 163 .add(jLabel1) 164 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 165 .add(classTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE) 166 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 167 .add(jButton1)) 168 .add(errorField)) 169 .addContainerGap()) 170 ); 171 layout.setVerticalGroup( 172 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 173 .add(layout.createSequentialGroup() 174 .addContainerGap() 175 .add(empty) 176 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 177 .add(fromBean) 178 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 179 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 180 .add(jButton1) 181 .add(classTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 182 .add(jLabel1)) 183 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 184 .add(errorField) 185 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 186 ); 187 } 189 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 191 } 195 private void emptyItemStateChanged(java.awt.event.ItemEvent evt) { checkStatus(); 197 } 199 private void checkStatus() { 200 if (empty.isSelected()) { 201 classTextField.setEnabled(false); 202 jButton1.setEnabled(false); 203 } else { 204 classTextField.setEnabled(true); 205 jButton1.setEnabled(true); 206 } 207 boolean validClassName = false; 208 try { 209 validClassName = empty.isSelected() || JsfFormCustomizer.classExists(classTextField); 210 } catch (IOException ioe) { 211 ErrorManager.getDefault().notify(ioe); 212 } 213 descriptor.setValid(hasModuleJsf && validClassName); 214 errorField.setText(hasModuleJsf ? 215 (validClassName ? "" : java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("MSG_InvalidClassName")) : 216 java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/palette/items/Bundle").getString("MSG_NoJSF")); 217 } 218 219 public void insertUpdate(DocumentEvent event) { 220 checkStatus(); 221 } 222 223 public void removeUpdate(DocumentEvent event) { 224 checkStatus(); 225 } 226 227 public void changedUpdate(DocumentEvent event) { 228 checkStatus(); 229 } 230 231 private javax.swing.JTextField classTextField; 233 private javax.swing.JRadioButton empty; 234 private javax.swing.JLabel errorField; 235 private javax.swing.JRadioButton fromBean; 236 private javax.swing.JButton jButton1; 237 private javax.swing.JFileChooser jFileChooser1; 238 private javax.swing.JLabel jLabel1; 239 private javax.swing.ButtonGroup populate; 240 242 public static class ManagedBeanRenderer extends JLabel implements ListCellRenderer { 243 244 public ManagedBeanRenderer() { 245 setOpaque(true); 246 setHorizontalAlignment(LEFT); 247 setVerticalAlignment(CENTER); 248 } 249 250 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 251 String text = "" + value; 252 if (value instanceof ManagedBean) { 253 ManagedBean bean = (ManagedBean) value; 254 if (isSelected) { 255 setBackground(list.getSelectionBackground()); 256 setForeground(list.getSelectionForeground()); 257 } else { 258 setBackground(list.getBackground()); 259 setForeground(list.getForeground()); 260 } 261 text = bean.getManagedBeanName() + "(" + bean.getManagedBeanClass() + ")"; } 263 setFont(list.getFont()); 264 setText(text); 265 return this; 266 } 267 } 268 } 269 | Popular Tags |