Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,24 @@ private void initialize() {

private void displayBibTeX(BibEntry entry, String bibTeX) {
if (entriesListView.getCheckModel().isChecked(entry)) {
bibTeXData.clear();
bibTeXData.appendText(bibTeX);
bibTeXData.appendText(OS.NEWLINE);
bibTeXData.moveTo(0);
bibTeXData.requestFollowCaret();
} else {
String currentText = bibTeXData.getText();
String newText = currentText.replace(bibTeX, "");
bibTeXData.clear();
bibTeXData.appendText(newText);
}
}

private void displayBibTeXesWithoutCheck(String bibTeXes) {
bibTeXData.clear();
bibTeXData.appendText(bibTeXes);
bibTeXData.moveTo(0);
bibTeXData.requestFollowCaret();
}

private void initBibTeX() {
bibTeXDataLabel.setText(Localization.lang("%0 source", "BibTeX"));
Expand All @@ -209,20 +219,23 @@ private void initBibTeX() {

public void unselectAll() {
entriesListView.getCheckModel().clearChecks();
bibTeXData.clear();
}

public void selectAllNewEntries() {
unselectAll();
for (BibEntry entry : entriesListView.getItems()) {
if (!viewModel.hasDuplicate(entry)) {
entriesListView.getCheckModel().check(entry);
displayBibTeX(entry, viewModel.getSourceString(entry));
displayBibTeX(entry, viewModel.getSourceString(entry, true));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The boolean parameter true is a magic value that makes the code less maintainable and readable. Consider creating an enum or named constant to clarify the parameter's purpose.

}
}
}

public void selectAllEntries() {
unselectAll();
entriesListView.getCheckModel().checkAll();
List<BibEntry> entries = entriesListView.getItems();
displayBibTeXesWithoutCheck(viewModel.getSourcesString(entries));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,25 @@ public boolean hasDuplicate(BibEntry entry) {
.containsDuplicate(selectedDb.getValue().getDatabase(), entry, selectedDb.getValue().getMode()).isPresent();
}

public String getSourceString(BibEntry entry) {
public String getSourcesString(List<BibEntry> entries) {
StringWriter writer = new StringWriter();
BibWriter bibWriter = new BibWriter(writer, OS.NEWLINE);
FieldWriter fieldWriter = FieldWriter.buildIgnoreHashes(preferences.getFieldPreferences());
String serializedStrings;
try {
new BibEntryWriter(fieldWriter, entryTypesManager).write(entry, bibWriter, selectedDb.getValue().getMode());
serializedStrings = new BibEntryWriter(fieldWriter, entryTypesManager).serializeAll(entries, selectedDb.getValue().getMode());
} catch (IOException ioException) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty string is returned on IOException without logging or proper error handling. This silently fails and may hide important issues from users and developers.

serializedStrings = "";
}
return serializedStrings;
}

public String getSourceString(BibEntry entry, boolean reformat) {
StringWriter writer = new StringWriter();
BibWriter bibWriter = new BibWriter(writer, OS.NEWLINE);
FieldWriter fieldWriter = FieldWriter.buildIgnoreHashes(preferences.getFieldPreferences());
try {
new BibEntryWriter(fieldWriter, entryTypesManager).write(entry, bibWriter, selectedDb.getValue().getMode(), reformat);
} catch (IOException ioException) {
return "";
}
Expand Down