Search in sources :

Example 6 with TMXEntry

use of org.omegat.core.data.TMXEntry in project omegat by omegat-org.

the class EditorController method activateEntry.

/**
 * Activates the current entry (if available) by displaying source text and embedding displayed text in
 * markers.
 * <p>
 * Also moves document focus to current entry, and makes sure fuzzy info displayed if available.
 */
public void activateEntry(CaretPosition pos) {
    UIThreadsUtil.mustBeSwingThread();
    SourceTextEntry ste = getCurrentEntry();
    if (ste == null) {
        return;
    }
    if (scrollPane.getViewport().getView() != editor) {
        // editor not displayed
        return;
    }
    if (!Core.getProject().isProjectLoaded()) {
        return;
    }
    SegmentBuilder builder = m_docSegList[displayedEntryIndex];
    // document centered at the destination segment.
    if (!builder.hasBeenCreated()) {
        loadDocument();
        activateEntry(pos);
        return;
    }
    previousTranslations = Core.getProject().getAllTranslations(ste);
    TMXEntry currentTranslation = previousTranslations.getCurrentTranslation();
    // forget about old marks
    builder.createSegmentElement(true, currentTranslation);
    Core.getNotes().setNoteText(currentTranslation.note);
    // then add new marks
    markerController.reprocessImmediately(builder);
    editor.undoManager.reset();
    history.insertNew(builder.segmentNumberInProject);
    setMenuEnabled();
    showStat();
    showLengthMessage();
    if (Preferences.isPreference(Preferences.EXPORT_CURRENT_SEGMENT)) {
        segmentExportImport.exportCurrentSegment(ste);
    }
    int te = editor.getOmDocument().getTranslationEnd();
    int ts = editor.getOmDocument().getTranslationStart();
    // 
    if (pos.position != null) {
        // check if outside of entry
        pos.position = Math.max(0, pos.position);
        pos.position = Math.min(pos.position, te - ts);
    }
    if (pos.selectionStart != null && pos.selectionEnd != null) {
        // check if outside of entry
        pos.selectionStart = Math.max(0, pos.selectionStart);
        pos.selectionEnd = Math.min(pos.selectionEnd, te - ts);
        if (pos.selectionStart >= pos.selectionEnd) {
            // if end after start
            pos.selectionStart = null;
            pos.selectionEnd = null;
        }
    }
    scrollForDisplayNearestSegments(pos);
    // check if file was changed
    if (previousDisplayedFileIndex != displayedFileIndex) {
        previousDisplayedFileIndex = displayedFileIndex;
        CoreEvents.fireEntryNewFile(Core.getProject().getProjectFiles().get(displayedFileIndex).filePath);
    }
    editor.autoCompleter.setVisible(false);
    editor.repaint();
    // fire event about new segment activated
    CoreEvents.fireEntryActivated(ste);
}
Also used : SourceTextEntry(org.omegat.core.data.SourceTextEntry) TMXEntry(org.omegat.core.data.TMXEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) Point(java.awt.Point)

Example 7 with TMXEntry

use of org.omegat.core.data.TMXEntry in project omegat by omegat-org.

the class EditorController method gotoEntry.

public void gotoEntry(String srcString, EntryKey key) {
    UIThreadsUtil.mustBeSwingThread();
    /*
         * Goto segment with contains matched source. Since it enough rarely executed code, it
         * will be better to find this segment each time, instead use additional memory storage.
         */
    List<SourceTextEntry> entries = Core.getProject().getAllEntries();
    for (int i = 0; i < entries.size(); i++) {
        SourceTextEntry ste = entries.get(i);
        if (srcString != null && !ste.getSrcText().equals(srcString)) {
            // source text not equals - there is no sense to checking this entry
            continue;
        }
        if (key != null) {
            // multiple translation
            if (!ste.getKey().equals(key)) {
                continue;
            }
        } else {
            // default translation - multiple shouldn't exist for this entry
            TMXEntry trans = Core.getProject().getTranslationInfo(entries.get(i));
            if (!trans.isTranslated() || !trans.defaultTranslation) {
                // we need exist alternative translation
                continue;
            }
        }
        gotoEntry(i + 1);
        break;
    }
}
Also used : SourceTextEntry(org.omegat.core.data.SourceTextEntry) Point(java.awt.Point) TMXEntry(org.omegat.core.data.TMXEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry)

Example 8 with TMXEntry

use of org.omegat.core.data.TMXEntry 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 isNewDefaultTrans = defaultTranslation && !oldTE.defaultTranslation;
    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 (isNewDefaultTrans || 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), defaultTranslation);
    // 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), !defaultTranslation);
            // 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) {
        } catch (Exception ex) {
            throw new RuntimeException(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)

Example 9 with TMXEntry

use of org.omegat.core.data.TMXEntry in project omegat by omegat-org.

the class SegmentExportImport method exportCurrentSegment.

/**
 * Export the current source and target segments in text files.
 */
public synchronized void exportCurrentSegment(final SourceTextEntry ste) {
    importFile.delete();
    if (ste == null) {
        // entry deactivated
        exportLastModified = Long.MAX_VALUE;
        return;
    }
    String s1 = ste.getSrcText();
    TMXEntry te = Core.getProject().getTranslationInfo(ste);
    String s2 = te.isTranslated() ? te.translation : "";
    File sourceFile = getFile(SOURCE_EXPORT);
    File targetFile = getFile(TARGET_EXPORT);
    try {
        writeFile(sourceFile, s1);
        writeFile(targetFile, s2);
        exportLastModified = sourceFile.lastModified();
    } catch (IOException ex) {
        Log.log(ex);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) TMXEntry(org.omegat.core.data.TMXEntry)

Example 10 with TMXEntry

use of org.omegat.core.data.TMXEntry in project omegat by omegat-org.

the class ReplaceFilter method skip.

private void skip() {
    EditorController ec = (EditorController) Core.getEditor();
    // try to find in current entry
    int pos = ec.getCurrentPositionInEntryTranslation();
    String str = ec.getCurrentTranslation();
    List<SearchMatch> found = getReplacementsForEntry(str);
    if (found != null) {
        for (SearchMatch m : found) {
            if (m.getStart() >= pos) {
                ec.setCaretPosition(new IEditor.CaretPosition(m.getStart(), m.getEnd()));
                ec.requestFocus();
                return;
            }
        }
    }
    // not found in current entry - find next entry
    int currentEntryNumber = ec.getCurrentEntryNumber();
    ec.commitAndDeactivate();
    // find to the end of project
    for (int i = currentEntryNumber + 1; i <= maxEntryNum; i++) {
        SourceTextEntry ste = entries.get(i);
        if (ste == null) {
            // entry not filtered
            continue;
        }
        TMXEntry en = Core.getProject().getTranslationInfo(ste);
        String trans = getEntryText(ste, en);
        if (trans == null) {
            continue;
        }
        found = getReplacementsForEntry(trans);
        if (found == null) {
            // no replacements
            continue;
        }
        for (SearchMatch m : found) {
            ec.gotoEntry(i, new CaretPosition(m.getStart(), m.getEnd()));
            ec.requestFocus();
            return;
        }
    }
    // find from the beginning of project
    for (int i = minEntryNum; i < currentEntryNumber; i++) {
        SourceTextEntry ste = entries.get(i);
        if (ste == null) {
            // entry not filtered
            continue;
        }
        TMXEntry en = Core.getProject().getTranslationInfo(ste);
        String trans = getEntryText(ste, en);
        if (trans == null) {
            continue;
        }
        found = getReplacementsForEntry(trans);
        if (found == null) {
            // no replacements
            continue;
        }
        for (SearchMatch m : found) {
            ec.gotoEntry(i, new CaretPosition(m.getStart(), m.getEnd()));
            ec.requestFocus();
            return;
        }
    }
    // not found
    ec.activateEntry();
}
Also used : IEditor(org.omegat.gui.editor.IEditor) EditorController(org.omegat.gui.editor.EditorController) SearchMatch(org.omegat.core.search.SearchMatch) SourceTextEntry(org.omegat.core.data.SourceTextEntry) CaretPosition(org.omegat.gui.editor.IEditor.CaretPosition) CaretPosition(org.omegat.gui.editor.IEditor.CaretPosition) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) TMXEntry(org.omegat.core.data.TMXEntry)

Aggregations

TMXEntry (org.omegat.core.data.TMXEntry)20 SourceTextEntry (org.omegat.core.data.SourceTextEntry)13 PrepareTMXEntry (org.omegat.core.data.PrepareTMXEntry)10 Map (java.util.Map)7 HashMap (java.util.HashMap)5 Point (java.awt.Point)4 EntryKey (org.omegat.core.data.EntryKey)4 IProject (org.omegat.core.data.IProject)4 Language (org.omegat.util.Language)4 ArrayList (java.util.ArrayList)3 NearString (org.omegat.core.matching.NearString)3 File (java.io.File)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 ExternalTMX (org.omegat.core.data.ExternalTMX)2 FileInfo (org.omegat.core.data.IProject.FileInfo)2 ITMXEntry (org.omegat.core.data.ITMXEntry)2 ProjectTMX (org.omegat.core.data.ProjectTMX)2 ProtectedPart (org.omegat.core.data.ProtectedPart)2 SearchMatch (org.omegat.core.search.SearchMatch)2