Search in sources :

Example 1 with Dictionary

use of com.teamdev.jxbrowser.spellcheck.Dictionary in project JxBrowser-Examples by TeamDev-IP.

the class SpellCheckSuggestions method main.

public static void main(String[] args) {
    // Enable heavyweight popup menu for the heavyweight
    // (default) BrowserView component. Otherwise – the popup
    // menu will be displayed under the BrowserView component.
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Spell Check Suggestions");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.set(ShowContextMenuCallback.class, (params, tell) -> {
        JPopupMenu popupMenu = new JPopupMenu();
        popupMenu.addPopupMenuListener(myPopupMenuListener(tell));
        // Add the suggestions menu items.
        SpellCheckMenu spellCheckMenu = params.spellCheckMenu();
        List<String> suggestions = spellCheckMenu.dictionarySuggestions();
        suggestions.forEach(suggestion -> {
            JMenuItem menuItem = new JMenuItem(suggestion);
            menuItem.addActionListener(e -> {
                browser.replaceMisspelledWord(suggestion);
                tell.close();
            });
            popupMenu.add(menuItem);
        });
        // Add menu separator if necessary.
        if (!suggestions.isEmpty()) {
            popupMenu.addSeparator();
        }
        // Add the "Add to Dictionary" menu item.
        JMenuItem addToDictionary = new JMenuItem(spellCheckMenu.addToDictionaryMenuItemText());
        addToDictionary.addActionListener(e -> {
            Dictionary dictionary = engine.spellChecker().customDictionary();
            dictionary.add(spellCheckMenu.misspelledWord());
            tell.close();
        });
        popupMenu.add(addToDictionary);
        // Display context menu at specified location.
        Point location = params.location();
        popupMenu.show(view, location.x(), location.y());
    });
    browser.mainFrame().ifPresent(mainFrame -> {
        mainFrame.loadHtml("<html><body><textarea rows='20' cols='30'>" + "Smple text with mitake.</textarea></body></html>");
    });
}
Also used : Dictionary(com.teamdev.jxbrowser.spellcheck.Dictionary) JFrame(javax.swing.JFrame) SpellCheckMenu(com.teamdev.jxbrowser.menu.SpellCheckMenu) Point(com.teamdev.jxbrowser.ui.Point) JMenuItem(javax.swing.JMenuItem) Engine(com.teamdev.jxbrowser.engine.Engine) JPopupMenu(javax.swing.JPopupMenu) Browser(com.teamdev.jxbrowser.browser.Browser)

Aggregations

Browser (com.teamdev.jxbrowser.browser.Browser)1 Engine (com.teamdev.jxbrowser.engine.Engine)1 SpellCheckMenu (com.teamdev.jxbrowser.menu.SpellCheckMenu)1 Dictionary (com.teamdev.jxbrowser.spellcheck.Dictionary)1 Point (com.teamdev.jxbrowser.ui.Point)1 JFrame (javax.swing.JFrame)1 JMenuItem (javax.swing.JMenuItem)1 JPopupMenu (javax.swing.JPopupMenu)1