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