Java tutorial
/* * Church Software * * Package : com.plugin.UI.Windows.Settings * Filename: ThemeSelection.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.Settings; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Properties; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JToolBar; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.church.gateway.Global; import com.church.tools.desktop.BaseInternalFrame; import com.plugin.UI.Windows.DesktopUI; import com.plugin.UI.Windows.IWindow; /** * The Class ThemeSelection. */ public class ThemeSelection implements IWindow { /** The log. */ Logger log = Logger.getLogger(this.getClass()); /** The _ title. */ String _Title = "Theme Selection"; /** The frame. */ protected BaseInternalFrame frame = null; /* * (non-Javadoc) * * @see com.plugin.UI.Windows.IWindow#getFrame() */ public BaseInternalFrame getFrame() { return frame; } /** The radio buttons. */ JRadioButton[] radioButtons = null; /** The theme. */ String[] theme = { "Toolkit Theme n.a" }; /** The theme_class. */ String[] theme_class = { "" }; /** * Instantiates a new theme selection. */ public ThemeSelection() { String xml = Global.THEMEXML; parseThemeXML(xml.trim()); int selected_idx = 1; try { FileInputStream fis = new FileInputStream("theme.properties"); Properties p = new Properties(); p.load(fis); selected_idx = Integer.parseInt(p.getProperty("class")); Global.THEME_SELECTION = selected_idx; fis.close(); } catch (NumberFormatException e5) { // TODO Auto-generated catch block System.out.println(e5); selected_idx = 1; } catch (FileNotFoundException e5) { // TODO Auto-generated catch block System.out.println(e5); selected_idx = 1; } catch (IOException e5) { // TODO Auto-generated catch block System.out.println(e5); selected_idx = 1; } frame = new BaseInternalFrame(_Title); // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(0, 0); frame.setClosable(true); frame.getContentPane().setLayout(new BorderLayout()); JLabel label = new JLabel("Theme Selection"); JButton preview = new JButton("Preview"); JButton ok = new JButton("Ok"); JButton cancel = new JButton("Cancel"); frame.getContentPane().add(label, BorderLayout.NORTH); JPanel jp = new JPanel(new GridLayout(theme.length, 1)); final ButtonGroup group = new ButtonGroup(); radioButtons = new JRadioButton[theme.length]; for (int i = 0; i < (theme.length); i++) { radioButtons[i] = new JRadioButton(theme[i]); group.add(radioButtons[i]); jp.add(radioButtons[i]); } radioButtons[selected_idx].setSelected(true); JScrollPane jsp = new JScrollPane(jp); jsp.setPreferredSize(new Dimension(150, 150)); jsp.setBorder(BorderFactory.createEmptyBorder()); frame.getContentPane().add(jsp, BorderLayout.CENTER); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { UIManager.setLookAndFeel(theme_class[Global.THEME_SELECTION]); updateAllUIs(); frame.dispose(); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block log.error(e1, e1); } catch (InstantiationException e1) { // TODO Auto-generated catch block log.error(e1, e1); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block log.error(e1, e1); } catch (UnsupportedLookAndFeelException e1) { // TODO Auto-generated catch block log.error(e1, e1); } } }); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { int theme_class_idx = 0; for (int i = 0; i < radioButtons.length; i++) { if (radioButtons[i].isSelected()) { UIManager.setLookAndFeel(theme_class[i]); theme_class_idx = i; Global.THEME_SELECTION = theme_class_idx; JOptionPane.showMessageDialog(DesktopUI._desktop, "Theme successfully changed!"); break; } } FileOutputStream fos = new FileOutputStream("theme.properties"); Properties p = new Properties(); p.setProperty("class", Integer.toString(theme_class_idx)); p.save(fos, "Toolkit Theme Properties"); fos.close(); updateAllUIs(); frame.dispose(); } catch (ClassNotFoundException e1) { System.out.println(e1); } catch (InstantiationException e2) { System.out.println(e2); } catch (IllegalAccessException e3) { System.out.println(e3); } catch (UnsupportedLookAndFeelException e4) { System.out.println(e4); } catch (FileNotFoundException e1) { System.out.println(e1); } catch (IOException e1) { System.out.println(e1); } } }); preview.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { int theme_class_idx = 0; for (int i = 0; i < radioButtons.length; i++) { if (radioButtons[i].isSelected()) { UIManager.setLookAndFeel(theme_class[i]); theme_class_idx = i; break; } } updateAllUIs(); } catch (ClassNotFoundException e1) { System.out.println(e1); } catch (InstantiationException e2) { System.out.println(e2); } catch (IllegalAccessException e3) { System.out.println(e3); } catch (UnsupportedLookAndFeelException e4) { System.out.println(e4); } } }); JPanel jj = new JPanel(); jj.add(preview); jj.add(ok); jj.add(cancel); frame.getContentPane().add(jj, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } /** * Parses the theme xml. * * @param xml the xml */ private void parseThemeXML(String xml) { try { Document doc = DocumentHelper.parseText(xml); Element elmt = doc.getRootElement(); List l = elmt.selectNodes("/themes/theme"); Element e = null; theme = new String[l.size()]; theme_class = new String[l.size()]; for (int i = 0; i < l.size(); i++) { e = (Element) l.get(i); theme[i] = e.attribute("name").getValue(); theme_class[i] = e.attribute("class").getValue(); } } catch (DocumentException e) { System.out.println(e); } } /** * Update all u is. */ public void updateAllUIs() { Frame frames[]; int i1; frames = Frame.getFrames(); i1 = 0; for (int i = 0; i < frames.length; i++) { updateWindowUI(frames[i]); } } /** * Update window ui. * * @param window the window */ public void updateWindowUI(Window window) { try { updateComponentTreeUI(window); } catch (Exception exception) { } Window windows[] = window.getOwnedWindows(); for (int i = 0; i < windows.length; i++) updateWindowUI(windows[i]); } /** * Update component tree ui. * * @param c the c */ public void updateComponentTreeUI(Component c) { updateComponentTreeUI0(c); c.invalidate(); c.validate(); c.repaint(); } /** * Update component tree u i0. * * @param c the c */ private void updateComponentTreeUI0(Component c) { Component[] children = null; if (c instanceof JToolBar) { children = ((JToolBar) c).getComponents(); if (children != null) { for (int i = 0; i < children.length; i++) { updateComponentTreeUI0(children[i]); } } ((JComponent) c).updateUI(); } else { if (c instanceof JComponent) { ((JComponent) c).updateUI(); } if (c instanceof JMenu) { children = ((JMenu) c).getMenuComponents(); } else if (c instanceof Container) { children = ((Container) c).getComponents(); } if (children != null) { for (int i = 0; i < children.length; i++) { updateComponentTreeUI0(children[i]); } } } } }