modules/graphics/src/main/java/javafx/css/Selector.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 javafx.css.PseudoClass;
  29 import javafx.css.Styleable;
  30 
  31 import java.io.DataInputStream;
  32 import java.io.DataOutputStream;
  33 import java.io.IOException;
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 import java.util.Set;
  37 
  38 /**
  39  * Used by CSSRule to determine whether or not the selector applies to a
  40  * given object.
  41  *

  42  */
  43 abstract public class Selector {
  44 
  45     private static class UniversalSelector {
  46         private static final Selector INSTANCE =
  47             new SimpleSelector("*", null, null, null);
  48     }
  49 
  50     public static Selector getUniversalSelector() {
  51         return UniversalSelector.INSTANCE;
  52     }
  53 
  54     private Rule rule;
  55     void setRule(Rule rule) {
  56         this.rule = rule;
  57     }
  58     Rule getRule() {
  59         return rule;
  60     }
  61 
  62     private int ordinal = -1;
  63     void setOrdinal(int ordinal) {
  64         this.ordinal = ordinal;
  65     }
  66     int getOrdinal() {
  67         return ordinal;
  68     }
  69 
  70     abstract Match createMatch();
  71 
  72     // same as the matches method expect return true/false rather than a match
  73     public abstract boolean applies(Styleable styleable);
  74     
  75     // same as applies, but will return pseudoclass state that it finds along the way
  76     abstract boolean applies(Styleable styleable, Set<PseudoClass>[] triggerStates, int bit);
  77     
  78     /**
  79      * Determines whether the current state of the node and its parents
  80      * matches the pseudo-classes defined (if any) for this selector.
  81      */
  82     public abstract boolean stateMatches(Styleable styleable, Set<PseudoClass> state);
  83 
  84     private static final int TYPE_SIMPLE = 1;
  85     private static final int TYPE_COMPOUND = 2;
  86 
  87     protected void writeBinary(DataOutputStream os, StringStore stringStore)
  88         throws IOException {
  89         if (this instanceof SimpleSelector) {
  90             os.writeByte(TYPE_SIMPLE);
  91         } else {
  92             os.writeByte(TYPE_COMPOUND);
  93         }
  94     }
  95 
  96     static Selector readBinary(int bssVersion, DataInputStream is, String[] strings)
  97         throws IOException {
  98         final int type = is.readByte();
  99         if (type == TYPE_SIMPLE)
 100             return SimpleSelector.readBinary(bssVersion, is,strings);
 101         else
 102             return CompoundSelector.readBinary(bssVersion, is,strings);
 103     }
 104     
 105     public static Selector createSelector(final String cssSelector) {
 106         if (cssSelector == null || cssSelector.length() == 0) {
 107             return null; // actually return a default no-match selector




   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 com.sun.javafx.css.Combinator;

  29 
  30 import java.io.DataInputStream;
  31 import java.io.DataOutputStream;
  32 import java.io.IOException;
  33 import java.util.ArrayList;
  34 import java.util.List;
  35 import java.util.Set;
  36 
  37 /**
  38  * Used by CSSRule to determine whether or not the selector applies to a
  39  * given object.
  40  *
  41  * @since 9
  42  */
  43 abstract public class Selector {
  44 
  45     private static class UniversalSelector {
  46         private static final Selector INSTANCE =
  47             new SimpleSelector("*", null, null, null);
  48     }
  49 
  50     static Selector getUniversalSelector() {
  51         return UniversalSelector.INSTANCE;
  52     }
  53 
  54     private Rule rule;
  55     void setRule(Rule rule) {
  56         this.rule = rule;
  57     }
  58     public Rule getRule() {
  59         return rule;
  60     }
  61 
  62     private int ordinal = -1;
  63     public void setOrdinal(int ordinal) {
  64         this.ordinal = ordinal;
  65     }
  66     public int getOrdinal() {
  67         return ordinal;
  68     }
  69 
  70     public abstract Match createMatch();
  71 
  72     // same as the matches method expect return true/false rather than a match
  73     public abstract boolean applies(Styleable styleable);
  74     
  75     // same as applies, but will return pseudoclass state that it finds along the way
  76     public abstract boolean applies(Styleable styleable, Set<PseudoClass>[] triggerStates, int bit);
  77     
  78     /**
  79      * Determines whether the current state of the node and its parents
  80      * matches the pseudo-classes defined (if any) for this selector.
  81      */
  82     public abstract boolean stateMatches(Styleable styleable, Set<PseudoClass> state);
  83 
  84     private static final int TYPE_SIMPLE = 1;
  85     private static final int TYPE_COMPOUND = 2;
  86 
  87     protected void writeBinary(DataOutputStream os, StyleConverter.StringStore stringStore)
  88         throws IOException {
  89         if (this instanceof SimpleSelector) {
  90             os.writeByte(TYPE_SIMPLE);
  91         } else {
  92             os.writeByte(TYPE_COMPOUND);
  93         }
  94     }
  95 
  96     static Selector readBinary(int bssVersion, DataInputStream is, String[] strings)
  97         throws IOException {
  98         final int type = is.readByte();
  99         if (type == TYPE_SIMPLE)
 100             return SimpleSelector.readBinary(bssVersion, is,strings);
 101         else
 102             return CompoundSelector.readBinary(bssVersion, is,strings);
 103     }
 104     
 105     public static Selector createSelector(final String cssSelector) {
 106         if (cssSelector == null || cssSelector.length() == 0) {
 107             return null; // actually return a default no-match selector