UIManagerUtils.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: UIManagerUtils.java,v 1.3 2005/10/10 18:02:57 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.util;
  22. import java.awt.Font;
  23. import java.lang.reflect.Method;
  24. import javax.swing.JComponent;
  25. import javax.swing.JEditorPane;
  26. import javax.swing.UIManager;
  27. import javax.swing.plaf.metal.MetalLookAndFeel;
  28. import javax.swing.text.View;
  29. import javax.swing.text.html.HTMLDocument;
  30. /**
  31.  * Utility for working with the UIManager
  32.  * @author Richard Bair
  33.  */
  34. public final class UIManagerUtils {
  35. /**
  36.  * Hidden constructor
  37.  */
  38. private UIManagerUtils() {
  39. }
  40. /**
  41.  * Initializes the object in the UIDefaults denoted by 'key' to defaultObj <strong>only if</strong>
  42.  * the key is not already in the UIDefaults.
  43.  * @param key
  44.  * @param defaultObj
  45.  */
  46. public static void initDefault(String key, Object defaultObj) {
  47. Object obj = UIManager.get(key);
  48. if (obj == null) {
  49. UIManager.put(key, defaultObj);
  50. }
  51. }
  52. /**
  53.  * Initializes the object in the UIDefaults denoted by 'key' to either the property in the metal look and feel
  54.  * associated with defaultMetalObjName, or the defaultObj if all else fails.
  55.  * @param key
  56.  * @param defaultMetalObjName
  57.  * @param defaultObj
  58.  */
  59. public static void initDefault(String key, String defaultMetalObjName, Object defaultObj) {
  60. Object obj = UIManager.get(key);
  61. if (obj == null) {
  62. try {
  63.                 Method m = ((MetalLookAndFeel)UIManager.getLookAndFeel()).getClass().getMethod(defaultMetalObjName, defaultObj.getClass());
  64.                 UIManager.put(key, m.invoke(UIManager.getLookAndFeel(), defaultMetalObjName));
  65. } catch (Exception e) {
  66. UIManager.put(key, defaultObj);
  67. }
  68. }
  69. }
  70.     
  71.   /**
  72.    * Forces the given component to use the given font for its html rendering.
  73.    * Text must have been set before calling this method.
  74.    * 
  75.    * @param component
  76.    * @param font
  77.    */
  78.   public static void htmlize(JComponent component, Font font) {    
  79.     String stylesheet = "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
  80.       + font.getName()
  81.       + "; font-size: "
  82.       + font.getSize()
  83.       + "pt;  }"
  84.       + "a, p, li { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
  85.       + font.getName()
  86.       + "; font-size: "
  87.       + font.getSize()
  88.       + "pt;  }";
  89.     try {
  90.       HTMLDocument doc = null;
  91.       if (component instanceof JEditorPane) {
  92.         if (((JEditorPane)component).getDocument() instanceof HTMLDocument) {
  93.           doc = (HTMLDocument)((JEditorPane)component).getDocument();
  94.         }
  95.       } else {
  96.         View v = (View)component
  97.           .getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
  98.         if (v != null && v.getDocument() instanceof HTMLDocument) {
  99.           doc = (HTMLDocument)v.getDocument();
  100.         }
  101.       }
  102.       if (doc != null) {
  103.         doc.getStyleSheet().loadRules(new java.io.StringReader(stylesheet),
  104.           null);
  105.       } // end of if (doc != null)
  106.     } catch (Exception e) {
  107.       e.printStackTrace();
  108.     }
  109.   }
  110. }