-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Open and Save Dialogs #1336
Changes from 1 commit
39dbf77
d84191d
fe8a9a4
56ff174
669a3a1
f368f78
09f072e
2b63014
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,18 +18,17 @@ | |
import java.awt.event.ActionEvent; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import javax.swing.AbstractAction; | ||
import javax.swing.JFileChooser; | ||
import javax.swing.JOptionPane; | ||
|
||
import net.sf.jabref.Globals; | ||
import net.sf.jabref.gui.FileDialogs; | ||
import net.sf.jabref.gui.FileListEntry; | ||
import net.sf.jabref.gui.JabRefFrame; | ||
import net.sf.jabref.gui.NewFileDialogs; | ||
import net.sf.jabref.gui.entryeditor.EntryEditor; | ||
import net.sf.jabref.gui.fieldeditors.FileListEditor; | ||
import net.sf.jabref.gui.util.component.CheckBoxMessage; | ||
|
@@ -44,6 +43,7 @@ | |
* Action for moving or renaming a file that is linked to from an entry in JabRef. | ||
*/ | ||
public class MoveFileAction extends AbstractAction { | ||
|
||
private static final Log LOGGER = LogFactory.getLog(MoveFileAction.class); | ||
|
||
private final JabRefFrame frame; | ||
|
@@ -54,6 +54,7 @@ public class MoveFileAction extends AbstractAction { | |
|
||
private static final String MOVE_RENAME = Localization.lang("Move/Rename file"); | ||
|
||
|
||
public MoveFileAction(JabRefFrame frame, EntryEditor eEditor, FileListEditor editor, boolean toFileDir) { | ||
this.frame = frame; | ||
this.eEditor = eEditor; | ||
|
@@ -89,7 +90,8 @@ public void actionPerformed(ActionEvent event) { | |
} | ||
} | ||
if (found < 0) { | ||
JOptionPane.showMessageDialog(frame, Localization.lang("File_directory_is_not_set_or_does_not_exist!"), MOVE_RENAME, JOptionPane.ERROR_MESSAGE); | ||
JOptionPane.showMessageDialog(frame, Localization.lang("File_directory_is_not_set_or_does_not_exist!"), | ||
MOVE_RENAME, JOptionPane.ERROR_MESSAGE); | ||
return; | ||
} | ||
File file = new File(ln); | ||
|
@@ -123,8 +125,7 @@ public void actionPerformed(ActionEvent event) { | |
answer = JOptionPane.showConfirmDialog(frame, Localization.lang("Move file to file directory?"), | ||
MOVE_RENAME, JOptionPane.YES_NO_OPTION); | ||
} else { | ||
answer = JOptionPane.showConfirmDialog(frame, cbm, MOVE_RENAME, | ||
JOptionPane.YES_NO_OPTION); | ||
answer = JOptionPane.showConfirmDialog(frame, cbm, MOVE_RENAME, JOptionPane.YES_NO_OPTION); | ||
} | ||
if (answer != JOptionPane.YES_OPTION) { | ||
return; | ||
|
@@ -143,23 +144,16 @@ public void actionPerformed(ActionEvent event) { | |
} | ||
chosenFile = sb.toString(); | ||
} else { | ||
chosenFile = FileDialogs.getNewFile(frame, file, Collections.singletonList(extension), | ||
JFileChooser.SAVE_DIALOG, false); | ||
System.out.println("Entry extension " + extension); | ||
chosenFile = new NewFileDialogs(frame, file.getPath()).saveNewFile().toString(); | ||
|
||
|
||
} | ||
if (chosenFile == null) { | ||
if (chosenFile.isEmpty()) { | ||
return; // canceled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now files are always overwritten? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, the overwrite dialog is implemented as part of the JFileChooser (overriden method), so it needs not to be handled everywhere |
||
} | ||
newFile = new File(chosenFile); | ||
// Check if the file already exists: | ||
if (newFile.exists() && (JOptionPane.showConfirmDialog(frame, | ||
Localization.lang("'%0' exists. Overwrite file?", newFile.getName()), MOVE_RENAME, | ||
JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)) { | ||
if (toFileDir) { | ||
return; | ||
} else { | ||
repeat = true; | ||
} | ||
} | ||
|
||
} | ||
|
||
if (!newFile.equals(file)) { | ||
|
@@ -170,29 +164,29 @@ public void actionPerformed(ActionEvent event) { | |
} | ||
if (success) { | ||
// Remove the original file: | ||
if (!file.delete()) { | ||
LOGGER.info("Cannot delete original file"); | ||
} | ||
Files.deleteIfExists(file.toPath()); | ||
|
||
// Relativise path, if possible. | ||
String canPath = new File(dirs.get(found)).getCanonicalPath(); | ||
if (newFile.getCanonicalPath().startsWith(canPath)) { | ||
if ((newFile.getCanonicalPath().length() > canPath.length()) && | ||
(newFile.getCanonicalPath().charAt(canPath.length()) == File.separatorChar)) { | ||
if ((newFile.getCanonicalPath().length() > canPath.length()) | ||
&& (newFile.getCanonicalPath().charAt(canPath.length()) == File.separatorChar)) { | ||
|
||
String newLink = newFile.getCanonicalPath().substring(1 + canPath.length()); | ||
editor.getTableModel().setEntry(selected, new FileListEntry(entry.description, newLink, entry.type)); | ||
editor.getTableModel().setEntry(selected, | ||
new FileListEntry(entry.description, newLink, entry.type)); | ||
} else { | ||
String newLink = newFile.getCanonicalPath().substring(canPath.length()); | ||
editor.getTableModel().setEntry(selected, new FileListEntry(entry.description, newLink, entry.type)); | ||
editor.getTableModel().setEntry(selected, | ||
new FileListEntry(entry.description, newLink, entry.type)); | ||
} | ||
|
||
} else { | ||
String newLink = newFile.getCanonicalPath(); | ||
editor.getTableModel().setEntry(selected, new FileListEntry(entry.description, newLink, entry.type)); | ||
editor.getTableModel().setEntry(selected, | ||
new FileListEntry(entry.description, newLink, entry.type)); | ||
} | ||
eEditor.updateField(editor); | ||
//JOptionPane.showMessageDialog(frame, Globals.lang("File moved"), | ||
// Globals.lang("Move/Rename file"), JOptionPane.INFORMATION_MESSAGE); | ||
frame.output(Localization.lang("File moved")); | ||
} else { | ||
JOptionPane.showMessageDialog(frame, Localization.lang("Move file failed"), MOVE_RENAME, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Below you create a
File newFile
from it, so probably no need to first convert it to a string. This actually happens in a lot of places.