stylesheet.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 938 行 · 第 1/2 页

JAVA
938
字号
/* StyleSheet.java --    Copyright (C) 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.text.html;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.io.IOException;import java.io.Reader;import java.io.Serializable;import java.io.StringReader;import java.net.MalformedURLException;import java.net.URL;import java.util.Enumeration;import java.util.Vector;import javax.swing.text.AttributeSet;import javax.swing.text.Element;import javax.swing.text.MutableAttributeSet;import javax.swing.text.SimpleAttributeSet;import javax.swing.text.Style;import javax.swing.text.StyleContext;import javax.swing.text.View;/** * This class adds support for defining the visual characteristics of HTML views * being rendered. This enables views to be customized by a look-and-feel, mulitple * views over the same model can be rendered differently. Each EditorPane has its  * own StyleSheet, but by default one sheet will be shared by all of the HTMLEditorKit * instances. An HTMLDocument can also have a StyleSheet, which holds specific CSS * specs.  *  *  In order for Views to store less state and therefore be more lightweight,  *  the StyleSheet can act as a factory for painters that handle some of the  *  rendering tasks. Since the StyleSheet may be used by views over multiple *  documents the HTML attributes don't effect the selector being used. *   *  The rules are stored as named styles, and other information is stored to  *  translate the context of an element to a rule. *  * @author Lillian Angel (langel@redhat.com) */public class StyleSheet extends StyleContext{  /** The base URL */  URL base;    /** Base font size (int) */  int baseFontSize;    /** The style sheets stored. */  StyleSheet[] styleSheet;    /**   * Constructs a StyleSheet.   */  public StyleSheet()  {    super();    baseFontSize = 4; // Default font size from CSS  }  /**   * Gets the style used to render the given tag. The element represents the tag   * and can be used to determine the nesting, where the attributes will differ   * if there is nesting inside of elements.   *    * @param t - the tag to translate to visual attributes   * @param e - the element representing the tag   * @return the set of CSS attributes to use to render the tag.   */  public Style getRule(HTML.Tag t, Element e)  {    // FIXME: Not implemented.    return null;  }    /**   * Gets the rule that best matches the selector. selector is a space   * separated String of element names. The attributes of the returned    * Style will change as rules are added and removed.   *    * @param selector - the element names separated by spaces   * @return the set of CSS attributes to use to render   */  public Style getRule(String selector)  {    // FIXME: Not implemented.    return null;   }    /**   * Adds a set if rules to the sheet. The rules are expected to be in valid   * CSS format. This is called as a result of parsing a <style> tag   *    * @param rule - the rule to add to the sheet   */  public void addRule(String rule)  {    CssParser cp = new CssParser();    try    {      cp.parse(base, new StringReader(rule), false, false);    }    catch (IOException io)    {      // Do nothing here.    }  }    /**   * Translates a CSS declaration into an AttributeSet. This is called   * as a result of encountering an HTML style attribute.   *    * @param decl - the declaration to get   * @return the AttributeSet representing the declaration   */  public AttributeSet getDeclaration(String decl)  {    if (decl == null)      return SimpleAttributeSet.EMPTY;    // FIXME: Not implemented.    return null;       }    /**   * Loads a set of rules that have been specified in terms of CSS grammar.   * If there are any conflicts with existing rules, the new rule is added.   *    * @param in - the stream to read the CSS grammar from.   * @param ref - the reference URL. It is the location of the stream, it may   * be null. All relative URLs specified in the stream will be based upon this   * parameter.   * @throws IOException - For any IO error while reading   */  public void loadRules(Reader in, URL ref) throws IOException  {    CssParser cp = new CssParser();    cp.parse(ref, in, false, false);  }    /**   * Gets a set of attributes to use in the view. This is a set of   * attributes that can be used for View.getAttributes   *    * @param v - the view to get the set for   * @return the AttributeSet to use in the view.   */  public AttributeSet getViewAttributes(View v)  {    // FIXME: Not implemented.    return null;  }    /**   * Removes a style previously added.   *    * @param nm - the name of the style to remove   */  public void removeStyle(String nm)  {    // FIXME: Not implemented.    super.removeStyle(nm);  }    /**   * Adds the rules from ss to those of the receiver. ss's rules will   * override the old rules. An added StyleSheet will never override the rules   * of the receiving style sheet.   *    * @param ss - the new StyleSheet.   */  public void addStyleSheet(StyleSheet ss)  {    if (styleSheet == null)      styleSheet = new StyleSheet[] {ss};    else      System.arraycopy(new StyleSheet[] {ss}, 0, styleSheet,                        styleSheet.length, 1);  }    /**   * Removes ss from those of the receiver   *    * @param ss - the StyleSheet to remove.   */  public void removeStyleSheet(StyleSheet ss)  {    if (styleSheet.length == 1 && styleSheet[0].equals(ss))      styleSheet = null;    else      {        for (int i = 0; i < styleSheet.length; i++)          {            StyleSheet curr = styleSheet[i];            if (curr.equals(ss))              {                StyleSheet[] tmp = new StyleSheet[styleSheet.length - 1];                if (i != 0 && i != (styleSheet.length - 1))                  {                    System.arraycopy(styleSheet, 0, tmp, 0, i);                    System.arraycopy(styleSheet, i + 1, tmp, i,                                     styleSheet.length - i - 1);                  }                else if (i == 0)                  System.arraycopy(styleSheet, 1, tmp, 0, styleSheet.length - 1);                else                  System.arraycopy(styleSheet, 0, tmp, 0, styleSheet.length - 1);                                styleSheet = tmp;                break;              }          }      }  }    /**   * Returns an array of the linked StyleSheets. May return null.   *    * @return - An array of the linked StyleSheets.   */  public StyleSheet[] getStyleSheets()  {    return styleSheet;  }    /**   * Imports a style sheet from the url. The rules are directly added to the   * receiver.   *    * @param url - the URL to import the StyleSheet from.   */  public void importStyleSheet(URL url)  {    // FIXME: Not implemented  }    /**   * Sets the base url. All import statements that are relative, will be   * relative to base.   *    * @param base -   *          the base URL.   */  public void setBase(URL base)  {    this.base = base;  }    /**   * Gets the base url.   *    * @return - the base   */  public URL getBase()  {    return base;  }    /**   * Adds a CSS attribute to the given set.   *    * @param attr - the attribute set   * @param key - the attribute to add   * @param value - the value of the key   */  public void addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key,                              String value)  {    attr.addAttribute(key, value);  }    /**   * Adds a CSS attribute to the given set.   * This method parses the value argument from HTML based on key.    * Returns true if it finds a valid value for the given key,    * and false otherwise.   *    * @param attr - the attribute set   * @param key - the attribute to add   * @param value - the value of the key   * @return true if a valid value was found.   */  public boolean addCSSAttributeFromHTML(MutableAttributeSet attr, CSS.Attribute key,                                         String value)  {    // FIXME: Need to parse value from HTML based on key.    attr.addAttribute(key, value);    return attr.containsAttribute(key, value);  }    /**   * Converts a set of HTML attributes to an equivalent set of CSS attributes.   *    * @param htmlAttrSet - the set containing the HTML attributes.   * @return the set of CSS attributes   */  public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)  {    // FIXME: Not implemented.    return null;      }  /**   * Adds an attribute to the given set and returns a new set. This is implemented   * to convert StyleConstants attributes to CSS before forwarding them to the superclass.   * The StyleConstants attribute do not have corresponding CSS entry, the attribute   * is stored (but will likely not be used).   *    * @param old - the old set   * @param key - the non-null attribute key   * @param value - the attribute value   * @return the updated set    */  public AttributeSet addAttribute(AttributeSet old, Object key,                                   Object value)  {    // FIXME: Not implemented.    return super.addAttribute(old, key, value);         }    /**   * Adds a set of attributes to the element. If any of these attributes are   * StyleConstants, they will be converted to CSS before forwarding to the    * superclass.   *    * @param old - the old set   * @param attr - the attributes to add   * @return the updated attribute set   */  public AttributeSet addAttributes(AttributeSet old, AttributeSet attr)  {    // FIXME: Not implemented.    return super.addAttributes(old, attr);             }    /**   * Removes an attribute from the set. If the attribute is a   * StyleConstants, it will be converted to CSS before forwarding to the    * superclass.   *    * @param old - the old set   * @param key - the non-null attribute key   * @return the updated set    */  public AttributeSet removeAttribute(AttributeSet old, Object key)  {    // FIXME: Not implemented.    return super.removeAttribute(old, key);      }    /**   * Removes an attribute from the set. If any of the attributes are   * StyleConstants, they will be converted to CSS before forwarding to the    * superclass.   *    * @param old - the old set   * @param attrs - the attributes to remove   * @return the updated set    */  public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)  {    // FIXME: Not implemented.    return super.removeAttributes(old, attrs);      }    /**   * Removes a set of attributes for the element. If any of the attributes is a   * StyleConstants, they will be converted to CSS before forwarding to the    * superclass.   *    * @param old - the old attribute set   * @param names - the attribute names   * @return the update attribute set   */  public AttributeSet removeAttributes(AttributeSet old, Enumeration names)  {    // FIXME: Not implemented.    return super.removeAttributes(old, names);          }    /**   * Creates a compact set of attributes that might be shared. This is a hook   * for subclasses that want to change the behaviour of SmallAttributeSet.   *    * @param a - the set of attributes to be represented in the compact form.   * @return the set of attributes created   */  protected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)  {    return super.createSmallAttributeSet(a);       }    /**   * Creates a large set of attributes. This set is not shared. This is a hook   * for subclasses that want to change the behaviour of the larger attribute   * storage format.   *    * @param a - the set of attributes to be represented in the larger form.   * @return the large set of attributes.   */  protected MutableAttributeSet createLargeAttributeSet(AttributeSet a)  {    return super.createLargeAttributeSet(a);       }    /**   * Gets the font to use for the given set.   *    * @param a - the set to get the font for.   * @return the font for the set   */  public Font getFont(AttributeSet a)  {    return super.getFont(a);      }    /**   * Takes a set of attributes and turns it into a foreground   * color specification. This is used to specify things like, brigher, more hue   * etc.   *    * @param a - the set to get the foreground color for   * @return the foreground color for the set   */  public Color getForeground(AttributeSet a)  {    return super.getForeground(a);     

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?