| 1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.StringTokenizer ; 26 import java.util.jar.JarEntry ; 27 import java.util.jar.JarFile ; 28 import javax.swing.JFileChooser ; 29 import javax.swing.event.DocumentEvent ; 30 import javax.swing.event.DocumentListener ; 31 import javax.swing.filechooser.FileFilter ; 32 import org.netbeans.modules.apisupport.project.Util; 33 import org.netbeans.modules.apisupport.project.ui.ModuleUISettings; 34 import org.netbeans.modules.apisupport.project.ui.UIUtil; 35 import org.openide.ErrorManager; 36 import org.openide.util.NbBundle; 37 38 43 final class LibraryStartVisualPanel extends BasicVisualPanel.NewTemplatePanel { 44 45 static final String PROP_LIBRARY_PATH = "LIBRARY_PATH_VALUE"; static final String PROP_LICENSE_PATH = "LICENSE_PATH_VALUE"; 48 private boolean listenersAttached; 49 private final DocumentListener libraryDL; 50 private final DocumentListener licenseDL; 51 52 53 public LibraryStartVisualPanel(final NewModuleProjectData data) { 54 super(data); 55 initComponents(); 56 initAccessibility(); 57 libraryDL = new UIUtil.DocumentAdapter() { 58 public void insertUpdate(DocumentEvent e) { 59 checkLibraryAndLicense(); 60 } 61 }; 62 licenseDL = new UIUtil.DocumentAdapter() { 63 public void insertUpdate(DocumentEvent e) { 64 checkLibraryAndLicense(); 65 } 66 }; 67 } 68 69 private void initAccessibility() { 70 this.getAccessibleContext().setAccessibleDescription(getMessage("ACS_LibraryStartVisualPanel")); 71 browseLibraryButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_BrowseLibraries")); 72 browseLicenceButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_BrowseLicense")); 73 txtLibrary.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_Library")); 74 txtLicense.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_License")); 75 } 76 77 private void checkLibraryAndLicense() { 78 String text = txtLibrary.getText().trim(); 79 if (text.length() > 0) { 80 StringTokenizer tokens = new StringTokenizer (text, File.pathSeparator); 81 while (tokens.hasMoreTokens()) { 82 String one = tokens.nextToken(); 83 File fil = new File (one); 84 if (!fil.exists()) { 85 setError(getMessage("MSG_Invalid_Library_Path")); 86 return; 87 } 88 try { 89 new JarFile (fil); } catch (IOException exc) { 91 setError(getMessage("MSG_Invalid_Library_Path")); 92 return; 93 } 94 String badOnes = populateProjectData(getData(), text, false); 95 if (badOnes != null) { 96 setWarning(NbBundle.getMessage(LibraryStartVisualPanel.class, "MSG_ClassInDefaultPackage", badOnes)); 97 return; 98 } 99 } 100 } else { 101 setError(getMessage("MSG_Library_Path_Not_Defined")); 102 return; 103 } 104 text = txtLicense.getText().trim(); 105 if (text.length() > 0) { 106 File fil = new File (text); 107 if (!fil.exists()) { 108 setError(getMessage("MSG_Invalid_License_Path")); 109 return; 110 } 111 } 112 markValid(); 113 } 114 115 void refreshData() { 116 120 } 129 130 131 void storeData() { 132 String jars = txtLibrary.getText().trim(); 133 getSettings().putProperty(PROP_LIBRARY_PATH, jars); 134 getSettings().putProperty(PROP_LICENSE_PATH, txtLicense.getText().trim()); 135 populateProjectData(getData(), jars, true); 136 } 144 145 static String populateProjectData(NewModuleProjectData data, String paths, boolean assignValues) { 146 if (data.getProjectName() != null && data.getCodeNameBase() != null && assignValues) { 147 return null; 148 } 149 String wrongOnes = null; 150 StringTokenizer tokens = new StringTokenizer (paths, File.pathSeparator); 151 boolean cutShortestPath = false; 152 boolean fileAlreadyMarked = false; 153 if (tokens.hasMoreTokens()) { 154 fileAlreadyMarked = false; 155 File fil = new File (tokens.nextToken()); 156 if (!fil.exists()) { 157 return wrongOnes; 159 } 160 String name = fil.getName(); 161 int inddd = name.lastIndexOf('.'); 162 if (inddd > -1) { 163 name = name.substring(0, inddd); 164 } 165 name = name.replaceAll("[0-9._-]+$", ""); if (assignValues) { 167 data.setProjectName(name); 168 } 169 JarFile jf = null; 170 String shortestPath = null; 171 try { 172 jf = new JarFile (fil); 173 Enumeration en = jf.entries(); 174 while (en.hasMoreElements()) { 175 JarEntry entry = (JarEntry )en.nextElement(); 176 if (!entry.isDirectory() && entry.getName().endsWith(".class")) { String nm = entry.getName(); 178 if (!Util.isValidJavaFQN(nm.substring(0, nm.length() - 6).replace('/', '.'))) { 179 continue; } 181 int index = nm.lastIndexOf('/'); 182 if (index > -1) { 183 String path = nm.substring(0, index); 184 if (shortestPath != null && path.length() == shortestPath.length() && !path.equals(shortestPath)) { 185 cutShortestPath = true; 186 } 187 if (shortestPath == null || path.length() < shortestPath.length()) { 188 shortestPath = path; 189 cutShortestPath = false; 190 } 191 } else { 192 if (!fileAlreadyMarked) { 194 wrongOnes = wrongOnes == null ? fil.getName() : wrongOnes + "," + fil.getName(); fileAlreadyMarked = true; 196 } 197 } 198 } 199 } 200 } catch (IOException e) { 201 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 202 } finally { 203 if (jf != null) { 204 try { 205 jf.close(); 206 } catch (IOException e) { 207 Util.err.notify(ErrorManager.INFORMATIONAL, e); 208 } 209 } 210 } 211 if (shortestPath != null && assignValues) { 212 shortestPath = shortestPath.replace('/', '.'); 213 if (cutShortestPath && shortestPath.indexOf('.') != shortestPath.lastIndexOf('.')) { 214 int ind = shortestPath.lastIndexOf('.'); 217 shortestPath = shortestPath.substring(0, ind); 218 } 219 data.setCodeNameBase(shortestPath); 220 } 221 } 222 return wrongOnes; 223 } 224 225 public void addNotify() { 226 super.addNotify(); 227 attachDocumentListeners(); 228 } 229 230 public void removeNotify() { 231 removeDocumentListeners(); 233 super.removeNotify(); 234 } 235 236 private void attachDocumentListeners() { 237 if (!listenersAttached) { 238 txtLibrary.getDocument().addDocumentListener(libraryDL); 239 txtLicense.getDocument().addDocumentListener(licenseDL); 240 listenersAttached = true; 241 } 242 } 243 244 private void removeDocumentListeners() { 245 if (listenersAttached) { 246 txtLibrary.getDocument().removeDocumentListener(libraryDL); 247 txtLicense.getDocument().removeDocumentListener(licenseDL); 248 listenersAttached = false; 249 } 250 } 251 252 private static String getMessage(String key) { 253 return NbBundle.getMessage(LibraryStartVisualPanel.class, key); 254 } 255 256 261 private void initComponents() { 263 java.awt.GridBagConstraints gridBagConstraints; 264 265 confPanel = new javax.swing.JPanel (); 266 lblLibrary = new javax.swing.JLabel (); 267 txtLibrary = new javax.swing.JTextField (); 268 lblLicense = new javax.swing.JLabel (); 269 txtLicense = new javax.swing.JTextField (); 270 browseLibraryButton = new javax.swing.JButton (); 271 browseLicenceButton = new javax.swing.JButton (); 272 filler = new javax.swing.JPanel (); 273 274 setLayout(new java.awt.GridBagLayout ()); 275 276 confPanel.setLayout(new java.awt.GridBagLayout ()); 277 278 lblLibrary.setLabelFor(txtLibrary); 279 org.openide.awt.Mnemonics.setLocalizedText(lblLibrary, org.openide.util.NbBundle.getMessage(LibraryStartVisualPanel.class, "LBL_Library_path")); 280 gridBagConstraints = new java.awt.GridBagConstraints (); 281 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 282 gridBagConstraints.insets = new java.awt.Insets (1, 0, 6, 12); 283 confPanel.add(lblLibrary, gridBagConstraints); 284 285 gridBagConstraints = new java.awt.GridBagConstraints (); 286 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 287 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 288 gridBagConstraints.insets = new java.awt.Insets (1, 0, 6, 0); 289 confPanel.add(txtLibrary, gridBagConstraints); 290 291 lblLicense.setLabelFor(txtLicense); 292 org.openide.awt.Mnemonics.setLocalizedText(lblLicense, org.openide.util.NbBundle.getMessage(LibraryStartVisualPanel.class, "LBL_License_Path")); 293 gridBagConstraints = new java.awt.GridBagConstraints (); 294 gridBagConstraints.gridx = 0; 295 gridBagConstraints.gridy = 1; 296 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 297 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 298 confPanel.add(lblLicense, gridBagConstraints); 299 300 gridBagConstraints = new java.awt.GridBagConstraints (); 301 gridBagConstraints.gridx = 1; 302 gridBagConstraints.gridy = 1; 303 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 304 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 305 gridBagConstraints.weightx = 1.0; 306 confPanel.add(txtLicense, gridBagConstraints); 307 308 org.openide.awt.Mnemonics.setLocalizedText(browseLibraryButton, org.openide.util.NbBundle.getMessage(LibraryStartVisualPanel.class, "CTL_BrowseButton_o")); 309 browseLibraryButton.addActionListener(new java.awt.event.ActionListener () { 310 public void actionPerformed(java.awt.event.ActionEvent evt) { 311 browseLibraryButtonActionPerformed(evt); 312 } 313 }); 314 315 gridBagConstraints = new java.awt.GridBagConstraints (); 316 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 317 gridBagConstraints.insets = new java.awt.Insets (0, 12, 6, 0); 318 confPanel.add(browseLibraryButton, gridBagConstraints); 319 320 org.openide.awt.Mnemonics.setLocalizedText(browseLicenceButton, org.openide.util.NbBundle.getMessage(LibraryStartVisualPanel.class, "CTL_BrowseButton_w")); 321 browseLicenceButton.addActionListener(new java.awt.event.ActionListener () { 322 public void actionPerformed(java.awt.event.ActionEvent evt) { 323 browseLicenceButtonActionPerformed(evt); 324 } 325 }); 326 327 gridBagConstraints = new java.awt.GridBagConstraints (); 328 gridBagConstraints.gridx = 2; 329 gridBagConstraints.gridy = 1; 330 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 331 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 332 confPanel.add(browseLicenceButton, gridBagConstraints); 333 334 gridBagConstraints = new java.awt.GridBagConstraints (); 335 gridBagConstraints.gridx = 0; 336 gridBagConstraints.gridy = 2; 337 gridBagConstraints.gridwidth = 3; 338 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 339 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 340 gridBagConstraints.weightx = 1.0; 341 gridBagConstraints.weighty = 1.0; 342 confPanel.add(filler, gridBagConstraints); 343 344 gridBagConstraints = new java.awt.GridBagConstraints (); 345 gridBagConstraints.gridx = 0; 346 gridBagConstraints.gridy = 0; 347 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 348 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 349 gridBagConstraints.weightx = 1.0; 350 gridBagConstraints.weighty = 1.0; 351 gridBagConstraints.insets = new java.awt.Insets (4, 0, 4, 0); 352 add(confPanel, gridBagConstraints); 353 354 } 356 private void browseLicenceButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (ModuleUISettings.getDefault().getLastChosenLibraryLocation()); 358 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 359 chooser.setMultiSelectionEnabled(false); 360 if (txtLicense.getText().trim().length() > 0) { 361 chooser.setSelectedFile(new File (txtLicense.getText().trim())); 362 } 363 int ret = chooser.showDialog(this, getMessage("LBL_Select")); 364 if (ret == JFileChooser.APPROVE_OPTION) { 365 txtLicense.setText(chooser.getSelectedFile().getAbsolutePath()); 366 ModuleUISettings.getDefault().setLastChosenLibraryLocation(txtLicense.getText()); 367 } 368 } 370 private void browseLibraryButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (ModuleUISettings.getDefault().getLastChosenLibraryLocation()); 372 File [] olds = convertStringToFiles(txtLibrary.getText().trim()); 373 chooser.setSelectedFiles(olds); 374 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 375 chooser.setMultiSelectionEnabled(true); 376 chooser.addChoosableFileFilter(new JarZipFilter()); 377 int ret = chooser.showDialog(this, getMessage("LBL_Select")); 378 if (ret == JFileChooser.APPROVE_OPTION) { 379 File [] files = chooser.getSelectedFiles(); 380 String path = ""; 381 for (int i = 0; i < files.length; i++) { 382 path = path + files[i] + ( i == files.length - 1 ? "" : File.pathSeparator); 383 } 384 txtLibrary.setText(path); 385 ModuleUISettings.getDefault().setLastChosenLibraryLocation(files[0].getParentFile().getAbsolutePath()); 386 } 387 } 389 static File [] convertStringToFiles(String path) { 390 StringTokenizer tok = new StringTokenizer (path, File.pathSeparator); 391 File [] olds = new File [tok.countTokens()]; 392 for (int i = 0; i < olds.length; i++) { 393 olds[i] = new File (tok.nextToken()); 394 } 395 return olds; 396 } 397 398 private javax.swing.JButton browseLibraryButton; 400 private javax.swing.JButton browseLicenceButton; 401 private javax.swing.JPanel confPanel; 402 private javax.swing.JPanel filler; 403 private javax.swing.JLabel lblLibrary; 404 private javax.swing.JLabel lblLicense; 405 private javax.swing.JTextField txtLibrary; 406 private javax.swing.JTextField txtLicense; 407 409 private static final class JarZipFilter extends FileFilter { 410 public boolean accept(File pathname) { 411 return pathname.isDirectory() || pathname.getName().endsWith("zip") || pathname.getName().endsWith("jar"); } 413 public String getDescription() { 414 return getMessage("LibraryStartVisualPanel_jar_zip_filter"); 415 } 416 } 417 418 } 419 | Popular Tags |