KickJava   Java API By Example, From Geeks To Geeks.

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


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.Image JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.File JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27 import javax.swing.filechooser.FileFilter JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.JTextComponent JavaDoc;
30 import org.netbeans.api.project.SourceGroup;
31 import org.netbeans.modules.html.palette.HTMLPaletteUtilities;
32 import org.netbeans.modules.editor.NbEditorUtilities;
33 import org.openide.DialogDescriptor;
34 import org.openide.DialogDisplayer;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.netbeans.modules.html.palette.BrowseFolders;
39 import org.openide.util.NbBundle;
40
41
42
43
44 /**
45  *
46  * @author Libor Kotouc
47  */

48 public class IMGCustomizer extends javax.swing.JPanel JavaDoc {
49     
50     private Dialog JavaDoc dialog = null;
51     private DialogDescriptor descriptor = null;
52     private boolean dialogOK = false;
53
54     IMG img;
55     JTextComponent JavaDoc target;
56             
57     public IMGCustomizer(IMG img, JTextComponent JavaDoc target) {
58         this.img = img;
59         this.target = target;
60         
61         initComponents();
62
63         jFileChooser1.setAcceptAllFileFilterUsed(false);
64         jFileChooser1.addChoosableFileFilter(
65             new FileFilter JavaDoc() {
66                 public boolean accept(File JavaDoc pathname) {
67                     return pathname.isDirectory() || FileUtil.toFileObject(pathname).getMIMEType().startsWith("image/"); // NOI18N
68
}
69                 public String JavaDoc getDescription() {
70                     String JavaDoc desc = NbBundle.getMessage(IMGCustomizer.class, "LBL_IMG_FileChooserDesc");
71                     return desc;
72                 }
73             }
74         );
75                 
76     }
77     
78     public boolean showDialog() {
79         
80         dialogOK = false;
81         
82         String JavaDoc displayName = "";
83         try {
84             displayName = NbBundle.getBundle("org.netbeans.modules.html.palette.items.resources.Bundle").getString("NAME_html-IMG"); // NOI18N
85
}
86         catch (Exception JavaDoc e) {}
87         
88         descriptor = new DialogDescriptor
89                 (this, NbBundle.getMessage(IMGCustomizer.class, "LBL_Customizer_InsertPrefix") + " " + displayName, true,
90                  DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION,
91                  new ActionListener JavaDoc() {
92                      public void actionPerformed(ActionEvent JavaDoc e) {
93                         if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) {
94                             evaluateInput();
95                             dialogOK = true;
96                         }
97                         dialog.dispose();
98              }
99          }
100                 );
101         
102         dialog = DialogDisplayer.getDefault().createDialog(descriptor);
103         dialog.setVisible(true);
104         repaint();
105         
106         return dialogOK;
107     }
108     
109     private void evaluateInput() {
110         
111         String JavaDoc loc = jTextField1.getText();
112         img.setLocation(loc);
113         
114         String JavaDoc width = jTextField2.getText();
115         img.setWidth(width);
116         
117         String JavaDoc height = jTextField3.getText();
118         img.setHeight(height);
119         
120         String JavaDoc alt = jTextField4.getText();
121         img.setAlttext(alt);
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         jLabel4 = new javax.swing.JLabel JavaDoc();
136         jTextField1 = new javax.swing.JTextField JavaDoc();
137         jButton1 = new javax.swing.JButton JavaDoc();
138         jTextField2 = new javax.swing.JTextField JavaDoc();
139         jTextField3 = new javax.swing.JTextField 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
145         jFileChooser1.setCurrentDirectory(null);
146
147         setLayout(new java.awt.GridBagLayout JavaDoc());
148
149         jLabel4.setLabelFor(jTextField4);
150         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "LBL_IMG_Alt"));
151         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
152         gridBagConstraints.gridx = 0;
153         gridBagConstraints.gridy = 3;
154         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
155         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
156         gridBagConstraints.weighty = 1.0;
157         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 12, 0);
158         add(jLabel4, gridBagConstraints);
159         jLabel4.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSN_IMG_Alt"));
160         jLabel4.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSD_IMG_Alt"));
161
162         jTextField1.setColumns(30);
163         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
164         gridBagConstraints.gridx = 1;
165         gridBagConstraints.gridy = 0;
166         gridBagConstraints.gridwidth = 2;
167         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
168         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
169         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
170         add(jTextField1, gridBagConstraints);
171
172         org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "LBL_IMG_Browse"));
173         jButton1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
174             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
175                 jButton1ActionPerformed(evt);
176             }
177         });
178
179         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
180         gridBagConstraints.gridx = 3;
181         gridBagConstraints.gridy = 0;
182         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
183         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 12);
184         add(jButton1, gridBagConstraints);
185         jButton1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSN_IMG_Browse"));
186         jButton1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSD_IMG_Browse"));
187
188         jTextField2.setColumns(10);
189         jTextField2.setMinimumSize(new java.awt.Dimension JavaDoc(100, 19));
190         jTextField2.setPreferredSize(new java.awt.Dimension JavaDoc(100, 19));
191         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
192         gridBagConstraints.gridx = 1;
193         gridBagConstraints.gridy = 1;
194         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
195         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
196         add(jTextField2, gridBagConstraints);
197
198         jTextField3.setColumns(10);
199         jTextField3.setMinimumSize(new java.awt.Dimension JavaDoc(100, 19));
200         jTextField3.setPreferredSize(new java.awt.Dimension JavaDoc(100, 19));
201         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
202         gridBagConstraints.gridx = 1;
203         gridBagConstraints.gridy = 2;
204         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
205         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
206         add(jTextField3, gridBagConstraints);
207
208         jLabel1.setLabelFor(jTextField1);
209         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "LBL_IMG_Location"));
210         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
211         gridBagConstraints.gridx = 0;
212         gridBagConstraints.gridy = 0;
213         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
214         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
215         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
216         add(jLabel1, gridBagConstraints);
217         jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSN_IMG_Location"));
218         jLabel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSD_IMG_Location"));
219
220         jLabel2.setLabelFor(jTextField2);
221         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "LBL_IMG_Width"));
222         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
223         gridBagConstraints.gridx = 0;
224         gridBagConstraints.gridy = 1;
225         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
226         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
227         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
228         add(jLabel2, gridBagConstraints);
229         jLabel2.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSN_IMG_Width"));
230         jLabel2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSD_IMG_Width"));
231
232         jLabel3.setLabelFor(jTextField3);
233         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "LBL_IMG_Height"));
234         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
235         gridBagConstraints.gridx = 0;
236         gridBagConstraints.gridy = 2;
237         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
238         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
239         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
240         add(jLabel3, gridBagConstraints);
241         jLabel3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSN_IMG_Height"));
242         jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(IMGCustomizer.class, "ACSD_IMG_Height"));
243
244         jTextField4.setColumns(30);
245         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
246         gridBagConstraints.gridx = 1;
247         gridBagConstraints.gridy = 3;
248         gridBagConstraints.gridwidth = 2;
249         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
250         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
251         gridBagConstraints.weightx = 1.0;
252         gridBagConstraints.weighty = 1.0;
253         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 12, 0);
254         add(jTextField4, gridBagConstraints);
255
256     }// </editor-fold>//GEN-END:initComponents
257

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

260         Document JavaDoc targetDoc = target.getDocument();
261         FileObject targetDocFO = NbEditorUtilities.getFileObject(targetDoc);
262         SourceGroup[] sg = HTMLPaletteUtilities.getSourceGroups(targetDocFO);
263         
264         File JavaDoc file = null;
265         if (sg.length > 0) {
266             FileObject fo = BrowseFolders.showDialog(sg, BrowseFolders.imageFileFilter);
267             if (fo != null)
268                 file = FileUtil.toFile(fo);
269         }
270         else {
271             jFileChooser1.setCurrentDirectory(FileUtil.toFile(targetDocFO.getParent()));
272             int returnVal = jFileChooser1.showOpenDialog(this);
273
274             if (returnVal == jFileChooser1.APPROVE_OPTION)
275                 file = jFileChooser1.getSelectedFile();
276         }
277         
278         if (file != null) {
279             String JavaDoc path = file.getAbsolutePath();
280             FileObject imgFO = FileUtil.toFileObject(file);
281             try {
282                 String JavaDoc relPathToImg = HTMLPaletteUtilities.getRelativePath(targetDocFO, imgFO);
283                 if (relPathToImg.length() > 0)
284                     path = relPathToImg;
285             }
286             catch (Exception JavaDoc e) {
287                 //eventual exceptions imply the absolute path to be used
288
}
289             
290             jTextField1.setText(path);
291             
292             try {
293                 Image JavaDoc img = new ImageIcon JavaDoc(file.getAbsolutePath()).getImage();
294                 int width = img.getWidth(null);
295                 jTextField2.setText(width != -1 ? new Integer JavaDoc(width).toString() : "");
296                 int height = img.getHeight(null);
297                 jTextField3.setText(height != -1 ? new Integer JavaDoc(height).toString() : "");
298                 jTextField4.setText(imgFO.getName());
299             }
300             catch (Exception JavaDoc ex) {
301                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
302             }
303         }
304     }//GEN-LAST:event_jButton1ActionPerformed
305

306     
307     // Variables declaration - do not modify//GEN-BEGIN:variables
308
private javax.swing.JButton JavaDoc jButton1;
309     private javax.swing.JFileChooser JavaDoc jFileChooser1;
310     private javax.swing.JLabel JavaDoc jLabel1;
311     private javax.swing.JLabel JavaDoc jLabel2;
312     private javax.swing.JLabel JavaDoc jLabel3;
313     private javax.swing.JLabel JavaDoc jLabel4;
314     private javax.swing.JTextField JavaDoc jTextField1;
315     private javax.swing.JTextField JavaDoc jTextField2;
316     private javax.swing.JTextField JavaDoc jTextField3;
317     private javax.swing.JTextField JavaDoc jTextField4;
318     // End of variables declaration//GEN-END:variables
319

320 }
321
Popular Tags