2727import com .sun .star .lang .WrappedTargetException ;
2828import com .sun .star .text .XTextCursor ;
2929import com .sun .star .text .XTextDocument ;
30+ import com .sun .star .uno .Exception ;
3031
3132/// This class processes CSL citations in JabRef and interacts directly with LibreOffice using an XTextDocument instance.
3233/// It is tightly coupled with {@link CSLReferenceMarkManager} for management of reference marks tied to the CSL citations.
@@ -48,9 +49,7 @@ public class CSLCitationOOAdapter {
4849 private final OpenOfficePreferences openOfficePreferences ;
4950
5051 private CitationStyle currentStyle ;
51- private boolean styleChanged ;
52- private boolean inTextUsed ;
53- private boolean inTextNatureChanged ;
52+ private CSLCitationType citationType ;
5453
5554 public CSLCitationOOAdapter (XTextDocument doc , Supplier <List <BibDatabaseContext >> databasesSupplier , OpenOfficePreferences openOfficePreferences , BibEntryTypesManager bibEntryTypesManager ) throws WrappedTargetException , NoSuchElementException {
5655 this .document = doc ;
@@ -65,36 +64,42 @@ public CSLCitationOOAdapter(XTextDocument doc, Supplier<List<BibDatabaseContext>
6564 }
6665
6766 markManager .readAndUpdateExistingMarks ();
68- this .inTextUsed = markManager .hasInTextMarks ();
67+ this .citationType = markManager .getCitationType ();
6968 }
7069
71- public void setStyle (CitationStyle newStyle ) {
70+ /// This method is used to determine whether citation style and citation type should be updated
71+ /// Citation type and citation style are extracted into one method for more readability and uniformity
72+ public void setCitationStyleParameters (CitationStyle newStyle , CSLCitationType newCitationType ) throws CreationException , Exception {
73+ boolean styleChanged ;
74+ boolean citationTypeIsChanged ;
75+
7276 if (currentStyle == null || !currentStyle .getName ().equals (newStyle .getName ())) {
7377 styleChanged = true ;
74- currentStyle = newStyle ;
78+ this . currentStyle = newStyle ;
7579 } else {
7680 styleChanged = false ;
7781 }
82+
83+ if (this .citationType != newCitationType ) {
84+ this .citationType = newCitationType ;
85+ citationTypeIsChanged = true ;
86+ } else {
87+ citationTypeIsChanged = false ;
88+ }
89+
90+ if (styleChanged || citationTypeIsChanged ) {
91+ updateAllCitationsWithNewStyle (currentStyle , newCitationType );
92+ }
7893 }
7994
8095 /// Inserts a citation for a group of entries.
8196 /// Comparable to LaTeX's \cite command.
8297 public void insertCitation (XTextCursor cursor , CitationStyle selectedStyle , List <BibEntry > entries , BibDatabaseContext bibDatabaseContext , BibEntryTypesManager bibEntryTypesManager )
8398 throws CreationException , com .sun .star .uno .Exception {
84- setStyle (selectedStyle );
85-
86- if (inTextUsed ) {
87- inTextNatureChanged = true ;
88- inTextUsed = false ;
89- } else {
90- inTextNatureChanged = false ;
91- }
92-
99+ // If current citation style is not the same as passed-in citation type, then change it to the new citation style
100+ // If current citation type is not "NORMAL", then change it to "NORMAL".
93101 // Placing this at the beginning reduces the number of updates needed by 1 (in the positive case)
94- if (styleChanged || inTextNatureChanged ) {
95- updateAllCitationsWithNewStyle (currentStyle , false );
96- styleChanged = false ;
97- }
102+ setCitationStyleParameters (selectedStyle , CSLCitationType .NORMAL );
98103
99104 String style = selectedStyle .getSource ();
100105 boolean isNumericStyle = selectedStyle .isNumericStyle ();
@@ -115,7 +120,7 @@ public void insertCitation(XTextCursor cursor, CitationStyle selectedStyle, List
115120 }
116121
117122 OOText ooText = OOFormat .setLocaleNone (OOText .fromString (formattedCitation ));
118- insertReferences (cursor , entries , ooText , isNumericStyle , false );
123+ insertReferences (cursor , entries , ooText , isNumericStyle , CSLCitationType . NORMAL );
119124 }
120125
121126 /// Inserts in-text citations for a group of entries.
@@ -124,19 +129,7 @@ public void insertCitation(XTextCursor cursor, CitationStyle selectedStyle, List
124129 /// @implNote Very similar to the {@link #insertCitation(XTextCursor, CitationStyle, List, BibDatabaseContext, BibEntryTypesManager) insertCitation} method.
125130 public void insertInTextCitation (XTextCursor cursor , CitationStyle selectedStyle , List <BibEntry > entries , BibDatabaseContext bibDatabaseContext , BibEntryTypesManager bibEntryTypesManager )
126131 throws CreationException , com .sun .star .uno .Exception {
127- setStyle (selectedStyle );
128-
129- if (!inTextUsed ) {
130- inTextUsed = true ;
131- inTextNatureChanged = true ;
132- } else {
133- inTextNatureChanged = false ;
134- }
135-
136- if (styleChanged || inTextNatureChanged ) {
137- updateAllCitationsWithNewStyle (currentStyle , true );
138- styleChanged = false ;
139- }
132+ setCitationStyleParameters (selectedStyle , CSLCitationType .IN_TEXT );
140133
141134 String style = selectedStyle .getSource ();
142135 boolean isNumericStyle = selectedStyle .isNumericStyle ();
@@ -172,16 +165,18 @@ public void insertInTextCitation(XTextCursor cursor, CitationStyle selectedStyle
172165 }
173166
174167 OOText ooText = OOFormat .setLocaleNone (OOText .fromString (finalText ));
175- insertReferences (cursor , List .of (currentEntry ), ooText , isNumericStyle , true );
168+ insertReferences (cursor , List .of (currentEntry ), ooText , isNumericStyle , CSLCitationType . IN_TEXT );
176169 }
177170 }
178171
179172 /// Inserts "empty" citations for a list of entries at the cursor to the document.
180173 /// Adds the entries to the list for which bibliography is to be generated.
181174 public void insertEmptyCitation (XTextCursor cursor , CitationStyle selectedStyle , List <BibEntry > entries )
182175 throws CreationException , com .sun .star .uno .Exception {
176+ setCitationStyleParameters (selectedStyle , CSLCitationType .EMPTY );
177+
183178 OOText emptyOOText = OOFormat .setLocaleNone (OOText .fromString ("" ));
184- insertReferences (cursor , entries , emptyOOText , selectedStyle .isNumericStyle (), false );
179+ insertReferences (cursor , entries , emptyOOText , selectedStyle .isNumericStyle (), CSLCitationType . EMPTY );
185180 }
186181
187182 /// Creates a "Bibliography" section in the document and inserts a list of references.
@@ -196,7 +191,7 @@ public void insertBibliography(XTextCursor cursor, CitationStyle selectedStyle,
196191
197192 markManager .setRealTimeNumberUpdateRequired (isNumericStyle );
198193 markManager .readAndUpdateExistingMarks ();
199- updateAllCitationsWithNewStyle (selectedStyle , inTextUsed );
194+ updateAllCitationsWithNewStyle (selectedStyle , citationType );
200195 markManager .readAndUpdateExistingMarks ();
201196
202197 OOText title = OOFormat .paragraph (OOText .fromString (openOfficePreferences .getCslBibliographyTitle ()), openOfficePreferences .getCslBibliographyHeaderFormat ());
@@ -234,7 +229,7 @@ public void insertBibliography(XTextCursor cursor, CitationStyle selectedStyle,
234229 }
235230
236231 /// Inserts references and also adds a space before the citation if not already present ("smart space").
237- private void insertReferences (XTextCursor cursor , List <BibEntry > entries , OOText ooText , boolean isNumericStyle , boolean isInText )
232+ private void insertReferences (XTextCursor cursor , List <BibEntry > entries , OOText ooText , boolean isNumericStyle , CSLCitationType citationType )
238233 throws CreationException , com .sun .star .uno .Exception {
239234 boolean preceedingSpaceExists ;
240235 XTextCursor checkCursor = cursor .getText ().createTextCursorByRange (cursor .getStart ());
@@ -251,10 +246,10 @@ private void insertReferences(XTextCursor cursor, List<BibEntry> entries, OOText
251246 preceedingSpaceExists = checkCursor .getString ().matches ("\\ R" );
252247 }
253248 }
254- markManager .insertReferenceIntoOO (entries , document , cursor , ooText , !preceedingSpaceExists , openOfficePreferences .getAddSpaceAfter (), isInText );
249+ markManager .insertReferenceIntoOO (entries , document , cursor , ooText , !preceedingSpaceExists , openOfficePreferences .getAddSpaceAfter (), citationType );
255250 markManager .setRealTimeNumberUpdateRequired (isNumericStyle );
256251 markManager .readAndUpdateExistingMarks ();
257- inTextUsed = markManager .hasInTextMarks ();
252+ this . citationType = markManager .getCitationType ();
258253 }
259254
260255 /// Transforms the numbers in the citation to globally-unique (and thus, reusable) numbers.
@@ -280,7 +275,7 @@ private String updateSingleOrMultipleCitationNumbers(String citation, List<BibEn
280275 /// However, all "generation" of CSL style citations (via {@link CitationStyleGenerator}) occur in this class, and not in {@link CSLReferenceMarkManager}.
281276 /// Furthermore, {@link CSLReferenceMarkManager} is not composed of {@link CitationStyle}.
282277 /// Hence, we keep {@link CSLReferenceMarkManager} independent of {@link CitationStyleGenerator} and {@link CitationStyle}, and keep the following two methods here.
283- private void updateAllCitationsWithNewStyle (CitationStyle style , boolean isInTextStyle )
278+ private void updateAllCitationsWithNewStyle (CitationStyle style , CSLCitationType citationType )
284279 throws com .sun .star .uno .Exception , CreationException {
285280 boolean isNumericStyle = style .isNumericStyle ();
286281 boolean isAlphaNumericStyle = style .isAlphanumericStyle ();
@@ -309,7 +304,11 @@ private void updateAllCitationsWithNewStyle(CitationStyle style, boolean isInTex
309304 // Next, we get the list of reference marks sorted in order of appearance in the document
310305 List <CSLReferenceMark > marksInOrder = markManager .getMarksInOrder ();
311306
312- if (isInTextStyle ) {
307+ if (citationType == CSLCitationType .EMPTY ) {
308+ for (CSLReferenceMark mark : marksInOrder ) {
309+ markManager .updateMarkAndTextWithNewStyle (mark , "" , CSLCitationType .EMPTY );
310+ }
311+ } else if (citationType == CSLCitationType .IN_TEXT ) {
313312 // Now, for each such reference mark, we get the entries to be updated
314313 for (CSLReferenceMark mark : marksInOrder ) {
315314 List <String > citationKeys = mark .getCitationKeys ();
@@ -350,7 +349,7 @@ private void updateAllCitationsWithNewStyle(CitationStyle style, boolean isInTex
350349 }
351350 }
352351
353- markManager .updateMarkAndTextWithNewStyle (mark , finalText .toString (), isInTextStyle );
352+ markManager .updateMarkAndTextWithNewStyle (mark , finalText .toString (), CSLCitationType . IN_TEXT );
354353 }
355354 } else {
356355 // Same flow as above - for each such reference mark, we get the entries to be updated
@@ -373,7 +372,7 @@ private void updateAllCitationsWithNewStyle(CitationStyle style, boolean isInTex
373372
374373 String formattedCitation = CSLFormatUtils .transformHTML (newCitation );
375374
376- markManager .updateMarkAndTextWithNewStyle (mark , formattedCitation , isInTextStyle );
375+ markManager .updateMarkAndTextWithNewStyle (mark , formattedCitation , CSLCitationType . NORMAL );
377376 }
378377 }
379378 }
0 commit comments