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.StyleOrigin;
30
31 import java.util.Set;
32
33
34 /** A marriage of pseudo-classes (potentially empty) to property and value */
35 public class CascadingStyle implements Comparable<CascadingStyle> {
36
37 /** */
38 private final Style style;
39 public Style getStyle() {
40 return style;
41 }
42
43 /** State variables, like "hover" or "pressed" */
44 private final Set<PseudoClass> pseudoClasses;
45
46 /* specificity of the selector that matched */
47 private final int specificity;
48
49 /* order in which this style appeared in the stylesheet */
50 private final int ordinal;
51
52 /*
53 * True if the property is -fx-skin. We want the skin property to
54 * sort less than all other properties.
55 */
56 private final boolean skinProp;
57
58 public CascadingStyle(final Style style, Set<PseudoClass> pseudoClasses,
59 final int specificity, final int ordinal) {
60 this.style = style;
61 this.pseudoClasses = pseudoClasses;
62 this.specificity = specificity;
63 this.ordinal = ordinal;
64 this.skinProp = "-fx-skin".equals(style.getDeclaration().getProperty());
65 }
66
67 // Wrapper to make StyleHelper's life a little easier
68 public String getProperty() {
69 return style.getDeclaration().getProperty();
70 }
71
72 // Wrapper to make StyleHelper's life a little easier
73 public Selector getSelector() {
74 return style.getSelector();
75 }
76
77 // Wrapper to make StyleHelper's life a little easier
78 public Rule getRule() {
79 return style.getDeclaration().getRule();
80 }
81
82 // Wrapper to make StyleHelper's life a little easier
83 public StyleOrigin getOrigin() {
84 return getRule().getOrigin();
85 }
86
87 // Wrapper to make StyleHelper's life a little easier
88 public ParsedValueImpl getParsedValueImpl() {
89 return style.getDeclaration().getParsedValueImpl();
90 }
91
92 @Override public String toString() { return getProperty(); }
93
94 /**
95 * When testing equality against another Style, we only care about
96 * the property and pseudo-classes. In other words, we only care about
97 * where the style is applied, not what is applied.
98 */
99 @Override public boolean equals(Object obj) {
100 if (obj == null) {
101 return false;
102 }
103 if (getClass() != obj.getClass()) {
104 return false;
105 }
106 CascadingStyle other = (CascadingStyle)obj;
107
108 final String property = getProperty();
109 final String otherProperty = other.getProperty();
110 if (property == null ? otherProperty != null : !property.equals(otherProperty)) {
111 return false;
112 }
113
114 // does [foo bar bang] contain all of [foo bar]?
115 if (pseudoClasses == null ? other.pseudoClasses != null : !pseudoClasses.containsAll(other.pseudoClasses)) {
116 return false;
117 }
118
119 return true;
120
121 }
122
123 /*
124 * Hash on property and pseudoclasses since
125 * obj1.hashCode() should equal obj2.hashCode() if obj1.equals(obj2)
126 */
127 @Override
128 public int hashCode() {
129 int hash = 7;
130 final String property = getProperty();
131 hash = 47 * hash + (property != null ? property.hashCode() : 0);
132 hash = 47 * hash + (pseudoClasses != null ? pseudoClasses.hashCode() : 0);
133 return hash;
134 }
135
136 /**
137 * Implementation of Comparable such that more specific styles get
138 * sorted before less specific ones.
139 */
140 @Override
141 public int compareTo(CascadingStyle other) {
142
143 //
144 // Important styles take the cake
145 // Importance being equal, then specificity is considered
146 // Specificity being equal, then the order of declaration decides.
147 //
148
149 final Declaration decl = style.getDeclaration();
150 final boolean important = decl != null ? decl.isImportant() : false;
151 final Rule rule = decl != null ? decl.getRule() : null;
152 final StyleOrigin source = rule != null ? rule.getOrigin() : null;
153
154 final Declaration otherDecl = other.style.getDeclaration();
155 final boolean otherImportant = otherDecl != null ? otherDecl.isImportant() : false;
156 final Rule otherRule = otherDecl != null ? otherDecl.getRule() : null;
157 final StyleOrigin otherSource = otherRule != null ? otherRule.getOrigin() : null;
158
159 int c = 0;
160
161 if (this.skinProp && !other.skinProp) {
|
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 java.util.Set;
29
30 /** A marriage of pseudo-classes (potentially empty) to property and value
31 *
32 * @since 9
33 */
34 public class CascadingStyle implements Comparable<CascadingStyle> {
35
36 /** */
37 private final Style style;
38 public Style getStyle() {
39 return style;
40 }
41
42 /** State variables, like "hover" or "pressed" */
43 private final Set<PseudoClass> pseudoClasses;
44
45 /* specificity of the selector that matched */
46 private final int specificity;
47
48 /* order in which this style appeared in the stylesheet */
49 private final int ordinal;
50
51 /*
52 * True if the property is -fx-skin. We want the skin property to
53 * sort less than all other properties.
54 */
55 private final boolean skinProp;
56
57 public CascadingStyle(final Style style, Set<PseudoClass> pseudoClasses,
58 final int specificity, final int ordinal) {
59 this.style = style;
60 this.pseudoClasses = pseudoClasses;
61 this.specificity = specificity;
62 this.ordinal = ordinal;
63 this.skinProp = "-fx-skin".equals(style.getDeclaration().getProperty());
64 }
65
66 public CascadingStyle(final Declaration decl, final Match match, final int ordinal) {
67 this(new Style(match.getSelector(), decl),
68 match.getPseudoClasses(),
69 match.getSpecificity(),
70 ordinal);
71 }
72
73 // Wrapper to make StyleHelper's life a little easier
74 public String getProperty() {
75 return style.getDeclaration().getProperty();
76 }
77
78 // Wrapper to make StyleHelper's life a little easier
79 public Selector getSelector() {
80 return style.getSelector();
81 }
82
83 // Wrapper to make StyleHelper's life a little easier
84 public Rule getRule() {
85 return style.getDeclaration().getRule();
86 }
87
88 // Wrapper to make StyleHelper's life a little easier
89 public StyleOrigin getOrigin() {
90 return getRule().getOrigin();
91 }
92
93 // Wrapper to make StyleHelper's life a little easier
94 public ParsedValue getParsedValue() {
95 return style.getDeclaration().getParsedValue();
96 }
97
98 @Override public String toString() { return getProperty(); }
99
100 /**
101 * When testing equality against another Style, we only care about
102 * the property and pseudo-classes. In other words, we only care about
103 * where the style is applied, not what is applied.
104 */
105 @Override public boolean equals(Object obj) {
106 if (obj == null) {
107 return false;
108 }
109 if (getClass() != obj.getClass()) {
110 return false;
111 }
112 CascadingStyle other = (CascadingStyle)obj;
113
114 final String property = getProperty();
115 final String otherProperty = other.getProperty();
116 if (property == null ? otherProperty != null : !property.equals(otherProperty)) {
117 return false;
118 }
119
120 // does [foo bar bang] contain all of [foo bar]?
121 if (pseudoClasses == null ? other.pseudoClasses != null : !pseudoClasses.containsAll(other.pseudoClasses)) {
122 return false;
123 }
124
125 return true;
126
127 }
128
129 /*
130 * Hash on property and pseudoclasses since
131 * obj1.hashCode() should equal obj2.hashCode() if obj1.equals(obj2)
132 */
133 @Override public int hashCode() {
134 int hash = 7;
135 final String property = getProperty();
136 hash = 47 * hash + (property != null ? property.hashCode() : 0);
137 hash = 47 * hash + (pseudoClasses != null ? pseudoClasses.hashCode() : 0);
138 return hash;
139 }
140
141 /**
142 * Implementation of Comparable such that more specific styles get
143 * sorted before less specific ones.
144 */
145 @Override public int compareTo(CascadingStyle other) {
146
147 //
148 // Important styles take the cake
149 // Importance being equal, then specificity is considered
150 // Specificity being equal, then the order of declaration decides.
151 //
152
153 final Declaration decl = style.getDeclaration();
154 final boolean important = decl != null ? decl.isImportant() : false;
155 final Rule rule = decl != null ? decl.getRule() : null;
156 final StyleOrigin source = rule != null ? rule.getOrigin() : null;
157
158 final Declaration otherDecl = other.style.getDeclaration();
159 final boolean otherImportant = otherDecl != null ? otherDecl.isImportant() : false;
160 final Rule otherRule = otherDecl != null ? otherDecl.getRule() : null;
161 final StyleOrigin otherSource = otherRule != null ? otherRule.getOrigin() : null;
162
163 int c = 0;
164
165 if (this.skinProp && !other.skinProp) {
|