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; 33 34 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName; 35 import com.oracle.javafx.scenebuilder.kit.util.MathUtils; 36 import java.util.ArrayList; 37 import java.util.HashMap; 38 import java.util.List; 39 import java.util.Map; 40 import javafx.geometry.BoundingBox; 41 import javafx.geometry.Bounds; 42 import javafx.scene.layout.Region; 43 44 /** 45 * 46 * 47 */ 48 public class RegionResizer extends AbstractResizer<Region> { 49 50 private final double originalMinWidth; 51 private final double originalMinHeight; 52 private final double originalPrefWidth; 53 private final double originalPrefHeight; 54 private final double originalMaxWidth; 55 private final double originalMaxHeight; 56 private final PropertyName minWidthName = new PropertyName("minWidth"); //NOI18N 57 private final PropertyName minHeightName = new PropertyName("minHeight"); //NOI18N 58 private final PropertyName prefWidthName = new PropertyName("prefWidth"); //NOI18N 59 private final PropertyName prefHeightName = new PropertyName("prefHeight"); //NOI18N 60 private final PropertyName maxWidthName = new PropertyName("maxWidth"); //NOI18N 61 private final PropertyName maxHeightName = new PropertyName("maxHeight"); //NOI18N 62 private final List<PropertyName> propertyNames = new ArrayList<>(); 63 64 public RegionResizer(Region sceneGraphObject) { 65 super(sceneGraphObject); 66 originalMinWidth = sceneGraphObject.getMinWidth(); 67 originalMinHeight = sceneGraphObject.getMinHeight(); 68 originalPrefWidth = sceneGraphObject.getPrefWidth(); 69 originalPrefHeight = sceneGraphObject.getPrefHeight(); 70 originalMaxWidth = sceneGraphObject.getMaxWidth(); 71 originalMaxHeight = sceneGraphObject.getMaxHeight(); 72 propertyNames.add(prefWidthName); 73 propertyNames.add(prefHeightName); 74 propertyNames.add(minWidthName); 75 propertyNames.add(minHeightName); 76 propertyNames.add(maxWidthName); 77 propertyNames.add(maxHeightName); 78 } 79 80 public static String makeSizeString(double size) { 81 final String result; 82 if (size == Double.MAX_VALUE) { 83 result = "MAX_VALUE"; //NOI18N 84 } else { 85 result = String.valueOf(size); 86 } 87 return result; 88 } 89 90 public static String makeComputedSizeString(double size) { 91 final String result; 92 if (size == Region.USE_COMPUTED_SIZE) { 93 result = "USE_COMPUTED_SIZE"; //NOI18N 94 } else { 95 result = makeSizeString(size); 96 } 97 return result; 98 } 99 100 public static String makePrefSizeString(double size) { 101 final String result; 102 if (size == Region.USE_PREF_SIZE) { 103 result = "USE_PREF_SIZE"; //NOI18N 104 } else { 105 result = makeComputedSizeString(size); 106 } 107 return result; 108 } 109 110 /* 111 * AbstractResizer 112 */ 113 114 @Override 115 public final Bounds computeBounds(double width, double height) { 116 return new BoundingBox(0, 0, Math.round(width), Math.round(height)); 117 } 118 119 120 @Override 121 public Feature getFeature() { 122 return Feature.FREE; 123 } 124 125 @Override 126 public void changeWidth(double weight) { 127 final double w = Math.round(weight); 128 129 sceneGraphObject.setPrefWidth(w); 130 131 if ((originalMinWidth != Region.USE_COMPUTED_SIZE) && (originalMinWidth != Region.USE_PREF_SIZE)) { 132 sceneGraphObject.setMinWidth(Math.min(w, originalMinWidth)); 133 } 134 if ((originalMaxWidth != Region.USE_COMPUTED_SIZE) && (originalMaxWidth != Region.USE_PREF_SIZE)) { 135 sceneGraphObject.setMaxWidth(Math.max(w, originalMaxWidth)); 136 } 137 } 138 139 @Override 140 public void changeHeight(double height) { 141 final double h = Math.round(height); 142 143 sceneGraphObject.setPrefHeight(h); 144 145 if ((originalMinHeight != Region.USE_COMPUTED_SIZE) && (originalMinHeight != Region.USE_PREF_SIZE)) { 146 sceneGraphObject.setMinHeight(Math.min(h, originalMinHeight)); 147 } 148 if ((originalMaxHeight != Region.USE_COMPUTED_SIZE) && (originalMaxHeight != Region.USE_PREF_SIZE)) { 149 sceneGraphObject.setMaxHeight(Math.max(h, originalMaxHeight)); 150 } 151 } 152 153 @Override 154 public void revertToOriginalSize() { 155 sceneGraphObject.setMinWidth(originalMinWidth); 156 sceneGraphObject.setMinHeight(originalMinHeight); 157 sceneGraphObject.setPrefWidth(originalPrefWidth); 158 sceneGraphObject.setPrefHeight(originalPrefHeight); 159 sceneGraphObject.setMaxWidth(originalMaxWidth); 160 sceneGraphObject.setMaxHeight(originalMaxHeight); 161 } 162 163 @Override 164 public List<PropertyName> getPropertyNames() { 165 return propertyNames; 166 } 167 168 @Override 169 public Object getValue(PropertyName propertyName) { 170 assert propertyName != null; 171 assert propertyNames.contains(propertyName); 172 173 final Object result; 174 if (propertyName.equals(minWidthName)) { 175 result = makePrefSizeString(sceneGraphObject.getMinWidth()); 176 } else if (propertyName.equals(minHeightName)) { 177 result = makePrefSizeString(sceneGraphObject.getMinHeight()); 178 } else if (propertyName.equals(prefWidthName)) { 179 result = makeComputedSizeString(sceneGraphObject.getPrefWidth()); 180 } else if (propertyName.equals(prefHeightName)) { 181 result = makeComputedSizeString(sceneGraphObject.getPrefHeight()); 182 } else if (propertyName.equals(maxWidthName)) { 183 result = makePrefSizeString(sceneGraphObject.getMaxWidth()); 184 } else if (propertyName.equals(maxHeightName)) { 185 result = makePrefSizeString(sceneGraphObject.getMaxHeight()); 186 } else { 187 // Emergency code 188 result = null; 189 } 190 191 return result; 192 } 193 194 @Override 195 public Map<PropertyName, Object> getChangeMap() { 196 final Map<PropertyName, Object> result = new HashMap<>(); 197 if (MathUtils.equals(sceneGraphObject.getMinWidth(), originalMinWidth) == false) { 198 result.put(minWidthName, sceneGraphObject.getMinWidth()); 199 } 200 if (MathUtils.equals(sceneGraphObject.getMinHeight(), originalMinHeight) == false) { 201 result.put(minHeightName, sceneGraphObject.getMinHeight()); 202 } 203 if (MathUtils.equals(sceneGraphObject.getPrefWidth(), originalPrefWidth) == false) { 204 result.put(prefWidthName, sceneGraphObject.getPrefWidth()); 205 } 206 if (MathUtils.equals(sceneGraphObject.getPrefHeight(), originalPrefHeight) == false) { 207 result.put(prefHeightName, sceneGraphObject.getPrefHeight()); 208 } 209 if (MathUtils.equals(sceneGraphObject.getMaxWidth(), originalMaxWidth) == false) { 210 result.put(maxWidthName, sceneGraphObject.getMaxWidth()); 211 } 212 if (MathUtils.equals(sceneGraphObject.getMaxHeight(), originalMaxHeight) == false) { 213 result.put(maxHeightName, sceneGraphObject.getMaxHeight()); 214 } 215 return result; 216 } 217 }