modules/graphics/src/main/java/javafx/css/converter/ShapeConverter.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.converters;
  27 
  28 import com.sun.javafx.css.StyleConverterImpl;
  29 import javafx.css.ParsedValue;
  30 import javafx.css.StyleConverter;
  31 import javafx.scene.shape.SVGPath;
  32 import javafx.scene.shape.Shape;
  33 import javafx.scene.text.Font;
  34 
  35 import java.util.Map;
  36 
  37 /**
  38  * Converts an SVG shape string into a Shape object.


  39  */
  40 public class ShapeConverter extends StyleConverterImpl<String, Shape> {
  41     private static final ShapeConverter INSTANCE = new ShapeConverter();
  42 
  43     public static StyleConverter<String, Shape> getInstance() { return INSTANCE; }
  44 
  45     @Override public Shape convert(ParsedValue<String, Shape> value, Font font) {
  46 
  47         Shape shape = super.getCachedValue(value);
  48         if (shape != null) return shape;
  49 
  50         String svg = value.getValue();
  51         if (svg == null || svg.isEmpty()) return null;
  52         SVGPath path = new SVGPath();
  53         path.setContent(svg);
  54 
  55         super.cacheValue(value, path);
  56 
  57         return path;
  58     }
  59 
  60     private static Map<ParsedValue<String, Shape>, Shape> cache;


   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.converter;
  27 

  28 import javafx.css.ParsedValue;
  29 import javafx.css.StyleConverter;
  30 import javafx.scene.shape.SVGPath;
  31 import javafx.scene.shape.Shape;
  32 import javafx.scene.text.Font;
  33 
  34 import java.util.Map;
  35 
  36 /**
  37  * Converts an SVG shape string into a Shape object.
  38  *
  39  * @since 9
  40  */
  41 public class ShapeConverter extends StyleConverter<String, Shape> {
  42     private static final ShapeConverter INSTANCE = new ShapeConverter();
  43 
  44     public static StyleConverter<String, Shape> getInstance() { return INSTANCE; }
  45 
  46     @Override public Shape convert(ParsedValue<String, Shape> value, Font font) {
  47 
  48         Shape shape = super.getCachedValue(value);
  49         if (shape != null) return shape;
  50 
  51         String svg = value.getValue();
  52         if (svg == null || svg.isEmpty()) return null;
  53         SVGPath path = new SVGPath();
  54         path.setContent(svg);
  55 
  56         super.cacheValue(value, path);
  57 
  58         return path;
  59     }
  60 
  61     private static Map<ParsedValue<String, Shape>, Shape> cache;