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

Java tutorial

Introduction

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

Source

/*
 * Church Software
 *
 * Package : com.plugin.UI.Windows
 * Filename: DesktopUI.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.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.LookAndFeel;
import javax.swing.border.EtchedBorder;
import javax.swing.plaf.basic.BasicDesktopIconUI;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.church.db.DB;
import com.church.gateway.Global;
import com.church.gateway.IconList;
import com.church.tools.desktop.JScrollableDesktopPane;

/**
 * The Class DesktopUI.
 */
public class DesktopUI extends JFrame {

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

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The _desktop. */
    public static JScrollableDesktopPane _desktop = null;

    /** The status bar. */
    public static JLabel statusBar = null;

    /**
     * Instantiates a new desktop ui.
     */
    public DesktopUI() {
        super(Global.selectedLanguage + " " + Global.SOFTWARE_NAME + " " + Global.VERSION);
        this.setFont(new Font(Global.standardOtherLangFont, Font.PLAIN, Global.standardOtherLangFontSize));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.addWindowListener(new WindowListener() {

            public void windowActivated(WindowEvent e) {
                // TODO Auto-generated method stub

            }

            public void windowClosed(WindowEvent e) {

            }

            public void windowClosing(WindowEvent e) {
                DB.closeConnection();
            }

            public void windowDeactivated(WindowEvent e) {
                // TODO Auto-generated method stub

            }

            public void windowDeiconified(WindowEvent e) {
                // TODO Auto-generated method stub

            }

            public void windowIconified(WindowEvent e) {
                // TODO Auto-generated method stub

            }

            public void windowOpened(WindowEvent e) {
                // TODO Auto-generated method stub

            }
        });

        Container content = getContentPane();
        content.setBackground(Color.white);
        _desktop = new JScrollableDesktopPane();
        // _desktop.setBackground(Color.WHITE);
        _desktop.setOpaque(false);

        // ///
        try {
            Image logo = ImageIO.read(new File(IconList.CHURCH_SOFTWARE_ICON));
            this.setIconImage(logo);
        } catch (IOException e2) {
        }
        content.add(_desktop, BorderLayout.CENTER);
        statusBar = new JLabel();
        statusBar.setText("Logged In User: " + Global.loggedInUser);
        statusBar.setBorder(new EtchedBorder());
        content.add(statusBar, BorderLayout.SOUTH);
        setSize(Global.SCREEN_X, Global.SCREEN_Y);
        Document doc;
        try {
            doc = DocumentHelper.parseText(Global.MENUS);
            Element root = doc.getRootElement();
            JMenuBar menuBar = new JMenuBar();
            traverseXML(root, menuBar);
            setJMenuBar(menuBar);
        } catch (DocumentException e) {
            System.out.println(e);
        }

        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setVisible(true);
    }

    /**
     * Traverse xml.
     * 
     * @param e the e
     * @param menuBar the menu bar
     */
    void traverseXML(Element e, JMenuBar menuBar) {
        JMenu menu = null;
        for (int i = 0; i < e.elements().size(); i++) {
            final Element e1 = (Element) e.elements().get(i);
            if (e1.attributeValue("class") != null) {

                if (!Global.moduleClasses.contains(e1.attributeValue("class"))) {
                    Global.moduleClasses.add(e1.attributeValue("class"));
                    Global.moduleCaptions.add(e1.attributeValue("text"));
                    Global.moduleIcons.add(e1.attributeValue("icon"));
                }
                String menucaption = e1.attributeValue("text");
                menucaption = processUnicodeUI(menucaption);
                if (menucaption.startsWith("$"))
                    menucaption = menucaption.replaceAll("Language", Global.selectedLanguage).substring(1);
                JMenuItem item = new JMenuItem(menucaption);
                String icon = e1.attributeValue("icon");
                if (icon != null)
                    item.setIcon(new ImageIcon("icons/" + Global.SELECTED_THEME + "/" + icon));
                if (e1.attributeValue("visible") != null
                        && e1.attributeValue("visible").equalsIgnoreCase("false")) {
                    item.setVisible(false);
                }
                if (e1.attributeValue("enable") != null && e1.attributeValue("enable").equalsIgnoreCase("false")) {
                    item.setEnabled(false);
                }
                String startup = e1.attributeValue("startup");
                if (startup == null)
                    startup = "false";
                item.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            Object o = Class.forName(e1.attributeValue("class")).newInstance();
                            JInternalFrame iFrame = ((IWindow) o).getFrame();
                            if (iFrame == null)
                                return;
                            iFrame.setFrameIcon(new ImageIcon(
                                    "icons/" + Global.SELECTED_THEME + "/" + e1.attributeValue("icon")));
                            //                     iFrame.getDesktopIcon().setUI(
                            //                           new MyDesktopIconUI(e1
                            //                                 .attributeValue("text"), iFrame
                            //                                 .getFrameIcon()));
                            iFrame.setIconifiable(true);

                            if (checkIfAlreadyOpen(iFrame)) {
                                iFrame.dispose();
                                return;
                            }

                            _desktop.add(iFrame);
                            iFrame.setSelected(true);
                            Global.setStatus(
                                    e1.attributeValue("text").replaceAll("Language", Global.selectedLanguage)
                                            + " loaded.");
                        } catch (ClassNotFoundException e1) {
                            System.out.println(e1);
                        } catch (InstantiationException e2) {
                            System.out.println(e2);
                        } catch (IllegalAccessException e3) {
                            System.out.println(e3);
                        } catch (PropertyVetoException e4) {
                            System.out.println(e4);
                        }
                    }
                });
                if (startup.equalsIgnoreCase("true"))
                    item.doClick();
                //            menu.add(item);
                //            String icon2 = e.attributeValue("icon");
                //            if (icon2 != null)
                //               menu.setIcon(new ImageIcon("icons/" + Global.SELECTED_THEME
                //                     + "/" + icon2));

                menuBar.add(item);
            } else {
                if (e1.attributeValue("text").equalsIgnoreCase("-"))
                    menuBar.add(new JSeparator());
                else {
                    String menucaption = e1.attributeValue("text");
                    menucaption = processUnicodeUI(menucaption);
                    if (menucaption.startsWith("$"))
                        menucaption = menucaption.replaceAll("Language", Global.selectedLanguage).substring(1);
                    menu = new JMenu(menucaption);
                    if (e1.attributeValue("visible") != null
                            && e1.attributeValue("visible").equalsIgnoreCase("false")) {
                        menu.setVisible(false);
                    }
                    if (e1.attributeValue("enable") != null
                            && e1.attributeValue("enable").equalsIgnoreCase("false")) {
                        menu.setEnabled(false);
                    }
                    traverseXML(e1, menu);
                    menuBar.add(menu);
                }
            }
        }

    }

    /**
     * Process unicode ui.
     * 
     * @param menucaption the menucaption
     * 
     * @return the string
     */
    private String processUnicodeUI(String menucaption) {
        if (Global.UiMenus != null && Global.useLangMenus)
            menucaption = Global.UiMenus.getProperty(menucaption, menucaption);
        return menucaption;
    }

    /**
     * Traverse xml.
     * 
     * @param e the e
     * @param menu the menu
     */
    void traverseXML(Element e, JMenu menu) {
        JMenu menu1 = null;
        for (int i = 0; i < e.elements().size(); i++) {
            final Element e1 = (Element) e.elements().get(i);
            if (e1.attributeValue("class") != null) {

                if (!Global.moduleClasses.contains(e1.attributeValue("class"))) {
                    Global.moduleClasses.add(e1.attributeValue("class"));
                    Global.moduleCaptions.add(e1.attributeValue("text"));
                    Global.moduleIcons.add(e1.attributeValue("icon"));
                }
                String menucaption = e1.attributeValue("text");
                menucaption = processUnicodeUI(menucaption);
                if (menucaption.startsWith("$"))
                    menucaption = menucaption.replaceAll("Language", Global.selectedLanguage).substring(1);
                JMenuItem item = new JMenuItem(menucaption);
                String icon = e1.attributeValue("icon");
                if (icon != null)
                    item.setIcon(new ImageIcon("icons/" + Global.SELECTED_THEME + "/" + icon));
                if (e1.attributeValue("visible") != null
                        && e1.attributeValue("visible").equalsIgnoreCase("false")) {
                    item.setVisible(false);
                }
                if (e1.attributeValue("enable") != null && e1.attributeValue("enable").equalsIgnoreCase("false")) {
                    item.setEnabled(false);
                }
                String startup = e1.attributeValue("startup");
                if (startup == null)
                    startup = "false";
                item.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            Object o = Class.forName(e1.attributeValue("class")).newInstance();

                            JInternalFrame iFrame = ((IWindow) o).getFrame();
                            if (iFrame == null)
                                return;
                            iFrame.setFrameIcon(new ImageIcon(
                                    "icons/" + Global.SELECTED_THEME + "/" + e1.attributeValue("icon")));
                            //                     iFrame.getDesktopIcon().setUI(
                            //                           new MyDesktopIconUI(e1
                            //                                 .attributeValue("text"), iFrame
                            //                                 .getFrameIcon()));
                            iFrame.setIconifiable(true);

                            if (checkIfAlreadyOpen(iFrame)) {
                                iFrame.dispose();
                                return;
                            }
                            _desktop.add(iFrame);
                            iFrame.setSelected(true);
                            Global.setStatus(
                                    e1.attributeValue("text").replaceAll("Language", Global.selectedLanguage)
                                            + " loaded.");
                        } catch (ClassNotFoundException e1) {
                            System.out.println(e1);
                        } catch (InstantiationException e2) {
                            System.out.println(e2);
                        } catch (IllegalAccessException e3) {
                            System.out.println(e3);
                        } catch (PropertyVetoException e4) {
                            System.out.println(e4);
                        }

                    }
                });
                if (startup.equalsIgnoreCase("true"))
                    item.doClick();
                menu.add(item);
                menu.add(item);
                String icon2 = e.attributeValue("icon");
                if (icon2 != null)
                    menu.setIcon(new ImageIcon("icons/" + Global.SELECTED_THEME + "/" + icon2));
            } else {
                if (e1.attributeValue("text").equalsIgnoreCase("-"))
                    menu.add(new JSeparator());
                else {
                    String menucaption = e1.attributeValue("text");
                    menucaption = processUnicodeUI(menucaption);
                    if (menucaption.startsWith("$"))
                        menucaption = menucaption.replaceAll("Language", Global.selectedLanguage).substring(1);
                    menu1 = new JMenu(menucaption);
                    if (e1.attributeValue("visible") != null
                            && e1.attributeValue("visible").equalsIgnoreCase("false")) {
                        menu1.setVisible(false);
                    }
                    if (e1.attributeValue("enable") != null
                            && e1.attributeValue("enable").equalsIgnoreCase("false")) {
                        menu1.setEnabled(false);
                    }
                    traverseXML(e1, menu1);
                    menu.add(menu1);
                }
            }
        }
    }

    /**
     * Check if already open.
     * 
     * @param newFrame the new frame
     * 
     * @return true, if successful
     */
    public boolean checkIfAlreadyOpen(JInternalFrame newFrame) {
        JInternalFrame[] frames = _desktop.getAllFrames();
        for (JInternalFrame frame : frames) {
            if (frame.getTitle().equalsIgnoreCase(newFrame.getTitle())) {
                try {
                    frame.setIcon(false);
                    frame.setSelected(true);
                    return true;
                } catch (PropertyVetoException e) {
                    log.error(e, e);
                }
            }
        }
        return false;
    }

    /**
     * The Class MyDesktopIconUI.
     */
    private static class MyDesktopIconUI extends BasicDesktopIconUI {

        /** The text label. */
        private JLabel textLabel;

        /** The icon label. */
        private JLabel iconLabel;

        /**
         * Instantiates a new my desktop icon ui.
         * 
         * @param text the text
         * @param icon the icon
         */
        public MyDesktopIconUI(String text, Icon icon) {
            textLabel = new JLabel(text);
            iconLabel = new JLabel(icon);
            iconLabel.setOpaque(false);
            textLabel.setOpaque(false);
        }

        /*
         * (non-Javadoc)
         * 
         * @see javax.swing.plaf.basic.BasicDesktopIconUI#installComponents()
         */
        protected void installComponents() {
            LookAndFeel.installBorder(desktopIcon, "");
            LookAndFeel.installProperty(desktopIcon, "opaque", Boolean.FALSE);
            desktopIcon.setLayout(new BorderLayout());
            desktopIcon.add(iconLabel, BorderLayout.NORTH);
            desktopIcon.add(textLabel, BorderLayout.SOUTH);
        }

        /*
         * (non-Javadoc)
         * 
         * @see javax.swing.plaf.basic.BasicDesktopIconUI#uninstallComponents()
         */
        protected void uninstallComponents() {
            desktopIcon.remove(textLabel);
            desktopIcon.remove(iconLabel);
            desktopIcon.setLayout(null);
        }
    }
}