|
4 | 4 | import java.io.IOException;
|
5 | 5 | import java.io.StringReader;
|
6 | 6 | import java.lang.reflect.InvocationTargetException;
|
7 |
| -import java.nio.charset.Charset; |
8 |
| -import java.nio.charset.UnsupportedCharsetException; |
9 | 7 | import java.nio.file.Path;
|
10 | 8 | import java.util.ArrayList;
|
11 | 9 | import java.util.Collections;
|
|
17 | 15 | import java.util.Set;
|
18 | 16 | import java.util.stream.Collectors;
|
19 | 17 |
|
20 |
| -import javax.swing.JOptionPane; |
21 |
| -import javax.swing.JTextArea; |
22 | 18 | import javax.swing.SwingUtilities;
|
23 | 19 | import javax.swing.undo.CannotRedoException;
|
24 | 20 | import javax.swing.undo.CannotUndoException;
|
|
73 | 69 | import org.jabref.gui.undo.UndoableChangeType;
|
74 | 70 | import org.jabref.gui.undo.UndoableFieldChange;
|
75 | 71 | import org.jabref.gui.undo.UndoableInsertEntry;
|
76 |
| -import org.jabref.gui.undo.UndoableKeyChange; |
77 | 72 | import org.jabref.gui.undo.UndoableRemoveEntry;
|
78 | 73 | import org.jabref.gui.util.DefaultTaskExecutor;
|
79 |
| -import org.jabref.gui.util.FileDialogConfiguration; |
80 | 74 | import org.jabref.gui.worker.CitationStyleToClipboardWorker;
|
81 | 75 | import org.jabref.gui.worker.SendAsEMailAction;
|
82 |
| -import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator; |
83 | 76 | import org.jabref.logic.citationstyle.CitationStyleCache;
|
84 | 77 | import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
|
85 |
| -import org.jabref.logic.exporter.BibtexDatabaseWriter; |
86 |
| -import org.jabref.logic.exporter.FileSaveSession; |
87 |
| -import org.jabref.logic.exporter.SaveException; |
88 |
| -import org.jabref.logic.exporter.SavePreferences; |
89 |
| -import org.jabref.logic.exporter.SaveSession; |
90 |
| -import org.jabref.logic.l10n.Encodings; |
91 | 78 | import org.jabref.logic.l10n.Localization;
|
92 | 79 | import org.jabref.logic.layout.Layout;
|
93 | 80 | import org.jabref.logic.layout.LayoutHelper;
|
94 | 81 | import org.jabref.logic.pdf.FileAnnotationCache;
|
95 | 82 | import org.jabref.logic.search.SearchQuery;
|
96 |
| -import org.jabref.logic.util.StandardFileType; |
97 | 83 | import org.jabref.logic.util.UpdateField;
|
98 | 84 | import org.jabref.logic.util.io.FileFinder;
|
99 | 85 | import org.jabref.logic.util.io.FileFinders;
|
|
116 | 102 | import org.jabref.model.entry.event.EntryEventSource;
|
117 | 103 | import org.jabref.model.entry.specialfields.SpecialField;
|
118 | 104 | import org.jabref.model.entry.specialfields.SpecialFieldValue;
|
119 |
| -import org.jabref.model.strings.StringUtil; |
120 | 105 | import org.jabref.preferences.JabRefPreferences;
|
121 | 106 | import org.jabref.preferences.PreviewPreferences;
|
122 | 107 |
|
123 | 108 | import com.google.common.eventbus.Subscribe;
|
124 |
| -import com.jgoodies.forms.builder.FormBuilder; |
125 |
| -import com.jgoodies.forms.layout.FormLayout; |
126 | 109 | import org.fxmisc.easybind.EasyBind;
|
127 | 110 | import org.fxmisc.easybind.Subscription;
|
128 | 111 | import org.slf4j.Logger;
|
@@ -298,11 +281,11 @@ private void setupActions() {
|
298 | 281 | actions.put(Actions.EDIT, this::showAndEdit);
|
299 | 282 |
|
300 | 283 | // The action for saving a database.
|
301 |
| - actions.put(Actions.SAVE, saveAction); |
| 284 | + actions.put(Actions.SAVE, saveAction::save); |
302 | 285 |
|
303 | 286 | actions.put(Actions.SAVE_AS, saveAction::saveAs);
|
304 | 287 |
|
305 |
| - actions.put(Actions.SAVE_SELECTED_AS_PLAIN, new SaveSelectedAction(SavePreferences.DatabaseSaveType.PLAIN_BIBTEX)); |
| 288 | + actions.put(Actions.SAVE_SELECTED_AS_PLAIN, saveAction::saveSelectedAsPlain); |
306 | 289 |
|
307 | 290 | // The action for copying selected entries.
|
308 | 291 | actions.put(Actions.COPY, this::copy);
|
@@ -670,87 +653,9 @@ public void runCommand(final Actions command) {
|
670 | 653 | }
|
671 | 654 | }
|
672 | 655 |
|
673 |
| - /** |
674 |
| - * FIXME: high code duplication with {@link SaveDatabaseAction#saveDatabase(File, boolean, Charset)} |
675 |
| - */ |
676 |
| - private boolean saveDatabase(File file, boolean selectedOnly, Charset encoding, |
677 |
| - SavePreferences.DatabaseSaveType saveType) |
678 |
| - throws SaveException { |
679 |
| - SaveSession session; |
680 |
| - final String SAVE_DATABASE = Localization.lang("Save library"); |
681 |
| - try { |
682 |
| - SavePreferences prefs = Globals.prefs.loadForSaveFromPreferences() |
683 |
| - .withEncoding(encoding) |
684 |
| - .withSaveType(saveType); |
685 |
| - |
686 |
| - BibtexDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>( |
687 |
| - FileSaveSession::new); |
688 |
| - if (selectedOnly) { |
689 |
| - session = databaseWriter.savePartOfDatabase(bibDatabaseContext, mainTable.getSelectedEntries(), prefs); |
690 |
| - } else { |
691 |
| - session = databaseWriter.saveDatabase(bibDatabaseContext, prefs); |
692 |
| - } |
693 |
| - |
694 |
| - registerUndoableChanges(session); |
695 |
| - } |
696 |
| - // FIXME: not sure if this is really thrown anywhere |
697 |
| - catch (UnsupportedCharsetException ex) { |
698 |
| - frame.getDialogService().showErrorDialogAndWait(Localization.lang("Save library"), Localization.lang("Could not save file.") |
699 |
| - + Localization.lang("Character encoding '%0' is not supported.", encoding.displayName())); |
700 |
| - throw new SaveException("rt"); |
701 |
| - } catch (SaveException ex) { |
702 |
| - if (ex.specificEntry()) { |
703 |
| - // Error occurred during processing of the entry. Highlight it: |
704 |
| - clearAndSelect(ex.getEntry()); |
705 |
| - showAndEdit(ex.getEntry()); |
706 |
| - } else { |
707 |
| - LOGGER.warn("Could not save", ex); |
708 |
| - } |
709 |
| - |
710 |
| - dialogService.showErrorDialogAndWait(SAVE_DATABASE, Localization.lang("Could not save file."), ex); |
711 |
| - throw new SaveException("rt"); |
712 |
| - } |
713 |
| - |
714 |
| - boolean commit = true; |
715 |
| - if (!session.getWriter().couldEncodeAll()) { |
716 |
| - FormBuilder builder = FormBuilder.create() |
717 |
| - .layout(new FormLayout("left:pref, 4dlu, fill:pref", "pref, 4dlu, pref")); |
718 |
| - JTextArea ta = new JTextArea(session.getWriter().getProblemCharacters()); |
719 |
| - ta.setEditable(false); |
720 |
| - builder.add(Localization.lang("The chosen encoding '%0' could not encode the following characters:", session.getEncoding().displayName())).xy(1, 1); |
721 |
| - builder.add(ta).xy(3, 1); |
722 |
| - builder.add(Localization.lang("What do you want to do?")).xy(1, 3); |
723 |
| - String tryDiff = Localization.lang("Try different encoding"); |
724 |
| - int answer = JOptionPane.showOptionDialog(null, builder.getPanel(), SAVE_DATABASE, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {Localization.lang("Save"), tryDiff, Localization.lang("Cancel")}, tryDiff); |
725 |
| - |
726 |
| - if (answer == JOptionPane.NO_OPTION) { |
727 |
| - |
728 |
| - // The user wants to use another encoding. |
729 |
| - Object choice = JOptionPane.showInputDialog(null, Localization.lang("Select encoding"), SAVE_DATABASE, JOptionPane.QUESTION_MESSAGE, null, Encodings.ENCODINGS_DISPLAYNAMES, encoding); |
730 |
| - if (choice == null) { |
731 |
| - commit = false; |
732 |
| - } else { |
733 |
| - Charset newEncoding = Charset.forName((String) choice); |
734 |
| - return saveDatabase(file, selectedOnly, newEncoding, saveType); |
735 |
| - } |
736 |
| - } else if (answer == JOptionPane.CANCEL_OPTION) { |
737 |
| - commit = false; |
738 |
| - } |
739 |
| - } |
740 |
| - |
741 |
| - if (commit) { |
742 |
| - session.commit(file.toPath()); |
743 |
| - this.bibDatabaseContext.getMetaData().setEncoding(encoding); // Make sure to remember which encoding we used. |
744 |
| - } else { |
745 |
| - session.cancel(); |
746 |
| - } |
747 |
| - |
748 |
| - return commit; |
749 |
| - } |
750 |
| - |
751 |
| - public void registerUndoableChanges(SaveSession session) { |
| 656 | + public void registerUndoableChanges(List<FieldChange> changes) { |
752 | 657 | NamedCompound ce = new NamedCompound(Localization.lang("Save actions"));
|
753 |
| - for (FieldChange change : session.getFieldChanges()) { |
| 658 | + for (FieldChange change : changes) { |
754 | 659 | ce.addEdit(new UndoableFieldChange(change));
|
755 | 660 | }
|
756 | 661 | ce.end();
|
@@ -1207,30 +1112,6 @@ public boolean showDeleteConfirmationDialog(int numberOfEntries) {
|
1207 | 1112 | }
|
1208 | 1113 | }
|
1209 | 1114 |
|
1210 |
| - /** |
1211 |
| - * If the relevant option is set, autogenerate keys for all entries that are lacking keys. |
1212 |
| - */ |
1213 |
| - public void autoGenerateKeysBeforeSaving() { |
1214 |
| - if (Globals.prefs.getBoolean(JabRefPreferences.GENERATE_KEYS_BEFORE_SAVING)) { |
1215 |
| - NamedCompound ce = new NamedCompound(Localization.lang("Autogenerate BibTeX keys")); |
1216 |
| - |
1217 |
| - BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(bibDatabaseContext, Globals.prefs.getBibtexKeyPatternPreferences()); |
1218 |
| - for (BibEntry bes : bibDatabaseContext.getDatabase().getEntries()) { |
1219 |
| - Optional<String> oldKey = bes.getCiteKeyOptional(); |
1220 |
| - if (StringUtil.isBlank(oldKey)) { |
1221 |
| - Optional<FieldChange> change = keyGenerator.generateAndSetKey(bes); |
1222 |
| - change.ifPresent(fieldChange -> ce.addEdit(new UndoableKeyChange(fieldChange))); |
1223 |
| - } |
1224 |
| - } |
1225 |
| - |
1226 |
| - // Store undo information, if any: |
1227 |
| - if (ce.hasEdits()) { |
1228 |
| - ce.end(); |
1229 |
| - getUndoManager().addEdit(ce); |
1230 |
| - } |
1231 |
| - } |
1232 |
| - } |
1233 |
| - |
1234 | 1115 | /**
|
1235 | 1116 | * Depending on whether a preview or an entry editor is showing, save the current divider location in the correct preference setting.
|
1236 | 1117 | */
|
@@ -1597,30 +1478,4 @@ public void action() {
|
1597 | 1478 | preview.print();
|
1598 | 1479 | }
|
1599 | 1480 | }
|
1600 |
| - |
1601 |
| - private class SaveSelectedAction implements BaseAction { |
1602 |
| - |
1603 |
| - private final SavePreferences.DatabaseSaveType saveType; |
1604 |
| - |
1605 |
| - public SaveSelectedAction(SavePreferences.DatabaseSaveType saveType) { |
1606 |
| - this.saveType = saveType; |
1607 |
| - } |
1608 |
| - |
1609 |
| - @Override |
1610 |
| - public void action() throws SaveException { |
1611 |
| - FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() |
1612 |
| - .withDefaultExtension(StandardFileType.BIBTEX_DB) |
1613 |
| - .addExtensionFilter(String.format("%1s %2s", "BibTex", Localization.lang("Library")), StandardFileType.BIBTEX_DB) |
1614 |
| - .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)) |
1615 |
| - .build(); |
1616 |
| - |
1617 |
| - Optional<Path> chosenFile = dialogService.showFileSaveDialog(fileDialogConfiguration); |
1618 |
| - if (chosenFile.isPresent()) { |
1619 |
| - Path path = chosenFile.get(); |
1620 |
| - saveDatabase(path.toFile(), true, Globals.prefs.getDefaultEncoding(), saveType); |
1621 |
| - frame.getFileHistory().newFile(path.toString()); |
1622 |
| - frame.output(Localization.lang("Saved selected to '%0'.", path.toString())); |
1623 |
| - } |
1624 |
| - } |
1625 |
| - } |
1626 | 1481 | }
|
0 commit comments