Search in sources :

Example 1 with JTextComponentSpellChecker

use of org.dts.spell.swing.JTextComponentSpellChecker in project ramus by Vitaliy-Yakovchuk.

the class TextAttributePlugin method getAttributeEditor.

@Override
public AttributeEditor getAttributeEditor(final Engine engine, final AccessRules rules, final Element element, final Attribute attribute, AttributeEditor old) {
    if (old != null)
        old.close();
    return new AbstractAttributeEditor() {

        private TextPanelWithLinksDetector area = new TextPanelWithLinksDetector(TextAttributePlugin.this);

        protected JTextComponentSpellChecker checker;

        private Object value;

        private JPopupMenu createSelectLanguageMenu() {
            JMenu lMenu = new JMenu(GlobalResourcesManager.getString("Editor.Language"));
            Language[] languages = SpellFactory.getLanguages();
            final Properties ps = engine.getProperties(USER_GUI_SPELL_PROPERTIES);
            Language dLanguage = getCurrentLanguage(ps);
            ButtonGroup group = new ButtonGroup();
            for (Language l : languages) {
                final String ln = l.getName();
                JRadioButtonMenuItem item = new JRadioButtonMenuItem(new AbstractAction(l.getLocalizedName()) {

                    /**
                     */
                    private static final long serialVersionUID = -6106361308637383251L;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        ps.setProperty(LANGUAGE, ln);
                        saveProperties(engine, ps);
                    }
                });
                lMenu.add(item);
                if ((dLanguage != null) && (dLanguage.equals(l)))
                    item.setSelected(true);
                group.add(item);
            }
            JPopupMenu menu = new JPopupMenu();
            JCheckBoxMenuItem item = new JCheckBoxMenuItem(GlobalResourcesManager.getString("Editor.SpellCheck"));
            item.setSelected(isSpellCheck(ps));
            item.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    Options.setBoolean(SPELL_CHECK, !isSpellCheck(ps), ps);
                    saveProperties(engine, ps);
                }
            });
            menu.add(item);
            menu.add(lMenu);
            return menu;
        }

        private void saveProperties(final Engine engine, final Properties ps) {
            engine.setProperties(USER_GUI_SPELL_PROPERTIES, ps);
            if (checker != null) {
                checker.stopRealtimeMarkErrors();
                checker = null;
            }
            createChecker();
        }

        private boolean isSpellCheck(final Properties ps) {
            return Options.getBoolean(SPELL_CHECK, true, ps);
        }

        private void createChecker() {
            Properties ps = engine.getProperties(USER_GUI_SPELL_PROPERTIES);
            if (isSpellCheck(ps)) {
                Language language = getCurrentLanguage(ps);
                if (language != null) {
                    checker = language.createTextComponentSpellChecker();
                    checker.startRealtimeMarkErrors(area);
                }
            }
        }

        private Language getCurrentLanguage(Properties ps) {
            String langName = Options.getString(LANGUAGE, "", ps);
            Language language;
            if ((langName == null) || (langName.equals("")))
                language = SpellFactory.getDefaultLanguage();
            else
                language = SpellFactory.findLanguage(langName);
            return language;
        }

        @Override
        public JComponent getComponent() {
            JScrollPane pane = new JScrollPane();
            pane.setViewportView(area);
            boolean canEdit = rules.canUpdateElement(element.getId(), attribute.getId());
            area.setEditable(canEdit);
            area.setComponentPopupMenu(createSelectLanguageMenu());
            createChecker();
            return pane;
        }

        @Override
        public Object getValue() {
            String res = area.getText();
            return ((res.equals("")) && (value == null)) ? null : res;
        }

        @Override
        public Object setValue(Object value) {
            this.value = value;
            area.setText((String) value);
            return value;
        }

        @Override
        public void close() {
            if (checker != null) {
                checker.stopRealtimeMarkErrors();
                checker = null;
            }
        }

        @Override
        public JComponent getLastComponent() {
            return area;
        }
    };
}
Also used : ActionEvent(java.awt.event.ActionEvent) JTextComponentSpellChecker(org.dts.spell.swing.JTextComponentSpellChecker) Properties(java.util.Properties) AbstractAttributeEditor(com.ramussoft.gui.common.AbstractAttributeEditor) Language(com.ramussoft.gui.spell.Language) ActionListener(java.awt.event.ActionListener)

Aggregations

AbstractAttributeEditor (com.ramussoft.gui.common.AbstractAttributeEditor)1 Language (com.ramussoft.gui.spell.Language)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 Properties (java.util.Properties)1 JTextComponentSpellChecker (org.dts.spell.swing.JTextComponentSpellChecker)1