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