Search in sources :

Example 6 with SaveException

use of org.jabref.logic.exporter.SaveException in project jabref by JabRef.

the class BasePanel method saveDatabase.

private boolean saveDatabase(File file, boolean selectedOnly, Charset enc, SavePreferences.DatabaseSaveType saveType) throws SaveException {
    SaveSession session;
    frame.block();
    final String SAVE_DATABASE = Localization.lang("Save library");
    try {
        SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs).withEncoding(enc).withSaveType(saveType);
        BibtexDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(FileSaveSession::new);
        if (selectedOnly) {
            session = databaseWriter.savePartOfDatabase(bibDatabaseContext, mainTable.getSelectedEntries(), prefs);
        } else {
            session = databaseWriter.saveDatabase(bibDatabaseContext, prefs);
        }
        registerUndoableChanges(session);
    }// FIXME: not sure if this is really thrown anywhere
     catch (UnsupportedCharsetException ex) {
        JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + ' ' + Localization.lang("Character encoding '%0' is not supported.", enc.displayName()), SAVE_DATABASE, JOptionPane.ERROR_MESSAGE);
        throw new SaveException("rt");
    } catch (SaveException ex) {
        if (ex.specificEntry()) {
            // Error occurred during processing of the entry. Highlight it:
            highlightEntry(ex.getEntry());
            showEntry(ex.getEntry());
        } else {
            LOGGER.warn("Could not save", ex);
        }
        JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + "\n" + ex.getMessage(), SAVE_DATABASE, JOptionPane.ERROR_MESSAGE);
        throw new SaveException("rt");
    } finally {
        frame.unblock();
    }
    boolean commit = true;
    if (!session.getWriter().couldEncodeAll()) {
        FormBuilder builder = FormBuilder.create().layout(new FormLayout("left:pref, 4dlu, fill:pref", "pref, 4dlu, pref"));
        JTextArea ta = new JTextArea(session.getWriter().getProblemCharacters());
        ta.setEditable(false);
        builder.add(Localization.lang("The chosen encoding '%0' could not encode the following characters:", session.getEncoding().displayName())).xy(1, 1);
        builder.add(ta).xy(3, 1);
        builder.add(Localization.lang("What do you want to do?")).xy(1, 3);
        String tryDiff = Localization.lang("Try different encoding");
        int answer = JOptionPane.showOptionDialog(frame, builder.getPanel(), SAVE_DATABASE, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] { Localization.lang("Save"), tryDiff, Localization.lang("Cancel") }, tryDiff);
        if (answer == JOptionPane.NO_OPTION) {
            // The user wants to use another encoding.
            Object choice = JOptionPane.showInputDialog(frame, Localization.lang("Select encoding"), SAVE_DATABASE, JOptionPane.QUESTION_MESSAGE, null, Encodings.ENCODINGS_DISPLAYNAMES, enc);
            if (choice == null) {
                commit = false;
            } else {
                Charset newEncoding = Charset.forName((String) choice);
                return saveDatabase(file, selectedOnly, newEncoding, saveType);
            }
        } else if (answer == JOptionPane.CANCEL_OPTION) {
            commit = false;
        }
    }
    if (commit) {
        session.commit(file.toPath());
        // Make sure to remember which encoding we used.
        this.bibDatabaseContext.getMetaData().setEncoding(enc);
    } else {
        session.cancel();
    }
    return commit;
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) FormBuilder(com.jgoodies.forms.builder.FormBuilder) SaveException(org.jabref.logic.exporter.SaveException) JTextArea(javax.swing.JTextArea) BibtexDatabaseWriter(org.jabref.logic.exporter.BibtexDatabaseWriter) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) Charset(java.nio.charset.Charset) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) SavePreferences(org.jabref.logic.exporter.SavePreferences) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) SaveSession(org.jabref.logic.exporter.SaveSession)

Aggregations

BibtexDatabaseWriter (org.jabref.logic.exporter.BibtexDatabaseWriter)6 FileSaveSession (org.jabref.logic.exporter.FileSaveSession)6 SaveException (org.jabref.logic.exporter.SaveException)6 SavePreferences (org.jabref.logic.exporter.SavePreferences)6 SaveSession (org.jabref.logic.exporter.SaveSession)5 Charset (java.nio.charset.Charset)3 Defaults (org.jabref.model.Defaults)3 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)3 FormBuilder (com.jgoodies.forms.builder.FormBuilder)2 FormLayout (com.jgoodies.forms.layout.FormLayout)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 JTextArea (javax.swing.JTextArea)2 ParserResult (org.jabref.logic.importer.ParserResult)2 File (java.io.File)1 IOException (java.io.IOException)1 BackingStoreException (java.util.prefs.BackingStoreException)1 JabRefException (org.jabref.JabRefException)1 IExportFormat (org.jabref.logic.exporter.IExportFormat)1 ImportException (org.jabref.logic.importer.ImportException)1 BibDatabase (org.jabref.model.database.BibDatabase)1