1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 java.util.ArrayList;
  29 import java.util.Collections;
  30 import java.util.Comparator;
  31 import java.util.HashMap;
  32 import java.util.List;
  33 import java.util.Map;
  34 
  35 import javafx.css.CascadingStyle;
  36 import javafx.css.Declaration;
  37 import javafx.css.Match;
  38 import javafx.css.Rule;
  39 import javafx.css.Selector;
  40 import javafx.css.Style;
  41 
  42 
  43 /**
  44  * A map of property name to the cascading styles that match a node.
  45  */
  46 public final class StyleMap {
  47 
  48     public static final StyleMap EMPTY_MAP = 
  49         new StyleMap(-1, Collections.<Selector>emptyList());
  50 
  51     /** Only StyleManager creates StyleMap */
  52     public StyleMap(int id, List<Selector> selectors) {
  53         this.id = id;
  54         this.selectors = selectors;
  55     }
  56 
  57     public int getId() {
  58         return id;
  59     }
  60 
  61     public boolean isEmpty() {
  62         if (selectors != null) return selectors.isEmpty();
  63         else if (cascadingStyles != null) return cascadingStyles.isEmpty();
  64         else return true;
  65     }
  66         
  67     public Map<String, List<CascadingStyle>> getCascadingStyles() {
  68 
  69         if (cascadingStyles == null) {
  70 
  71             if (selectors == null || selectors.isEmpty()) {
  72                 cascadingStyles = Collections.emptyMap();
  73                 return cascadingStyles;
  74             }
  75 
  76             //
  77             // Creating the map is a three step process. First, create
  78             // a list of CascadingStyles. Second, sort the CascadingStyles.
  79             // And, finally, loop through the CascadingStyles to put them
  80             // into the Map by property name.
  81             //
  82             List<CascadingStyle> cascadingStyleList = new ArrayList<>();
  83 
  84             int ordinal = 0;
  85             for (int i=0, iMax=selectors.size(); i<iMax; i++) {
  86 
  87                 final Selector selector = selectors.get(i);
  88 
  89                 final Match match = selector.createMatch();
  90 
  91                 final Rule rule = selector.getRule();
  92 
  93                 for (int d = 0, dmax = rule.getDeclarations().size(); d < dmax; d++) {
  94                     final Declaration decl = rule.getDeclarations().get(d);
  95 
  96                     // ordinal increments at declaration level since
  97                     // there may be more than one declaration for the
  98                     // same attribute within a selector or within a stylesheet
  99                     final CascadingStyle s = new CascadingStyle(decl, match, ordinal++);
 100 
 101                     cascadingStyleList.add(s);
 102 
 103                 }
 104             }
 105 
 106             if (cascadingStyleList.isEmpty()) {
 107                 cascadingStyles = Collections.emptyMap();
 108                 return cascadingStyles;
 109             }
 110 
 111             // apply the cascade. CascadingStyle's primary sort key is the
 112             // property name, so the same properties should be in sequence.
 113             Collections.sort(cascadingStyleList, cascadingStyleComparator);
 114 
 115             // there may be more entries in this HashMap than we need if there
 116             // is more than one CascadingStyle per property. But this is
 117             // still better than
 118             final int nCascadingStyles = cascadingStyleList.size();
 119             cascadingStyles = new HashMap<>(nCascadingStyles);
 120 
 121             CascadingStyle cascadingStyle = cascadingStyleList.get(0);
 122             String property = cascadingStyle.getProperty();
 123 
 124 
 125             for (int fromIndex=0; fromIndex<nCascadingStyles; /*increment is in code*/) {
 126 
 127                 List<CascadingStyle> value = cascadingStyles.get(property);
 128                 if (value == null)  {
 129 
 130                     int toIndex = fromIndex;
 131                     final String currentProperty = property;
 132 
 133                     while (++toIndex < nCascadingStyles) {
 134                         cascadingStyle = cascadingStyleList.get(toIndex);
 135                         property = cascadingStyle.getProperty();
 136                         if (property.equals(currentProperty) == false) break;
 137                     }
 138 
 139                     cascadingStyles.put(currentProperty, cascadingStyleList.subList(fromIndex, toIndex));
 140 
 141                     fromIndex = toIndex;
 142 
 143 
 144                 } else {
 145                     // logic is broken!
 146                     assert(false);
 147                 }
 148             }
 149 
 150             selectors.clear();
 151             selectors = null;
 152 
 153         }
 154 
 155         return cascadingStyles;
 156     }
 157 
 158     private static final Comparator<CascadingStyle> cascadingStyleComparator =
 159             (o1, o2) -> {
 160 
 161                 //
 162                 // primary sort is on property. If the property names are the same,
 163                 // then go through the cascade logic. Otherwise, sort by property
 164                 // name since the cascade doesn't matter for dissimilar properties.
 165                 //
 166                 // What we want to end up with is a list where all the same properties
 167                 // are together in the list.
 168                 //
 169                 String thisProperty = o1.getProperty();
 170                 String otherProperty = o2.getProperty();
 171 
 172                 int c = thisProperty.compareTo(otherProperty);
 173                 if (c != 0) return c;
 174 
 175                 return o1.compareTo(o2);
 176 
 177             };
 178 
 179     private final int id; // unique per container
 180     private List<Selector> selectors;
 181     private Map<String, List<CascadingStyle>> cascadingStyles;
 182 }