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.FXOMDocument;
  38 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance;
  39 import com.oracle.javafx.scenebuilder.kit.metadata.property.value.IntegerPropertyMetadata;
  40 import com.oracle.javafx.scenebuilder.kit.metadata.util.InspectorPath;
  41 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  42 import javafx.scene.Node;
  43 import javafx.scene.layout.GridPane;
  44 
  45 /**
  46  *
  47  */
  48 public class MoveCellContentJob extends Job {
  49 
  50     private static final IntegerPropertyMetadata columnIndexMeta =
  51             new IntegerPropertyMetadata(
  52                 new PropertyName("columnIndex", GridPane.class), //NOI18N
  53                 true, /* readWrite */
  54                 0, /* defaultValue */
  55                 InspectorPath.UNUSED);
  56     private static final IntegerPropertyMetadata rowIndexMeta =
  57             new IntegerPropertyMetadata(
  58                 new PropertyName("rowIndex", GridPane.class), //NOI18N
  59                 true, /* readWrite */
  60                 0, /* defaultValue */
  61                 InspectorPath.UNUSED);
  62 
  63     private final FXOMInstance fxomObject;
  64     private final int columnIndexDelta;
  65     private final int rowIndexDelta;
  66     private int oldColumnIndex = -1;
  67     private int oldRowIndex = -1;
  68 
  69     public MoveCellContentJob(FXOMInstance fxomObject,
  70             int columnIndexDelta, int rowIndexDelta,
  71             EditorController editorController) {
  72         super(editorController);
  73         assert fxomObject != null;
  74         assert fxomObject.getSceneGraphObject() instanceof Node;
  75 
  76         this.fxomObject = fxomObject;
  77         this.columnIndexDelta = columnIndexDelta;
  78         this.rowIndexDelta = rowIndexDelta;
  79     }
  80 
  81     /*
  82      * Job
  83      */
  84 
  85     @Override
  86     public boolean isExecutable() {
  87         return true;
  88     }
  89 
  90     @Override
  91     public void execute() {
  92         oldColumnIndex = columnIndexMeta.getValue(fxomObject);
  93         oldRowIndex = rowIndexMeta.getValue(fxomObject);
  94 
  95         assert oldColumnIndex + columnIndexDelta >= 0;
  96         assert oldRowIndex + rowIndexDelta >= 0;
  97 
  98         // Now same as redo()
  99         redo();
 100     }
 101 
 102     @Override
 103     public void undo() {
 104         assert isExecutable();
 105 
 106         final FXOMDocument fxomDocument = getEditorController().getFxomDocument();
 107         fxomDocument.beginUpdate();
 108         columnIndexMeta.setValue(fxomObject, oldColumnIndex);
 109         rowIndexMeta.setValue(fxomObject, oldRowIndex);
 110         fxomDocument.endUpdate();
 111     }
 112 
 113     @Override
 114     public void redo() {
 115         assert isExecutable();
 116 
 117         final FXOMDocument fxomDocument = getEditorController().getFxomDocument();
 118         fxomDocument.beginUpdate();
 119         columnIndexMeta.setValue(fxomObject, oldColumnIndex + columnIndexDelta);
 120         rowIndexMeta.setValue(fxomObject, oldRowIndex + rowIndexDelta);
 121         fxomDocument.endUpdate();
 122     }
 123 
 124     @Override
 125     public String getDescription() {
 126         return getClass().getSimpleName();
 127     }
 128 
 129 }