Package com.inet.jortho

Source Code of com.inet.jortho.DictionaryEditDialog

/*
*  JOrtho
*
*  Copyright (C) 2005-2009 by i-net software
*
*  This program is free software; you can redistribute it and/or
*  modify it under the terms of the GNU General Public License as
*  published by the Free Software Foundation; either version 2 of the
*  License, or (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but
*  WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
*  USA.
*  Created on 24.12.2007
*/
package com.inet.jortho;

import jriaffe.client.NotificationCenter;
import jriaffe.client.ui.components.ApplicationWindow;
import jriaffe.client.ui.components.FormLayout;
import jriaffe.client.ui.components.FormLayoutConstraints;
import jriaffe.client.ui.components.RoundedPanel;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;

import javax.swing.*;
import javax.swing.text.JTextComponent;


/**
* Implements edit dialog for the user dictionary.
*
* @author Volker Berlin
*/
class DictionaryEditDialog extends RoundedPanel {

    private final JList list;
    private final JButton delete;
    private final JButton cancel;
    private boolean isModify;
    JTextComponent jText;

    DictionaryEditDialog(JTextComponent jText) {
        this.jText = jText;
        setLayout(new FormLayout(1));

        DefaultListModel data = new DefaultListModel();
        loadWordList(data);
        list = new JList(data);
        addZone(FormLayoutConstraints.Zone.WEST, list);

        addZone(FormLayoutConstraints.Zone.NORTH, new JLabel(Utils.getResource("editDictionary")));

        delete = new JButton(Utils.getResource("delete"));
        delete.setName("delete");
        add(delete);

        cancel = new JButton(Utils.getResource("close"));
        cancel.setName("close");
        add(cancel);
    }

    private void addZone(FormLayoutConstraints.Zone zone, JComponent comp) {
        JPanel zonePanel = new JPanel();
        FormLayout pLayout = new FormLayout(1);
        pLayout.setOrigin(new Dimension(10, 10));
        zonePanel.setLayout(pLayout);
        zonePanel.setOpaque(false);
        zonePanel.add(comp);
        FormLayoutConstraints listConstraint = new FormLayoutConstraints();
        listConstraint.setZone(zone);
        add(zonePanel, listConstraint);

    }

    public Object getEventHandler() {
        return this;
    }

    public void clickDelete() {
       int[] selected = list.getSelectedIndices();
        Arrays.sort(selected);
        for (int i = selected.length - 1; i >= 0; i--) {
            ((DefaultListModel) list.getModel()).remove(selected[i]);
            isModify = true;
        }
    }

    public void clickClose() {
        NotificationCenter.getDefaultNotificationCenter().postNotification(ApplicationWindow.NOTIFY_REMOVE_POPUP, this);
    }

    /**
     * Load all words from the user dictionary if available
     *
     * @param data
     */
    private void loadWordList(DefaultListModel data) {
        UserDictionaryProvider provider = SpellChecker.getUserDictionaryProvider();
        if (provider != null) {
            Iterator<String> userWords = provider.getWords(SpellChecker.getCurrentLocale());
            if (userWords != null) {
                ArrayList<String> wordList = new ArrayList<String>();
                while (userWords.hasNext()) {
                    String word = userWords.next();
                    if (word != null && word.length() > 1) {
                        wordList.add(word);
                    }
                }
                // Liste alphabetical sorting with the user language
                Collections.sort(wordList, Collator.getInstance());
                for (String str : wordList) {
                    data.addElement(str);
                }
            }
        }
    }

    public void onClose() {
        if (isModify) {
            UserDictionaryProvider provider = SpellChecker.getUserDictionaryProvider();
            if (provider != null) {
                ListModel model = list.getModel();
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < model.getSize(); i++) {
                    if (builder.length() != 0) {
                        builder.append('\n');
                    }
                    builder.append(model.getElementAt(i));
                }
                provider.setUserWords(builder.toString());
            }
            SpellChecker.reloadCurrentDictionary();
        }
    }

}
TOP

Related Classes of com.inet.jortho.DictionaryEditDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.