Skip to content

Commit 9ed570f

Browse files
subhramitkoppor
authored andcommitted
Reformat codebase (more carefully) (JabRef#13885)
* Fix non record comments by carl # Conflicts: # jabgui/src/main/java/org/jabref/gui/edit/automaticfiededitor/MoveFieldValueAction.java # jabgui/src/main/java/org/jabref/gui/mergeentries/threewaymerge/cell/sidebuttons/ToggleMergeUnmergeButton.java # jabgui/src/main/java/org/jabref/gui/mergeentries/threewaymerge/fieldsmerger/CommentMerger.java # jabgui/src/main/java/org/jabref/gui/mergeentries/threewaymerge/fieldsmerger/FieldMerger.java # jabgui/src/main/java/org/jabref/gui/mergeentries/threewaymerge/fieldsmerger/FileMerger.java # jabgui/src/main/java/org/jabref/gui/mergeentries/threewaymerge/fieldsmerger/GroupMerger.java # jabgui/src/main/java/org/jabref/gui/mergeentries/threewaymerge/fieldsmerger/KeywordMerger.java # jablib/src/main/java/org/jabref/logic/layout/format/HTMLChars.java # jablib/src/main/java/org/jabref/model/entry/identifier/ArXivIdentifier.java # jablib/src/main/java/org/jabref/model/entry/identifier/EprintIdentifier.java * Add file exceptions * Remove shebang line * Remove shebang line * Remove shebang line * Expand variables & rename class --------- Co-authored-by: Oliver Kopp <[email protected]>
1 parent 7276eb0 commit 9ed570f

File tree

2 files changed

+216
-16
lines changed

2 files changed

+216
-16
lines changed

jabgui/src/main/java/org/jabref/gui/cleanup/CleanupAction.java

Lines changed: 131 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
package org.jabref.gui.cleanup;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Optional;
36
import java.util.function.Supplier;
7+
import java.util.stream.Collectors;
48

59
import javax.swing.undo.UndoManager;
610

11+
import javafx.application.Platform;
12+
713
import org.jabref.gui.DialogService;
814
import org.jabref.gui.LibraryTab;
915
import org.jabref.gui.StateManager;
1016
import org.jabref.gui.actions.ActionHelper;
1117
import org.jabref.gui.actions.SimpleCommand;
18+
import org.jabref.gui.undo.NamedCompoundEdit;
19+
import org.jabref.gui.undo.UndoableFieldChange;
20+
import org.jabref.logic.JabRefException;
21+
import org.jabref.logic.cleanup.CleanupPreferences;
22+
import org.jabref.logic.cleanup.CleanupWorker;
23+
import org.jabref.logic.l10n.Localization;
1224
import org.jabref.logic.preferences.CliPreferences;
25+
import org.jabref.logic.util.BackgroundTask;
1326
import org.jabref.logic.util.TaskExecutor;
27+
import org.jabref.model.FieldChange;
28+
import org.jabref.model.database.BibDatabaseContext;
29+
import org.jabref.model.entry.BibEntry;
1430

1531
public class CleanupAction extends SimpleCommand {
1632

@@ -20,6 +36,10 @@ public class CleanupAction extends SimpleCommand {
2036
private final StateManager stateManager;
2137
private final TaskExecutor taskExecutor;
2238
private final UndoManager undoManager;
39+
private final List<JabRefException> failures;
40+
41+
private boolean isCanceled;
42+
private int modifiedEntriesCount;
2343

2444
public CleanupAction(Supplier<LibraryTab> tabSupplier,
2545
CliPreferences preferences,
@@ -33,6 +53,7 @@ public CleanupAction(Supplier<LibraryTab> tabSupplier,
3353
this.stateManager = stateManager;
3454
this.taskExecutor = taskExecutor;
3555
this.undoManager = undoManager;
56+
this.failures = new ArrayList<>();
3657

3758
this.executable.bind(ActionHelper.needsEntriesSelected(stateManager));
3859
}
@@ -43,16 +64,119 @@ public void execute() {
4364
return;
4465
}
4566

67+
if (stateManager.getSelectedEntries().isEmpty()) { // None selected. Inform the user to select entries first.
68+
dialogService.showInformationDialogAndWait(Localization.lang("Cleanup entry"), Localization.lang("First select entries to clean up."));
69+
return;
70+
}
71+
72+
isCanceled = false;
73+
modifiedEntriesCount = 0;
74+
4675
CleanupDialog cleanupDialog = new CleanupDialog(
47-
tabSupplier,
4876
stateManager.getActiveDatabase().get(),
49-
preferences,
50-
dialogService,
51-
stateManager,
52-
taskExecutor,
53-
undoManager
77+
preferences.getCleanupPreferences(),
78+
preferences.getFilePreferences()
79+
);
80+
81+
Optional<CleanupPreferences> chosenPreset = dialogService.showCustomDialogAndWait(cleanupDialog);
82+
83+
chosenPreset.ifPresent(preset -> {
84+
if (preset.isActive(CleanupPreferences.CleanupStep.RENAME_PDF) && preferences.getAutoLinkPreferences().shouldAskAutoNamingPdfs()) {
85+
boolean confirmed = dialogService.showConfirmationDialogWithOptOutAndWait(Localization.lang("Autogenerate PDF Names"),
86+
Localization.lang("Auto-generating PDF-Names does not support undo. Continue?"),
87+
Localization.lang("Autogenerate PDF Names"),
88+
Localization.lang("Cancel"),
89+
Localization.lang("Do not ask again"),
90+
optOut -> preferences.getAutoLinkPreferences().setAskAutoNamingPdfs(!optOut));
91+
if (!confirmed) {
92+
isCanceled = true;
93+
return;
94+
}
95+
}
96+
97+
preferences.getCleanupPreferences().setActiveJobs(preset.getActiveJobs());
98+
preferences.getCleanupPreferences().setFieldFormatterCleanups(preset.getFieldFormatterCleanups());
99+
100+
BackgroundTask.wrap(() -> cleanup(stateManager.getActiveDatabase().get(), preset))
101+
.onSuccess(result -> showResults())
102+
.onFailure(dialogService::showErrorDialogAndWait)
103+
.executeWith(taskExecutor);
104+
});
105+
}
106+
107+
/**
108+
* Runs the cleanup on the entry and records the change.
109+
*
110+
* @return true iff entry was modified
111+
*/
112+
private boolean doCleanup(BibDatabaseContext databaseContext, CleanupPreferences preset, BibEntry entry, NamedCompoundEdit compoundEdit) {
113+
// Create and run cleaner
114+
CleanupWorker cleaner = new CleanupWorker(
115+
databaseContext,
116+
preferences.getFilePreferences(),
117+
preferences.getTimestampPreferences()
54118
);
55119

56-
dialogService.showCustomDialogAndWait(cleanupDialog);
120+
List<FieldChange> changes = cleaner.cleanup(preset, entry);
121+
122+
// Register undo action
123+
for (FieldChange change : changes) {
124+
compoundEdit.addEdit(new UndoableFieldChange(change));
125+
}
126+
127+
failures.addAll(cleaner.getFailures());
128+
129+
return !changes.isEmpty();
130+
}
131+
132+
private void showResults() {
133+
if (isCanceled) {
134+
return;
135+
}
136+
137+
if (modifiedEntriesCount > 0) {
138+
tabSupplier.get().markBaseChanged();
139+
}
140+
141+
if (modifiedEntriesCount == 0) {
142+
dialogService.notify(Localization.lang("No entry needed a clean up"));
143+
} else if (modifiedEntriesCount == 1) {
144+
dialogService.notify(Localization.lang("One entry needed a clean up"));
145+
} else {
146+
dialogService.notify(Localization.lang("%0 entries needed a clean up", Integer.toString(modifiedEntriesCount)));
147+
}
148+
}
149+
150+
private void cleanup(BibDatabaseContext databaseContext, CleanupPreferences cleanupPreferences) {
151+
this.failures.clear();
152+
153+
// undo granularity is on set of all entries
154+
NamedCompoundEdit compoundEdit = new NamedCompoundEdit(Localization.lang("Clean up entries"));
155+
156+
for (BibEntry entry : List.copyOf(stateManager.getSelectedEntries())) {
157+
if (doCleanup(databaseContext, cleanupPreferences, entry, compoundEdit)) {
158+
modifiedEntriesCount++;
159+
}
160+
}
161+
162+
compoundEdit.end();
163+
164+
if (compoundEdit.hasEdits()) {
165+
undoManager.addEdit(compoundEdit);
166+
}
167+
168+
if (!failures.isEmpty()) {
169+
showFailures(failures);
170+
}
171+
}
172+
173+
private void showFailures(List<JabRefException> failures) {
174+
String message = failures.stream()
175+
.map(exception -> "- " + exception.getLocalizedMessage())
176+
.collect(Collectors.joining("\n"));
177+
178+
Platform.runLater(() ->
179+
dialogService.showErrorDialogAndWait(Localization.lang("File Move Errors"), message)
180+
);
57181
}
58182
}
Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
package org.jabref.gui.cleanup;
22

3+
import java.util.List;
4+
import java.util.Optional;
5+
36
import javax.swing.undo.UndoManager;
47

8+
import javafx.application.Platform;
9+
510
import org.jabref.gui.DialogService;
611
import org.jabref.gui.StateManager;
712
import org.jabref.gui.actions.ActionHelper;
813
import org.jabref.gui.actions.SimpleCommand;
14+
import org.jabref.gui.undo.NamedCompoundEdit;
15+
import org.jabref.gui.undo.UndoableFieldChange;
16+
import org.jabref.logic.JabRefException;
17+
import org.jabref.logic.cleanup.CleanupPreferences;
18+
import org.jabref.logic.cleanup.CleanupWorker;
19+
import org.jabref.logic.l10n.Localization;
920
import org.jabref.logic.preferences.CliPreferences;
21+
import org.jabref.model.FieldChange;
22+
import org.jabref.model.database.BibDatabaseContext;
1023
import org.jabref.model.entry.BibEntry;
1124

1225
public class CleanupSingleAction extends SimpleCommand {
@@ -17,6 +30,9 @@ public class CleanupSingleAction extends SimpleCommand {
1730
private final BibEntry entry;
1831
private final UndoManager undoManager;
1932

33+
private boolean isCanceled;
34+
private int modifiedEntriesCount;
35+
2036
public CleanupSingleAction(BibEntry entry, CliPreferences preferences, DialogService dialogService, StateManager stateManager, UndoManager undoManager) {
2137
this.entry = entry;
2238
this.preferences = preferences;
@@ -29,19 +45,79 @@ public CleanupSingleAction(BibEntry entry, CliPreferences preferences, DialogSer
2945

3046
@Override
3147
public void execute() {
32-
if (stateManager.getActiveDatabase().isEmpty()) {
33-
return;
34-
}
48+
isCanceled = false;
3549

3650
CleanupDialog cleanupDialog = new CleanupDialog(
37-
entry,
3851
stateManager.getActiveDatabase().get(),
39-
preferences,
40-
dialogService,
41-
stateManager,
42-
undoManager
52+
preferences.getCleanupPreferences(),
53+
preferences.getFilePreferences()
54+
);
55+
56+
Optional<CleanupPreferences> chosenPreset = dialogService.showCustomDialogAndWait(cleanupDialog);
57+
58+
chosenPreset.ifPresent(preset -> {
59+
if (preset.isActive(CleanupPreferences.CleanupStep.RENAME_PDF) && preferences.getAutoLinkPreferences().shouldAskAutoNamingPdfs()) {
60+
boolean confirmed = dialogService.showConfirmationDialogWithOptOutAndWait(Localization.lang("Autogenerate PDF Names"),
61+
Localization.lang("Auto-generating PDF-Names does not support undo. Continue?"),
62+
Localization.lang("Autogenerate PDF Names"),
63+
Localization.lang("Cancel"),
64+
Localization.lang("Do not ask again"),
65+
optOut -> preferences.getAutoLinkPreferences().setAskAutoNamingPdfs(!optOut));
66+
if (!confirmed) {
67+
isCanceled = true;
68+
return;
69+
}
70+
}
71+
72+
preferences.getCleanupPreferences().setActiveJobs(preset.getActiveJobs());
73+
preferences.getCleanupPreferences().setFieldFormatterCleanups(preset.getFieldFormatterCleanups());
74+
75+
cleanup(stateManager.getActiveDatabase().get(), preset);
76+
});
77+
}
78+
79+
/**
80+
* Runs the cleanup on the entry and records the change.
81+
*/
82+
private void doCleanup(BibDatabaseContext databaseContext, CleanupPreferences preset, BibEntry entry, NamedCompoundEdit compoundEdit) {
83+
// Create and run cleaner
84+
CleanupWorker cleaner = new CleanupWorker(
85+
databaseContext,
86+
preferences.getFilePreferences(),
87+
preferences.getTimestampPreferences()
4388
);
4489

45-
dialogService.showCustomDialogAndWait(cleanupDialog);
90+
List<FieldChange> changes = cleaner.cleanup(preset, entry);
91+
92+
// Register undo action
93+
for (FieldChange change : changes) {
94+
compoundEdit.addEdit(new UndoableFieldChange(change));
95+
}
96+
97+
if (!cleaner.getFailures().isEmpty()) {
98+
this.showFailures(cleaner.getFailures());
99+
}
100+
}
101+
102+
private void cleanup(BibDatabaseContext databaseContext, CleanupPreferences cleanupPreferences) {
103+
// undo granularity is on entry level
104+
NamedCompoundEdit compoundEdit = new NamedCompoundEdit(Localization.lang("Cleanup entry"));
105+
106+
doCleanup(databaseContext, cleanupPreferences, entry, compoundEdit);
107+
108+
compoundEdit.end();
109+
if (compoundEdit.hasEdits()) {
110+
undoManager.addEdit(compoundEdit);
111+
}
112+
}
113+
114+
private void showFailures(List<JabRefException> failures) {
115+
StringBuilder sb = new StringBuilder();
116+
for (JabRefException exception : failures) {
117+
sb.append("- ").append(exception.getLocalizedMessage()).append("\n");
118+
}
119+
Platform.runLater(() ->
120+
dialogService.showErrorDialogAndWait(Localization.lang("File Move Errors"), sb.toString())
121+
);
46122
}
47123
}

0 commit comments

Comments
 (0)