com.plugin.UI.Windows.UserSearchResultsDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.plugin.UI.Windows.UserSearchResultsDialog.java

Source

/*
 * Church Software
 *
 * Package : com.plugin.UI.Windows
 * Filename: UserSearchResultsDialog.java
 * Author  : Felix Chandrakumar <i@fc.id.au>
 * Website : www.churchsw.org
 * 
 * PUBLIC DOMAIN
 * 
 * As the Word of God says,
 * 
 * Freely you have received, freely give. - (Mat 10:8). For all things are from Jesus Christ,
 * by Jesus Christ, and for Jesus Christ. - (Romans 11:36)
 * 
 * Free means completely free! Church Software has no restrictions at all. The source code, 
 * binary and everything that is not licensed by others but included in Church Software bundle
 * is released to public domain and you can modify anything to fit your church needs. Public 
 * domain simply means everyone can use it without any restrictions.
 * 
 * WHY PUBLIC DOMAIN?
 * 
 * The manifestation of the Spirit is given to each one for the profit of all: for to one is
 * given the word of wisdom through the Spirit, to another the word of knowledge through the
 * same Spirit, to another faith by the same Spirit, to another gifts of healings by the same
 * Spirit, to another the working of miracles, to another prophecy, to another discerning of
 * spirits, to another different kinds of tongues, to another the interpretation of tongues.
 * But one and the same Spirit works all these things, distributing to each one individually
 * as He wills. - 1 Co 12:7-11
 * 
 * Woe unto them! for they have gone in the way of Cain, and ran greedily after the error of
 * Balaam for reward, and perished in the rebellion of Korah. - Jud 1:11
 * 
 * Jesus Christ gives word of knowledge through Holy Spirit for the profit of all. I dont
 * want to run greedily after the error of Balaam for reward. Balaam is a prophet of God who
 * went greedily to Balak. Unfortunately, many christian organizations, churches, bands, song
 * writers, musicians etc market the gifts of Holy Spirit, doing the same error of Balam for
 * reward. My Bible says, Woe to them and I don't want to be a part of them.
 */
package com.plugin.UI.Windows;

import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import org.apache.commons.codec.language.Soundex;
import org.apache.log4j.Logger;

import com.church.db.ChurchMemberUtils;
import com.church.db.ChurchMembersData;
import com.church.gateway.Global;
import com.church.tools.MemberID;
import com.plugin.UI.Windows.ChurchAdmin.ChurchMembers;

/**
 * The Class UserSearchResultsDialog.
 */
public class UserSearchResultsDialog extends JDialog {

    /** The log. */
    Logger log = Logger.getLogger(this.getClass());

    /** The term. */
    String term = null;

    /** The table. */
    JTable table = null;

    /** The tm. */
    DefaultTableModel tm = null;

    /**
     * Instantiates a new user search results dialog.
     * 
     * @param owner the owner
     * @param _term the _term
     */
    public UserSearchResultsDialog(Frame owner, String _term) {
        super(owner);
        try {
            initialize(_term);

            term = _term;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            log.error(e, e);
        }
    }

    /**
     * Initialize.
     * 
     * @param _term the _term
     * 
     * @throws Exception the exception
     */
    private void initialize(String _term) throws Exception {
        ArrayList<String[]> arr = new ArrayList<String[]>();
        for (ChurchMembersData mem : ChurchMemberUtils.getChurchMembers()) {
            if (isSoundex(mem, _term.toUpperCase())) {
                String[] arrin = new String[5];
                arrin[0] = MemberID.getTranslatedID(mem.getMember_id(), mem.getName_english());
                arrin[1] = mem.getName_english() + " (" + mem.getName_otherLang() + ")";
                arrin[2] = mem.getStreet();
                arrin[3] = mem.getSuburb();
                arrin[4] = mem.getState();
                arr.add(arrin);
            }
        }
        String[][] arr3 = new String[arr.size()][5];
        for (int i = 0; i < arr.size(); i++)
            arr3[i] = arr.get(i);
        String columns[] = new String[] { "MemberID", "Name", "Street", "Suburb", "State" };
        tm = new DefaultTableModel(arr3, columns) {
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        table = new JTable(tm);

        table.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                String member_id = (String) tm.getValueAt(table.getSelectedRow(), 0);
                ChurchMembers.setSelectMember(member_id);
                if (e.getClickCount() == 2) {
                    new MemberDialog(Global.DISPLAY_CHURCH_MEMBER, MemberID.getOriginalID(member_id));
                }
            }

            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });

        JScrollPane jsp = new JScrollPane(table);
        jsp.setSize(640, 260);
        this.getContentPane().add(jsp);
        this.pack();
        this.setTitle("Church Members Search Results - " + _term);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /**
     * Checks if is soundex.
     * 
     * @param mem the mem
     * @param _term the _term
     * 
     * @return true, if is soundex
     * 
     * @throws Exception the exception
     */
    private boolean isSoundex(ChurchMembersData mem, String _term) throws Exception {
        if (isSoundexCalc(mem.getName_english().toUpperCase(), _term))
            return true;
        if (isSoundexCalc(mem.getStreet().toUpperCase(), _term))
            return true;
        if (isSoundexCalc(mem.getSuburb().toUpperCase(), _term))
            return true;
        if (isSoundexCalc(mem.getState().toUpperCase(), _term))
            return true;
        return false;
    }

    /**
     * Checks if is soundex calc.
     * 
     * @param str the str
     * @param _term the _term
     * 
     * @return true, if is soundex calc
     */
    private boolean isSoundexCalc(String str, String _term) {
        Soundex s = new Soundex();
        try {
            if (s.difference(str, _term) == 4)
                return true;
        } catch (Exception e) {
            return false;
        }
        if (str.indexOf(" ") != -1) {
            String[] str2 = str.split("[ ]");
            for (int i = 0; i < str2.length; i++) {

                try {
                    if (s.difference(str.split("[ ]")[i], _term) == 4)
                        return true;
                } catch (Exception e) {
                    return false;
                }
            }
        }

        return false;
    }

} // @jve:decl-index=0:visual-constraint="10,10"