modules/graphics/src/main/java/javafx/css/converter/InsetsConverter.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.Size;
  29 import com.sun.javafx.css.StyleConverterImpl;
  30 import javafx.css.ParsedValue;
  31 import javafx.css.StyleConverter;
  32 import javafx.geometry.Insets;
  33 import javafx.scene.text.Font;
  34 
  35 /**
  36  * Convert <size>{1,4} to Insets. The size values are interpreted as
  37  * top, right, bottom, left.
  38  * If only top is given, that value is used on all sides.
  39  * If there is only top and right, then bottom is set to top and left is set to right.
  40  * If top, right and bottom are given, then left is set to right.


  41  */
  42 public final class InsetsConverter extends StyleConverterImpl<ParsedValue[], Insets> {
  43 
  44     // lazy, thread-safe instatiation
  45     private static class Holder {
  46         static final InsetsConverter INSTANCE = new InsetsConverter();
  47         static final SequenceConverter SEQUENCE_INSTANCE = new SequenceConverter();
  48     }
  49 
  50     public static StyleConverter<ParsedValue[], Insets> getInstance() {
  51         return Holder.INSTANCE;
  52     }
  53 
  54     private InsetsConverter() {
  55         super();
  56     }
  57 
  58     @Override
  59     public Insets convert(ParsedValue<ParsedValue[], Insets> value, Font font) {
  60         ParsedValue[] sides = value.getValue();
  61         double top = ((Size)sides[0].convert(font)).pixels(font);
  62         double right = (sides.length > 1) ? ((Size)sides[1].convert(font)).pixels(font) : top;
  63         double bottom = (sides.length > 2) ? ((Size)sides[2].convert(font)).pixels(font) : top;
  64         double left = (sides.length > 3) ? ((Size)sides[3].convert(font)).pixels(font) : right;
  65         return new Insets(top, right, bottom, left);
  66     }
  67 
  68     @Override
  69     public String toString() {
  70         return "InsetsConverter";
  71     }
  72 
  73     /**
  74      * Convert a <size>{1,4} [,<size>{1,4}]*  an Insets[]
  75      */
  76     public static final class SequenceConverter extends StyleConverterImpl<ParsedValue<ParsedValue[], Insets>[], Insets[]> {
  77 
  78         public static SequenceConverter getInstance() {
  79             return Holder.SEQUENCE_INSTANCE;
  80         }
  81 
  82         private SequenceConverter() {
  83             super();
  84         }
  85 
  86         @Override
  87         public Insets[] convert(ParsedValue<ParsedValue<ParsedValue[], Insets>[], Insets[]> value, Font font) {
  88             ParsedValue<ParsedValue[], Insets>[] layers = value.getValue();
  89             Insets[] insets = new Insets[layers.length];
  90             for (int layer = 0; layer < layers.length; layer++) {
  91                 insets[layer] = InsetsConverter.getInstance().convert(layers[layer], font);
  92             }
  93             return insets;
  94         }
  95 
  96         @Override


   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.Size;

  29 import javafx.css.ParsedValue;
  30 import javafx.css.StyleConverter;
  31 import javafx.geometry.Insets;
  32 import javafx.scene.text.Font;
  33 
  34 /**
  35  * Convert <size>{1,4} to Insets. The size values are interpreted as
  36  * top, right, bottom, left.
  37  * If only top is given, that value is used on all sides.
  38  * If there is only top and right, then bottom is set to top and left is set to right.
  39  * If top, right and bottom are given, then left is set to right.
  40  *
  41  * @since 9
  42  */
  43 public final class InsetsConverter extends StyleConverter<ParsedValue[], Insets> {
  44 
  45     // lazy, thread-safe instatiation
  46     private static class Holder {
  47         static final InsetsConverter INSTANCE = new InsetsConverter();
  48         static final SequenceConverter SEQUENCE_INSTANCE = new SequenceConverter();
  49     }
  50 
  51     public static StyleConverter<ParsedValue[], Insets> getInstance() {
  52         return Holder.INSTANCE;
  53     }
  54 
  55     private InsetsConverter() {
  56         super();
  57     }
  58 
  59     @Override
  60     public Insets convert(ParsedValue<ParsedValue[], Insets> value, Font font) {
  61         ParsedValue[] sides = value.getValue();
  62         double top = ((Size)sides[0].convert(font)).pixels(font);
  63         double right = (sides.length > 1) ? ((Size)sides[1].convert(font)).pixels(font) : top;
  64         double bottom = (sides.length > 2) ? ((Size)sides[2].convert(font)).pixels(font) : top;
  65         double left = (sides.length > 3) ? ((Size)sides[3].convert(font)).pixels(font) : right;
  66         return new Insets(top, right, bottom, left);
  67     }
  68 
  69     @Override
  70     public String toString() {
  71         return "InsetsConverter";
  72     }
  73 
  74     /**
  75      * Convert a <size>{1,4} [,<size>{1,4}]*  an Insets[]
  76      */
  77     public static final class SequenceConverter extends StyleConverter<ParsedValue<ParsedValue[], Insets>[], Insets[]> {
  78 
  79         public static SequenceConverter getInstance() {
  80             return Holder.SEQUENCE_INSTANCE;
  81         }
  82 
  83         private SequenceConverter() {
  84             super();
  85         }
  86 
  87         @Override
  88         public Insets[] convert(ParsedValue<ParsedValue<ParsedValue[], Insets>[], Insets[]> value, Font font) {
  89             ParsedValue<ParsedValue[], Insets>[] layers = value.getValue();
  90             Insets[] insets = new Insets[layers.length];
  91             for (int layer = 0; layer < layers.length; layer++) {
  92                 insets[layer] = InsetsConverter.getInstance().convert(layers[layer], font);
  93             }
  94             return insets;
  95         }
  96 
  97         @Override