1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package com.oracle.javafx.scenebuilder.kit.metadata.property.value;
  33 
  34 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument;
  35 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance;
  36 import com.oracle.javafx.scenebuilder.kit.metadata.util.InspectorPath;
  37 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  38 import com.oracle.javafx.scenebuilder.kit.util.MathUtils;
  39 import java.util.Objects;
  40 import javafx.geometry.HPos;
  41 import javafx.geometry.VPos;
  42 import javafx.scene.layout.Priority;
  43 import javafx.scene.layout.ColumnConstraints;
  44 
  45 /**
  46  *
  47  */
  48 public class ColumnConstraintsPropertyMetadata extends ComplexPropertyMetadata<ColumnConstraints> {
  49 
  50     private static final ColumnConstraints DEFAULT = new ColumnConstraints();
  51 
  52     private final BooleanPropertyMetadata fillWidthMetadata
  53             = new BooleanPropertyMetadata(new PropertyName("fillWidth"),
  54                     true, DEFAULT.isFillWidth(), InspectorPath.UNUSED);
  55     private final DoublePropertyMetadata maxWidthMetadata
  56             = new DoublePropertyMetadata(new PropertyName("maxWidth"),
  57                     DoublePropertyMetadata.DoubleKind.USE_COMPUTED_SIZE, true,
  58                     DEFAULT.getMaxWidth(), InspectorPath.UNUSED);
  59     private final DoublePropertyMetadata minWidthMetadata
  60             = new DoublePropertyMetadata(new PropertyName("minWidth"),
  61             DoublePropertyMetadata.DoubleKind.USE_COMPUTED_SIZE, true,
  62                     DEFAULT.getMinWidth(), InspectorPath.UNUSED);
  63     private final DoublePropertyMetadata percentWidthMetadata
  64             = new DoublePropertyMetadata(new PropertyName("percentWidth"),
  65             DoublePropertyMetadata.DoubleKind.PERCENTAGE, true,
  66                     DEFAULT.getPercentWidth(), InspectorPath.UNUSED);
  67     private final DoublePropertyMetadata prefWidthMetadata
  68             = new DoublePropertyMetadata(new PropertyName("prefWidth"),
  69             DoublePropertyMetadata.DoubleKind.USE_PREF_SIZE, true,
  70                     DEFAULT.getPrefWidth(), InspectorPath.UNUSED);
  71     private final EnumerationPropertyMetadata halignmentMetadata
  72             = new EnumerationPropertyMetadata(new PropertyName("halignment"),
  73             VPos.class, EnumerationPropertyMetadata.EQUIV_INHERITED, true, InspectorPath.UNUSED);
  74     private final EnumerationPropertyMetadata hgrowMetadata
  75             = new EnumerationPropertyMetadata(new PropertyName("hgrow"),
  76             Priority.class, EnumerationPropertyMetadata.EQUIV_INHERITED, true, InspectorPath.UNUSED);
  77 
  78     public ColumnConstraintsPropertyMetadata(PropertyName name, boolean readWrite,
  79             ColumnConstraints defaultValue, InspectorPath inspectorPath) {
  80         super(name, ColumnConstraints.class, readWrite, defaultValue, inspectorPath);
  81     }
  82 
  83     /*
  84      * Utility
  85      */
  86 
  87     public static boolean equals(ColumnConstraints c1, ColumnConstraints c2) {
  88         assert c1 != null;
  89         assert c2 != null;
  90 
  91         final boolean result;
  92         if (c1 == c2) {
  93             result = true;
  94         } else {
  95             result = Objects.equals(c1.getHalignment(),c2.getHalignment())
  96                     && Objects.equals(c1.getHgrow(), c2.getHgrow())
  97                     && MathUtils.equals(c1.getMaxWidth(), c2.getMaxWidth())
  98                     && MathUtils.equals(c1.getMinWidth(), c2.getMinWidth())
  99                     && MathUtils.equals(c1.getPercentWidth(), c2.getPercentWidth())
 100                     && MathUtils.equals(c1.getPrefWidth(), c2.getPrefWidth());
 101         }
 102 
 103         return result;
 104     }
 105 
 106     /*
 107      * ComplexPropertyMetadata
 108      */
 109 
 110     @Override
 111     public FXOMInstance makeFxomInstanceFromValue(ColumnConstraints value, FXOMDocument fxomDocument) {
 112         final FXOMInstance result = new FXOMInstance(fxomDocument, value.getClass());
 113 
 114         fillWidthMetadata.setValue(result, value.isFillWidth());
 115         maxWidthMetadata.setValue(result, value.getMaxWidth());
 116         minWidthMetadata.setValue(result, value.getMinWidth());
 117         percentWidthMetadata.setValue(result, value.getPercentWidth());
 118         prefWidthMetadata.setValue(result, value.getPrefWidth());
 119 
 120         final HPos halignment = value.getHalignment();
 121         if (halignment == null) {
 122             halignmentMetadata.setValue(result, halignmentMetadata.getDefaultValue());
 123         } else {
 124             halignmentMetadata.setValue(result, halignment.toString());
 125         }
 126 
 127         final Priority hgrow = value.getHgrow();
 128         if (hgrow == null) {
 129             hgrowMetadata.setValue(result, hgrowMetadata.getDefaultValue());
 130         } else {
 131             hgrowMetadata.setValue(result, hgrow.toString());
 132         }
 133 
 134         return result;
 135     }
 136 
 137 }