KickJava   Java API By Example, From Geeks To Geeks.

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


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

45 public class ACustomizer extends javax.swing.JPanel JavaDoc {
46     
47     private Dialog JavaDoc dialog = null;
48     private DialogDescriptor descriptor = null;
49     private boolean dialogOK = false;
50
51     A a;
52     JTextComponent JavaDoc target;
53             
54     public ACustomizer(A a, JTextComponent JavaDoc target) {
55         this.a = a;
56         this.target = target;
57
58         initComponents();
59
60         jComboBox1.setModel(new DefaultComboBoxModel JavaDoc(A.protocols));
61         jComboBox1.setSelectedIndex(a.getProtocolIndex());
62         jComboBox2.setModel(new DefaultComboBoxModel JavaDoc(A.targets));
63         jComboBox2.setSelectedIndex(a.getTargetIndex());
64     }
65     
66     public boolean showDialog() {
67         
68         dialogOK = false;
69         
70         String JavaDoc displayName = "";
71         try {
72             displayName = NbBundle.getBundle("org.netbeans.modules.html.palette.items.resources.Bundle").getString("NAME_html-A"); // NOI18N
73
}
74         catch (Exception JavaDoc e) {}
75         
76         descriptor = new DialogDescriptor
77                 (this, NbBundle.getMessage(ACustomizer.class, "LBL_Customizer_InsertPrefix") + " " + displayName, true,
78                  DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION,
79                  new ActionListener JavaDoc() {
80                      public void actionPerformed(ActionEvent JavaDoc e) {
81                         if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) {
82                             evaluateInput();
83                             dialogOK = true;
84                         }
85                         dialog.dispose();
86              }
87          }
88                 );
89         
90         dialog = DialogDisplayer.getDefault().createDialog(descriptor);
91         dialog.setVisible(true);
92         repaint();
93         
94         return dialogOK;
95     }
96     
97     private void evaluateInput() {
98         
99         int protocolIndex = jComboBox1.getSelectedIndex();
100         a.setProtocolIndex(protocolIndex);
101         
102         String JavaDoc url = jTextField1.getText();
103         a.setUrl(url);
104         
105         String JavaDoc text = jTextField2.getText();
106         a.setText(text);
107
108         int targetIndex = jComboBox2.getSelectedIndex();
109         a.setTargetIndex(targetIndex);
110         if (targetIndex == -1) // new or no value selected
111
a.setTarget(jComboBox2.getSelectedItem().toString());
112         
113     }
114     
115     /** This method is called from within the constructor to
116      * initialize the form.
117      * WARNING: Do NOT modify this code. The content of this method is
118      * always regenerated by the Form Editor.
119      */

120     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
121
private void initComponents() {
122         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
123
124         jFileChooser1 = new javax.swing.JFileChooser JavaDoc();
125         jLabel4 = new javax.swing.JLabel JavaDoc();
126         jTextField1 = new javax.swing.JTextField JavaDoc();
127         jButton1 = new javax.swing.JButton JavaDoc();
128         jLabel1 = new javax.swing.JLabel JavaDoc();
129         jLabel2 = new javax.swing.JLabel JavaDoc();
130         jLabel3 = new javax.swing.JLabel JavaDoc();
131         jTextField2 = new javax.swing.JTextField JavaDoc();
132         jComboBox1 = new javax.swing.JComboBox JavaDoc();
133         jComboBox2 = new javax.swing.JComboBox JavaDoc();
134
135         jFileChooser1.setCurrentDirectory(null);
136
137         setLayout(new java.awt.GridBagLayout JavaDoc());
138
139         jLabel4.setLabelFor(jComboBox2);
140         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(ACustomizer.class, "LBL_A_Target"));
141         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
142         gridBagConstraints.gridx = 0;
143         gridBagConstraints.gridy = 3;
144         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
145         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
146         gridBagConstraints.weighty = 1.0;
147         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 12, 0);
148         add(jLabel4, gridBagConstraints);
149         jLabel4.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSN_A_Target"));
150         jLabel4.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSD_A_Target"));
151
152         jTextField1.setColumns(35);
153         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
154         gridBagConstraints.gridx = 1;
155         gridBagConstraints.gridy = 1;
156         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
157         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
158         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
159         add(jTextField1, gridBagConstraints);
160
161         org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(ACustomizer.class, "LBL_A_Browse"));
162         jButton1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
163             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
164                 jButton1ActionPerformed(evt);
165             }
166         });
167
168         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
169         gridBagConstraints.gridx = 2;
170         gridBagConstraints.gridy = 1;
171         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
172         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 12);
173         add(jButton1, gridBagConstraints);
174         jButton1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSN_A_Browse"));
175         jButton1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSD_A_Browse"));
176
177         jLabel1.setLabelFor(jComboBox1);
178         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ACustomizer.class, "LBL_A_Protocol"));
179         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
180         gridBagConstraints.gridx = 0;
181         gridBagConstraints.gridy = 0;
182         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
183         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
184         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
185         add(jLabel1, gridBagConstraints);
186         jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSN_A_Protocol"));
187         jLabel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSD_A_Protocol"));
188
189         jLabel2.setLabelFor(jTextField1);
190         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ACustomizer.class, "LBL_A_URL"));
191         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
192         gridBagConstraints.gridx = 0;
193         gridBagConstraints.gridy = 1;
194         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
195         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
196         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
197         add(jLabel2, gridBagConstraints);
198         jLabel2.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSN_A_URL"));
199         jLabel2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSD_A_URL"));
200
201         jLabel3.setLabelFor(jTextField2);
202         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(ACustomizer.class, "LBL_A_Text"));
203         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
204         gridBagConstraints.gridx = 0;
205         gridBagConstraints.gridy = 2;
206         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
207         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
208         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
209         add(jLabel3, gridBagConstraints);
210         jLabel3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSN_A_Text"));
211         jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ACustomizer.class, "ACSD_A_Text"));
212
213         jTextField2.setColumns(35);
214         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
215         gridBagConstraints.gridx = 1;
216         gridBagConstraints.gridy = 2;
217         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
218         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
219         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
220         add(jTextField2, gridBagConstraints);
221
222         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
223         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
224         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
225         add(jComboBox1, gridBagConstraints);
226
227         jComboBox2.setEditable(true);
228         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
229         gridBagConstraints.gridx = 1;
230         gridBagConstraints.gridy = 3;
231         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
232         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
233         gridBagConstraints.weightx = 1.0;
234         gridBagConstraints.weighty = 1.0;
235         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 12, 0);
236         add(jComboBox2, gridBagConstraints);
237
238     }// </editor-fold>//GEN-END:initComponents
239

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

242         Document JavaDoc targetDoc = target.getDocument();
243         FileObject targetDocFO = NbEditorUtilities.getFileObject(targetDoc);
244         SourceGroup[] sg = HTMLPaletteUtilities.getSourceGroups(targetDocFO);
245         
246         File JavaDoc file = null;
247         if (sg.length > 0) {
248             FileObject fo = BrowseFolders.showDialog(sg);
249             if (fo != null)
250                 file = FileUtil.toFile(fo);
251         }
252         else {
253             jFileChooser1.setCurrentDirectory(FileUtil.toFile(targetDocFO.getParent()));
254             int returnVal = jFileChooser1.showOpenDialog(this);
255
256             if (returnVal == jFileChooser1.APPROVE_OPTION)
257                 file = jFileChooser1.getSelectedFile();
258         }
259         
260         if (file != null) {
261             String JavaDoc path = file.getAbsolutePath();
262             FileObject aFO = FileUtil.toFileObject(file);
263             try {
264                 String JavaDoc relPathToFile = HTMLPaletteUtilities.getRelativePath(targetDocFO, aFO);
265                 if (relPathToFile.length() > 0)
266                     path = relPathToFile;
267             }
268             catch (Exception JavaDoc e) {
269                 //eventual exceptions imply the absolute path to be used
270
}
271             
272             jTextField1.setText(path);
273         }
274     }//GEN-LAST:event_jButton1ActionPerformed
275

276     
277     // Variables declaration - do not modify//GEN-BEGIN:variables
278
private javax.swing.JButton JavaDoc jButton1;
279     private javax.swing.JComboBox JavaDoc jComboBox1;
280     private javax.swing.JComboBox JavaDoc jComboBox2;
281     private javax.swing.JFileChooser JavaDoc jFileChooser1;
282     private javax.swing.JLabel JavaDoc jLabel1;
283     private javax.swing.JLabel JavaDoc jLabel2;
284     private javax.swing.JLabel JavaDoc jLabel3;
285     private javax.swing.JLabel JavaDoc jLabel4;
286     private javax.swing.JTextField JavaDoc jTextField1;
287     private javax.swing.JTextField JavaDoc jTextField2;
288     // End of variables declaration//GEN-END:variables
289

290 }
291
Popular Tags