Skip to content

Commit

Permalink
Followup to Issue #3167 (#3202)
Browse files Browse the repository at this point in the history
* Add braces on closing entry editor

* Fix checkstyle

* Fix reassignment of parameter

* added tests and incorporated reviewer feedback

* Fix formatting
  • Loading branch information
snisnisniksonah authored and Siedlerchr committed Sep 12, 2017
1 parent 2c5e7f2 commit 58fec29
Show file tree
Hide file tree
Showing 22 changed files with 685 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed an issue where the arrow keys in the search bar did not work as expected [#3081](https://github.com/JabRef/jabref/issues/3081)
- We fixed wrong hotkey being displayed at "automatically file links" in the entry editor
- We fixed an issue where metadata syncing with local and shared database were unstable. It will also fix syncing groups and sub-groups in database. [#2284](https://github.com/JabRef/jabref/issues/2284)
- We fixed an issue where it was possible to leave the entry editor with an imbalance of braces. [#3167](https://github.com/JabRef/jabref/issues/3167)
- Renaming files now truncates the filename to not exceed the limit of 255 chars [#2622](https://github.com/JabRef/jabref/issues/2622)

### Removed
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.swing.AbstractAction;
import javax.swing.Action;
Expand Down Expand Up @@ -71,6 +73,7 @@
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.importer.EntryBasedFetcher;
import org.jabref.logic.importer.WebFetchers;
import org.jabref.logic.integrity.BracesCorrector;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.search.SearchQueryHighlightListener;
import org.jabref.logic.util.OS;
Expand All @@ -88,6 +91,7 @@
import org.apache.commons.logging.LogFactory;
import org.fxmisc.easybind.EasyBind;


/**
* GUI component that allows editing of the fields of a BibEntry (i.e. the
* one that shows up, when you double click on an entry in the table)
Expand Down Expand Up @@ -631,14 +635,22 @@ public void actionPerformed(ActionEvent e) {
}

private class CloseAction extends AbstractAction {

private CloseAction() {
super(Localization.lang("Close window"), IconTheme.JabRefIcon.CLOSE.getSmallIcon());
putValue(Action.SHORT_DESCRIPTION, Localization.lang("Close window"));
}

@Override
public void actionPerformed(ActionEvent e) {
Map<String,String> cleanedEntries = entry
.getFieldMap()
.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, f -> BracesCorrector.apply(f.getValue())));
if (!cleanedEntries.equals(entry.getFieldMap())) {
frame.output(Localization.lang("Added missing braces."));
}
entry.setField(cleanedEntries);
panel.entryEditorClosing(EntryEditor.this);
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/jabref/logic/integrity/BracesCorrector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.jabref.logic.integrity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BracesCorrector {

private static final Pattern PATTERN_ESCAPED_CURLY_BRACES = Pattern.compile("(\\\\\\{)|(\\\\\\})");
//private static final Pattern PATTERN_ESCAPED_CLOSING_CURLY_BRACE = Pattern.compile("");

public static String apply(String input) {
if (input == null) {
return null;
} else {
Matcher matcher = PATTERN_ESCAPED_CURLY_BRACES.matcher(input);
String addedBraces = input;
String c = matcher.replaceAll("");

long diff = c.chars().filter(ch -> ch == '{').count() - c.chars().filter(ch -> ch == '}').count();
while (diff != 0) {
if (diff < 0) {
addedBraces = "{" + addedBraces;
diff++;
} else {
addedBraces = addedBraces + "}";
diff--;
}
}
return addedBraces;
}
}

}
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Add_to_group=Tilføj_i_gruppe

Added_group_"%0".=Tilføjede_gruppe_"%0".

Added_missing_braces.=

Added_new=Tilføjede_ny

Added_string=Tilføjede_streng
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#!
#!created/edited_by_Popeye_version_0.55_(github.com/JabRef/popeye)
#!encoding:UTF-8
#! created/edited by Popeye version 0.55 (github.com/JabRef/popeye)
#! encoding:UTF-8

%0_contains_the_regular_expression_<b>%1</b>=%0_den_regulären_Ausdruck_<b>%1</b>_enthält

Expand Down Expand Up @@ -69,6 +69,8 @@ Add_to_group=Zu_Gruppe_hinzufügen

Added_group_"%0".=Gruppe_"%0"_hinzugefügt.

Added_missing_braces.=Fehlende_Klammern_hinzugefügt.

Added_new=Neu_hinzugefügt

Added_string=String_hinzugefügt
Expand Down
Loading

0 comments on commit 58fec29

Please sign in to comment.