From b856dd4b690501476a06e9b2a058b2f82f7051ef Mon Sep 17 00:00:00 2001 From: antalk2 Date: Tue, 2 Mar 2021 21:20:58 +0100 Subject: [PATCH] Unmerge LO citations (#7455) --- CHANGELOG.md | 1 + .../org/jabref/gui/openoffice/OOBibBase.java | 64 +++++++++++++++++++ .../gui/openoffice/OpenOfficePanel.java | 20 +++++- src/main/resources/l10n/JabRef_en.properties | 3 + 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd05732f4ca..99b84dd7dbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue with TexGroups on Linux systems, where the modification of an aux-file did not trigger an auto-update for TexGroups. Furthermore, the detection of file modifications is now more reliable. [#7412](https://github.com/JabRef/jabref/pull/7412) - We fixed an issue where the Unicode to Latex formatter produced wrong results for characters with a codepoint higher than Character.MAX_VALUE. [#7387](https://github.com/JabRef/jabref/issues/7387) - We fixed an issue where a non valid value as font size results in an uncaught exception. [#7415](https://github.com/JabRef/jabref/issues/7415) +- We fixed an issue where "Merge citations" in the Openoffice/Libreoffice integration panel did not have a corresponding opposite. [#7454](https://github.com/JabRef/jabref/issues/7454) - We fixed an issue where drag and drop of bib files for opening resulted in uncaught exceptions [#7464](https://github.com/JabRef/jabref/issues/7464) ### Removed diff --git a/src/main/java/org/jabref/gui/openoffice/OOBibBase.java b/src/main/java/org/jabref/gui/openoffice/OOBibBase.java index 0f58da2cd26..076c2ce2360 100644 --- a/src/main/java/org/jabref/gui/openoffice/OOBibBase.java +++ b/src/main/java/org/jabref/gui/openoffice/OOBibBase.java @@ -1259,6 +1259,70 @@ public void combineCiteMarkers(List databases, OOBibStyle style) } } + /** + * Do the opposite of combineCiteMarkers. + * Combined markers are split, with a space inserted between. + */ + public void unCombineCiteMarkers(List databases, OOBibStyle style) + throws IOException, WrappedTargetException, NoSuchElementException, IllegalArgumentException, + UndefinedCharacterFormatException, UnknownPropertyException, PropertyVetoException, CreationException, + BibEntryNotFoundException { + XNameAccess nameAccess = getReferenceMarks(); + List names = getSortedReferenceMarks(nameAccess); + + final XTextRangeCompare compare = UnoRuntime.queryInterface(XTextRangeCompare.class, text); + + int pivot = 0; + boolean madeModifications = false; + while (pivot < (names.size())) { + XTextRange range1 = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(names.get(pivot))) + .getAnchor(); + + XTextCursor textCursor = range1.getText().createTextCursorByRange(range1); + + // If we are supposed to set character format for citations, test this before + // making any changes. This way we can throw an exception before any reference + // marks are removed, preventing damage to the user's document: + if (style.isFormatCitations()) { + XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, textCursor); + String charStyle = style.getCitationCharacterFormat(); + try { + xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle); + } catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | + WrappedTargetException ex) { + // Setting the character format failed, so we throw an exception that + // will result in an error message for the user: + throw new UndefinedCharacterFormatException(charStyle); + } + } + + List keys = parseRefMarkName(names.get(pivot)); + if (keys.size() > 1) { + removeReferenceMark(names.get(pivot)); + + // Insert bookmark for each key + int last = keys.size() - 1; + int i = 0; + for (String key : keys) { + String newName = getUniqueReferenceMarkName(key, OOBibBase.AUTHORYEAR_PAR); + insertReferenceMark(newName, "tmp", textCursor, true, style); + textCursor.collapseToEnd(); + if (i != last) { + textCursor.setString(" "); + textCursor.collapseToEnd(); + } + i++; + } + madeModifications = true; + } + pivot++; + } + if (madeModifications) { + updateSortedReferenceMarks(); + refreshCiteMarkers(databases, style); + } + } + public BibDatabase generateDatabase(List databases) throws NoSuchElementException, WrappedTargetException { BibDatabase resultDatabase = new BibDatabase(); diff --git a/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java b/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java index ad473cfab1b..635d9469a31 100644 --- a/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java +++ b/src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java @@ -83,6 +83,7 @@ public class OpenOfficePanel { private final Button pushEntriesAdvanced = new Button(Localization.lang("Cite special")); private final Button update; private final Button merge = new Button(Localization.lang("Merge citations")); + private final Button unmerge = new Button(Localization.lang("Separate citations")); private final Button manageCitations = new Button(Localization.lang("Manage citations")); private final Button exportCitations = new Button(Localization.lang("Export cited")); private final Button settingsB = new Button(Localization.lang("Settings")); @@ -239,6 +240,21 @@ private void initPanel() { LOGGER.warn("Problem combining cite markers", ex); } }); + + unmerge.setMaxWidth(Double.MAX_VALUE); + unmerge.setTooltip(new Tooltip(Localization.lang("Separate merged citations"))); + unmerge.setOnAction(e -> { + try { + ooBase.unCombineCiteMarkers(getBaseList(), style); + } catch (UndefinedCharacterFormatException ex) { + reportUndefinedCharacterFormat(ex); + } catch (com.sun.star.lang.IllegalArgumentException | UnknownPropertyException | PropertyVetoException | + CreationException | NoSuchElementException | WrappedTargetException | IOException | + BibEntryNotFoundException ex) { + LOGGER.warn("Problem uncombining cite markers", ex); + } + }); + ContextMenu settingsMenu = createSettingsPopup(); settingsB.setMaxWidth(Double.MAX_VALUE); settingsB.setContextMenu(settingsMenu); @@ -258,6 +274,7 @@ private void initPanel() { pushEntriesAdvanced.setDisable(true); update.setDisable(true); merge.setDisable(true); + unmerge.setDisable(true); manageCitations.setDisable(true); exportCitations.setDisable(true); @@ -271,7 +288,7 @@ private void initPanel() { flow.setHgap(4); flow.setPrefWrapLength(200); flow.getChildren().addAll(setStyleFile, pushEntries, pushEntriesInt); - flow.getChildren().addAll(pushEntriesAdvanced, pushEntriesEmpty, merge); + flow.getChildren().addAll(pushEntriesAdvanced, pushEntriesEmpty, merge, unmerge); flow.getChildren().addAll(manageCitations, exportCitations, settingsB); vbox.setFillWidth(true); @@ -422,6 +439,7 @@ protected OOBibBase call() throws Exception { pushEntriesAdvanced.setDisable(false); update.setDisable(false); merge.setDisable(false); + unmerge.setDisable(false); manageCitations.setDisable(false); exportCitations.setDisable(false); }); diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 420c56cd4b1..2df2bb3322b 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -2282,6 +2282,9 @@ No\ metadata\ found.\ Creating\ empty\ entry\ with\ file\ link=No metadata found Processing\ file\ %0=Processing file %0 Export\ selected=Export selected +Separate\ merged\ citations=Separate merged citations +Separate\ citations=Separate citations + Unprotect\ terms=Unprotect terms Error\ connecting\ to\ Writer\ document=Error connecting to Writer document You\ need\ to\ open\ Writer\ with\ a\ document\ before\ connecting=You need to open Writer with a document before connecting