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.BatchSelectionJob;
  37 import com.oracle.javafx.scenebuilder.kit.editor.job.Job;
  38 import com.oracle.javafx.scenebuilder.kit.editor.selection.AbstractSelectionGroup;
  39 import com.oracle.javafx.scenebuilder.kit.editor.selection.GridSelectionGroup;
  40 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance;
  41 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject;
  42 import com.oracle.javafx.scenebuilder.kit.metadata.property.value.list.RowConstraintsListPropertyMetadata;
  43 import com.oracle.javafx.scenebuilder.kit.metadata.util.InspectorPath;
  44 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  45 import java.util.ArrayList;
  46 import java.util.Collections;
  47 import java.util.List;
  48 import javafx.scene.layout.GridPane;
  49 
  50 /**
  51  *
  52  */
  53 public class InsertRowJob extends BatchSelectionJob {
  54 
  55     private static final RowConstraintsListPropertyMetadata rowContraintsMeta =
  56             new RowConstraintsListPropertyMetadata(
  57                 new PropertyName("rowConstraints"), //NOI18N
  58                 true, /* readWrite */
  59                 Collections.emptyList(), /* defaultValue */
  60                 InspectorPath.UNUSED);
  61 
  62     private final FXOMInstance gridPaneObject;
  63     private final int rowIndex;
  64     private final int insertCount;
  65 
  66     public InsertRowJob(FXOMObject gridPaneObject,
  67             int rowIndex, int insertCount, EditorController editorController) {
  68         super(editorController);
  69 
  70         assert gridPaneObject instanceof FXOMInstance;
  71         assert gridPaneObject.getSceneGraphObject() instanceof GridPane;
  72         assert rowIndex >= 0;
  73         assert rowIndex <= rowContraintsMeta.getValue((FXOMInstance)gridPaneObject).size();
  74         assert insertCount >= 1;
  75 
  76         this.gridPaneObject = (FXOMInstance)gridPaneObject;
  77         this.rowIndex = rowIndex;
  78         this.insertCount = insertCount;
  79     }
  80 
  81     /*
  82      * CompositeJob
  83      */
  84 
  85     @Override
  86     protected List<Job> makeSubJobs() {
  87         final List<Job> result = new ArrayList<>();
  88 
  89         final Job insertJob
  90                 = new InsertRowConstraintsJob(gridPaneObject, rowIndex, insertCount, getEditorController());
  91         result.add(insertJob);
  92 
  93         final int lastRowIndex = rowContraintsMeta.getValue(gridPaneObject).size()-1;
  94         for (int r = lastRowIndex; r >= rowIndex; r--) {
  95             final Job moveJob
  96                     = new MoveRowContentJob(gridPaneObject, r, +insertCount, getEditorController());
  97             if (moveJob.isExecutable()) {
  98                 result.add(moveJob);
  99             } // else column is empty : no children to move
 100         }
 101 
 102         return result;
 103     }
 104 
 105     @Override
 106     protected String makeDescription() {
 107         return getClass().getSimpleName();
 108     }
 109 
 110     @Override
 111     protected AbstractSelectionGroup getNewSelectionGroup() {
 112         return new GridSelectionGroup(gridPaneObject, GridSelectionGroup.Type.ROW, rowIndex);
 113     }
 114 }