[jsword-devel] Look and Feel Switching

DM Smith jsword-devel@crosswire.org
Tue, 09 Mar 2004 13:03:34 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_53e1_df7_4fec
Content-Type: text/plain; format=flowed

Here are changes to LookAndFeelUtil that implement delayed (startup only) 
laf switching (patch attached, also contains changes to remove calls to 
deleted methods in other classes):

public class LookAndFeelUtil {
    /**
     * Prevent Instansiation
     */
    private LookAndFeelUtil() {
    }

    /**
     * The Options customization
     */
    public static Class getLookAndFeel() {
        return current;
    }

    /**
     * The Options customization
     */
    public static void setLookAndFeel(Class new_class)
        throws InstantiationException, IllegalAccessException {
        try {
            // new_class is null if the user enters a bogus value
            LookAndFeel laf = (LookAndFeel) new_class.newInstance();
            if (current != null && !new_class.equals(current)) {
                JOptionPane.showMessageDialog(null,
	    "The Look and Feel will change on the next startup.");
            } else {
                UIManager.setLookAndFeel(laf);
            }
            current = new_class;
        } catch (Exception ex) {
            throw new IllegalArgumentException("Invalid Look and Feel 
name");
        }
    }

    /**
     * Hack the windows look and feel to make the fonts more readable on 
bigger
     * screens.
     */
    public static void tweakLookAndFeel() {
        try {
            System.setProperty("winlaf.forceTahoma", "true");
            Class clazz = 
Class.forName("net.java.plaf.LookAndFeelPatchManager");
            Method init = clazz.getMethod("initialize", new Class[0]);
            init.invoke(null, new Object[0]);
            log.debug("installed Windows LookAndFeelPatchManager");
        } catch (Exception ex) {
            log.warn("Failed to install Windows laf tweak tool: " + ex);
        }
    }

    /**
     * The current PLAF (and the default value)
     */
    private static Class current;

    /**
     * The log stream
     */
    private static final Logger log = 
Logger.getLogger(LookAndFeelUtil.class);

    // Set the Look And Feel to the system default
    static {
        try {
            String lafClassName = UIManager.getSystemLookAndFeelClassName();
            current = Class.forName(lafClassName);
            UIManager.setLookAndFeel(lafClassName);
        } catch (Exception e) {
            log.debug("Could not set the System Look and Feel");
        }
    }
}

_________________________________________________________________
Create a Job Alert on MSN Careers and enter for a chance to win $1000! 
http://msn.careerbuilder.com/promo/kaday.htm?siteid=CBMSN_1K&sc_extcmp=JS_JASweep_MSNHotm2

------=_NextPart_000_53e1_df7_4fec
Content-Type: text/plain; name="patchlaf.txt"; format=flowed
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="patchlaf.txt"

Index: 
java/common/org/crosswire/common/config/swing/AbstractConfigEditor.java
===================================================================
retrieving revision 1.1
diff -u -r1.1 AbstractConfigEditor.java
--- 
java/common/org/crosswire/common/config/swing/AbstractConfigEditor.java	30 
Jan 2004 18:28:24 -0000	1.1
+++ 
java/common/org/crosswire/common/config/swing/AbstractConfigEditor.java	9 
Mar 2004 17:53:03 -0000
@@ -26,7 +26,6 @@
import org.crosswire.common.swing.EdgeBorder;
import org.crosswire.common.swing.FormPane;
import org.crosswire.common.swing.GuiUtil;
-import org.crosswire.common.swing.LookAndFeelUtil;
import org.crosswire.common.util.Logger;
import org.crosswire.common.util.Reporter;

@@ -104,7 +103,6 @@
         if (dialog == null)
         {
             dialog = new JDialog((JFrame) SwingUtilities.getRoot(parent));
-            LookAndFeelUtil.addComponentToUpdate(dialog);

             dialog.getRootPane().setDefaultButton(ok);
             dialog.getContentPane().add(this);
@@ -288,7 +286,6 @@
     {
         if (dialog != null)
         {
-            LookAndFeelUtil.removeComponentToUpdate(dialog);
             dialog.setVisible(false);
         }
     }
Index: java/common/org/crosswire/common/config/swing/WizardConfigEditor.java
===================================================================
retrieving revision 1.1
diff -u -r1.1 WizardConfigEditor.java
--- java/common/org/crosswire/common/config/swing/WizardConfigEditor.java	30 
Jan 2004 18:28:24 -0000	1.1
+++ java/common/org/crosswire/common/config/swing/WizardConfigEditor.java	9 
Mar 2004 17:53:07 -0000
@@ -27,7 +27,6 @@
import org.apache.commons.lang.StringUtils;
import org.crosswire.common.swing.EdgeBorder;
import org.crosswire.common.swing.FormPane;
-import org.crosswire.common.swing.LookAndFeelUtil;
import org.crosswire.common.util.Logger;
import org.crosswire.common.util.Reporter;

@@ -252,7 +251,6 @@
     public void showDialog(Component parent)
     {
         dialog = new JDialog((JFrame) SwingUtilities.getRoot(parent));
-        LookAndFeelUtil.addComponentToUpdate(dialog);

         dialog.getRootPane().setDefaultButton(apply);
         dialog.getContentPane().add(this);
Index: java/common/org/crosswire/common/swing/LookAndFeelUtil.java
===================================================================
retrieving revision 1.15
diff -u -r1.15 LookAndFeelUtil.java
--- java/common/org/crosswire/common/swing/LookAndFeelUtil.java	5 Feb 2004 
23:26:20 -0000	1.15
+++ java/common/org/crosswire/common/swing/LookAndFeelUtil.java	9 Mar 2004 
17:53:11 -0000
@@ -1,229 +1,104 @@
package org.crosswire.common.swing;
-
-import java.awt.Component;
-import java.awt.Window;
import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
+import javax.swing.JOptionPane;
import javax.swing.LookAndFeel;
-import javax.swing.SwingUtilities;
import javax.swing.UIManager;
-
import org.crosswire.common.util.Logger;
-
/**
- * LookAndFeelUtil declares the Choices and actions
- * needed to dynamically change the look and feel (PLAF) and to add new
- * PLAFs without needing to restart.
- *
- * <p><table border='1' cellPadding='3' cellSpacing='0'>
- * <tr><td bgColor='white' class='TableRowColor'><font size='-7'>
- *
- * Distribution Licence:<br />
- * JSword is free software; you can redistribute it
- * and/or modify it under the terms of the GNU General Public License,
- * version 2 as published by the Free Software Foundation.<br />
- * 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.<br />
- * The License is available on the internet
- * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, or by writing 
to:
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
- * MA 02111-1307, USA<br />
- * The copyright to this program is held by it's authors.
- * </font></td></tr></table>
+ * LookAndFeelUtil declares the Choices and actions needed to dynamically
+ * change the look and feel (PLAF) and to add new PLAFs without needing to
+ * restart.
+ *
+ * <p>
+ * <table border='1' cellPadding='3' cellSpacing='0'>
+ * <tr>
+ * <td bgColor='white' class='TableRowColor'><font size='-7'>
+ *
+ * Distribution Licence: <br />
+ * JSword is free software; you can redistribute it and/or modify it under 
the
+ * terms of the GNU General Public License, version 2 as published by the 
Free
+ * Software Foundation. <br />
+ * 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. <br />
+ * The License is available on the internet <a
+ * href='http://www.gnu.org/copyleft/gpl.html'>here </a>, or by writing to:
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA <br />
+ * The copyright to this program is held by it's authors. </font></td>
+ * </tr>
+ * </table>
+ *
  * @see gnu.gpl.Licence
  * @author Joe Walker [joe at eireneh dot com]
  * @author Mark Goodwin [mark at thorubio dot org]
+ * @author DM Smith [dmsmith555 at hotmail dot com]
  * @version $Id: LookAndFeelUtil.java,v 1.15 2004/02/05 23:26:20 joe Exp $
  */
-public class LookAndFeelUtil
-{
-    /**
-     * Prevent Instansiation
-     */
-    private LookAndFeelUtil()
-    {
-    }
-
-    /**
-     * The Options customization
-     */
-    public static Class getLookAndFeel()
-    {
-        return current;
-    }
-
-    /**
-     * The Options customization
-     */
-    public static void setLookAndFeel(Class new_class) throws 
InstantiationException, IllegalAccessException
-    {
-        current = new_class;
-
-        LookAndFeel laf = (LookAndFeel) current.newInstance();
-        setLookAndFeel(laf);
-    }
-
-    /**
-     * Make the specified PLAF the current
-     * @param plaf The PLAF to install
-     */
-    public static void setLookAndFeel(LookAndFeel plaf)
-    {
-        // The global new setting
-        try
-        {
-            UIManager.setLookAndFeel(plaf);
-        }
-        catch (Exception ex)
-        {
-            throw new IllegalArgumentException("Invalid Look and Feel 
name");
-        }
-
-        updateComponents();
-    }
-
-    /**
-     * Make all the windows fall into line with the current look
-     */
-    public static void updateComponents()
-    {
-        // Re-jig all the frames
-        Iterator it = windows.iterator();
-        while (it.hasNext())
-        {
-            Component comp = (Component) it.next();
-            SwingUtilities.updateComponentTreeUI(comp);
-
-            if (comp instanceof Window)
-            {
-                GuiUtil.restrainedRePack((Window) comp);
-            }
-        }
-    }
-
-    /**
-     * Add a Component to the list that need to be updated when the L&F 
changes.
-     * In general you will only need to add Windows to this list because 
the
-     * changes recurse down the hierachy, however if you have Components 
that
-     * are temporarily (whenever a L&F change could occur) not part of this
-     * hierachy then add them in also so they get updated with everything 
else.
-     * when the PLAF changes.
-     * @param comp The Component to be registered
-     */
-    public static void addComponentToUpdate(Component comp)
-    {
-        // Should we add ourselves as a ComponentListener?
-        // Probably not. Knowning what is registered may
-        // then be complex.
-
-        windows.add(comp);
-        SwingUtilities.updateComponentTreeUI(comp);
-        // window.addContainerListener(new CustomContainerListener());
-    }
-
-    /**
-     * Remove a Frame from the list that need to be updated
-     * when the PLAF changes.
-     * @param comp The Component to be de-registered
-     */
-    public static void removeComponentToUpdate(Component comp)
-    {
-        windows.remove(comp);
-    }
-
-    /**
-     * Hack the windows look and feel to make the fonts more readable on
-     * bigger screens.
-     */
-    public static void tweakLookAndFeel()
-    {
-        try
-        {
-            System.setProperty("winlaf.forceTahoma", "true");
-            Class clazz = 
Class.forName("net.java.plaf.LookAndFeelPatchManager");
-            Method init = clazz.getMethod("initialize", new Class[0]);
-            init.invoke(null, new Object[0]);
-
-            log.debug("installed LookAndFeelPatchManager");
-        }
-        catch (Exception ex)
-        {
-            log.warn("Failed to install windows laf tweak tool: "+ex);
-        }
-
-        LookAndFeel currentlnf = UIManager.getLookAndFeel();
-        if 
(currentlnf.getClass().getName().equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"))
-        {
-            /*
-            UIDefaults defaults = UIManager.getDefaults();
-            Font menufont = defaults.getFont("Menu.font");
-
-            defaults.put("ProgressBar.font", menufont);
-            defaults.put("ToggleButton.font", menufont);
-            defaults.put("Panel.font", menufont);
-            defaults.put("TableHeader.font", menufont);
-            defaults.put("TextField.font", menufont);
-            defaults.put("Button.font", menufont);
-            defaults.put("Label.font", menufont);
-            defaults.put("ScrollPane.font", menufont);
-            defaults.put("List.font", menufont);
-            defaults.put("EditorPane.font", menufont);
-            defaults.put("Table.font", menufont);
-            defaults.put("TabbedPane.font", menufont);
-            defaults.put("RadioButton.font", menufont);
-            defaults.put("TextPane.font", menufont);
-            defaults.put("TitledBorder.font", menufont);
-            defaults.put("ComboBox.font", menufont);
-            defaults.put("CheckBox.font", menufont);
-            defaults.put("Tree.font", menufont);
-            defaults.put("Viewport.font", menufont);
-            */
-        }
-    }
-
-    /**
-     * The frames to update
-     */
-    private static transient List windows = new ArrayList();
-
-    /**
-     * The current PLAF (and the default value)
-     */
-    private static Class current;
-
-    /**
-     * The log stream
-     */
-    private static final Logger log = 
Logger.getLogger(LookAndFeelUtil.class);
-
-    /**
-     * Setup the defaults Hashtable
-     */
-    static
-    {
-        // try to set the default look and feel to the system default
-        try
-        {
-            current = 
Class.forName(UIManager.getSystemLookAndFeelClassName());
-        }
-        catch (ClassNotFoundException ex)
-        {
-            log.warn("Failed to initialise system default LAF", ex);
-            current = javax.swing.plaf.metal.MetalLookAndFeel.class;
-        }
-
-        /*
-        Class[] impls = 
Project.resource().getImplementors(LookAndFeel.class);
-        for (int i=0; i<impls.length; i++)
-        {
-            LookAndFeel lnf = (LookAndFeel) impls[i].newInstance();
-            defaults.put(lnf.getName(), impls[i].getName());
-        }
-        */
-    }
+public class LookAndFeelUtil {
+	/**
+	 * Prevent Instansiation
+	 */
+	private LookAndFeelUtil() {
+	}
+	/**
+	 * The Options customization
+	 */
+	public static Class getLookAndFeel() {
+		return current;
+	}
+	/**
+	 * The Options customization
+	 */
+	public static void setLookAndFeel(Class new_class)
+			throws InstantiationException, IllegalAccessException {
+		try {
+			// new_class is null if the user enters a bogus value
+			LookAndFeel laf = (LookAndFeel) new_class.newInstance();
+			if (current != null && !new_class.equals(current)) {
+				JOptionPane.showMessageDialog(null,
+						"The Look and Feel will change on the next startup.");
+			} else {
+				UIManager.setLookAndFeel(laf);
+			}
+			current = new_class;
+		} catch (Exception ex) {
+			throw new IllegalArgumentException("Invalid Look and Feel name");
+		}
+	}
+	/**
+	 * Hack the windows look and feel to make the fonts more readable on 
bigger
+	 * screens.
+	 */
+	public static void tweakLookAndFeel() {
+		try {
+			System.setProperty("winlaf.forceTahoma", "true");
+			Class clazz = Class.forName("net.java.plaf.LookAndFeelPatchManager");
+			Method init = clazz.getMethod("initialize", new Class[0]);
+			init.invoke(null, new Object[0]);
+			log.debug("installed Windows LookAndFeelPatchManager");
+		} catch (Exception ex) {
+			log.warn("Failed to install Windows laf tweak tool: " + ex);
+		}
+	}
+	/**
+	 * The current PLAF (and the default value)
+	 */
+	private static Class current;
+	/**
+	 * The log stream
+	 */
+	private static final Logger log = Logger.getLogger(LookAndFeelUtil.class);
+
+    // Set the Look And Feel to the system default
+    static {
+		try {
+			String lafClassName = UIManager.getSystemLookAndFeelClassName();
+			current = Class.forName(lafClassName);
+			UIManager.setLookAndFeel(lafClassName);
+		} catch (Exception e) {
+			log.debug("Could not set the System Look and Feel");
+		}
+	}
}
Index: java/map/org/crosswire/jsword/map/view/Mapper.java
===================================================================
retrieving revision 1.22
diff -u -r1.22 Mapper.java
--- java/map/org/crosswire/jsword/map/view/Mapper.java	8 Feb 2004 23:03:23 
-0000	1.22
+++ java/map/org/crosswire/jsword/map/view/Mapper.java	9 Mar 2004 17:53:23 
-0000
@@ -38,7 +38,6 @@
import org.crosswire.common.swing.ExceptionPane;
import org.crosswire.common.swing.ExtensionFileFilter;
import org.crosswire.common.swing.GuiUtil;
-import org.crosswire.common.swing.LookAndFeelUtil;
import org.crosswire.common.util.Reporter;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookFilters;
@@ -143,7 +142,6 @@

         try
         {
-            LookAndFeelUtil.addComponentToUpdate(this);

             Document xmlconfig = Project.instance().getDocument("config");
             config.add(xmlconfig);
Index: 
java/swing/org/crosswire/jsword/view/swing/book/TabbedDisplayPane.java
===================================================================
retrieving revision 1.15
diff -u -r1.15 TabbedDisplayPane.java
--- java/swing/org/crosswire/jsword/view/swing/book/TabbedDisplayPane.java	8 
Feb 2004 23:03:22 -0000	1.15
+++ java/swing/org/crosswire/jsword/view/swing/book/TabbedDisplayPane.java	9 
Mar 2004 17:53:27 -0000
@@ -15,7 +15,6 @@
import javax.swing.event.HyperlinkListener;
import javax.xml.transform.TransformerException;

-import org.crosswire.common.swing.LookAndFeelUtil;
import org.crosswire.common.util.LogicError;
import org.crosswire.common.util.Reporter;
import org.crosswire.jsword.book.Book;
@@ -58,13 +57,6 @@
         idps.add(pnlView);

         initialize();
-
-        // There are times when tab_main or pnl_view are not in visible or
-        // attached to the main widget hierachy, so when we change L&F the
-        // changes do not get propogated through. The solution is to 
register
-        // them with the L&F handler to be altered when the L&F changes.
-        LookAndFeelUtil.addComponentToUpdate(pnlView);
-        LookAndFeelUtil.addComponentToUpdate(tabMain);
     }

     /**
Index: java/swing/org/crosswire/jsword/view/swing/desktop/Desktop.java
===================================================================
retrieving revision 1.41
diff -u -r1.41 Desktop.java
--- java/swing/org/crosswire/jsword/view/swing/desktop/Desktop.java	8 Feb 
2004 23:03:22 -0000	1.41
+++ java/swing/org/crosswire/jsword/view/swing/desktop/Desktop.java	9 Mar 
2004 17:53:27 -0000
@@ -182,7 +182,6 @@

         // Configuration
         startJob.setProgress("General configuration");
-        LookAndFeelUtil.addComponentToUpdate(frame);

         // Keep track of the selected DisplayArea
         FocusManager.getCurrentManager().addPropertyChangeListener(new 
PropertyChangeListener()
Index: java/swing/org/crosswire/jsword/view/swing/desktop/TDIViewLayout.java
===================================================================
retrieving revision 1.7
diff -u -r1.7 TDIViewLayout.java
--- java/swing/org/crosswire/jsword/view/swing/desktop/TDIViewLayout.java	30 
Aug 2003 22:14:08 -0000	1.7
+++ java/swing/org/crosswire/jsword/view/swing/desktop/TDIViewLayout.java	9 
Mar 2004 17:53:30 -0000
@@ -8,7 +8,6 @@
import javax.swing.BorderFactory;
import javax.swing.JTabbedPane;

-import org.crosswire.common.swing.LookAndFeelUtil;
import org.crosswire.jsword.view.swing.book.BibleViewPane;

/**
@@ -43,7 +42,6 @@
     public TDIViewLayout()
     {
         tab_main.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
-        LookAndFeelUtil.addComponentToUpdate(tab_main);
     }

     /* (non-Javadoc)


------=_NextPart_000_53e1_df7_4fec--