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.editor.EditorController;
  36 import com.oracle.javafx.scenebuilder.kit.editor.job.Job;
  37 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance;
  38 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject;
  39 import com.oracle.javafx.scenebuilder.kit.metadata.property.value.list.RowConstraintsListPropertyMetadata;
  40 import com.oracle.javafx.scenebuilder.kit.metadata.util.InspectorPath;
  41 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  42 import java.util.ArrayList;
  43 import java.util.Collections;
  44 import java.util.List;
  45 import javafx.scene.layout.GridPane;
  46 import javafx.scene.layout.RowConstraints;
  47 
  48 /**
  49  *
  50  */
  51 public class InsertRowConstraintsJob extends Job {
  52 
  53     private static final RowConstraintsListPropertyMetadata rowContraintsMeta =
  54             new RowConstraintsListPropertyMetadata(
  55                 new PropertyName("rowConstraints"), //NOI18N
  56                 true, /* readWrite */
  57                 Collections.emptyList(), /* defaultValue */
  58                 InspectorPath.UNUSED);
  59 
  60     private final FXOMInstance gridPaneObject;
  61     private final int rowIndex;
  62     private final int insertCount;
  63 
  64     public InsertRowConstraintsJob(FXOMObject gridPaneObject,
  65             int rowIndex, int insertCount, EditorController editorController) {
  66         super(editorController);
  67 
  68         assert gridPaneObject instanceof FXOMInstance;
  69         assert gridPaneObject.getSceneGraphObject() instanceof GridPane;
  70         assert rowIndex >= 0;
  71         assert rowIndex <= rowContraintsMeta.getValue((FXOMInstance)gridPaneObject).size();
  72         assert insertCount >= 1;
  73 
  74         this.gridPaneObject = (FXOMInstance)gridPaneObject;
  75         this.rowIndex = rowIndex;
  76         this.insertCount = insertCount;
  77     }
  78 
  79     /*
  80      * Job
  81      */
  82     @Override
  83     public boolean isExecutable() {
  84         return true;
  85     }
  86 
  87     @Override
  88     public void execute() {
  89         // Same as redo()
  90         redo();
  91     }
  92 
  93     @Override
  94     public void undo() {
  95         final List<RowConstraints> constraintsList
  96                 = new ArrayList<>(rowContraintsMeta.getValue(gridPaneObject));
  97         assert rowIndex < constraintsList.size();
  98         for (int i = 0; i < insertCount; i++) {
  99             constraintsList.remove(rowIndex);
 100         }
 101         rowContraintsMeta.setValue(gridPaneObject, constraintsList);
 102     }
 103 
 104     @Override
 105     public void redo() {
 106         final List<RowConstraints> constraintsList
 107                 = new ArrayList<>(rowContraintsMeta.getValue(gridPaneObject));
 108         final RowConstraints template;
 109         if (rowIndex >= 1) {
 110             template = constraintsList.get(rowIndex-1);
 111         } else {
 112             template = null;
 113         }
 114         for (int i = 0; i < insertCount; i++) {
 115             constraintsList.add(rowIndex, makeRowConstraints(template));
 116         }
 117         rowContraintsMeta.setValue(gridPaneObject, constraintsList);
 118     }
 119 
 120     @Override
 121     public String getDescription() {
 122         return getClass().getSimpleName();
 123     }
 124 
 125 
 126     /*
 127      * Private
 128      */
 129 
 130     private RowConstraints makeRowConstraints(RowConstraints template) {
 131         final RowConstraints result = new RowConstraints();
 132         if (rowIndex >= 1) {
 133             result.setFillHeight(template.isFillHeight());
 134             result.setValignment(template.getValignment());
 135             result.setVgrow(template.getVgrow());
 136             result.setMaxHeight(template.getMaxHeight());
 137             result.setMinHeight(template.getMinHeight());
 138             result.setPercentHeight(template.getPercentHeight());
 139             result.setPrefHeight(template.getPrefHeight());
 140         }
 141         return result;
 142     }
 143 }