Search in sources :

Example 1 with CreateGlossaryEntry

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;
}
Also used : IMainWindow(org.omegat.gui.main.IMainWindow) Window(java.awt.Window) WindowAdapter(java.awt.event.WindowAdapter) JTextComponent(javax.swing.text.JTextComponent) ProjectProperties(org.omegat.core.data.ProjectProperties) CreateGlossaryEntry(org.omegat.gui.dialogs.CreateGlossaryEntry) WindowEvent(java.awt.event.WindowEvent) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) CreateGlossaryEntry(org.omegat.gui.dialogs.CreateGlossaryEntry) File(java.io.File) WindowFocusListener(java.awt.event.WindowFocusListener)

Aggregations

Component (java.awt.Component)1 Window (java.awt.Window)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 WindowFocusListener (java.awt.event.WindowFocusListener)1 File (java.io.File)1 JTextComponent (javax.swing.text.JTextComponent)1 ProjectProperties (org.omegat.core.data.ProjectProperties)1 CreateGlossaryEntry (org.omegat.gui.dialogs.CreateGlossaryEntry)1 IMainWindow (org.omegat.gui.main.IMainWindow)1