Search in sources :

Example 1 with OptimisticLockingFail

use of org.omegat.core.data.IProject.OptimisticLockingFail in project omegat by omegat-org.

the class EditorController method commitAndDeactivate.

void commitAndDeactivate(ForceTranslation forceTranslation, String newTrans) {
    UIThreadsUtil.mustBeSwingThread();
    Document3 doc = editor.getOmDocument();
    doc.stopEditMode();
    // segment was active
    SegmentBuilder sb = m_docSegList[displayedEntryIndex];
    SourceTextEntry entry = sb.ste;
    TMXEntry oldTE = Core.getProject().getTranslationInfo(entry);
    PrepareTMXEntry newen = new PrepareTMXEntry();
    newen.source = sb.ste.getSrcText();
    newen.note = Core.getNotes().getNoteText();
    if (forceTranslation != null) {
        // there is force translation
        switch(forceTranslation) {
            case UNTRANSLATED:
                newen.translation = null;
                break;
            case EMPTY:
                newen.translation = "";
                break;
            case EQUALS_TO_SOURCE:
                newen.translation = newen.source;
                break;
        }
    } else {
        // translation from editor
        if (newTrans.isEmpty()) {
            // empty translation
            if (oldTE.isTranslated() && "".equals(oldTE.translation)) {
                // It's an empty translation which should remain empty
                newen.translation = "";
            } else {
                // will be untranslated
                newen.translation = null;
            }
        } else if (newTrans.equals(newen.source)) {
            // equals to source
            if (Preferences.isPreference(Preferences.ALLOW_TRANS_EQUAL_TO_SRC)) {
                // translation can be equals to source
                newen.translation = newTrans;
            } else {
                // translation can't be equals to source
                if (oldTE.source.equals(oldTE.translation)) {
                    // but it was equals to source before
                    newen.translation = oldTE.translation;
                } else {
                    // set untranslated
                    newen.translation = null;
                }
            }
        } else {
            // new translation is not empty and not equals to source - just change
            newen.translation = newTrans;
        }
    }
    boolean defaultTranslation = sb.isDefaultTranslation();
    boolean isNewAltTrans = !defaultTranslation && oldTE.defaultTranslation;
    boolean translationChanged = !Objects.equals(oldTE.translation, newen.translation);
    boolean noteChanged = !StringUtil.nvl(oldTE.note, "").equals(StringUtil.nvl(newen.note, ""));
    if (!isNewAltTrans && !translationChanged && noteChanged) {
        // Only note was changed, and we are not making a new alt translation.
        Core.getProject().setNote(entry, oldTE, newen.note);
    } else if (translationChanged || noteChanged) {
        while (true) {
            // iterate before optimistic locking will be resolved
            try {
                Core.getProject().setTranslation(entry, newen, defaultTranslation, null, previousTranslations);
                break;
            } catch (OptimisticLockingFail ex) {
                String result = new ConflictDialogController().show(ex.getOldTranslationText(), ex.getNewTranslationText(), newen.translation);
                if (result == newen.translation) {
                    // next iteration
                    previousTranslations = ex.getPrevious();
                } else {
                    // use remote - don't save user's translation
                    break;
                }
            }
        }
    }
    m_docSegList[displayedEntryIndex].createSegmentElement(false, Core.getProject().getTranslationInfo(m_docSegList[displayedEntryIndex].ste));
    // find all identical sources and redraw them
    for (int i = 0; i < m_docSegList.length; i++) {
        if (i == displayedEntryIndex) {
            // current entry, skip
            continue;
        }
        SegmentBuilder builder = m_docSegList[i];
        if (!builder.hasBeenCreated()) {
            // Skip because segment has not been drawn yet
            continue;
        }
        if (builder.ste.getSrcText().equals(entry.getSrcText())) {
            // the same source text - need to update
            builder.createSegmentElement(false, Core.getProject().getTranslationInfo(builder.ste));
            // then add new marks
            markerController.reprocessImmediately(builder);
        }
    }
    Core.getNotes().clear();
    // then add new marks
    markerController.reprocessImmediately(m_docSegList[displayedEntryIndex]);
    editor.undoManager.reset();
    // validate tags if required
    if (entry != null && Preferences.isPreference(Preferences.TAG_VALIDATE_ON_LEAVE)) {
        String file = getCurrentFile();
        new SwingWorker<Boolean, Void>() {

            protected Boolean doInBackground() throws Exception {
                return Core.getTagValidation().checkInvalidTags(entry);
            }

            @Override
            protected void done() {
                try {
                    if (!get()) {
                        Core.getIssues().showForFiles(Pattern.quote(file), entry.entryNum());
                    }
                } catch (InterruptedException | ExecutionException e) {
                    LOGGER.log(Level.SEVERE, "Exception when validating tags on leave", e);
                }
            }
        }.execute();
    }
    // team sync for save thread
    if (Core.getProject().isTeamSyncPrepared()) {
        try {
            Core.executeExclusively(false, Core.getProject()::teamSync);
        } catch (InterruptedException ex) {
        } catch (TimeoutException ex) {
        }
    }
}
Also used : Point(java.awt.Point) TimeoutException(java.util.concurrent.TimeoutException) BadLocationException(javax.swing.text.BadLocationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ConflictDialogController(org.omegat.gui.dialogs.ConflictDialogController) SourceTextEntry(org.omegat.core.data.SourceTextEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) OptimisticLockingFail(org.omegat.core.data.IProject.OptimisticLockingFail) TMXEntry(org.omegat.core.data.TMXEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Point (java.awt.Point)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 BadLocationException (javax.swing.text.BadLocationException)1 OptimisticLockingFail (org.omegat.core.data.IProject.OptimisticLockingFail)1 PrepareTMXEntry (org.omegat.core.data.PrepareTMXEntry)1 SourceTextEntry (org.omegat.core.data.SourceTextEntry)1 TMXEntry (org.omegat.core.data.TMXEntry)1 ConflictDialogController (org.omegat.gui.dialogs.ConflictDialogController)1