Skip to content

Commit

Permalink
Fix personal journal list saving
Browse files Browse the repository at this point in the history
JabRef#1394
File was not created before writing to it.
Use nio methods
  • Loading branch information
Siedlerchr committed May 13, 2016
1 parent 8a6b059 commit 63368db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fixed [#1340](https://github.com/JabRef/jabref/issues/1340): Edit -> Mark Specific Color Dysfunctional on OSX
- Fixed [#1245](https://github.com/JabRef/jabref/issues/1245): Empty jstyle properties can now be specified as ""
- Fixed [#1364](https://github.com/JabRef/jabref/issues/1364): Windows: install to LOCALAPPDATA directory for non-admin users
- Fixed [#1365](https://github.com/JabRef/jabref/issues/1365): Default label pattern back to "[auth][year]"
- Fixed [#1365](https://github.com/JabRef/jabref/issues/1365): Default label pattern back to `[auth][year]`
- Fixed [#796](https://github.com/JabRef/jabref/issues/796): Undoing more than one entry at the same time is now working
- Fixed [#1353](https://github.com/JabRef/jabref/issues/1353): Fetch-Preview did not display updated BibTeX-Key after clicking on `Generate Now`
- Fixed [#1381](https://github.com/JabRef/jabref/issues/1381): File links containing blanks are broken if non-default viewer is set
- Fixed sourceforge bug 1000: shorttitleINI can generate the initials of the shorttitle
- Fixed [#1394](https://github.com/JabRef/jabref/issues/1394): Personal journal abbrevations could not be saved

### Removed
- Removed possibility to export entries/databases to an `.sql` file, as the logic cannot easily use the correct escape logic
Expand Down
76 changes: 34 additions & 42 deletions src/main/java/net/sf/jabref/gui/journals/ManageJournalsPanel.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2003-2015 JabRef contributors.
/* Copyright (C) 2003-2016 JabRef contributors.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
Expand All @@ -24,10 +24,14 @@
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -189,23 +193,23 @@ public ManageJournalsPanel(final JabRefFrame frame) {
});

browseNew.addActionListener(e -> {
File old = null;
if (!"".equals(newNameTf.getText())) {
old = new File(newNameTf.getText());
Path old = null;
if (!newNameTf.getText().isEmpty()) {
old = Paths.get(newNameTf.getText());
}
String name = FileDialogs.getNewFile(frame, old, null, JFileChooser.SAVE_DIALOG, false);
String name = FileDialogs.getNewFile(frame, old.toFile(), null, JFileChooser.SAVE_DIALOG, false);
if (name != null) {
newNameTf.setText(name);
newFile.setSelected(true);
}
});

browseOld.addActionListener(e -> {
File old = null;
if (!"".equals(personalFile.getText())) {
old = new File(personalFile.getText());
Path old = null;
if (!personalFile.getText().isEmpty()) {
old = Paths.get(personalFile.getText());
}
String name = FileDialogs.getNewFile(frame, old, null, JFileChooser.OPEN_DIALOG, false);
String name = FileDialogs.getNewFile(frame, old.toFile(), null, JFileChooser.OPEN_DIALOG, false);
if (name != null) {
personalFile.setText(name);
oldFile.setSelected(true);
Expand All @@ -216,15 +220,8 @@ public ManageJournalsPanel(final JabRefFrame frame) {

ok.addActionListener(e -> {
if (readyToClose()) {
try {
storeSettings();
dialog.dispose();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,
Localization.lang("Error opening file") + ": " + ex.getMessage(),
Localization.lang("Error opening file"), JOptionPane.ERROR_MESSAGE);
LOGGER.debug("Cannot find abbreviation file", ex);
}
storeSettings();
dialog.dispose();
}
});

Expand Down Expand Up @@ -291,8 +288,6 @@ private void buildExternalsPanel() {
builder.add(addExtPan).xy(1, row + 2);
builder.add(Box.createVerticalGlue()).xy(1, row + 2);

//builder.getPanel().setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.green));
//externalFilesPanel.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.red));
JScrollPane pane = new JScrollPane(builder.getPanel());
pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
externalFilesPanel.setMinimumSize(new Dimension(400, 400));
Expand Down Expand Up @@ -320,7 +315,7 @@ private void setupExternals() {
private void setupUserTable() {
List<Abbreviation> userAbbreviations = new ArrayList<>();
String filename = personalFile.getText();
if ((!filename.isEmpty()) && new File(filename).exists()) {
if (!filename.isEmpty() && Files.exists(Paths.get(filename))) {
try {
userAbbreviations = JournalAbbreviationLoader.readJournalListFromFile(new File(filename),
Globals.prefs.getDefaultEncoding());
Expand All @@ -336,7 +331,7 @@ private void setupUserTable() {
}

private boolean readyToClose() {
File f;
Path filePath;
if (newFile.isSelected()) {
if (newNameTf.getText().isEmpty()) {
if (tableModel.getRowCount() > 0) {
Expand All @@ -348,31 +343,29 @@ private boolean readyToClose() {
return true;
}
} else {
f = new File(newNameTf.getText());
return !f.exists() || (JOptionPane.showConfirmDialog(this,
Localization.lang("'%0' exists. Overwrite file?", f.getName()),
filePath = Paths.get(newNameTf.getText());
return !Files.exists(filePath) || (JOptionPane.showConfirmDialog(this,
Localization.lang("'%0' exists. Overwrite file?", filePath.getFileName().toString()),
Localization.lang("Store journal abbreviations"),
JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION);
}
}
return true;
}

private void storeSettings() throws FileNotFoundException {
File f = null;
private void storeSettings() {
Path filePath = null;
if (newFile.isSelected()) {
if (!newNameTf.getText().isEmpty()) {
f = new File(newNameTf.getText());
filePath = Paths.get(newNameTf.getText());
}
} else {
f = new File(personalFile.getText());
filePath = Paths.get(personalFile.getText());

}

if (f != null) {
if (!f.exists()) {
throw new FileNotFoundException(f.getAbsolutePath());
}
try (FileOutputStream stream = new FileOutputStream(f, false);
if (filePath != null) {
try (OutputStream stream = Files.newOutputStream(filePath, StandardOpenOption.CREATE);
OutputStreamWriter writer = new OutputStreamWriter(stream, Globals.prefs.getDefaultEncoding())) {
for (JournalEntry entry : tableModel.getJournals()) {
writer.write(entry.getName());
Expand All @@ -383,7 +376,7 @@ private void storeSettings() throws FileNotFoundException {
} catch (IOException e) {
LOGGER.warn("Problem writing abbreviation file", e);
}
String filename = f.getPath();
String filename = filePath.toString();
if ("".equals(filename)) {
filename = null;
}
Expand Down Expand Up @@ -461,15 +454,12 @@ public BrowseAction(JTextField tc, boolean dir) {
public void actionPerformed(ActionEvent e) {
String chosen;
if (dir) {
chosen = FileDialogs.getNewDir(frame, new File(comp.getText()), "", JFileChooser.OPEN_DIALOG,
false);
chosen = FileDialogs.getNewDir(frame, new File(comp.getText()), "", JFileChooser.OPEN_DIALOG, false);
} else {
chosen = FileDialogs.getNewFile(frame, new File(comp.getText()), "", JFileChooser.OPEN_DIALOG,
false);
chosen = FileDialogs.getNewFile(frame, new File(comp.getText()), "", JFileChooser.OPEN_DIALOG, false);
}
if (chosen != null) {
File nFile = new File(chosen);
comp.setText(nFile.getPath());
comp.setText(Paths.get(chosen).toString());
}
}
}
Expand All @@ -480,6 +470,7 @@ class AbbreviationsTableModel extends AbstractTableModel implements ActionListen
Localization.lang("Abbreviation")};
private List<JournalEntry> journals;


public void setJournals(List<Abbreviation> abbreviations) {
this.journals = new ArrayList<>();
for (Abbreviation abbreviation : abbreviations) {
Expand Down Expand Up @@ -676,6 +667,7 @@ public boolean equals(Object o) {
public int hashCode() {
return this.name.hashCode();
}

public String getName() {
return name;
}
Expand Down

0 comments on commit 63368db

Please sign in to comment.