com.sun.javafx.css.Stylesheet Maven / Gradle / Ivy
Show all versions of openjfx-78-backport Show documentation
/*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.javafx.css;
import com.sun.javafx.collections.TrackableObservableList;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.css.StyleOrigin;
/**
* A stylesheet which can apply properties to a tree of objects. A stylesheet
* is a collection of zero or more {@link Rule Rules}, each of which is applied
* to each object in the tree. Typically the selector will examine the object to
* determine whether or not it is applicable, and if so it will apply certain
* property values to the object.
*
* Stylesheets can be parsed from CSS documents or created programmatically.
* Once created, stylesheets can be freely modified, but the modifications do
* not affect styled objects until a subsequent {@link #applyTo} or
* {@link #reapply}.
*
*/
public class Stylesheet {
private final URL url;
/** The URL from which the stylesheet was loaded.
* @return The URL from which the stylesheet was loaded, or null if
* the stylesheet was created from an inline style.
*/
public URL getUrl() {
return url;
}
/**
* True if this style came from user stylesheet, we need to know this so
* that we can make user important styles have higher priority than
* author styles
*/
private StyleOrigin origin = StyleOrigin.AUTHOR;
public StyleOrigin getOrigin() {
return origin;
}
public void setOrigin(StyleOrigin origin) {
this.origin = origin;
}
/** All the rules contained in the stylesheet in the order they are in the file */
private final ObservableList rules = new TrackableObservableList() {
@Override
protected void onChanged(Change c) {
c.reset();
while (c.next()) {
if (c.wasAdded()) {
for(Rule rule : c.getAddedSubList()) {
rule.setStylesheet(Stylesheet.this);
}
} else if (c.wasRemoved()) {
for (Rule rule : c.getRemoved()) {
if (rule.getStylesheet() == Stylesheet.this) rule.setStylesheet(null);
}
}
}
}
};
/** List of all font faces */
private final List fontFaces = new ArrayList();
/**
* Constructs a stylesheet with the base URI defaulting to the root
* path of the application.
*/
public Stylesheet() {
// ClassLoader cl = Thread.currentThread().getContextClassLoader();
// this.url = (cl != null) ? cl.getResource("") : null;
//
// RT-17344
// The above code is unreliable. The getResource call is intended
// to return the root path of the Application instance, but it sometimes
// returns null. Here, we'll set url to null and then when a url is
// resolved, the url path can be used in the getResource call. For
// example, if the css is -fx-image: url("images/duke.png"), we can
// do cl.getResouce("images/duke.png") in URLConverter
//
this(null);
}
/**
* Constructs a Stylesheet using the given URL as the base URI. The
* parameter may not be null.
*/
public Stylesheet(URL url) {
this.url = url;
}
public List getRules() {
return rules;
}
public List getFontFaces() {
return fontFaces;
}
@Override public boolean equals(Object obj) {
if (this == obj) return true;
if (obj instanceof Stylesheet) {
Stylesheet other = (Stylesheet)obj;
if (this.url == null && other.url == null) {
return true;
} else if (this.url == null || other.url == null) {
return false;
} else {
// convert to Strings, as URL.equals is slow. See here:
// http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html
String thisUrlString = this.url.toExternalForm();
String otherUrlString = other.url.toExternalForm();
return thisUrlString.equals(otherUrlString);
}
}
return false;
}
@Override public int hashCode() {
int hash = 7;
hash = 13 * hash + (this.url != null ? this.url.hashCode() : 0);
return hash;
}
/** Returns a string representation of this object. */
public @Override String toString() {
StringBuilder sbuf = new StringBuilder();
sbuf.append("/* ");
if (url != null) sbuf.append(url);
if (rules.isEmpty()) {
sbuf.append(" */");
} else {
sbuf.append(" */\n");
for(int r=0; r persistedRules = new ArrayList(nRules);
for (int n=0; n