KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.io.IOException JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import javax.swing.ListCellRenderer JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31 import javax.swing.event.DocumentEvent JavaDoc;
32 import javax.swing.event.DocumentListener JavaDoc;
33 import javax.swing.text.JTextComponent JavaDoc;
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 /**
41  * @author Pavel Buzek
42  */

43 public class JsfTableCustomizer extends javax.swing.JPanel JavaDoc implements DocumentListener JavaDoc {
44     
45     private Dialog JavaDoc dialog = null;
46     private DialogDescriptor descriptor = null;
47     private boolean dialogOK = false;
48
49     private final boolean hasModuleJsf;
50     JsfTable jsfTable;
51     JTextComponent JavaDoc target;
52             
53     public JsfTableCustomizer(JsfTable jsfTable, JTextComponent JavaDoc target) {
54         this.jsfTable = jsfTable;
55         this.target = target;
56         
57         initComponents();
58         errorField.setForeground(UIManager.getColor("nb.errorForeground")); //NOI18N
59
hasModuleJsf = JsfForm.hasModuleJsf(target);
60         classTextField.getDocument().addDocumentListener(this);
61     }
62     
63     public boolean showDialog() {
64         
65         dialogOK = false;
66         
67         String JavaDoc displayName = NbBundle.getMessage(JsfTableCustomizer.class, "NAME_jsp-JsfTable"); // NOI18N
68

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 JavaDoc() {
73                      public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc 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     /** This method is called from within the constructor to
102      * initialize the form.
103      * WARNING: Do NOT modify this code. The content of this method is
104      * always regenerated by the Form Editor.
105      */

106     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
107
private void initComponents() {
108         jFileChooser1 = new javax.swing.JFileChooser JavaDoc();
109         populate = new javax.swing.ButtonGroup JavaDoc();
110         jLabel1 = new javax.swing.JLabel JavaDoc();
111         classTextField = new javax.swing.JTextField JavaDoc();
112         jButton1 = new javax.swing.JButton JavaDoc();
113         empty = new javax.swing.JRadioButton JavaDoc();
114         fromBean = new javax.swing.JRadioButton JavaDoc();
115         errorField = new javax.swing.JLabel JavaDoc();
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 JavaDoc() {
124             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc(0, 0, 0, 0));
134         empty.addItemListener(new java.awt.event.ItemListener JavaDoc() {
135             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc 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 JavaDoc(0, 0, 0, 0));
144         fromBean.addItemListener(new java.awt.event.ItemListener JavaDoc() {
145             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc 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     }// </editor-fold>//GEN-END:initComponents
188

189     private void jButton1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton1ActionPerformed
190

191 //TODO: RETOUCHE FQN search
192
//FQNSearch.showFastOpen(classTextField);
193
}//GEN-LAST:event_jButton1ActionPerformed
194

195     private void emptyItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_emptyItemStateChanged
196
checkStatus();
197     }//GEN-LAST:event_emptyItemStateChanged
198

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 JavaDoc 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 JavaDoc event) {
220         checkStatus();
221     }
222
223     public void removeUpdate(DocumentEvent JavaDoc event) {
224         checkStatus();
225     }
226
227     public void changedUpdate(DocumentEvent JavaDoc event) {
228         checkStatus();
229     }
230     
231     // Variables declaration - do not modify//GEN-BEGIN:variables
232
private javax.swing.JTextField JavaDoc classTextField;
233     private javax.swing.JRadioButton JavaDoc empty;
234     private javax.swing.JLabel JavaDoc errorField;
235     private javax.swing.JRadioButton JavaDoc fromBean;
236     private javax.swing.JButton JavaDoc jButton1;
237     private javax.swing.JFileChooser JavaDoc jFileChooser1;
238     private javax.swing.JLabel JavaDoc jLabel1;
239     private javax.swing.ButtonGroup JavaDoc populate;
240     // End of variables declaration//GEN-END:variables
241

242     public static class ManagedBeanRenderer extends JLabel JavaDoc implements ListCellRenderer JavaDoc {
243         
244         public ManagedBeanRenderer() {
245             setOpaque(true);
246             setHorizontalAlignment(LEFT);
247             setVerticalAlignment(CENTER);
248         }
249         
250         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
251             String JavaDoc 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() + ")"; //NOI18N
262
}
263             setFont(list.getFont());
264             setText(text);
265             return this;
266         }
267     }
268 }
269
Popular Tags