Skip to content

Commit

Permalink
Implemented Filter filter for jstyle files
Browse files Browse the repository at this point in the history
JabRef#1294
Extended BrowseAction to display file filter
Fixed NPE when selecting invalid jstyle (prevented style dialog from opening)
  • Loading branch information
Siedlerchr committed Apr 26, 2016
1 parent b61a7f5 commit 28f224d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
- Implemented [#1233](https://github.com/JabRef/jabref/issues/1233): Group side pane now takes up all the remaining space
- Added integrity check detecting HTML-encoded characters
- Added missing help files
- Implemented [feature request #1294](https://github.com/JabRef/jabref/issues/1233): Added possibility to filter for `*.jstyle` files in OpenOffice/LibreOffice style selection dialog. Open style selection dialog in directory of last selected file

### Fixed
- Fixed [#473](https://github.com/JabRef/jabref/issues/473): Values in an entry containing symbols like ' are now properly escaped for exporting to the database
- Fixed [#1270](https://github.com/JabRef/jabref/issues/1270): Auto save is now working again as expected (without leaving a bunch of temporary files behind)
- Fixed [#1234](https://github.com/JabRef/jabref/issues/1234): NPE when getting information from retrieved DOI
- Fixed [1245](https://github.com/JabRef/jabref/issues/1245): Empty jstyle properties can now be specified as ""

- Fixed: Selecting invalid jstyle causes NPE and prevents opening of style selection dialog
### Removed
- Removed possibility to export entries/databases to an `.sql` file, as the logic cannot easily use the correct escape logic

Expand Down
20 changes: 11 additions & 9 deletions src/main/java/net/sf/jabref/gui/actions/BrowseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,35 @@ public final class BrowseAction extends AbstractAction {
private final JTextField comp;
private final boolean dir;
private final JComponent focusTarget;
private final String extension;

public static BrowseAction buildForDir(JFrame frame, JTextField tc) {
return new BrowseAction(frame, tc, true, null);
return new BrowseAction(frame, tc, true, null, Globals.NONE);
}

public static BrowseAction buildForDir(JTextField tc) {
return new BrowseAction(null, tc, true, null);
return new BrowseAction(null, tc, true, null, Globals.NONE);
}

public static BrowseAction buildForFile(JTextField tc) {
return new BrowseAction(null, tc, false, null);
return new BrowseAction(null, tc, false, null, Globals.NONE);
}

public static BrowseAction buildForFile(JTextField tc, JComponent focusTarget) {
return new BrowseAction(null, tc, false, focusTarget);
public static BrowseAction buildForFile(JTextField tc, JComponent focusTarget, String extension) {
return new BrowseAction(null, tc, false, focusTarget, extension);
}

public static BrowseAction buildForDir(JTextField tc, JComponent focusTarget) {
return new BrowseAction(null, tc, true, focusTarget);
return new BrowseAction(null, tc, true, focusTarget, null);
}

private BrowseAction(JFrame frame, JTextField tc, boolean dir, JComponent focusTarget) {
private BrowseAction(JFrame frame, JTextField tc, boolean dir, JComponent focusTarget, String extension) {
super(Localization.lang("Browse"));
this.frame = frame;
this.dir = dir;
this.comp = tc;
this.focusTarget = focusTarget;
this.extension = extension;
}

@Override
Expand All @@ -82,10 +84,10 @@ public void actionPerformed(ActionEvent e) {

private String askUser() {
if (dir) {
return FileDialogs.getNewDir(frame, new File(comp.getText()), Globals.NONE,
return FileDialogs.getNewDir(frame, new File(comp.getText()), extension,
JFileChooser.OPEN_DIALOG, false);
} else {
return FileDialogs.getNewFile(frame, new File(comp.getText()), Globals.NONE,
return FileDialogs.getNewFile(frame, new File(comp.getText()), extension,
JFileChooser.OPEN_DIALOG, false);
}
}
Expand Down
35 changes: 19 additions & 16 deletions src/main/java/net/sf/jabref/gui/openoffice/StyleSelectDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ class StyleSelectDialog {
private final BibEntry prevEntry = new BibEntry(IdGenerator.next());

private boolean okPressed;

private final StyleLoader loader;

private final OpenOfficePreferences preferences;


Expand All @@ -127,15 +125,18 @@ public StyleSelectDialog(JabRefFrame frame, OpenOfficePreferences preferences, S
}

private void init() {


setupPopupMenu();

addButton.addActionListener(actionEvent -> {
AddFileDialog addDialog = new AddFileDialog();
addDialog.setDirectoryPath(preferences.getCurrentStyle());
addDialog.setVisible(true);
addDialog.getFileName().ifPresent(loader::addStyle);
addDialog.getFileName().ifPresent(fileName -> {
loader.addStyle(fileName);
preferences.setCurrentStyle(fileName);
});
updateStyles();

});
addButton.setToolTipText(Localization.lang("Add style file"));

Expand All @@ -149,7 +150,6 @@ private void init() {
preview.setEntry(prevEntry);

setupTable();

updateStyles();

// Build dialog
Expand All @@ -172,13 +172,11 @@ private void init() {

@Override
public void actionPerformed(ActionEvent event) {
if ((table.getRowCount() == 0) || (table.getSelectedRowCount() == 0)) {
JOptionPane.showMessageDialog(diag,
Localization
.lang("You must select a valid style file."),
Localization.lang("Style selection"), JOptionPane.ERROR_MESSAGE);
return;
}
if ((table.getRowCount() == 0) || (table.getSelectedRowCount() == 0)) {
JOptionPane.showMessageDialog(diag, Localization.lang("You must select a valid style file."),
Localization.lang("Style selection"), JOptionPane.ERROR_MESSAGE);
return;
}
okPressed = true;
storeSettings();
diag.dispose();
Expand Down Expand Up @@ -258,7 +256,6 @@ private void setupPopupMenu() {
popup.add(remove);
popup.add(reload);


// Add action listener to "Edit" menu item, which is supposed to open the style file in an external editor:
edit.addActionListener(actionEvent -> getSelectedStyle().ifPresent(style -> {
Optional<ExternalFileType> type = ExternalFileTypes.getInstance().getExternalFileTypeByExt("jstyle");
Expand Down Expand Up @@ -359,6 +356,7 @@ public Optional<OOBibStyle> getStyle() {
}
return Optional.empty();
}

/**
* Get the currently selected style.
* @return the selected style, or empty if no style is selected.
Expand Down Expand Up @@ -425,7 +423,7 @@ public boolean isOkPressed() {
}

private void tablePopup(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
popup.show(e.getComponent(), e.getX(), e.getY());
}

private void displayStyle(OOBibStyle style) {
Expand Down Expand Up @@ -497,7 +495,7 @@ public AddFileDialog() {
super(diag, Localization.lang("Add style file"), true);

JButton browse = new JButton(Localization.lang("Browse"));
browse.addActionListener(BrowseAction.buildForFile(newFile));
browse.addActionListener(BrowseAction.buildForFile(newFile, null, ".jstyle"));

// Build content panel
FormBuilder builder = FormBuilder.create();
Expand Down Expand Up @@ -547,5 +545,10 @@ public Optional<String> getFileName() {
}
return Optional.empty();
}

public void setDirectoryPath(String path) {
this.newFile.setText(path);
}

}
}
11 changes: 9 additions & 2 deletions src/main/java/net/sf/jabref/logic/openoffice/StyleLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ public void addStyle(String filename) {
OOBibStyle newStyle = new OOBibStyle(new File(filename), repository, encoding);
if (externalStyles.contains(newStyle)) {
LOGGER.info("External style file " + filename + " already existing.");
} else {
} else if (newStyle.isValid()) {
externalStyles.add(newStyle);
storeExternalStyles();
} else {
LOGGER.error(String.format("Style with filename %s is invalid", filename));
}
} catch (FileNotFoundException e) {
// The file couldn't be found... should we tell anyone?
Expand All @@ -89,7 +91,12 @@ private void loadExternalStyles() {
List<String> lists = preferences.getExternalStyles();
for (String filename : lists) {
try {
externalStyles.add(new OOBibStyle(new File(filename), repository, encoding));
OOBibStyle style = new OOBibStyle(new File(filename), repository, encoding);
if (style.isValid()) {
externalStyles.add(style);
} else {
LOGGER.error(String.format("Style with filename %s is invalid", filename));
}
} catch (FileNotFoundException e) {
// The file couldn't be found... should we tell anyone?
LOGGER.info("Cannot find external style file " + filename, e);
Expand Down

0 comments on commit 28f224d

Please sign in to comment.