Lines 55-63
Link Here
|
55 |
import java.net.MalformedURLException; |
55 |
import java.net.MalformedURLException; |
56 |
import java.net.URI; |
56 |
import java.net.URI; |
57 |
import java.net.URISyntaxException; |
57 |
import java.net.URISyntaxException; |
|
|
58 |
import java.util.ArrayList; |
58 |
import java.util.Collection; |
59 |
import java.util.Collection; |
59 |
import java.util.Arrays; |
60 |
import java.util.Arrays; |
60 |
import java.util.Collections; |
61 |
import java.util.Collections; |
|
|
62 |
import java.util.List; |
61 |
import javax.swing.AbstractAction; |
63 |
import javax.swing.AbstractAction; |
62 |
import javax.swing.DefaultListCellRenderer; |
64 |
import javax.swing.DefaultListCellRenderer; |
63 |
import javax.swing.JComponent; |
65 |
import javax.swing.JComponent; |
Lines 67-72
Link Here
|
67 |
import javax.swing.event.ListSelectionEvent; |
69 |
import javax.swing.event.ListSelectionEvent; |
68 |
import javax.swing.event.ListSelectionListener; |
70 |
import javax.swing.event.ListSelectionListener; |
69 |
import javax.swing.filechooser.FileFilter; |
71 |
import javax.swing.filechooser.FileFilter; |
|
|
72 |
import org.netbeans.api.annotations.common.CheckForNull; |
73 |
import org.netbeans.api.annotations.common.NonNull; |
74 |
import org.netbeans.api.annotations.common.NullAllowed; |
70 |
import org.netbeans.api.project.ant.FileChooser; |
75 |
import org.netbeans.api.project.ant.FileChooser; |
71 |
import org.netbeans.spi.java.project.support.JavadocAndSourceRootDetection; |
76 |
import org.netbeans.spi.java.project.support.JavadocAndSourceRootDetection; |
72 |
import org.netbeans.spi.project.libraries.LibraryCustomizerContext; |
77 |
import org.netbeans.spi.project.libraries.LibraryCustomizerContext; |
Lines 116-121
Link Here
|
116 |
this.upButton.setEnabled(enabled && indices.length>0 && indices[0]>0); |
121 |
this.upButton.setEnabled(enabled && indices.length>0 && indices[0]>0); |
117 |
} |
122 |
} |
118 |
|
123 |
|
|
|
124 |
@CheckForNull |
125 |
static URI[] select( |
126 |
@NonNull final String volumeType, |
127 |
@NonNull final String libName, |
128 |
@NonNull final File[] lastFolder, |
129 |
@NullAllowed final Component owner, |
130 |
@NullAllowed final File baseFolder) { |
131 |
assert volumeType != null; |
132 |
assert lastFolder != null; |
133 |
assert lastFolder.length == 1; |
134 |
final File libFolder = baseFolder == null ? |
135 |
null: |
136 |
FileUtil.normalizeFile(new File(baseFolder, libName)); |
137 |
FileChooser chooser = new FileChooser(baseFolder, libFolder); |
138 |
// for now variable based paths are disabled in library definition |
139 |
// can be revisit if it is needed |
140 |
chooser.setFileHidingEnabled(false); |
141 |
chooser.setAcceptAllFileFilterUsed(false); |
142 |
if (volumeType.equals(J2SELibraryTypeProvider.VOLUME_TYPE_CLASSPATH)) { |
143 |
chooser.setMultiSelectionEnabled (true); |
144 |
chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenClasses")); |
145 |
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
146 |
chooser.setFileFilter (new ArchiveFileFilter(NbBundle.getMessage( |
147 |
J2SEVolumeCustomizer.class,"TXT_Classpath"),new String[] {"ZIP","JAR"})); //NOI18N |
148 |
chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectCP")); |
149 |
chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectCP").charAt(0)); |
150 |
} else if (volumeType.equals(J2SELibraryTypeProvider.VOLUME_TYPE_JAVADOC)) { |
151 |
chooser.setMultiSelectionEnabled (true); |
152 |
chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenJavadoc")); |
153 |
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
154 |
chooser.setFileFilter (new ArchiveFileFilter(NbBundle.getMessage( |
155 |
J2SEVolumeCustomizer.class,"TXT_Javadoc"),new String[] {"ZIP","JAR"})); //NOI18N |
156 |
chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectJD")); |
157 |
chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectJD").charAt(0)); |
158 |
} else if (volumeType.equals(J2SELibraryTypeProvider.VOLUME_TYPE_SRC)) { |
159 |
chooser.setMultiSelectionEnabled (true); |
160 |
chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenSources")); |
161 |
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
162 |
chooser.setFileFilter (new ArchiveFileFilter(NbBundle.getMessage( |
163 |
J2SEVolumeCustomizer.class,"TXT_Sources"),new String[] {"ZIP","JAR"})); //NOI18N |
164 |
chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectSRC")); |
165 |
chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectSRC").charAt(0)); |
166 |
} |
167 |
if (lastFolder[0] != null) { |
168 |
chooser.setCurrentDirectory (lastFolder[0]); |
169 |
} else if (baseFolder != null) { |
170 |
chooser.setCurrentDirectory (baseFolder); |
171 |
} |
172 |
if (chooser.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) { |
173 |
try { |
174 |
lastFolder[0] = chooser.getCurrentDirectory(); |
175 |
return pathsToURIs ( |
176 |
chooser.getSelectedPaths(), |
177 |
volumeType, |
178 |
baseFolder); |
179 |
} catch (MalformedURLException mue) { |
180 |
Exceptions.printStackTrace(mue); |
181 |
} catch (URISyntaxException ue) { |
182 |
Exceptions.printStackTrace(ue); |
183 |
} catch (IOException ex) { |
184 |
Exceptions.printStackTrace(ex); |
185 |
} |
186 |
} |
187 |
return null; |
188 |
} |
189 |
|
119 |
|
190 |
|
120 |
private void postInitComponents () { |
191 |
private void postInitComponents () { |
121 |
this.content.setCellRenderer(new ContentRenderer()); |
192 |
this.content.setCellRenderer(new ContentRenderer()); |
Lines 346-399
Link Here
|
346 |
}//GEN-LAST:event_removeResource |
417 |
}//GEN-LAST:event_removeResource |
347 |
|
418 |
|
348 |
private void addResource(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addResource |
419 |
private void addResource(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addResource |
349 |
File baseFolder = null; |
420 |
final boolean arp = allowRelativePaths != null && allowRelativePaths.booleanValue(); |
350 |
File libFolder = null; |
421 |
final File baseFolder = arp ? |
351 |
if (allowRelativePaths != null && allowRelativePaths.booleanValue()) { |
422 |
FileUtil.normalizeFile(new File(URI.create(area.getLocation().toExternalForm())).getParentFile()): |
352 |
baseFolder = FileUtil.normalizeFile(new File(URI.create(area.getLocation().toExternalForm())).getParentFile()); |
423 |
null; |
353 |
libFolder = FileUtil.normalizeFile(new File(baseFolder, impl.getName())); |
424 |
final File[] cwd = new File[]{lastFolder}; |
354 |
} |
425 |
final URI[] res = select(volumeType, impl.getName(), cwd, this, baseFolder); |
355 |
FileChooser chooser = new FileChooser(baseFolder, libFolder); |
426 |
if (res != null) { |
356 |
FileUtil.preventFileChooserSymlinkTraversal(chooser, null); |
|
|
357 |
// for now variable based paths are disabled in library definition |
358 |
// can be revisit if it is needed |
359 |
chooser.setFileHidingEnabled(false); |
360 |
chooser.setAcceptAllFileFilterUsed(false); |
361 |
if (this.volumeType.equals(J2SELibraryTypeProvider.VOLUME_TYPE_CLASSPATH)) { |
362 |
chooser.setMultiSelectionEnabled (true); |
363 |
chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenClasses")); |
364 |
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
365 |
chooser.setFileFilter (new ArchiveFileFilter(NbBundle.getMessage( |
366 |
J2SEVolumeCustomizer.class,"TXT_Classpath"),new String[] {"ZIP","JAR"})); //NOI18N |
367 |
chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectCP")); |
368 |
chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectCP").charAt(0)); |
369 |
} |
370 |
else if (this.volumeType.equals(J2SELibraryTypeProvider.VOLUME_TYPE_JAVADOC)) { |
371 |
chooser.setMultiSelectionEnabled (true); |
372 |
chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenJavadoc")); |
373 |
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
374 |
chooser.setFileFilter (new ArchiveFileFilter(NbBundle.getMessage( |
375 |
J2SEVolumeCustomizer.class,"TXT_Javadoc"),new String[] {"ZIP","JAR"})); //NOI18N |
376 |
chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectJD")); |
377 |
chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectJD").charAt(0)); |
378 |
} |
379 |
else if (this.volumeType.equals(J2SELibraryTypeProvider.VOLUME_TYPE_SRC)) { |
380 |
chooser.setMultiSelectionEnabled (true); |
381 |
chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenSources")); |
382 |
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
383 |
chooser.setFileFilter (new ArchiveFileFilter(NbBundle.getMessage( |
384 |
J2SEVolumeCustomizer.class,"TXT_Sources"),new String[] {"ZIP","JAR"})); //NOI18N |
385 |
chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectSRC")); |
386 |
chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectSRC").charAt(0)); |
387 |
} |
388 |
if (lastFolder != null) { |
389 |
chooser.setCurrentDirectory (lastFolder); |
390 |
} else if (baseFolder != null) { |
391 |
chooser.setCurrentDirectory (baseFolder); |
392 |
} |
393 |
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { |
394 |
try { |
427 |
try { |
395 |
lastFolder = chooser.getCurrentDirectory(); |
428 |
lastFolder = cwd[0]; |
396 |
addFiles (chooser.getSelectedPaths(), area != null ? area.getLocation() : null, this.volumeType); |
429 |
addFiles (res, arp); |
397 |
} catch (MalformedURLException mue) { |
430 |
} catch (MalformedURLException mue) { |
398 |
Exceptions.printStackTrace(mue); |
431 |
Exceptions.printStackTrace(mue); |
399 |
} catch (URISyntaxException ue) { |
432 |
} catch (URISyntaxException ue) { |
Lines 435-485
Link Here
|
435 |
} |
468 |
} |
436 |
}//GEN-LAST:event_addURLButtonActionPerformed |
469 |
}//GEN-LAST:event_addURLButtonActionPerformed |
437 |
|
470 |
|
438 |
private void addFiles (String[] fileNames, URL libraryLocation, String volume) throws MalformedURLException, URISyntaxException { |
471 |
private void addFiles (URI[] toAdd, boolean allowRelativePaths) throws MalformedURLException, URISyntaxException { |
439 |
int firstIndex = this.model.getSize(); |
472 |
int firstIndex = this.model.getSize(); |
440 |
for (int i = 0; i < fileNames.length; i++) { |
473 |
for (URI uri : toAdd) { |
441 |
File f = new File(fileNames[i]); |
474 |
if (allowRelativePaths) { |
442 |
URI uri = LibrariesSupport.convertFilePathToURI(fileNames[i]); |
|
|
443 |
if (allowRelativePaths != null && allowRelativePaths.booleanValue()) { |
444 |
File realFile = f; |
445 |
if (!f.isAbsolute()) { |
446 |
assert area != null; |
447 |
if (area != null) { |
448 |
realFile = FileUtil.normalizeFile(new File( |
449 |
new File(URI.create(area.getLocation().toExternalForm())).getParentFile(), f.getPath())); |
450 |
} |
451 |
} |
452 |
String jarPath = checkFile(realFile, volume); |
453 |
if (FileUtil.isArchiveFile(realFile.toURI().toURL())) { |
454 |
uri = LibrariesSupport.getArchiveRoot(uri); |
455 |
if (jarPath != null) { |
456 |
assert uri.toString().endsWith("!/") : uri.toString(); //NOI18N |
457 |
uri = URI.create(uri.toString() + encodePath(jarPath)); |
458 |
} |
459 |
} else if (!uri.toString().endsWith("/")){ //NOI18N |
460 |
try { |
461 |
uri = new URI(uri.toString()+"/"); //NOI18N |
462 |
} catch (URISyntaxException ex) { |
463 |
throw new AssertionError(ex); |
464 |
} |
465 |
} |
466 |
model.addResource(uri); |
475 |
model.addResource(uri); |
467 |
} else { |
476 |
} else { |
468 |
assert f.isAbsolute() : f.getPath(); |
|
|
469 |
f = FileUtil.normalizeFile (f); |
470 |
String jarPath = checkFile(f, volume); |
471 |
uri = f.toURI(); |
472 |
if (FileUtil.isArchiveFile(uri.toURL())) { |
473 |
uri = LibrariesSupport.getArchiveRoot(uri); |
474 |
if (jarPath != null) { |
475 |
assert uri.toString().endsWith("!/") : uri.toString(); //NOI18N |
476 |
uri = URI.create(uri.toString() + encodePath(jarPath)); |
477 |
} |
478 |
} else if (!uri.toString().endsWith("/")){ //NOI18N |
479 |
uri = URI.create(uri.toString()+"/"); //NOI18N |
480 |
} |
481 |
model.addResource(uri.toURL()); //Has to be added as URL, model asserts it |
477 |
model.addResource(uri.toURL()); //Has to be added as URL, model asserts it |
482 |
|
|
|
483 |
} |
478 |
} |
484 |
} |
479 |
} |
485 |
int lastIndex = this.model.getSize()-1; |
480 |
int lastIndex = this.model.getSize()-1; |
Lines 501-507
Link Here
|
501 |
return new URI(null, null, path, null).getRawPath(); |
496 |
return new URI(null, null, path, null).getRawPath(); |
502 |
} |
497 |
} |
503 |
|
498 |
|
504 |
private String checkFile(File f, String volume) { |
499 |
@NonNull |
|
|
500 |
private static URI[] pathsToURIs( |
501 |
@NonNull final String[] fileNames, |
502 |
@NonNull final String volume, |
503 |
@NullAllowed final File baseFolder) throws MalformedURLException, URISyntaxException { |
504 |
final List<URI> result = new ArrayList<URI>(fileNames.length); |
505 |
for (int i = 0; i < fileNames.length; i++) { |
506 |
File f = new File(fileNames[i]); |
507 |
URI uri = LibrariesSupport.convertFilePathToURI(fileNames[i]); |
508 |
if (baseFolder != null) { |
509 |
File realFile = f; |
510 |
if (!f.isAbsolute()) { |
511 |
realFile = FileUtil.normalizeFile(new File( |
512 |
baseFolder, f.getPath())); |
513 |
} |
514 |
String jarPath = checkFile(realFile, volume); |
515 |
if (FileUtil.isArchiveFile(realFile.toURI().toURL())) { |
516 |
uri = LibrariesSupport.getArchiveRoot(uri); |
517 |
if (jarPath != null) { |
518 |
assert uri.toString().endsWith("!/") : uri.toString(); //NOI18N |
519 |
uri = URI.create(uri.toString() + encodePath(jarPath)); |
520 |
} |
521 |
} else if (!uri.toString().endsWith("/")){ //NOI18N |
522 |
try { |
523 |
uri = new URI(uri.toString()+"/"); //NOI18N |
524 |
} catch (URISyntaxException ex) { |
525 |
throw new AssertionError(ex); |
526 |
} |
527 |
} |
528 |
result.add(uri); |
529 |
} else { |
530 |
assert f.isAbsolute() : f.getPath(); |
531 |
f = FileUtil.normalizeFile (f); |
532 |
String jarPath = checkFile(f, volume); |
533 |
uri = f.toURI(); |
534 |
if (FileUtil.isArchiveFile(uri.toURL())) { |
535 |
uri = LibrariesSupport.getArchiveRoot(uri); |
536 |
if (jarPath != null) { |
537 |
assert uri.toString().endsWith("!/") : uri.toString(); //NOI18N |
538 |
uri = URI.create(uri.toString() + encodePath(jarPath)); |
539 |
} |
540 |
} else if (!uri.toString().endsWith("/")){ //NOI18N |
541 |
uri = URI.create(uri.toString()+"/"); //NOI18N |
542 |
} |
543 |
result.add(uri); |
544 |
} |
545 |
} |
546 |
return result.toArray(new URI[result.size()]); |
547 |
} |
548 |
|
549 |
private static String checkFile(File f, String volume) { |
505 |
FileObject fo = FileUtil.toFileObject(f); |
550 |
FileObject fo = FileUtil.toFileObject(f); |
506 |
if (volume.equals(J2SELibraryTypeProvider.VOLUME_TYPE_JAVADOC)) { |
551 |
if (volume.equals(J2SELibraryTypeProvider.VOLUME_TYPE_JAVADOC)) { |
507 |
if (fo != null) { |
552 |
if (fo != null) { |