Search in sources :

Example 21 with Caret

use of javax.swing.text.Caret in project freeplane by freeplane.

the class CheckerListener method popupMenuWillBecomeVisible.

public void popupMenuWillBecomeVisible(final PopupMenuEvent ev) {
    if (SpellChecker.getCurrentDictionary() == null) {
        menu.setEnabled(false);
        return;
    }
    final JPopupMenu popup = (JPopupMenu) ev.getSource();
    final Component invoker = popup.getInvoker();
    if (invoker instanceof JTextComponent) {
        final JTextComponent jText = (JTextComponent) invoker;
        if (!jText.isEditable()) {
            // Suggestions only for editable text components
            menu.setEnabled(false);
            return;
        }
        final Caret caret = jText.getCaret();
        int offs = Math.min(caret.getDot(), caret.getMark());
        final Point p = jText.getMousePosition();
        if (p != null) {
            // use position from mouse click and not from editor cursor position
            offs = jText.viewToModel(p);
        }
        try {
            final Document doc = jText.getDocument();
            if (offs > 0 && (offs >= doc.getLength() || Character.isWhitespace(doc.getText(offs, 1).charAt(0)))) {
                // if the next character is a white space then use the word on the left site
                offs--;
            }
            if (offs < 0) {
                // occur if there nothing under the mouse pointer
                menu.setEnabled(false);
                return;
            }
            // get the word from current position
            final int begOffs = Utilities.getWordStart(jText, offs);
            final int endOffs = Utilities.getWordEnd(jText, offs);
            final String word = jText.getText(begOffs, endOffs - begOffs);
            // find the first invalid word from current position
            final Tokenizer tokenizer = new Tokenizer(jText, dictionary, locale, offs, options);
            String invalidWord;
            do {
                invalidWord = tokenizer.nextInvalidWord();
            } while (tokenizer.getWordOffset() < begOffs);
            menu.removeAll();
            if (!word.equals(invalidWord)) {
                // the current word is not invalid
                menu.setEnabled(false);
                return;
            }
            if (dictionary == null) {
                // without dictionary it is disabled
                menu.setEnabled(false);
                return;
            }
            final List<Suggestion> list = dictionary.searchSuggestions(word);
            // Disable then menu item if there are no suggestions
            menu.setEnabled(list.size() > 0);
            final boolean needCapitalization = tokenizer.isFirstWordInSentence() && Utils.isFirstCapitalized(word);
            for (int i = 0; i < list.size() && i < options.getSuggestionsLimitMenu(); i++) {
                final Suggestion sugestion = list.get(i);
                String sugestionWord = sugestion.getWord();
                if (needCapitalization) {
                    sugestionWord = Utils.getCapitalized(sugestionWord);
                }
                final JMenuItem item = new JMenuItem(sugestionWord);
                menu.add(item);
                final String newWord = sugestionWord;
                item.addActionListener(new ActionListener() {

                    public void actionPerformed(final ActionEvent e) {
                        jText.setSelectionStart(begOffs);
                        jText.setSelectionEnd(endOffs);
                        jText.replaceSelection(newWord);
                    }
                });
            }
            final UserDictionaryProvider provider = SpellChecker.getUserDictionaryProvider();
            if (provider == null) {
                return;
            }
            final JMenuItem addToDic = new JMenuItem(Utils.getResource("addToDictionary"));
            addToDic.addActionListener(new ActionListener() {

                public void actionPerformed(final ActionEvent e) {
                    final UserDictionaryProvider provider = SpellChecker.getUserDictionaryProvider();
                    if (provider != null) {
                        provider.addWord(word);
                    }
                    dictionary.add(word);
                    dictionary.trimToSize();
                    AutoSpellChecker.refresh(jText);
                }
            });
            if (list.size() > 0) {
                if (menu instanceof JMenu) {
                    ((JMenu) menu).addSeparator();
                } else if (menu instanceof JPopupMenu) {
                    ((JPopupMenu) menu).addSeparator();
                }
            }
            menu.add(addToDic);
            menu.setEnabled(true);
        } catch (final BadLocationException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : ActionEvent(java.awt.event.ActionEvent) JTextComponent(javax.swing.text.JTextComponent) Point(java.awt.Point) Document(javax.swing.text.Document) JPopupMenu(javax.swing.JPopupMenu) Point(java.awt.Point) ActionListener(java.awt.event.ActionListener) JComponent(javax.swing.JComponent) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) JMenuItem(javax.swing.JMenuItem) Caret(javax.swing.text.Caret) JMenu(javax.swing.JMenu) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

Caret (javax.swing.text.Caret)21 DefaultCaret (javax.swing.text.DefaultCaret)8 JTextComponent (javax.swing.text.JTextComponent)8 Point (java.awt.Point)6 BadLocationException (javax.swing.text.BadLocationException)5 Document (javax.swing.text.Document)5 JPopupMenu (javax.swing.JPopupMenu)4 KeyStroke (javax.swing.KeyStroke)3 FocusEvent (java.awt.event.FocusEvent)2 FocusListener (java.awt.event.FocusListener)2 ArrayList (java.util.ArrayList)2 BaseDocument (org.netbeans.editor.BaseDocument)2 MDoc (gov.sandia.n2a.db.MDoc)1 MNode (gov.sandia.n2a.db.MNode)1 Visitor (gov.sandia.n2a.db.MNode.Visitor)1 ParsedValue (gov.sandia.n2a.eqset.Variable.ParsedValue)1 Component (java.awt.Component)1 Font (java.awt.Font)1 FontMetrics (java.awt.FontMetrics)1 Rectangle (java.awt.Rectangle)1