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);
}
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;
}
}
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);
}
}
}
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);
}
}
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();
}
Aggregations