modules/graphics/src/main/java/javafx/css/Rule.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.css;
  27 
  28 import com.sun.javafx.collections.TrackableObservableList;
  29 import javafx.collections.ListChangeListener.Change;
  30 import javafx.collections.ObservableList;
  31 import javafx.css.PseudoClass;
  32 import javafx.css.StyleOrigin;
  33 import javafx.scene.Node;
  34 


  35 import java.io.ByteArrayInputStream;
  36 import java.io.ByteArrayOutputStream;
  37 import java.io.DataInputStream;
  38 import java.io.DataOutputStream;
  39 import java.io.IOException;
  40 import java.util.ArrayList;
  41 import java.util.List;
  42 import java.util.Set;
  43 
  44 /*
  45  * A selector is a collection of selectors and declarations.


  46  */
  47 final public class Rule {
  48 
  49     private List<Selector> selectors = null;
  50 
  51     /**
  52      * The list returned from this method should be treated as unmodifiable.
  53      * Tools should use {@link #getSelectors()} which tracks adds and removes.
  54      */
  55     public List<Selector>  getUnobservedSelectorList() {
  56         if (selectors == null) {
  57             selectors = new ArrayList<Selector>();
  58         }
  59         return selectors;
  60     }
  61 
  62     private List<Declaration> declarations = null;
  63     /**
  64      * The list returned from this method should be treated as unmodifiable.
  65      * Tools should use {@link #getDeclarations()} which tracks adds and removes.
  66      */
  67     public List<Declaration> getUnobservedDeclarationList() {
  68 
  69         if (declarations == null && serializedDecls != null) {
  70 
  71             try {
  72                 ByteArrayInputStream bis = new ByteArrayInputStream(serializedDecls);
  73                 DataInputStream dis = new DataInputStream(bis);
  74 
  75                 short nDeclarations = dis.readShort();
  76                 declarations = new ArrayList<Declaration>(nDeclarations);
  77                 for (int i = 0; i < nDeclarations; i++) {
  78 
  79                     Declaration decl = Declaration.readBinary(bssVersion, dis, stylesheet.getStringStore());
  80                     decl.rule = Rule.this;
  81 
  82                     if (stylesheet != null && stylesheet.getUrl() != null) {
  83                         String stylesheetUrl = stylesheet.getUrl();
  84                         decl.fixUrl(stylesheetUrl);
  85                     }
  86 
  87                     declarations.add(decl);


 149     /* package */
 150     void setStylesheet(Stylesheet stylesheet) {
 151 
 152         this.stylesheet = stylesheet;
 153         
 154         if (stylesheet != null && stylesheet.getUrl() != null) {
 155             final String stylesheetUrl = stylesheet.getUrl();
 156 
 157             int nDeclarations = declarations != null ? declarations.size() : 0;
 158             for (int d=0; d<nDeclarations; d++) {
 159                 declarations.get(d).fixUrl(stylesheetUrl);
 160             }
 161         }
 162     }
 163 
 164     public StyleOrigin getOrigin() {
 165         return stylesheet != null ? stylesheet.getOrigin() : null;
 166     }
 167 
 168 
 169     public Rule(List<Selector> selectors, List<Declaration> declarations) {
 170 
 171         this.selectors = selectors;
 172         this.declarations = declarations;
 173         serializedDecls = null;
 174         this.bssVersion = Stylesheet.BINARY_CSS_VERSION;
 175 
 176         int sMax = selectors != null ? selectors.size() : 0;
 177         for(int i = 0; i < sMax; i++) {
 178             Selector sel = selectors.get(i);
 179             sel.setRule(Rule.this);
 180         }
 181 
 182         int dMax = declarations != null ? declarations.size() : 0;
 183         for (int d=0; d<dMax; d++) {
 184             Declaration decl = declarations.get(d);
 185             decl.rule = this;
 186         }
 187     }
 188 
 189     private byte[] serializedDecls;


 200         for(int i = 0; i < sMax; i++) {
 201             Selector sel = selectors.get(i);
 202             sel.setRule(Rule.this);
 203         }
 204 
 205     }
 206 
 207     // Return mask of selectors that match
 208     long applies(Node node, Set<PseudoClass>[] triggerStates) {
 209         long mask = 0;
 210         for (int i = 0; i < selectors.size(); i++) {
 211             Selector sel = selectors.get(i);
 212             if (sel.applies(node, triggerStates, 0)) {                
 213                 mask |= 1l << i;
 214             }
 215         }
 216         return mask;
 217     }
 218 
 219     /** Converts this object to a string. */
 220     @Override
 221     public String toString() {
 222         StringBuilder sb = new StringBuilder();
 223         if (selectors.size()>0) {
 224             sb.append(selectors.get(0));
 225         }
 226         for (int n=1; n<selectors.size(); n++) {
 227             sb.append(',');
 228             sb.append(selectors.get(n));
 229         }
 230         sb.append("{\n");
 231         int nDeclarations = declarations != null ? declarations.size() : 0;
 232         for (int n=0; n<nDeclarations; n++) {
 233             sb.append("\t");
 234             sb.append(declarations.get(n));
 235             sb.append("\n");
 236         }
 237         sb .append("}");
 238         return sb.toString();
 239     }
 240 
 241     /*


 302                     }
 303                 }
 304             };
 305 
 306         }
 307 
 308         private ObservableList<Selector> getSelectors() {
 309             return selectorObservableList;
 310         }
 311 
 312         private ObservableList<Declaration> getDeclarations() {
 313             return declarationObservableList;
 314         }
 315 
 316         private final Rule rule;
 317         private final ObservableList<Selector> selectorObservableList;
 318         private final ObservableList<Declaration> declarationObservableList;
 319 
 320     }
 321 
 322     final void writeBinary(DataOutputStream os, StringStore stringStore)
 323             throws IOException {
 324 
 325         final int nSelectors = this.selectors != null ? this.selectors.size() : 0;
 326         os.writeShort(nSelectors);
 327         for (int i = 0; i < nSelectors; i++) {
 328             Selector sel = this.selectors.get(i);
 329             sel.writeBinary(os, stringStore);
 330         }
 331 
 332         List<Declaration> decls = getUnobservedDeclarationList();
 333         if (decls != null) {
 334 
 335             ByteArrayOutputStream bos = new ByteArrayOutputStream(5192);
 336             DataOutputStream dos = new DataOutputStream(bos);
 337 
 338             int nDeclarations =  decls.size();
 339             dos.writeShort(nDeclarations);
 340 
 341             for (int i = 0; i < nDeclarations; i++) {
 342                 Declaration decl = declarations.get(i);




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.css;
  27 

  28 import javafx.collections.ListChangeListener.Change;
  29 import javafx.collections.ObservableList;


  30 import javafx.scene.Node;
  31 
  32 import com.sun.javafx.collections.TrackableObservableList;
  33 
  34 import java.io.ByteArrayInputStream;
  35 import java.io.ByteArrayOutputStream;
  36 import java.io.DataInputStream;
  37 import java.io.DataOutputStream;
  38 import java.io.IOException;
  39 import java.util.ArrayList;
  40 import java.util.List;
  41 import java.util.Set;
  42 
  43 /*
  44  * A selector is a collection of selectors and declarations.
  45  *
  46  * @since 9
  47  */
  48 final public class Rule {
  49 
  50     private List<Selector> selectors = null;
  51 
  52     /**
  53      * The list returned from this method should be treated as unmodifiable.
  54      * Tools should use {@link #getSelectors()} which tracks adds and removes.
  55      */
  56     List<Selector>  getUnobservedSelectorList() {
  57         if (selectors == null) {
  58             selectors = new ArrayList<Selector>();
  59         }
  60         return selectors;
  61     }
  62 
  63     private List<Declaration> declarations = null;
  64     /**
  65      * The list returned from this method should be treated as unmodifiable.
  66      * Tools should use {@link #getDeclarations()} which tracks adds and removes.
  67      */
  68     List<Declaration> getUnobservedDeclarationList() {
  69 
  70         if (declarations == null && serializedDecls != null) {
  71 
  72             try {
  73                 ByteArrayInputStream bis = new ByteArrayInputStream(serializedDecls);
  74                 DataInputStream dis = new DataInputStream(bis);
  75 
  76                 short nDeclarations = dis.readShort();
  77                 declarations = new ArrayList<Declaration>(nDeclarations);
  78                 for (int i = 0; i < nDeclarations; i++) {
  79 
  80                     Declaration decl = Declaration.readBinary(bssVersion, dis, stylesheet.getStringStore());
  81                     decl.rule = Rule.this;
  82 
  83                     if (stylesheet != null && stylesheet.getUrl() != null) {
  84                         String stylesheetUrl = stylesheet.getUrl();
  85                         decl.fixUrl(stylesheetUrl);
  86                     }
  87 
  88                     declarations.add(decl);


 150     /* package */
 151     void setStylesheet(Stylesheet stylesheet) {
 152 
 153         this.stylesheet = stylesheet;
 154         
 155         if (stylesheet != null && stylesheet.getUrl() != null) {
 156             final String stylesheetUrl = stylesheet.getUrl();
 157 
 158             int nDeclarations = declarations != null ? declarations.size() : 0;
 159             for (int d=0; d<nDeclarations; d++) {
 160                 declarations.get(d).fixUrl(stylesheetUrl);
 161             }
 162         }
 163     }
 164 
 165     public StyleOrigin getOrigin() {
 166         return stylesheet != null ? stylesheet.getOrigin() : null;
 167     }
 168 
 169 
 170     Rule(List<Selector> selectors, List<Declaration> declarations) {
 171 
 172         this.selectors = selectors;
 173         this.declarations = declarations;
 174         serializedDecls = null;
 175         this.bssVersion = Stylesheet.BINARY_CSS_VERSION;
 176 
 177         int sMax = selectors != null ? selectors.size() : 0;
 178         for(int i = 0; i < sMax; i++) {
 179             Selector sel = selectors.get(i);
 180             sel.setRule(Rule.this);
 181         }
 182 
 183         int dMax = declarations != null ? declarations.size() : 0;
 184         for (int d=0; d<dMax; d++) {
 185             Declaration decl = declarations.get(d);
 186             decl.rule = this;
 187         }
 188     }
 189 
 190     private byte[] serializedDecls;


 201         for(int i = 0; i < sMax; i++) {
 202             Selector sel = selectors.get(i);
 203             sel.setRule(Rule.this);
 204         }
 205 
 206     }
 207 
 208     // Return mask of selectors that match
 209     long applies(Node node, Set<PseudoClass>[] triggerStates) {
 210         long mask = 0;
 211         for (int i = 0; i < selectors.size(); i++) {
 212             Selector sel = selectors.get(i);
 213             if (sel.applies(node, triggerStates, 0)) {                
 214                 mask |= 1l << i;
 215             }
 216         }
 217         return mask;
 218     }
 219 
 220     /** Converts this object to a string. */
 221     @Override public String toString() {

 222         StringBuilder sb = new StringBuilder();
 223         if (selectors.size()>0) {
 224             sb.append(selectors.get(0));
 225         }
 226         for (int n=1; n<selectors.size(); n++) {
 227             sb.append(',');
 228             sb.append(selectors.get(n));
 229         }
 230         sb.append("{\n");
 231         int nDeclarations = declarations != null ? declarations.size() : 0;
 232         for (int n=0; n<nDeclarations; n++) {
 233             sb.append("\t");
 234             sb.append(declarations.get(n));
 235             sb.append("\n");
 236         }
 237         sb .append("}");
 238         return sb.toString();
 239     }
 240 
 241     /*


 302                     }
 303                 }
 304             };
 305 
 306         }
 307 
 308         private ObservableList<Selector> getSelectors() {
 309             return selectorObservableList;
 310         }
 311 
 312         private ObservableList<Declaration> getDeclarations() {
 313             return declarationObservableList;
 314         }
 315 
 316         private final Rule rule;
 317         private final ObservableList<Selector> selectorObservableList;
 318         private final ObservableList<Declaration> declarationObservableList;
 319 
 320     }
 321 
 322     final void writeBinary(DataOutputStream os, StyleConverter.StringStore stringStore)
 323             throws IOException {
 324 
 325         final int nSelectors = this.selectors != null ? this.selectors.size() : 0;
 326         os.writeShort(nSelectors);
 327         for (int i = 0; i < nSelectors; i++) {
 328             Selector sel = this.selectors.get(i);
 329             sel.writeBinary(os, stringStore);
 330         }
 331 
 332         List<Declaration> decls = getUnobservedDeclarationList();
 333         if (decls != null) {
 334 
 335             ByteArrayOutputStream bos = new ByteArrayOutputStream(5192);
 336             DataOutputStream dos = new DataOutputStream(bos);
 337 
 338             int nDeclarations =  decls.size();
 339             dos.writeShort(nDeclarations);
 340 
 341             for (int i = 0; i < nDeclarations; i++) {
 342                 Declaration decl = declarations.get(i);