Lines 56-65
Link Here
|
56 |
import javax.swing.event.ChangeListener; |
56 |
import javax.swing.event.ChangeListener; |
57 |
import javax.swing.event.DocumentEvent; |
57 |
import javax.swing.event.DocumentEvent; |
58 |
import javax.swing.event.DocumentListener; |
58 |
import javax.swing.event.DocumentListener; |
|
|
59 |
import org.netbeans.api.annotations.common.CheckForNull; |
60 |
import org.netbeans.api.annotations.common.NonNull; |
61 |
import org.netbeans.api.annotations.common.NullAllowed; |
59 |
import org.netbeans.api.project.Project; |
62 |
import org.netbeans.api.project.Project; |
60 |
import org.netbeans.api.project.ProjectUtils; |
63 |
import org.netbeans.api.project.ProjectUtils; |
61 |
import org.netbeans.api.project.SourceGroup; |
64 |
import org.netbeans.api.project.SourceGroup; |
62 |
import org.netbeans.api.project.Sources; |
|
|
63 |
import org.openide.awt.Mnemonics; |
65 |
import org.openide.awt.Mnemonics; |
64 |
import org.openide.filesystems.FileObject; |
66 |
import org.openide.filesystems.FileObject; |
65 |
import org.openide.filesystems.FileUtil; |
67 |
import org.openide.filesystems.FileUtil; |
Lines 67-72
Link Here
|
67 |
import org.openide.loaders.DataObjectNotFoundException; |
69 |
import org.openide.loaders.DataObjectNotFoundException; |
68 |
import org.openide.util.ChangeSupport; |
70 |
import org.openide.util.ChangeSupport; |
69 |
import static org.netbeans.modules.project.ui.Bundle.*; |
71 |
import static org.netbeans.modules.project.ui.Bundle.*; |
|
|
72 |
import org.openide.filesystems.FileChooserBuilder; |
70 |
import org.openide.util.NbBundle.Messages; |
73 |
import org.openide.util.NbBundle.Messages; |
71 |
|
74 |
|
72 |
/** |
75 |
/** |
Lines 79-95
Link Here
|
79 |
private static final Dimension PREF_DIM = new Dimension(500, 340); |
82 |
private static final Dimension PREF_DIM = new Dimension(500, 340); |
80 |
|
83 |
|
81 |
private final ListCellRenderer CELL_RENDERER = new GroupCellRenderer(); |
84 |
private final ListCellRenderer CELL_RENDERER = new GroupCellRenderer(); |
82 |
|
85 |
|
|
|
86 |
@NullAllowed |
83 |
private Project project; |
87 |
private Project project; |
|
|
88 |
@NonNull |
84 |
private String expectedExtension; |
89 |
private String expectedExtension; |
85 |
private final ChangeSupport changeSupport = new ChangeSupport(this); |
90 |
private final ChangeSupport changeSupport = new ChangeSupport(this); |
86 |
private SourceGroup[] folders; |
91 |
private final SourceGroup[] folders; |
87 |
private boolean isFolder; |
92 |
private boolean isFolder; |
88 |
private boolean freeFileExtension; |
93 |
private boolean freeFileExtension; |
89 |
|
94 |
|
90 |
@SuppressWarnings("LeakingThisInConstructor") |
95 |
@SuppressWarnings("LeakingThisInConstructor") |
91 |
@Messages("LBL_SimpleTargetChooserPanel_Name=Name and Location") |
96 |
@Messages("LBL_SimpleTargetChooserPanel_Name=Name and Location") |
92 |
public SimpleTargetChooserPanelGUI( Project project, SourceGroup[] folders, Component bottomPanel, boolean isFolder, boolean freeFileExtension) { |
97 |
public SimpleTargetChooserPanelGUI( @NullAllowed Project project, @NonNull SourceGroup[] folders, Component bottomPanel, boolean isFolder, boolean freeFileExtension) { |
93 |
this.project = project; |
98 |
this.project = project; |
94 |
this.folders = folders.clone(); |
99 |
this.folders = folders.clone(); |
95 |
this.isFolder = isFolder; |
100 |
this.isFolder = isFolder; |
Lines 121-137
Link Here
|
121 |
"LBL_TargetChooser_FileName_Label=File &Name:", |
126 |
"LBL_TargetChooser_FileName_Label=File &Name:", |
122 |
"LBL_TargetChooser_Folder_Label=Fo&lder:", |
127 |
"LBL_TargetChooser_Folder_Label=Fo&lder:", |
123 |
"LBL_TargetChooser_CreatedFile_Label=&Created File:", |
128 |
"LBL_TargetChooser_CreatedFile_Label=&Created File:", |
124 |
"# sample folder name", "LBL_folder_name=folder" |
129 |
"# sample folder name", "LBL_folder_name=folder", |
|
|
130 |
"LBL_TargetChooser_NoProject=None" |
125 |
}) |
131 |
}) |
126 |
final void initValues(FileObject template, FileObject preselectedFolder, String documentName) { |
132 |
final void initValues(FileObject template, @NullAllowed FileObject preselectedFolder, String documentName) { |
127 |
assert project != null; |
133 |
if (project != null) { |
128 |
|
134 |
projectTextField.setText(ProjectUtils.getInformation(project).getDisplayName()); |
129 |
projectTextField.setText(ProjectUtils.getInformation(project).getDisplayName()); |
135 |
} else { |
130 |
|
136 |
projectTextField.setText(LBL_TargetChooser_NoProject()); |
131 |
Sources sources = ProjectUtils.getSources( project ); |
|
|
132 |
|
133 |
if (folders == null) { |
134 |
folders = sources.getSourceGroups( Sources.TYPE_GENERIC ); |
135 |
} |
137 |
} |
136 |
|
138 |
|
137 |
if ( folders.length < 2 ) { |
139 |
if ( folders.length < 2 ) { |
Lines 158-163
Link Here
|
158 |
} |
160 |
} |
159 |
folderTextField.setText(getRelativeNativeName(rootFolder, preselectedFolder)); |
161 |
folderTextField.setText(getRelativeNativeName(rootFolder, preselectedFolder)); |
160 |
} |
162 |
} |
|
|
163 |
else if (project == null && preselectedFolder != null) { |
164 |
folderTextField.setText(preselectedFolder.getPath().replace('/', File.separatorChar)); |
165 |
} |
161 |
|
166 |
|
162 |
String ext = template == null ? "" : template.getExt(); // NOI18N |
167 |
String ext = template == null ? "" : template.getExt(); // NOI18N |
163 |
expectedExtension = ext.length() == 0 ? "" : "." + ext; // NOI18N |
168 |
expectedExtension = ext.length() == 0 ? "" : "." + ext; // NOI18N |
Lines 206-220
Link Here
|
206 |
public SourceGroup getTargetGroup() { |
211 |
public SourceGroup getTargetGroup() { |
207 |
return (SourceGroup)locationComboBox.getSelectedItem(); |
212 |
return (SourceGroup)locationComboBox.getSelectedItem(); |
208 |
} |
213 |
} |
209 |
|
214 |
|
|
|
215 |
@CheckForNull |
210 |
public String getTargetFolder() { |
216 |
public String getTargetFolder() { |
211 |
|
|
|
212 |
String folderName = folderTextField.getText().trim(); |
217 |
String folderName = folderTextField.getText().trim(); |
213 |
|
218 |
if ( folderName.isEmpty() ) { |
214 |
if ( folderName.length() == 0 ) { |
219 |
if (project == null) { |
|
|
220 |
String home = System.getProperty("user.home"); |
221 |
if (home != null && new File(home).isDirectory()) { |
222 |
return home; |
223 |
} |
224 |
} |
225 |
|
215 |
return null; |
226 |
return null; |
216 |
} |
227 |
} |
217 |
else { |
228 |
else { |
|
|
229 |
if (project == null && !new File(folderName).isAbsolute()) { |
230 |
String home = System.getProperty("user.home"); |
231 |
if (home != null && new File(home).isDirectory()) { |
232 |
folderName = FileUtil.getFileDisplayName(FileUtil.toFileObject(new File(home))) + File.separatorChar + folderName; |
233 |
} |
234 |
} |
235 |
|
218 |
return folderName.replace( File.separatorChar, '/' ); // NOI18N |
236 |
return folderName.replace( File.separatorChar, '/' ); // NOI18N |
219 |
} |
237 |
} |
220 |
} |
238 |
} |
Lines 392-397
Link Here
|
392 |
private javax.swing.JSeparator targetSeparator; |
410 |
private javax.swing.JSeparator targetSeparator; |
393 |
// End of variables declaration//GEN-END:variables |
411 |
// End of variables declaration//GEN-END:variables |
394 |
|
412 |
|
|
|
413 |
@CheckForNull |
395 |
private SourceGroup getPreselectedGroup( SourceGroup[] groups, FileObject folder ) { |
414 |
private SourceGroup getPreselectedGroup( SourceGroup[] groups, FileObject folder ) { |
396 |
for( int i = 0; folder != null && i < groups.length; i++ ) { |
415 |
for( int i = 0; folder != null && i < groups.length; i++ ) { |
397 |
if( FileUtil.isParentOf( groups[i].getRootFolder(), folder ) |
416 |
if( FileUtil.isParentOf( groups[i].getRootFolder(), folder ) |
Lines 418-440
Link Here
|
418 |
|
437 |
|
419 |
return path == null ? "" : path.replace( '/', File.separatorChar ); // NOI18N |
438 |
return path == null ? "" : path.replace( '/', File.separatorChar ); // NOI18N |
420 |
} |
439 |
} |
421 |
|
440 |
|
422 |
private void updateCreatedFolder() { |
441 |
private void updateCreatedFolder() { |
423 |
|
|
|
424 |
SourceGroup sg = (SourceGroup)locationComboBox.getSelectedItem(); |
442 |
SourceGroup sg = (SourceGroup)locationComboBox.getSelectedItem(); |
425 |
if (sg == null) { |
443 |
FileObject root = sg != null ? sg.getRootFolder() : null; |
426 |
return; |
444 |
String documentName = documentNameTextField.getText().trim(); |
|
|
445 |
String folderName = getTargetFolder(); |
446 |
if (folderName == null) { |
447 |
folderName = ""; |
427 |
} |
448 |
} |
428 |
FileObject root = sg.getRootFolder(); |
449 |
|
429 |
if (root == null) { |
450 |
String createdFileName = (root != null ? FileUtil.getFileDisplayName( root ) : "") + |
430 |
return; |
451 |
( root == null || folderName.startsWith("/") || folderName.startsWith( File.separator ) ? "" : "/" ) + // NOI18N |
431 |
} |
|
|
432 |
|
433 |
String folderName = folderTextField.getText().trim(); |
434 |
String documentName = documentNameTextField.getText().trim(); |
435 |
|
436 |
String createdFileName = FileUtil.getFileDisplayName( root ) + |
437 |
( folderName.startsWith("/") || folderName.startsWith( File.separator ) ? "" : "/" ) + // NOI18N |
438 |
folderName + |
452 |
folderName + |
439 |
( folderName.endsWith("/") || folderName.endsWith( File.separator ) || folderName.length() == 0 ? "" : "/" ) + // NOI18N |
453 |
( folderName.endsWith("/") || folderName.endsWith( File.separator ) || folderName.length() == 0 ? "" : "/" ) + // NOI18N |
440 |
documentName + (!freeFileExtension || documentName.indexOf('.') == -1 ? expectedExtension : ""); |
454 |
documentName + (!freeFileExtension || documentName.indexOf('.') == -1 ? expectedExtension : ""); |
Lines 449-470
Link Here
|
449 |
|
463 |
|
450 |
public @Override void actionPerformed(ActionEvent e) { |
464 |
public @Override void actionPerformed(ActionEvent e) { |
451 |
if ( browseButton == e.getSource() ) { |
465 |
if ( browseButton == e.getSource() ) { |
452 |
FileObject fo=null; |
466 |
if (project != null) { |
453 |
// Show the browse dialog |
467 |
FileObject fo; |
454 |
|
468 |
// Show the browse dialog |
455 |
SourceGroup group = (SourceGroup)locationComboBox.getSelectedItem(); |
469 |
|
456 |
if (group == null) { // #161478 |
470 |
SourceGroup group = (SourceGroup)locationComboBox.getSelectedItem(); |
457 |
return; |
471 |
if (group == null) { // #161478 |
|
|
472 |
return; |
473 |
} |
474 |
|
475 |
fo = BrowseFolders.showDialog( new SourceGroup[] { group }, |
476 |
project, |
477 |
folderTextField.getText().replace( File.separatorChar, '/' ) ); // NOI18N |
478 |
|
479 |
if ( fo != null && fo.isFolder() ) { |
480 |
String relPath = FileUtil.getRelativePath( group.getRootFolder(), fo ); |
481 |
folderTextField.setText( relPath.replace( '/', File.separatorChar ) ); // NOI18N |
482 |
} |
458 |
} |
483 |
} |
459 |
|
484 |
else { |
460 |
fo = BrowseFolders.showDialog( new SourceGroup[] { group }, |
485 |
String previousTargetFolder = getTargetFolder(); |
461 |
project, |
486 |
File targetFolder = |
462 |
folderTextField.getText().replace( File.separatorChar, '/' ) ); // NOI18N |
487 |
new FileChooserBuilder(SimpleTargetChooserPanel.class) |
463 |
|
488 |
.setDirectoriesOnly(true) |
464 |
if ( fo != null && fo.isFolder() ) { |
489 |
.setDefaultWorkingDirectory(new File(previousTargetFolder != null ? previousTargetFolder : ".")) |
465 |
String relPath = FileUtil.getRelativePath( group.getRootFolder(), fo ); |
490 |
.forceUseOfDefaultWorkingDirectory(previousTargetFolder != null) |
466 |
folderTextField.setText( relPath.replace( '/', File.separatorChar ) ); // NOI18N |
491 |
.showSaveDialog(); |
467 |
} |
492 |
|
|
|
493 |
FileObject fo = targetFolder != null ? FileUtil.toFileObject(targetFolder) : null; |
494 |
|
495 |
if ( fo != null && fo.isFolder() ) { |
496 |
folderTextField.setText( fo.getPath().replace( '/', File.separatorChar ) ); // NOI18N |
497 |
} |
498 |
} |
468 |
} |
499 |
} |
469 |
else if ( locationComboBox == e.getSource() ) { |
500 |
else if ( locationComboBox == e.getSource() ) { |
470 |
updateCreatedFolder(); |
501 |
updateCreatedFolder(); |