Search in sources :

Example 1 with SearchMatch

use of org.omegat.core.search.SearchMatch 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)

Example 2 with SearchMatch

use of org.omegat.core.search.SearchMatch in project omegat by omegat-org.

the class ReplaceFilter method replaceAll.

/**
 * Replace all occurrences in all entries.
 */
public void replaceAll() {
    for (SourceTextEntry ste : entries.values()) {
        TMXEntry en = Core.getProject().getTranslationInfo(ste);
        String trans = getEntryText(ste, en);
        if (trans == null) {
            continue;
        }
        // Avoid to replace more than once with variables when entries have duplicates
        if ((en.defaultTranslation) && (ste.getDuplicate() == SourceTextEntry.DUPLICATE.NEXT)) {
            // Already replaced when we parsed the first entry
            continue;
        }
        List<SearchMatch> found = getReplacementsForEntry(trans);
        if (found != null) {
            int off = 0;
            StringBuilder o = new StringBuilder(trans);
            for (SearchMatch m : found) {
                o.replace(m.getStart() + off, m.getEnd() + off, m.getReplacement());
                off += m.getReplacement().length() - m.getLength();
            }
            PrepareTMXEntry prepare = new PrepareTMXEntry(en);
            prepare.translation = o.toString();
            Core.getProject().setTranslation(ste, prepare, en.defaultTranslation, null);
        }
    }
    EditorController ec = (EditorController) Core.getEditor();
    ec.refreshEntries(entries.keySet());
}
Also used : SearchMatch(org.omegat.core.search.SearchMatch) EditorController(org.omegat.gui.editor.EditorController) SourceTextEntry(org.omegat.core.data.SourceTextEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) TMXEntry(org.omegat.core.data.TMXEntry)

Example 3 with SearchMatch

use of org.omegat.core.search.SearchMatch in project omegat by omegat-org.

the class ReplaceFilter method replace.

private void replace() {
    EditorController ec = (EditorController) Core.getEditor();
    // is caret inside match ?
    int pos = ec.getCurrentPositionInEntryTranslation();
    String str = ec.getCurrentTranslation();
    List<SearchMatch> found = getReplacementsForEntry(str);
    if (found != null) {
        for (SearchMatch m : found) {
            if (m.getStart() <= pos && pos <= m.getEnd()) {
                // yes - replace
                ec.replacePartOfText(m.getReplacement(), m.getStart(), m.getEnd());
                break;
            }
        }
    }
    // skip to next
    skip();
}
Also used : EditorController(org.omegat.gui.editor.EditorController) SearchMatch(org.omegat.core.search.SearchMatch)

Example 4 with SearchMatch

use of org.omegat.core.search.SearchMatch in project omegat by omegat-org.

the class ReplaceMarker method getMarksForEntry.

@Override
public List<Mark> getMarksForEntry(SourceTextEntry ste, String sourceText, String translationText, boolean isActive) throws Exception {
    IEditorFilter filter = Core.getEditor().getFilter();
    if (filter == null || !(filter instanceof ReplaceFilter)) {
        return Collections.emptyList();
    }
    ReplaceFilter replaceFilter = (ReplaceFilter) filter;
    List<SearchMatch> matches = replaceFilter.getReplacementsForEntry(translationText);
    if (matches == null) {
        return Collections.emptyList();
    }
    List<Mark> r = new ArrayList<Mark>(matches.size());
    for (SearchMatch s : matches) {
        Mark m = new Mark(Mark.ENTRY_PART.TRANSLATION, s.getStart(), s.getEnd());
        m.painter = PAINTER;
        r.add(m);
    }
    return r;
}
Also used : ReplaceFilter(org.omegat.gui.editor.filter.ReplaceFilter) IEditorFilter(org.omegat.gui.editor.IEditorFilter) SearchMatch(org.omegat.core.search.SearchMatch) ArrayList(java.util.ArrayList)

Aggregations

SearchMatch (org.omegat.core.search.SearchMatch)4 EditorController (org.omegat.gui.editor.EditorController)3 PrepareTMXEntry (org.omegat.core.data.PrepareTMXEntry)2 SourceTextEntry (org.omegat.core.data.SourceTextEntry)2 TMXEntry (org.omegat.core.data.TMXEntry)2 ArrayList (java.util.ArrayList)1 IEditor (org.omegat.gui.editor.IEditor)1 CaretPosition (org.omegat.gui.editor.IEditor.CaretPosition)1 IEditorFilter (org.omegat.gui.editor.IEditorFilter)1 ReplaceFilter (org.omegat.gui.editor.filter.ReplaceFilter)1