use of org.omegat.gui.dialogs.CreateGlossaryEntry in project omegat by omegat-org.
the class GlossaryTextArea method showCreateGlossaryEntryDialog.
@Override
public void showCreateGlossaryEntryDialog(final Frame parent) {
CreateGlossaryEntry d = createGlossaryEntryDialog;
if (d != null) {
d.requestFocus();
return;
}
ProjectProperties props = Core.getProject().getProjectProperties();
final File out = new File(props.getWriteableGlossary());
final CreateGlossaryEntry dialog = new CreateGlossaryEntry(parent);
String txt = dialog.getGlossaryFileText().getText();
txt = MessageFormat.format(txt, out.getAbsolutePath());
dialog.getGlossaryFileText().setText(txt);
dialog.getSourceText().requestFocus();
dialog.addWindowFocusListener(new WindowFocusListener() {
@Override
public void windowLostFocus(WindowEvent e) {
}
@Override
public void windowGainedFocus(WindowEvent e) {
Window w = e.getOppositeWindow();
if (w != null) {
String sel = getSelectedText(w.getMostRecentFocusOwner());
if (!StringUtil.isEmpty(sel)) {
if (StringUtil.isEmpty(dialog.getSourceText().getText())) {
setText(dialog.getSourceText(), sel);
} else if (StringUtil.isEmpty(dialog.getTargetText().getText())) {
setText(dialog.getTargetText(), sel);
} else if (StringUtil.isEmpty(dialog.getCommentText().getText())) {
setText(dialog.getCommentText(), sel);
}
}
}
}
private String getSelectedText(Component comp) {
String result = null;
if (comp instanceof JTextComponent) {
result = ((JTextComponent) comp).getSelectedText();
if (!StringUtil.isEmpty(result)) {
result = EditorUtils.removeDirectionChars(result);
}
}
return result;
}
private void setText(JTextComponent comp, String text) {
comp.setText(text);
comp.requestFocus();
comp.selectAll();
}
});
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
createGlossaryEntryDialog = null;
if (dialog.getReturnStatus() == CreateGlossaryEntry.RET_OK) {
String src = StringUtil.normalizeUnicode(dialog.getSourceText().getText()).trim();
String loc = StringUtil.normalizeUnicode(dialog.getTargetText().getText()).trim();
String com = StringUtil.normalizeUnicode(dialog.getCommentText().getText()).trim();
if (!StringUtil.isEmpty(src) && !StringUtil.isEmpty(loc)) {
try {
GlossaryReaderTSV.append(out, new GlossaryEntry(src, loc, com, true, out.getPath()));
} catch (Exception ex) {
Log.log(ex);
}
}
}
}
});
StaticUIUtils.persistGeometry(dialog, Preferences.CREATE_GLOSSARY_GEOMETRY_PREFIX);
dialog.setVisible(true);
createGlossaryEntryDialog = dialog;
}
Aggregations