|
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,31 @@ 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 | + List<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(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 | + |
321 | 342 | public List<BibEntry> handleStringData(String data) throws FetcherException { |
322 | 343 | if ((data == null) || data.isEmpty()) { |
323 | 344 | return Collections.emptyList(); |
|
0 commit comments