1 19 20 package org.netbeans.modules.html.palette.items; 21 import java.awt.Dialog ; 22 import java.awt.Image ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.File ; 26 import javax.swing.ImageIcon ; 27 import javax.swing.filechooser.FileFilter ; 28 import javax.swing.text.Document ; 29 import javax.swing.text.JTextComponent ; 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 48 public class IMGCustomizer extends javax.swing.JPanel { 49 50 private Dialog dialog = null; 51 private DialogDescriptor descriptor = null; 52 private boolean dialogOK = false; 53 54 IMG img; 55 JTextComponent target; 56 57 public IMGCustomizer(IMG img, JTextComponent target) { 58 this.img = img; 59 this.target = target; 60 61 initComponents(); 62 63 jFileChooser1.setAcceptAllFileFilterUsed(false); 64 jFileChooser1.addChoosableFileFilter( 65 new FileFilter () { 66 public boolean accept(File pathname) { 67 return pathname.isDirectory() || FileUtil.toFileObject(pathname).getMIMEType().startsWith("image/"); } 69 public String getDescription() { 70 String 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 displayName = ""; 83 try { 84 displayName = NbBundle.getBundle("org.netbeans.modules.html.palette.items.resources.Bundle").getString("NAME_html-IMG"); } 86 catch (Exception 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 () { 92 public void actionPerformed(ActionEvent 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 loc = jTextField1.getText(); 112 img.setLocation(loc); 113 114 String width = jTextField2.getText(); 115 img.setWidth(width); 116 117 String height = jTextField3.getText(); 118 img.setHeight(height); 119 120 String alt = jTextField4.getText(); 121 img.setAlttext(alt); 122 123 } 124 125 130 private void initComponents() { 132 java.awt.GridBagConstraints gridBagConstraints; 133 134 jFileChooser1 = new javax.swing.JFileChooser (); 135 jLabel4 = new javax.swing.JLabel (); 136 jTextField1 = new javax.swing.JTextField (); 137 jButton1 = new javax.swing.JButton (); 138 jTextField2 = new javax.swing.JTextField (); 139 jTextField3 = new javax.swing.JTextField (); 140 jLabel1 = new javax.swing.JLabel (); 141 jLabel2 = new javax.swing.JLabel (); 142 jLabel3 = new javax.swing.JLabel (); 143 jTextField4 = new javax.swing.JTextField (); 144 145 jFileChooser1.setCurrentDirectory(null); 146 147 setLayout(new java.awt.GridBagLayout ()); 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 (); 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 (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 (); 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 (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 () { 174 public void actionPerformed(java.awt.event.ActionEvent evt) { 175 jButton1ActionPerformed(evt); 176 } 177 }); 178 179 gridBagConstraints = new java.awt.GridBagConstraints (); 180 gridBagConstraints.gridx = 3; 181 gridBagConstraints.gridy = 0; 182 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 183 gridBagConstraints.insets = new java.awt.Insets (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 (100, 19)); 190 jTextField2.setPreferredSize(new java.awt.Dimension (100, 19)); 191 gridBagConstraints = new java.awt.GridBagConstraints (); 192 gridBagConstraints.gridx = 1; 193 gridBagConstraints.gridy = 1; 194 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 195 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 196 add(jTextField2, gridBagConstraints); 197 198 jTextField3.setColumns(10); 199 jTextField3.setMinimumSize(new java.awt.Dimension (100, 19)); 200 jTextField3.setPreferredSize(new java.awt.Dimension (100, 19)); 201 gridBagConstraints = new java.awt.GridBagConstraints (); 202 gridBagConstraints.gridx = 1; 203 gridBagConstraints.gridy = 2; 204 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 205 gridBagConstraints.insets = new java.awt.Insets (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 (); 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 (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 (); 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 (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 (); 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 (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 (); 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 (12, 12, 12, 0); 254 add(jTextField4, gridBagConstraints); 255 256 } 258 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 260 Document targetDoc = target.getDocument(); 261 FileObject targetDocFO = NbEditorUtilities.getFileObject(targetDoc); 262 SourceGroup[] sg = HTMLPaletteUtilities.getSourceGroups(targetDocFO); 263 264 File 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 path = file.getAbsolutePath(); 280 FileObject imgFO = FileUtil.toFileObject(file); 281 try { 282 String relPathToImg = HTMLPaletteUtilities.getRelativePath(targetDocFO, imgFO); 283 if (relPathToImg.length() > 0) 284 path = relPathToImg; 285 } 286 catch (Exception e) { 287 } 289 290 jTextField1.setText(path); 291 292 try { 293 Image img = new ImageIcon (file.getAbsolutePath()).getImage(); 294 int width = img.getWidth(null); 295 jTextField2.setText(width != -1 ? new Integer (width).toString() : ""); 296 int height = img.getHeight(null); 297 jTextField3.setText(height != -1 ? new Integer (height).toString() : ""); 298 jTextField4.setText(imgFO.getName()); 299 } 300 catch (Exception ex) { 301 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 302 } 303 } 304 } 306 307 private javax.swing.JButton jButton1; 309 private javax.swing.JFileChooser jFileChooser1; 310 private javax.swing.JLabel jLabel1; 311 private javax.swing.JLabel jLabel2; 312 private javax.swing.JLabel jLabel3; 313 private javax.swing.JLabel jLabel4; 314 private javax.swing.JTextField jTextField1; 315 private javax.swing.JTextField jTextField2; 316 private javax.swing.JTextField jTextField3; 317 private javax.swing.JTextField jTextField4; 318 320 } 321 | Popular Tags |