Skip to content

Commit e49562f

Browse files
Merge pull request #19 from DD2480-Group1/paste-function-revert
2 parents 404e781 + dd821b9 commit e49562f

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/main/java/org/jabref/gui/externalfiles/ImportHandler.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.jabref.gui.StateManager;
1919
import org.jabref.gui.duplicationFinder.DuplicateResolverDialog;
2020
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
21+
import org.jabref.gui.libraryproperties.constants.ConstantsItemModel;
2122
import org.jabref.gui.undo.UndoableInsertEntries;
2223
import org.jabref.gui.util.BackgroundTask;
2324
import org.jabref.gui.util.DefaultTaskExecutor;
@@ -40,7 +41,9 @@
4041
import org.jabref.logic.util.io.FileUtil;
4142
import org.jabref.model.FieldChange;
4243
import org.jabref.model.database.BibDatabaseContext;
44+
import org.jabref.model.database.KeyCollisionException;
4345
import org.jabref.model.entry.BibEntry;
46+
import org.jabref.model.entry.BibtexString;
4447
import org.jabref.model.entry.LinkedFile;
4548
import org.jabref.model.entry.field.StandardField;
4649
import org.jabref.model.entry.identifier.ArXivIdentifier;
@@ -311,13 +314,31 @@ private void generateKeys(List<BibEntry> entries) {
311314
public List<BibEntry> handleBibTeXData(String entries) {
312315
BibtexParser parser = new BibtexParser(preferencesService.getImportFormatPreferences(), fileUpdateMonitor);
313316
try {
314-
return parser.parseEntries(new ByteArrayInputStream(entries.getBytes(StandardCharsets.UTF_8)));
317+
List<BibEntry> result = parser.parseEntries(new ByteArrayInputStream(entries.getBytes(StandardCharsets.UTF_8)));
318+
List<BibtexString> stringConstants = parser.getStringValues();
319+
importStringConstantsWithDuplicateCheck(stringConstants);
320+
return result;
315321
} catch (ParseException ex) {
316322
LOGGER.error("Could not paste", ex);
317323
return Collections.emptyList();
318324
}
319325
}
320326

327+
public void importStringConstantsWithDuplicateCheck(List<BibtexString> stringConstants) {
328+
for(BibtexString stringConstantToAdd : stringConstants) {
329+
try {
330+
ConstantsItemModel checker = new ConstantsItemModel(stringConstantToAdd.getName(), stringConstantToAdd.getContent());
331+
if(checker.combinedValidationValidProperty().get()) {
332+
bibDatabaseContext.getDatabase().addString(stringConstantToAdd);
333+
} else {
334+
dialogService.showErrorDialogAndWait(Localization.lang("Pasted string constant \"%0\" was not added because it is not a valid string constant", stringConstantToAdd.getName()));
335+
}
336+
} catch (KeyCollisionException ex) {
337+
dialogService.showErrorDialogAndWait(Localization.lang("Pasted string constant %0 was not imported because it already exists in this library", stringConstantToAdd.getName()));
338+
}
339+
}
340+
}
341+
321342
public List<BibEntry> handleStringData(String data) throws FetcherException {
322343
if ((data == null) || data.isEmpty()) {
323344
return Collections.emptyList();

src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public List<BibEntry> parseEntries(InputStream inputStream) throws ParseExceptio
117117
}
118118
}
119119

120+
public List<BibtexString> getStringValues() {
121+
return database.getStringValues().stream().toList();
122+
}
123+
120124
public Optional<BibEntry> parseSingleEntry(String bibtexString) throws ParseException {
121125
return parseEntries(bibtexString).stream().findFirst();
122126
}

src/main/resources/l10n/JabRef_en.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,3 +2643,7 @@ Source\ URL=Source URL
26432643
Redownload\ file=Redownload file
26442644
Redownload\ missing\ files=Redownload missing files
26452645
Redownload\ missing\ files\ for\ current\ library?=Redownload missing files for current library?
2646+
2647+
Pasted\ string\ constant\ "%0"\ was\ not\ added\ because\ it\ is\ not\ a\ valid\ string\ constant=Pasted string constant "%0" was not added because it is not a valid string constant
2648+
Pasted\ string\ constant\ %0\ was\ not\ imported\ because\ it\ already\ exists\ in\ this\ library=Pasted string constant %0 was not imported because it already exists in this library
2649+

0 commit comments

Comments
 (0)