|
18 | 18 | import org.jabref.gui.StateManager; |
19 | 19 | import org.jabref.gui.duplicationFinder.DuplicateResolverDialog; |
20 | 20 | import org.jabref.gui.fieldeditors.LinkedFileViewModel; |
| 21 | +import org.jabref.gui.libraryproperties.constants.ConstantsItemModel; |
21 | 22 | import org.jabref.gui.undo.UndoableInsertEntries; |
22 | 23 | import org.jabref.gui.util.BackgroundTask; |
23 | 24 | import org.jabref.gui.util.DefaultTaskExecutor; |
|
40 | 41 | import org.jabref.logic.util.io.FileUtil; |
41 | 42 | import org.jabref.model.FieldChange; |
42 | 43 | import org.jabref.model.database.BibDatabaseContext; |
| 44 | +import org.jabref.model.database.KeyCollisionException; |
43 | 45 | import org.jabref.model.entry.BibEntry; |
| 46 | +import org.jabref.model.entry.BibtexString; |
44 | 47 | import org.jabref.model.entry.LinkedFile; |
45 | 48 | import org.jabref.model.entry.field.StandardField; |
46 | 49 | import org.jabref.model.entry.identifier.ArXivIdentifier; |
@@ -311,13 +314,36 @@ private void generateKeys(List<BibEntry> entries) { |
311 | 314 | public List<BibEntry> handleBibTeXData(String entries) { |
312 | 315 | BibtexParser parser = new BibtexParser(preferencesService.getImportFormatPreferences(), fileUpdateMonitor); |
313 | 316 | 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 | + Collection<BibtexString> stringConstants = parser.getStringValues(); |
| 319 | + importStringConstantsWithDuplicateCheck(stringConstants); |
| 320 | + return result; |
315 | 321 | } catch (ParseException ex) { |
316 | 322 | LOGGER.error("Could not paste", ex); |
317 | 323 | return Collections.emptyList(); |
318 | 324 | } |
319 | 325 | } |
320 | 326 |
|
| 327 | + public void importStringConstantsWithDuplicateCheck(Collection<BibtexString> stringConstants) { |
| 328 | + List<String> failures = new ArrayList<>(); |
| 329 | + |
| 330 | + for (BibtexString stringConstantToAdd : stringConstants) { |
| 331 | + try { |
| 332 | + ConstantsItemModel checker = new ConstantsItemModel(stringConstantToAdd.getName(), stringConstantToAdd.getContent()); |
| 333 | + if (checker.combinedValidationValidProperty().get()) { |
| 334 | + bibDatabaseContext.getDatabase().addString(stringConstantToAdd); |
| 335 | + } else { |
| 336 | + failures.add(Localization.lang("String constant \"%0\" was not imported because it is not a valid string constant", stringConstantToAdd.getName())); |
| 337 | + } |
| 338 | + } catch (KeyCollisionException ex) { |
| 339 | + failures.add(Localization.lang("String constant %0 was not imported because it already exists in this library", stringConstantToAdd.getName())); |
| 340 | + } |
| 341 | + } |
| 342 | + if (!failures.isEmpty()) { |
| 343 | + dialogService.showWarningDialogAndWait(Localization.lang("Importing String constants"), Localization.lang("Could not import the following string constants:\n %0", String.join("\n", failures))); |
| 344 | + } |
| 345 | + } |
| 346 | + |
321 | 347 | public List<BibEntry> handleStringData(String data) throws FetcherException { |
322 | 348 | if ((data == null) || data.isEmpty()) { |
323 | 349 | return Collections.emptyList(); |
@@ -356,7 +382,7 @@ private List<BibEntry> tryImportFormats(String data) { |
356 | 382 | } |
357 | 383 |
|
358 | 384 | private List<BibEntry> fetchByDOI(DOI doi) throws FetcherException { |
359 | | - LOGGER.info("Found DOI identifer in clipboard"); |
| 385 | + LOGGER.info("Found DOI identifier in clipboard"); |
360 | 386 | Optional<BibEntry> entry = new DoiFetcher(preferencesService.getImportFormatPreferences()).performSearchById(doi.getDOI()); |
361 | 387 | return OptionalUtil.toList(entry); |
362 | 388 | } |
|
0 commit comments