KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > html > palette > items > FORMCustomizer


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.html.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.File JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27 import org.netbeans.api.project.SourceGroup;
28 import org.netbeans.modules.html.palette.HTMLPaletteUtilities;
29 import org.netbeans.modules.editor.NbEditorUtilities;
30 import org.openide.DialogDescriptor;
31 import org.openide.DialogDisplayer;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.netbeans.modules.html.palette.BrowseFolders;
35 import org.openide.util.NbBundle;
36
37
38
39
40 /**
41  *
42  * @author Libor Kotouc
43  */

44 public class FORMCustomizer extends javax.swing.JPanel JavaDoc {
45     
46     private Dialog JavaDoc dialog = null;
47     private DialogDescriptor descriptor = null;
48     private boolean dialogOK = false;
49
50     FORM form;
51     JTextComponent JavaDoc target;
52             
53     public FORMCustomizer(FORM form, JTextComponent JavaDoc target) {
54         this.form = form;
55         this.target = target;
56         
57         initComponents();
58     }
59     
60     public boolean showDialog() {
61         
62         dialogOK = false;
63         
64         String JavaDoc displayName = "";
65         try {
66             displayName = NbBundle.getBundle("org.netbeans.modules.html.palette.items.resources.Bundle").getString("NAME_html-FORM"); // NOI18N
67
}
68         catch (Exception JavaDoc e) {}
69         
70         descriptor = new DialogDescriptor
71                 (this, NbBundle.getMessage(FORMCustomizer.class, "LBL_Customizer_InsertPrefix") + " " + displayName, true,
72                  DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION,
73                  new ActionListener JavaDoc() {
74                      public void actionPerformed(ActionEvent JavaDoc e) {
75                         if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) {
76                             evaluateInput();
77                             dialogOK = true;
78                         }
79                         dialog.dispose();
80              }
81          }
82                 );
83         
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 action = jTextField1.getText();
94         form.setAction(action);
95
96         if (jRadioButton1.isSelected())
97             form.setMethod(FORM.METHOD_GET);
98         if (jRadioButton2.isSelected())
99             form.setMethod(FORM.METHOD_POST);
100
101         if (jRadioButton3.isSelected())
102             form.setEnc(FORM.ENC_URLENC);
103         if (jRadioButton4.isSelected())
104             form.setEnc(FORM.ENC_MULTI);
105         
106         String JavaDoc name = jTextField4.getText();
107         form.setName(name);
108         
109     }
110     
111     private void setEncoding() {
112         
113         if (jRadioButton1.isSelected()) { // GET method
114
jRadioButton3.setSelected(true);
115             jRadioButton3.setEnabled(false);
116             jRadioButton4.setEnabled(false);
117         }
118         else if (jRadioButton2.isSelected()) { // POST method
119
jRadioButton3.setEnabled(true);
120             jRadioButton4.setEnabled(true);
121             jRadioButton3.setSelected(true);
122         }
123     }
124     
125     /** This method is called from within the constructor to
126      * initialize the form.
127      * WARNING: Do NOT modify this code. The content of this method is
128      * always regenerated by the Form Editor.
129      */

130     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
131
private void initComponents() {
132         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
133
134         jFileChooser1 = new javax.swing.JFileChooser JavaDoc();
135         buttonGroup1 = new javax.swing.ButtonGroup JavaDoc();
136         buttonGroup2 = new javax.swing.ButtonGroup JavaDoc();
137         jLabel4 = new javax.swing.JLabel JavaDoc();
138         jTextField1 = new javax.swing.JTextField JavaDoc();
139         jButton1 = new javax.swing.JButton JavaDoc();
140         jLabel1 = new javax.swing.JLabel JavaDoc();
141         jLabel2 = new javax.swing.JLabel JavaDoc();
142         jLabel3 = new javax.swing.JLabel JavaDoc();
143         jTextField4 = new javax.swing.JTextField JavaDoc();
144         jRadioButton1 = new javax.swing.JRadioButton JavaDoc();
145         jRadioButton2 = new javax.swing.JRadioButton JavaDoc();
146         jRadioButton3 = new javax.swing.JRadioButton JavaDoc();
147         jRadioButton4 = new javax.swing.JRadioButton JavaDoc();
148
149         jFileChooser1.setCurrentDirectory(null);
150
151         setLayout(new java.awt.GridBagLayout JavaDoc());
152
153         jLabel4.setLabelFor(jTextField4);
154         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "LBL_FORM_Name"));
155         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
156         gridBagConstraints.gridx = 0;
157         gridBagConstraints.gridy = 6;
158         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
159         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
160         gridBagConstraints.weighty = 1.0;
161         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 12, 0);
162         add(jLabel4, gridBagConstraints);
163         jLabel4.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSN_FORM_Name"));
164         jLabel4.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSD_FORM_Name"));
165
166         jTextField1.setColumns(30);
167         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
168         gridBagConstraints.gridx = 1;
169         gridBagConstraints.gridy = 0;
170         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
171         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
172         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
173         add(jTextField1, gridBagConstraints);
174
175         org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "LBL_FORM_Browse"));
176         jButton1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
177             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
178                 jButton1ActionPerformed(evt);
179             }
180         });
181
182         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
183         gridBagConstraints.gridx = 2;
184         gridBagConstraints.gridy = 0;
185         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
186         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 12);
187         add(jButton1, gridBagConstraints);
188         jButton1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSN_FORM_Browse"));
189         jButton1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSD_FORM_Browse"));
190
191         jLabel1.setLabelFor(jTextField1);
192         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "LBL_FORM_Action"));
193         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
194         gridBagConstraints.gridx = 0;
195         gridBagConstraints.gridy = 0;
196         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
197         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
198         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
199         add(jLabel1, gridBagConstraints);
200         jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSN_FORM_Action"));
201         jLabel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSD_FORM_Action"));
202
203         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "LBL_FORM_Method"));
204         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
205         gridBagConstraints.gridx = 0;
206         gridBagConstraints.gridy = 1;
207         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
208         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
209         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
210         add(jLabel2, gridBagConstraints);
211         jLabel2.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSN_FORM_Method"));
212         jLabel2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSD_FORM_Method"));
213
214         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "LBL_FORM_Encoding"));
215         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
216         gridBagConstraints.gridx = 0;
217         gridBagConstraints.gridy = 3;
218         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
219         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
220         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
221         add(jLabel3, gridBagConstraints);
222         jLabel3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSN_FORM_Encoding"));
223         jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSD_FORM_Encoding"));
224
225         jTextField4.setColumns(30);
226         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
227         gridBagConstraints.gridx = 1;
228         gridBagConstraints.gridy = 6;
229         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
230         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
231         gridBagConstraints.weightx = 1.0;
232         gridBagConstraints.weighty = 1.0;
233         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 12, 0);
234         add(jTextField4, gridBagConstraints);
235
236         buttonGroup1.add(jRadioButton1);
237         jRadioButton1.setSelected(true);
238         org.openide.awt.Mnemonics.setLocalizedText(jRadioButton1, "&GET");
239         jRadioButton1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
240         jRadioButton1.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
241         jRadioButton1.addItemListener(new java.awt.event.ItemListener JavaDoc() {
242             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
243                 jRadioButton1ItemStateChanged(evt);
244             }
245         });
246
247         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
248         gridBagConstraints.gridx = 1;
249         gridBagConstraints.gridy = 1;
250         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
251         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
252         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
253         add(jRadioButton1, gridBagConstraints);
254         jRadioButton1.getAccessibleContext().setAccessibleName("GET");
255         jRadioButton1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSD_FORM_GET"));
256
257         buttonGroup1.add(jRadioButton2);
258         org.openide.awt.Mnemonics.setLocalizedText(jRadioButton2, "&POST");
259         jRadioButton2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
260         jRadioButton2.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
261         jRadioButton2.addItemListener(new java.awt.event.ItemListener JavaDoc() {
262             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
263                 jRadioButton2ItemStateChanged(evt);
264             }
265         });
266
267         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
268         gridBagConstraints.gridx = 1;
269         gridBagConstraints.gridy = 2;
270         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
271         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
272         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
273         add(jRadioButton2, gridBagConstraints);
274         jRadioButton2.getAccessibleContext().setAccessibleName("POST");
275         jRadioButton2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSD_FORM_POST"));
276
277         buttonGroup2.add(jRadioButton3);
278         jRadioButton3.setSelected(true);
279         org.openide.awt.Mnemonics.setLocalizedText(jRadioButton3, "application/x-www-form-&urlencoded");
280         jRadioButton3.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
281         jRadioButton3.setEnabled(false);
282         jRadioButton3.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
283         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
284         gridBagConstraints.gridx = 1;
285         gridBagConstraints.gridy = 3;
286         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
287         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
288         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
289         add(jRadioButton3, gridBagConstraints);
290         jRadioButton3.getAccessibleContext().setAccessibleName("application/x-www-form-urlencoded");
291         jRadioButton3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSD_FORM_urlenc"));
292
293         buttonGroup2.add(jRadioButton4);
294         org.openide.awt.Mnemonics.setLocalizedText(jRadioButton4, "multipart/form-&data");
295         jRadioButton4.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
296         jRadioButton4.setEnabled(false);
297         jRadioButton4.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
298         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
299         gridBagConstraints.gridx = 1;
300         gridBagConstraints.gridy = 4;
301         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
302         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
303         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
304         add(jRadioButton4, gridBagConstraints);
305         jRadioButton4.getAccessibleContext().setAccessibleName("multipart/form-data");
306         jRadioButton4.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FORMCustomizer.class, "ACSD_FORM_multi"));
307
308     }// </editor-fold>//GEN-END:initComponents
309

310     private void jRadioButton2ItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_jRadioButton2ItemStateChanged
311
setEncoding();
312     }//GEN-LAST:event_jRadioButton2ItemStateChanged
313

314     private void jRadioButton1ItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_jRadioButton1ItemStateChanged
315
setEncoding();
316     }//GEN-LAST:event_jRadioButton1ItemStateChanged
317

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

320         Document JavaDoc targetDoc = target.getDocument();
321         FileObject targetDocFO = NbEditorUtilities.getFileObject(targetDoc);
322         SourceGroup[] sg = HTMLPaletteUtilities.getSourceGroups(targetDocFO);
323         
324         File JavaDoc file = null;
325         if (sg.length > 0) {
326             FileObject fo = BrowseFolders.showDialog(sg);
327             if (fo != null)
328                 file = FileUtil.toFile(fo);
329         }
330         else {
331             jFileChooser1.setCurrentDirectory(FileUtil.toFile(targetDocFO.getParent()));
332             int returnVal = jFileChooser1.showOpenDialog(this);
333
334             if (returnVal == jFileChooser1.APPROVE_OPTION)
335                 file = jFileChooser1.getSelectedFile();
336         }
337         
338         if (file != null) {
339             String JavaDoc path = file.getAbsolutePath();
340             FileObject actionFO = FileUtil.toFileObject(file);
341             try {
342                 String JavaDoc relPathToAction = HTMLPaletteUtilities.getRelativePath(targetDocFO, actionFO);
343                 if (relPathToAction.length() > 0)
344                     path = relPathToAction;
345             }
346             catch (Exception JavaDoc e) {
347                 //eventual exceptions imply the absolute path to be used
348
}
349             
350             jTextField1.setText(path);
351         }
352     }//GEN-LAST:event_jButton1ActionPerformed
353

354     
355     // Variables declaration - do not modify//GEN-BEGIN:variables
356
private javax.swing.ButtonGroup JavaDoc buttonGroup1;
357     private javax.swing.ButtonGroup JavaDoc buttonGroup2;
358     private javax.swing.JButton JavaDoc jButton1;
359     private javax.swing.JFileChooser JavaDoc jFileChooser1;
360     private javax.swing.JLabel JavaDoc jLabel1;
361     private javax.swing.JLabel JavaDoc jLabel2;
362     private javax.swing.JLabel JavaDoc jLabel3;
363     private javax.swing.JLabel JavaDoc jLabel4;
364     private javax.swing.JRadioButton JavaDoc jRadioButton1;
365     private javax.swing.JRadioButton JavaDoc jRadioButton2;
366     private javax.swing.JRadioButton JavaDoc jRadioButton3;
367     private javax.swing.JRadioButton JavaDoc jRadioButton4;
368     private javax.swing.JTextField JavaDoc jTextField1;
369     private javax.swing.JTextField JavaDoc jTextField4;
370     // End of variables declaration//GEN-END:variables
371

372 }
373
Popular Tags