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.editor.panel.content.driver.resizer.shape;
  33 
  34 import com.oracle.javafx.scenebuilder.kit.editor.panel.content.driver.resizer.AbstractResizer;
  35 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  36 import com.oracle.javafx.scenebuilder.kit.util.MathUtils;
  37 import java.util.ArrayList;
  38 import java.util.HashMap;
  39 import java.util.List;
  40 import java.util.Map;
  41 import javafx.geometry.BoundingBox;
  42 import javafx.geometry.Bounds;
  43 import javafx.scene.text.Text;
  44 
  45 /**
  46  *
  47  *
  48  */
  49 public class TextResizer extends AbstractResizer<Text> {
  50 
  51     private final double originalWrappingWidth;
  52     private final PropertyName wrappingWidthName = new PropertyName("wrappingWidth"); //NOI18N
  53     private final List<PropertyName> propertyNames = new ArrayList<>();
  54 
  55     public TextResizer(Text sceneGraphObject) {
  56         super(sceneGraphObject);
  57         originalWrappingWidth = sceneGraphObject.getWrappingWidth();
  58         propertyNames.add(wrappingWidthName);
  59     }
  60 
  61     /*
  62      * AbstractResizer
  63      */
  64 
  65     @Override
  66     public final Bounds computeBounds(double width, double height) {
  67         final double minX = sceneGraphObject.getX();
  68         final double minY = sceneGraphObject.getY();
  69         return new BoundingBox(minX, minY, width, height);
  70     }
  71 
  72     @Override
  73     public Feature getFeature() {
  74         return Feature.WIDTH_ONLY;
  75     }
  76 
  77     @Override
  78     public void changeWidth(double width) {
  79         // When wrappingWidth is set to 0, text will
  80         // jump from a nearer zero column to a single line.
  81         // Not nice : so we set a min of 1 to avoid this effect.
  82         sceneGraphObject.setWrappingWidth(Math.max(1.0, width));
  83     }
  84 
  85     @Override
  86     public void changeHeight(double height) {
  87     }
  88 
  89     @Override
  90     public void revertToOriginalSize() {
  91         sceneGraphObject.setWrappingWidth(originalWrappingWidth);
  92     }
  93 
  94     @Override
  95     public List<PropertyName> getPropertyNames() {
  96         return propertyNames;
  97     }
  98 
  99     @Override
 100     public Object getValue(PropertyName propertyName) {
 101         assert propertyName != null;
 102         assert propertyNames.contains(propertyName);
 103 
 104         final Object result;
 105         if (propertyName.equals(wrappingWidthName)) {
 106             result = sceneGraphObject.getWrappingWidth();
 107         } else {
 108             // Emergency code
 109             result = null;
 110         }
 111 
 112         return result;
 113     }
 114 
 115     @Override
 116     public Map<PropertyName, Object> getChangeMap() {
 117         final Map<PropertyName, Object> result = new HashMap<>();
 118         if (MathUtils.equals(sceneGraphObject.getWrappingWidth(), originalWrappingWidth) == false) {
 119             result.put(wrappingWidthName, sceneGraphObject.getWrappingWidth());
 120         }
 121         return result;
 122     }
 123 
 124 }