|
1 | 1 | package org.jabref.gui.mergeentries;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
3 | 4 | import java.util.List;
|
4 | 5 | import java.util.Optional;
|
5 | 6 | import java.util.concurrent.atomic.AtomicInteger;
|
6 |
| -import java.util.stream.Collectors; |
7 | 7 |
|
8 | 8 | import javax.swing.undo.UndoManager;
|
9 | 9 |
|
@@ -48,26 +48,56 @@ private void configureTask() {
|
48 | 48 | @Override
|
49 | 49 | public List<String> call() throws Exception {
|
50 | 50 | try {
|
| 51 | + if (isCancelled()) { |
| 52 | + notifyCancellation(); |
| 53 | + return List.of(); |
| 54 | + } |
| 55 | + |
51 | 56 | List<String> updatedEntries = processMergeEntries();
|
| 57 | + |
| 58 | + if (isCancelled()) { |
| 59 | + notifyCancellation(); |
| 60 | + finalizeOperation(updatedEntries); |
| 61 | + return updatedEntries; |
| 62 | + } |
| 63 | + |
52 | 64 | finalizeOperation(updatedEntries);
|
53 | 65 | LOGGER.debug("Merge operation completed. Processed: {}, Successfully updated: {}",
|
54 | 66 | processedEntries.get(), successfulUpdates.get());
|
55 | 67 | notifySuccess(successfulUpdates.get());
|
56 | 68 | return updatedEntries;
|
57 | 69 | } catch (Exception e) {
|
| 70 | + if (isCancelled()) { |
| 71 | + notifyCancellation(); |
| 72 | + return List.of(); |
| 73 | + } |
58 | 74 | LOGGER.error("Critical error during merge operation", e);
|
59 | 75 | notifyError(e);
|
60 | 76 | throw e;
|
61 | 77 | }
|
62 | 78 | }
|
63 | 79 |
|
| 80 | + private void notifyCancellation() { |
| 81 | + LOGGER.debug("Merge operation was cancelled. Processed: {}, Successfully updated: {}", |
| 82 | + processedEntries.get(), successfulUpdates.get()); |
| 83 | + context.notificationService().notify( |
| 84 | + Localization.lang("Merge operation cancelled after updating %0 entry(s)", successfulUpdates.get())); |
| 85 | + } |
| 86 | + |
64 | 87 | private List<String> processMergeEntries() {
|
65 |
| - return context.entries().stream() |
66 |
| - .takeWhile(_ -> !isCancelled()) |
67 |
| - .map(this::processSingleEntryWithProgress) |
68 |
| - .filter(Optional::isPresent) |
69 |
| - .map(Optional::get) |
70 |
| - .collect(Collectors.toList()); |
| 88 | + List<String> updatedEntries = new ArrayList<>(); |
| 89 | + |
| 90 | + for (BibEntry entry : context.entries()) { |
| 91 | + Optional<String> result = processSingleEntryWithProgress(entry); |
| 92 | + result.ifPresent(updatedEntries::add); |
| 93 | + |
| 94 | + if (isCancelled()) { |
| 95 | + LOGGER.debug("Cancellation requested after processing entry {}", processedEntries.get()); |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return updatedEntries; |
71 | 101 | }
|
72 | 102 |
|
73 | 103 | private Optional<String> processSingleEntryWithProgress(BibEntry entry) {
|
|
0 commit comments