Module java.desktop

Class StyleSheet

java.lang.Object
javax.swing.text.StyleContext
javax.swing.text.html.StyleSheet
所有已实现的接口:
Serializable, AbstractDocument.AttributeContext

public class StyleSheet extends StyleContext
用于定义正在呈现的HTML视图的视觉特征的支持。 StyleSheet 用于将 HTML 模型转换为视觉特征。这使得视图可以通过外观和感觉进行自定义,可以对同一模型上的多个视图进行不同的呈现等。这可以被视为 CSS 规则存储库。CSS 属性的键是 CSS.Attribute 类型的对象。值的类型取决于 StyleSheet 实现,但要求 toString 方法返回 CSS 值的字符串表示形式。

HTML 视图实现获取其属性的主要入口点是 getViewAttributes 方法。应该实现此方法以建立用于将属性与视图关联的所需策略。每个 HTMLEditorKit(即每个关联的 JEditorPane)可以有自己的 StyleSheet,但默认情况下,所有 HTMLEditorKit 实例将共享一个样式表。HTMLDocument 实例也可以有一个 StyleSheet,其中保存了文档特定的 CSS 规范。

为了使视图存储更少的状态,因此更轻量级,StyleSheet 可以充当处理某些呈现任务的绘制器的工厂。这允许实现确定他们想要缓存什么,并且共享可能在选择器通常适用于多个视图的级别。由于 StyleSheet 可能被用于多个文档上的视图,并且通常 HTML 属性不影响所使用的选择器,因此共享的潜力是显著的。

规则存储为命名样式,并存储其他信息以快速将元素的上下文转换为规则。以下代码片段将显示命名样式,因此包含的 CSS 规则。


  
   import java.util.*;
   import javax.swing.text.*;
   import javax.swing.text.html.*;
  
   public class ShowStyles {
  
       public static void main(String[] args) {
         HTMLEditorKit kit = new HTMLEditorKit();
         HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
         StyleSheet styles = doc.getStyleSheet();
  
         Enumeration rules = styles.getStyleNames();
         while (rules.hasMoreElements()) {
             String name = (String) rules.nextElement();
             Style rule = styles.getStyle(name);
             System.out.println(rule.toString());
         }
         System.exit(0);
       }
   }
  
 

关于 CSS 样式何时应该覆盖元素定义的视觉属性的语义尚未明确定义。例如,html <body bgcolor=red> 使 body 具有红色背景。但是,如果 html 文件还包含 CSS 规则 body { background: blue },那么 body 的背景颜色应该是什么颜色就不太清楚了。当前的实现赋予元素中定义的视觉属性最高优先级,即始终在任何样式之前检查它们。因此,在前面的示例中,背景将具有红色,因为 body 元素将背景颜色定义为红色。

如前所述,这支持 CSS。我们不支持完整的 CSS 规范。请参考 CSS 类的 javadoc,以查看我们支持哪些属性。我们目前不支持的两个主要 CSS 解析相关概念是伪选择器,例如 A:link { color: red },以及 important 修饰符。

实现注意事项:
此实现目前不完整。可以用完整的替代实现来替换它。此类的未来版本将提供更好的 CSS 支持。
  • Constructor Details

    • StyleSheet

      public StyleSheet()
      构造一个 StyleSheet
  • Method Details

    • getRule

      public Style getRule(HTML.Tag t, Element e)
      获取用于呈现给定 HTML 标记类型的样式。给定的元素表示标记,可用于确定在嵌套情况下属性将有所不同的情况。
      参数:
      t - 要转换为视觉属性的类型
      e - 表示标记的元素;该元素可用于确定在嵌套在其他元素内部时属性将有所不同的情况
      返回:
      用于呈现标记的 CSS 属性集
    • getRule

      public Style getRule(String selector)
      获取与字符串形式中给定的选择器最匹配的规则。其中 selector 是元素名称的以空格分隔的字符串。例如,selector 可能是 'html body tr td''

      返回的 Style 的属性将随着添加和删除规则而更改。也就是说,如果您要求具有选择器 "table p" 的规则,并且添加了具有选择器 "p" 的新规则,则返回的 Style 将包括来自规则 "p" 的新属性。

      参数:
      selector - 元素名称的以空格分隔的字符串。
      返回:
      最匹配选择器的规则。
    • addRule

      public void addRule(String rule)
      向样式表添加一组规则。这些规则应该是有效的 CSS 格式。通常,这将作为解析
      Parameters:
      rule - a set of rules
    • getDeclaration

      public AttributeSet getDeclaration(String decl)
      Translates a CSS declaration to an AttributeSet that represents the CSS declaration. Typically this would be called as a result of encountering an HTML style attribute.
      Parameters:
      decl - a CSS declaration
      Returns:
      a set of attributes that represents the CSS declaration.
    • loadRules

      public void loadRules(Reader in, URL ref) throws IOException
      Loads a set of rules that have been specified in terms of CSS1 grammar. If there are collisions with existing rules, the newly specified rule will win.
      Parameters:
      in - the stream to read the CSS grammar from
      ref - the reference URL. This value represents the location of the stream and may be null. All relative URLs specified in the stream will be based upon this parameter.
      Throws:
      IOException - if I/O error occurred.
    • getViewAttributes

      public AttributeSet getViewAttributes(View v)
      Fetches a set of attributes to use in the view for displaying. This is basically a set of attributes that can be used for View.getAttributes.
      Parameters:
      v - a view
      Returns:
      the of attributes
    • removeStyle

      public void removeStyle(String nm)
      Removes a named style previously added to the document.
      Overrides:
      removeStyle in class StyleContext
      Parameters:
      nm - the name of the style to remove
    • addStyleSheet

      public void addStyleSheet(StyleSheet ss)
      Adds the rules from the StyleSheet ss to those of the receiver. ss's rules will override the rules of any previously added style sheets. An added StyleSheet will never override the rules of the receiving style sheet.
      Parameters:
      ss - a StyleSheet
      Since:
      1.3
    • removeStyleSheet

      public void removeStyleSheet(StyleSheet ss)
      Removes the StyleSheet ss from those of the receiver.
      Parameters:
      ss - a StyleSheet
      Since:
      1.3
    • getStyleSheets

      public StyleSheet[] getStyleSheets()
      Returns an array of the linked StyleSheets. Will return null if there are no linked StyleSheets.
      返回:
      一个样式表数组。
      自版本:
      1.3
    • importStyleSheet

      public void importStyleSheet(URL url)
      url导入样式表。生成的规则直接添加到接收器中。如果不希望规则成为接收器的一部分,请创建一个新的StyleSheet并使用addStyleSheet将其链接。
      参数:
      url - 一个url
      自版本:
      1.3
    • setBase

      public void setBase(URL base)
      设置基础。所有相对的导入语句将相对于base
      参数:
      base - 一个基础。
      自版本:
      1.3
    • getBase

      public URL getBase()
      返回基础。
      返回:
      基础。
      自版本:
      1.3
    • addCSSAttribute

      public void addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key, String value)
      向给定集合添加CSS属性。
      参数:
      attr - 一组属性
      key - 一个CSS属性
      value - 一个HTML属性值
      自版本:
      1.3
    • addCSSAttributeFromHTML

      public boolean addCSSAttributeFromHTML(MutableAttributeSet attr, CSS.Attribute key, String value)
      向给定集合添加CSS属性。
      参数:
      attr - 一组属性
      key - 一个CSS属性
      value - 一个HTML属性值
      返回:
      如果HTML属性value可以转换为CSS属性,则返回true,否则返回false
      自版本:
      1.3
    • translateHTMLToCSS

      public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
      将一组HTML属性转换为等效的CSS属性集。
      参数:
      htmlAttrSet - 包含HTML属性的AttributeSet。
      返回:
      CSS属性集。
    • addAttribute

      public AttributeSet addAttribute(AttributeSet old, Object key, Object value)
      向给定集合添加属性,并返回新的代表性集合。在转发到超类行为之前,此方法被重新实现为将StyleConstant属性转换为CSS。如果StyleConstants属性没有对应的CSS条目,则该属性将被存储(但可能不会被使用)。
      指定者:
      addAttribute 在接口 AbstractDocument.AttributeContext
      覆盖:
      addAttribute 在类 StyleContext
      参数:
      old - 旧属性集
      key - 非空属性键
      value - 属性值
      返回:
      更新后的属性集
      参见:
    • addAttributes

      public AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
      向元素添加一组属性。如果其中任何属性是StyleConstants属性,则在转发到超类行为之前将其转换为CSS。
      指定者:
      addAttributes 在接口 AbstractDocument.AttributeContext
      覆盖:
      addAttributes 在类 StyleContext
      参数:
      old - 旧属性集
      attr - 要添加的属性
      返回:
      更新后的属性集
      参见:
    • removeAttribute

      public AttributeSet removeAttribute(AttributeSet old, Object key)
      从集合中删除一个属性。如果属性是StyleConstants属性,则在转发到超类行为之前将请求转换为CSS属性。
      指定者:
      removeAttribute 在接口 AbstractDocument.AttributeContext
      覆盖:
      removeAttribute 在类 StyleContext
      参数:
      old - 旧属性集
      key - 非空属性名称
      返回:
      更新后的属性集
      参见:
    • removeAttributes

      public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names)
      为元素删除一组属性。如果任何属性是StyleConstants属性,则在转发到超类行为之前将请求转换为CSS属性。
      指定者:
      removeAttributes 在接口 AbstractDocument.AttributeContext
      覆盖:
      removeAttributes 在类 StyleContext
      参数:
      old - 旧属性集
      names - 属性名称
      返回:
      更新后的属性集
      参见:
    • removeAttributes

      public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
      删除一组属性。如果任何属性是StyleConstants属性,则在转发到超类行为之前将请求转换为CSS属性。
      指定者:
      removeAttributes 在接口 AbstractDocument.AttributeContext
      覆盖:
      removeAttributes 在类 StyleContext
      参数:
      old - 旧属性集
      attrs - 属性
      返回:
      更新后的属性集
      参见:
    • createSmallAttributeSet

      protected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
      创建一个紧凑的属性集,可能会被共享。这是希望改变SmallAttributeSet行为的子类的挂钩。这可以重新实现为返回一个提供某种属性转换的AttributeSet。
      覆盖:
      createSmallAttributeSet 在类 StyleContext
      参数:
      a - 要以紧凑形式表示的属性集。
      返回:
      可能会被共享的紧凑属性集
    • createLargeAttributeSet

      protected MutableAttributeSet createLargeAttributeSet(AttributeSet a)
      创建一个大型属性集,应该在空间和时间之间进行权衡。此集合不会被共享。这是希望改变较大属性存储格式行为的子类的挂钩(默认情况下为SimpleAttributeSet)。这可以重新实现为返回一个MutableAttributeSet,提供某种属性转换。
      覆盖:
      createLargeAttributeSet 在类 StyleContext
      参数:
      a - 要以较大形式表示的属性集。
      返回:
      应该在空间和时间之间进行权衡的大型属性集
    • getFont

      public Font getFont(AttributeSet a)
      获取用于给定属性集的字体。
      覆盖:
      getFont 在类 StyleContext
      参数:
      a - 属性集
      返回:
      字体
    • getForeground

      public Color getForeground(AttributeSet a)
      将一组属性转换为前景色规范。这可能用于指定更亮、更色调等内容。
      覆盖:
      getForeground 在类 StyleContext
      参数:
      a - 属性集
      返回:
      颜色
    • getBackground

      public Color getBackground(AttributeSet a)
      将一组属性转换为背景颜色规范。这可能用于指定更亮、更饱和等内容。
      覆盖:
      getBackground 在类 StyleContext
      参数:
      a - 属性集
      返回:
      颜色
    • getBoxPainter

      public StyleSheet.BoxPainter getBoxPainter(AttributeSet a)
      获取给定一组CSS属性使用的框格式化程序。
      参数:
      a - 一组CSS属性
      返回:
      框格式化程序
    • getListPainter

      public StyleSheet.ListPainter getListPainter(AttributeSet a)
      获取给定一组CSS属性使用的列表格式化程序。
      参数:
      a - 一组CSS属性
      返回:
      列表格式化程序
    • setBaseFontSize

      public void setBaseFontSize(int sz)
      设置基本字体大小,有效值为1到7之间。
      参数:
      sz - 字体大小
    • setBaseFontSize

      public void setBaseFontSize(String size)
      从传入的字符串设置基本字体大小。该字符串可以标识特定的字体大小,合法值为1到7之间,或标识相对字体大小,如+1或-2。
      参数:
      size - 字体大小
    • getIndexOfSize

      public static int getIndexOfSize(float pt)
      返回HTML/CSS大小模型的索引。
      参数:
      pt - 点的大小
      返回:
      HTML/CSS大小模型的索引
    • getPointSize

      public float getPointSize(int index)
      给定一个大小索引,返回点大小值。
      参数:
      index - 大小索引
      返回:
      点大小值
    • getPointSize

      public float getPointSize(String size)
      给定类似“+2”、“-2”或“2”的字符串,返回一个点大小值。
      参数:
      size - 描述字体大小的CSS字符串
      返回:
      点大小值
    • stringToColor

      public Color stringToColor(String string)
      将颜色字符串(如“RED”或“#NNNNNN”)转换为颜色。注意:这只会转换HTML3.2颜色字符串或长度为7的字符串;否则将返回null。
      参数:
      string - 颜色字符串,如“RED”或“#NNNNNN”
      返回:
      颜色