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;
  33 
  34 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance;
  35 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  36 import com.oracle.javafx.scenebuilder.kit.metadata.util.InspectorPath;
  37 import java.util.HashMap;
  38 import java.util.Map;
  39 
  40 /**
  41  *
  42  *
  43  */
  44 public abstract class ValuePropertyMetadata extends PropertyMetadata {
  45 
  46     private final boolean readWrite;
  47     private final InspectorPath inspectorPath;
  48 
  49 
  50     private final Map<Class<?>, Object> defaultValueAlternatives = new HashMap<>();
  51 
  52     public ValuePropertyMetadata(PropertyName name, boolean readWrite, InspectorPath inspectorPath) {
  53         super(name);
  54         this.readWrite = readWrite;
  55         this.inspectorPath = inspectorPath;
  56     }
  57 
  58     public boolean isReadWrite() {
  59         return readWrite;
  60     }
  61 
  62 
  63     public InspectorPath getInspectorPath() {
  64         return inspectorPath;
  65     }
  66 
  67     public abstract Class<?> getValueClass();
  68     public abstract Object getDefaultValueObject();
  69     public abstract Object getValueObject(FXOMInstance fxomInstance);
  70     public abstract void setValueObject(FXOMInstance fxomInstance, Object valueObject);
  71 
  72     public Map<Class<?>, Object> getDefaultValueAlternatives() {
  73         return defaultValueAlternatives;
  74     }
  75 
  76 
  77     /**
  78      * Returns true if getName().getResidenceClass() != null.
  79      * @return true if getName().getResidenceClass() != null.
  80      */
  81     public boolean isStaticProperty() {
  82         return getName().getResidenceClass() != null;
  83     }
  84 
  85     /**
  86      * Sets the property value in the scene graph object.
  87      * FXOM instance is unchanged.
  88      * Value is lost at next scene graph reconstruction.
  89      *
  90      * @param fxomInstance an fxom instance (never null)
  91      * @param value a value conform with the property typing
  92      */
  93     public void setValueInSceneGraphObject(FXOMInstance fxomInstance, Object value) {
  94         assert fxomInstance != null;
  95         assert fxomInstance.getSceneGraphObject() != null;
  96         getName().setValue(fxomInstance.getSceneGraphObject(), value);
  97     }
  98 
  99     /**
 100      * Gets the property value in the scene graph object.
 101      * Result might be different from getValueObject().
 102      * For example, if Button.text contains a resource key 'button-key'
 103      * and a resource bundle assign 'OK' to this key:
 104      *    - getValueObject() -> '%button-key'
 105      *    - getValueInSceneGraphObject() -> 'OK'
 106      *
 107      * @param fxomInstance an fxom instance (never null)
 108      * @return value of this property in the scene graph object associated
 109      *         fxomInstance
 110      */
 111     public Object getValueInSceneGraphObject(FXOMInstance fxomInstance) {
 112         assert fxomInstance != null;
 113         return getName().getValue(fxomInstance.getSceneGraphObject());
 114     }
 115 
 116     /*
 117      * Object
 118      */
 119 
 120     @Override
 121     public int hashCode() {  // To please FindBugs
 122         return super.hashCode();
 123     }
 124 
 125     @Override
 126     public boolean equals(Object obj) {  // To please FindBugs
 127         if (obj == null) {
 128             return false;
 129         }
 130         if (PropertyMetadata.class != obj.getClass()) {
 131             return false;
 132         }
 133 
 134         return super.equals(obj);
 135     }
 136 
 137 }