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.fxom;
  33 
  34 import com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueElement;
  35 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import java.util.Map;
  39 
  40 /**
  41  *
  42  *
  43  */
  44 class TransientProperty extends TransientNode {
  45 
  46     private final PropertyName name;
  47     private final GlueElement propertyElement;
  48     private final List<FXOMObject> values = new ArrayList<>();
  49     private final List<FXOMProperty> collectedProperties = new ArrayList<>();
  50 
  51     public TransientProperty(
  52             TransientNode parentNode,
  53             PropertyName name,
  54             GlueElement propertyElement) {
  55         super(parentNode);
  56 
  57         assert name != null;
  58         assert propertyElement != null;
  59         assert propertyElement.getTagName().equals(name.toString());
  60 
  61         this.name = name;
  62         this.propertyElement = propertyElement;
  63     }
  64 
  65     public List<FXOMObject> getValues() {
  66         return values;
  67     }
  68 
  69     public List<FXOMProperty> getCollectedProperties() {
  70         return collectedProperties;
  71     }
  72 
  73     public FXOMProperty makeFxomProperty(FXOMDocument fxomDocument) {
  74         final FXOMProperty result;
  75 
  76         if (collectedProperties.isEmpty()) {
  77             /*
  78              * Two cases:
  79              *
  80              * 1) (values.size() == 0)
  81              *
  82              *    => it's a textual property expressed as plain text
  83              *    => for example with Button.text:
  84              *    <Button><text>OK</text></Button>
  85              *
  86              * 2) (values.size() == 1) &&
  87              *    (values.get(0).properties().size() == 0) &&
  88              *    (values.get(0).getFxValue() != null
  89              *
  90              *    => it's a textual property expressed with fx:value
  91              *    => we create an FXOMPropertyT instance
  92              *    => for example with Button.text:
  93              *
  94              *    <Button><text><String fx:value="OK"/></text></Button>
  95              *
  96              *
  97              * 3) else
  98              *
  99              *    => it's a complex property
 100              *    => we create an FXOMPropertyC instance
 101              */
 102 
 103             if (values.isEmpty()) {
 104                 // Case #1
 105                 assert propertyElement.getChildren().isEmpty();
 106                 assert propertyElement.getContent().isEmpty() == false;
 107                 result = new FXOMPropertyT(fxomDocument, name,
 108                         propertyElement, null, propertyElement.getContentText());
 109             }
 110             else if ((values.size() == 1) && (values.get(0) instanceof FXOMInstance)) {
 111                 final FXOMInstance value = (FXOMInstance) values.get(0);
 112                 final String fxValue = value.getFxValue();
 113                 if (fxValue != null) {
 114                     // Case #2
 115                     result = new FXOMPropertyT(fxomDocument, name,
 116                             propertyElement, value.getGlueElement(), fxValue);
 117                 } else {
 118                     // Case #3
 119                     result = new FXOMPropertyC(fxomDocument, name,
 120                             values, propertyElement);
 121                 }
 122             } else {
 123                 // Case #3
 124                 result = new FXOMPropertyC(fxomDocument, name,
 125                         values, propertyElement);
 126             }
 127         } else {
 128             // It is a property of type Map ; currently we don't support
 129             // map property editing ; so we create a fake value.
 130             assert getSceneGraphObject() instanceof Map;
 131             result = new FXOMPropertyT(fxomDocument, name, "fake-value");
 132         }
 133 
 134         return result;
 135     }
 136 }