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.shape.Rectangle;
  44 
  45 /**
  46  *
  47  *
  48  */
  49 public class RectangleResizer extends AbstractResizer<Rectangle> {
  50 
  51     private final double originalWidth;
  52     private final double originalHeight;
  53     private final PropertyName widthName  = new PropertyName("width"); //NOI18N
  54     private final PropertyName heightName = new PropertyName("height"); //NOI18N
  55     private final List<PropertyName> propertyNames = new ArrayList<>();
  56 
  57     public RectangleResizer(Rectangle sceneGraphObject) {
  58         super(sceneGraphObject);
  59         originalWidth  = sceneGraphObject.getWidth();
  60         originalHeight = sceneGraphObject.getHeight();
  61         propertyNames.add(widthName);
  62         propertyNames.add(heightName);
  63     }
  64 
  65     /*
  66      * AbstractResizer
  67      */
  68 
  69     @Override
  70     public final Bounds computeBounds(double width, double height) {
  71         final double minX = sceneGraphObject.getX();
  72         final double minY = sceneGraphObject.getY();
  73         return new BoundingBox(minX, minY, Math.round(width), Math.round(height));
  74     }
  75 
  76     @Override
  77     public Feature getFeature() {
  78         return Feature.FREE;
  79     }
  80 
  81     @Override
  82     public void changeWidth(double width) {
  83         sceneGraphObject.setWidth(Math.round(width));
  84     }
  85 
  86     @Override
  87     public void changeHeight(double height) {
  88         sceneGraphObject.setHeight(Math.round(height));
  89     }
  90 
  91     @Override
  92     public void revertToOriginalSize() {
  93         sceneGraphObject.setWidth(originalWidth);
  94         sceneGraphObject.setHeight(originalHeight);
  95     }
  96 
  97     @Override
  98     public List<PropertyName> getPropertyNames() {
  99         return propertyNames;
 100     }
 101 
 102     @Override
 103     public Object getValue(PropertyName propertyName) {
 104         assert propertyName != null;
 105         assert propertyNames.contains(propertyName);
 106 
 107         final Object result;
 108         if (propertyName.equals(widthName)) {
 109             result = sceneGraphObject.getWidth();
 110         } else if (propertyName.equals(heightName)) {
 111             result = sceneGraphObject.getHeight();
 112         } else {
 113             // Emergency code
 114             result = null;
 115         }
 116 
 117         return result;
 118     }
 119 
 120     @Override
 121     public Map<PropertyName, Object> getChangeMap() {
 122         final Map<PropertyName, Object> result = new HashMap<>();
 123         if (MathUtils.equals(sceneGraphObject.getWidth(), originalWidth) == false) {
 124             result.put(widthName, sceneGraphObject.getWidth());
 125         }
 126         if (MathUtils.equals(sceneGraphObject.getHeight(), originalHeight) == false) {
 127             result.put(heightName, sceneGraphObject.getHeight());
 128         }
 129         return result;
 130     }
 131 
 132 }