001    /*
002     * $Id: UIManagerUtils.java,v 1.4 2005/10/26 11:44:30 kleopatra Exp $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    package org.jdesktop.swingx.util;
022    
023    import java.awt.Font;
024    import java.lang.reflect.Method;
025    import java.util.logging.Level;
026    import java.util.logging.Logger;
027    
028    import javax.swing.JComponent;
029    import javax.swing.JEditorPane;
030    import javax.swing.UIManager;
031    import javax.swing.plaf.metal.MetalLookAndFeel;
032    import javax.swing.text.View;
033    import javax.swing.text.html.HTMLDocument;
034    
035    /**
036     * Utility for working with the UIManager
037     * @author Richard Bair
038     */
039    // PENDING: JW - is this still used? Yes, by BasicTipOfTheDayUI
040    public final class UIManagerUtils {
041        
042        private static final Logger LOG = Logger.getLogger(UIManagerUtils.class
043                .getName());
044            /**
045         * Hidden constructor
046         */
047        private UIManagerUtils() {
048        }
049        
050        /**
051         * Initializes the object in the UIDefaults denoted by 'key' to defaultObj <strong>only if</strong>
052         * the key is not already in the UIDefaults.
053         * @param key
054         * @param defaultObj
055         */
056        public static void initDefault(String key, Object defaultObj) {
057            Object obj = UIManager.get(key);
058            if (obj == null) {
059                UIManager.put(key, defaultObj);
060            }
061        }
062    
063        /**
064         * Initializes the object in the UIDefaults denoted by 'key' to either the property in the metal look and feel
065         * associated with defaultMetalObjName, or the defaultObj if all else fails.
066         * @param key
067         * @param defaultMetalObjName
068         * @param defaultObj
069         */
070        public static void initDefault(String key, String defaultMetalObjName, Object defaultObj) {
071            Object obj = UIManager.get(key);
072            if (obj == null) {
073                try {
074                    Method m = ((MetalLookAndFeel)UIManager.getLookAndFeel()).getClass().getMethod(defaultMetalObjName, defaultObj.getClass());
075                    UIManager.put(key, m.invoke(UIManager.getLookAndFeel(), defaultMetalObjName));
076                } catch (Exception e) {
077                    UIManager.put(key, defaultObj);
078                }
079            }
080        }
081        
082      /**
083       * Forces the given component to use the given font for its html rendering.
084       * Text must have been set before calling this method.
085       * 
086       * @param component
087       * @param font
088       */
089      public static void htmlize(JComponent component, Font font) {    
090        String stylesheet = "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
091          + font.getName()
092          + "; font-size: "
093          + font.getSize()
094          + "pt;  }"
095          + "a, p, li { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
096          + font.getName()
097          + "; font-size: "
098          + font.getSize()
099          + "pt;  }";
100    
101        try {
102          HTMLDocument doc = null;
103          if (component instanceof JEditorPane) {
104            if (((JEditorPane)component).getDocument() instanceof HTMLDocument) {
105              doc = (HTMLDocument)((JEditorPane)component).getDocument();
106            }
107          } else {
108            View v = (View)component
109              .getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
110            if (v != null && v.getDocument() instanceof HTMLDocument) {
111              doc = (HTMLDocument)v.getDocument();
112            }
113          }
114          if (doc != null) {
115            doc.getStyleSheet().loadRules(new java.io.StringReader(stylesheet),
116              null);
117          } // end of if (doc != null)
118        } catch (Exception e) {
119            // TODO change to something meaningful JW - when do we expect this to happen?
120            LOG.log(Level.WARNING, "", e);
121    
122        }
123      }
124    }