37
37
import org .jabref .logic .externalfiles .LinkedFileHandler ;
38
38
import org .jabref .logic .l10n .Localization ;
39
39
import org .jabref .logic .net .URLDownload ;
40
- import org .jabref .logic .util .io .FileUtil ;
41
40
import org .jabref .logic .xmp .XmpPreferences ;
42
41
import org .jabref .logic .xmp .XmpUtilWriter ;
43
42
import org .jabref .model .database .BibDatabaseContext ;
@@ -194,24 +193,34 @@ public void openFolder() {
194
193
}
195
194
}
196
195
197
- public void rename () {
196
+ public void renameToSuggestion () {
197
+ renameFileToName (linkedFileHandler .getSuggestedFileName ());
198
+ }
199
+
200
+ public void askForNameAndRename () {
201
+ String oldFile = this .linkedFile .getLink ();
202
+ Path oldFilePath = Paths .get (oldFile );
203
+ Optional <String > askedFileName = dialogService .showInputDialogWithDefaultAndWait (Localization .lang ("Rename file" ), Localization .lang ("New Filename" ), oldFilePath .getFileName ().toString ());
204
+ askedFileName .ifPresent (this ::renameFileToName );
205
+ }
206
+
207
+ public void renameFileToName (String targetFileName ) {
198
208
if (linkedFile .isOnlineLink ()) {
199
209
// Cannot rename remote links
200
210
return ;
201
211
}
202
212
203
213
Optional <Path > file = linkedFile .findIn (databaseContext , filePreferences );
204
214
if (file .isPresent ()) {
205
- performRenameWithConflictCheck ();
215
+ performRenameWithConflictCheck (targetFileName );
206
216
} else {
207
217
dialogService .showErrorDialogAndWait (Localization .lang ("File not found" ), Localization .lang ("Could not find file '%0'." , linkedFile .getLink ()));
208
218
}
209
219
}
210
220
211
- private void performRenameWithConflictCheck () {
212
- Optional <Path > fileConflictCheck = linkedFileHandler .findExistingFile (linkedFile , entry );
221
+ private void performRenameWithConflictCheck (String targetFileName ) {
222
+ Optional <Path > fileConflictCheck = linkedFileHandler .findExistingFile (linkedFile , entry , targetFileName );
213
223
if (fileConflictCheck .isPresent ()) {
214
- String targetFileName = linkedFileHandler .getSuggestedFileName ();
215
224
boolean confirmOverwrite = dialogService .showConfirmationDialogAndWait (
216
225
Localization .lang ("File exists" ),
217
226
Localization .lang ("'%0' exists. Overwrite file?" , targetFileName ),
@@ -233,7 +242,7 @@ private void performRenameWithConflictCheck() {
233
242
}
234
243
235
244
try {
236
- linkedFileHandler .renameToSuggestedName ( );
245
+ linkedFileHandler .renameToName ( targetFileName );
237
246
} catch (IOException e ) {
238
247
dialogService .showErrorDialogAndWait (Localization .lang ("Rename failed" ), Localization .lang ("JabRef cannot access the file because it is being used by another process." ));
239
248
}
@@ -302,7 +311,7 @@ public boolean isGeneratedPathSameAsOriginal() {
302
311
303
312
public void moveToDefaultDirectoryAndRename () {
304
313
moveToDefaultDirectory ();
305
- rename ();
314
+ renameToSuggestion ();
306
315
}
307
316
308
317
/**
@@ -355,17 +364,6 @@ public void edit() {
355
364
});
356
365
}
357
366
358
- public void renameFile () {
359
- String oldFile = this .linkedFile .getLink ();
360
- Path oldFilePath = Paths .get (oldFile );
361
- Optional <String > editedFile = dialogService .showInputDialogWithDefaultAndWait (Localization .lang ("Rename file" ), Localization .lang ("New Filename" ), oldFilePath .getFileName ().toString ());
362
- editedFile .ifPresent (file -> {
363
- Path newFile = Paths .get (oldFile ).resolveSibling (file );
364
- this .linkedFile .setLink (newFile .toString ());
365
- FileUtil .renameFile (Paths .get (oldFile ), newFile );
366
- });
367
- }
368
-
369
367
public void writeXMPMetadata () {
370
368
// Localization.lang("Writing XMP-metadata...")
371
369
BackgroundTask <Void > writeTask = BackgroundTask .wrap (() -> {
0 commit comments