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 
  33 package com.oracle.javafx.scenebuilder.kit.editor.job.gridpane.v2;
  34 
  35 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject;
  36 import com.oracle.javafx.scenebuilder.kit.util.GridBounds;
  37 import javafx.geometry.HPos;
  38 import javafx.geometry.VPos;
  39 import javafx.scene.Node;
  40 import javafx.scene.layout.GridPane;
  41 import javafx.scene.layout.Priority;
  42 
  43 /**
  44  *
  45  */
  46 public class GridSnapshotItem {
  47 
  48         private final Integer columnIndex;
  49         private final Integer rowIndex;
  50         private final Integer columnSpan;
  51         private final Integer rowSpan;
  52         private final Priority vgrow;
  53         private final Priority hgrow;
  54         private final VPos valignment;
  55         private final HPos halignment;
  56 
  57         public GridSnapshotItem(FXOMObject fxomObject) {
  58             assert fxomObject != null;
  59             assert fxomObject.getSceneGraphObject() instanceof Node;
  60 
  61             final Node node = (Node) fxomObject.getSceneGraphObject();
  62             this.columnIndex = GridPane.getColumnIndex(node);
  63             this.rowIndex = GridPane.getRowIndex(node);
  64             this.columnSpan = GridPane.getColumnSpan(node);
  65             this.rowSpan = GridPane.getRowSpan(node);
  66             this.vgrow = GridPane.getVgrow(node);
  67             this.hgrow = GridPane.getHgrow(node);
  68             this.valignment = GridPane.getValignment(node);
  69             this.halignment = GridPane.getHalignment(node);
  70         }
  71 
  72         public GridSnapshotItem(FXOMObject fxomObject, int columnIndex, int rowIndex) {
  73             assert fxomObject != null;
  74             assert fxomObject.getSceneGraphObject() instanceof Node;
  75             assert columnIndex >= 0;
  76             assert rowIndex >= 0;
  77 
  78             this.columnIndex = columnIndex;
  79             this.rowIndex = rowIndex;
  80             this.columnSpan = null;
  81             this.rowSpan = null;
  82             this.vgrow = null;
  83             this.hgrow = null;
  84             this.valignment = null;
  85             this.halignment = null;
  86         }
  87 
  88         public int getColumnIndex() {
  89             return (columnIndex == null) ? 0 : columnIndex;
  90         }
  91 
  92         public int getRowIndex() {
  93             return (rowIndex == null) ? 0 : rowIndex;
  94         }
  95 
  96         public int getColumnSpan() {
  97             return (columnSpan == null) ? 1 : columnSpan;
  98         }
  99 
 100         public int getRowSpan() {
 101             return (rowSpan == null) ? 1 : rowSpan;
 102         }
 103 
 104         public Priority getVgrow() {
 105             return vgrow;
 106         }
 107 
 108         public Priority getHgrow() {
 109             return hgrow;
 110         }
 111 
 112         public VPos getValignment() {
 113             return valignment;
 114         }
 115 
 116         public HPos getHalignment() {
 117             return halignment;
 118         }
 119 
 120         public GridBounds getBounds() {
 121             final int actualColumnIndex = (columnIndex == null) ? 0 : columnIndex;
 122             final int actualRowIndex = (rowIndex == null) ? 0 : rowIndex;
 123 
 124             final int actualColumnSpan;
 125             if ((columnSpan == null) || (columnSpan == GridPane.REMAINING)) {
 126                 actualColumnSpan = 1;
 127             } else {
 128                 actualColumnSpan = columnSpan;
 129             }
 130             final int actualRowSpan;
 131             if ((rowSpan == null) || (rowSpan == GridPane.REMAINING)) {
 132                 actualRowSpan = 1;
 133             } else {
 134                 actualRowSpan = rowSpan;
 135             }
 136 
 137             return new GridBounds(actualColumnIndex, actualRowIndex, actualColumnSpan, actualRowSpan);
 138         }
 139 }