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.job;
  33 
  34 import com.oracle.javafx.scenebuilder.kit.editor.EditorController;
  35 import com.oracle.javafx.scenebuilder.kit.editor.selection.Selection;
  36 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument;
  37 import java.util.ArrayList;
  38 import java.util.Collections;
  39 import java.util.List;
  40 
  41 /**
  42  *
  43  */
  44 public class BatchJob extends Job {
  45 
  46     private final List<Job> subJobs = new ArrayList<>();
  47     private final boolean shouldRefreshSceneGraph;
  48     private final boolean shouldUpdateSelection;
  49     private final String description;
  50 
  51     public BatchJob(EditorController editorController,
  52             boolean shouldRefreshSceneGraph,
  53             boolean shouldUpdateSelection,
  54             String description) {
  55         super(editorController);
  56         this.description = description;
  57         this.shouldRefreshSceneGraph = shouldRefreshSceneGraph;
  58         this.shouldUpdateSelection = shouldUpdateSelection;
  59     }
  60 
  61     public BatchJob(EditorController editorController,
  62             boolean shouldRefreshSceneGraph, String description) {
  63         super(editorController);
  64         this.description = description;
  65         this.shouldRefreshSceneGraph = shouldRefreshSceneGraph;
  66         this.shouldUpdateSelection = true;
  67     }
  68 
  69      public BatchJob(EditorController editorController, String description) {
  70          super(editorController);
  71          this.description = description;
  72          this.shouldRefreshSceneGraph = true;
  73          this.shouldUpdateSelection = true;
  74     }
  75 
  76    public BatchJob(EditorController editorController) {
  77         super(editorController);
  78         this.description = getClass().getSimpleName();
  79         this.shouldRefreshSceneGraph = true;
  80         this.shouldUpdateSelection = true;
  81     }
  82 
  83     public void addSubJob(Job subJob) {
  84         assert subJob != null;
  85         this.subJobs.add(subJob);
  86     }
  87 
  88     public void addSubJobs(List<Job> subJobs) {
  89         assert subJobs != null;
  90         this.subJobs.addAll(subJobs);
  91     }
  92 
  93     public void prependSubJob(Job subJob) {
  94         assert subJob != null;
  95         this.subJobs.add(0, subJob);
  96     }
  97 
  98     public List<Job> getSubJobs() {
  99         return Collections.unmodifiableList(subJobs);
 100     }
 101 
 102     /*
 103      * Job
 104      */
 105 
 106     @Override
 107     public boolean isExecutable() {
 108         return subJobs.isEmpty() == false;
 109     }
 110 
 111     @Override
 112     public void execute() {
 113         final Selection selection = getEditorController().getSelection();
 114         final FXOMDocument fxomDocument = getEditorController().getFxomDocument();
 115         if (shouldUpdateSelection) {
 116             selection.beginUpdate();
 117         }
 118         if (shouldRefreshSceneGraph) {
 119             fxomDocument.beginUpdate();
 120         }
 121         for (Job subJob : subJobs) {
 122             subJob.execute();
 123         }
 124         if (shouldRefreshSceneGraph) {
 125             fxomDocument.endUpdate();
 126         }
 127         if (shouldUpdateSelection) {
 128             selection.endUpdate();
 129         }
 130     }
 131 
 132     @Override
 133     public void undo() {
 134         final Selection selection = getEditorController().getSelection();
 135         final FXOMDocument fxomDocument = getEditorController().getFxomDocument();
 136         if (shouldUpdateSelection) {
 137             selection.beginUpdate();
 138         }
 139         if (shouldRefreshSceneGraph) {
 140             fxomDocument.beginUpdate();
 141         }
 142         for (int i = subJobs.size()-1; i >= 0; i--) {
 143             subJobs.get(i).undo();
 144         }
 145         if (shouldRefreshSceneGraph) {
 146             fxomDocument.endUpdate();
 147         }
 148         if (shouldUpdateSelection) {
 149             selection.endUpdate();
 150         }
 151     }
 152 
 153     @Override
 154     public void redo() {
 155         final Selection selection = getEditorController().getSelection();
 156         final FXOMDocument fxomDocument = getEditorController().getFxomDocument();
 157 
 158         if (shouldUpdateSelection) {
 159             selection.beginUpdate();
 160         }
 161         if (shouldRefreshSceneGraph) {
 162             fxomDocument.beginUpdate();
 163         }
 164         for (Job subJob : subJobs) {
 165             subJob.redo();
 166         }
 167         if (shouldRefreshSceneGraph) {
 168             fxomDocument.endUpdate();
 169         }
 170         if (shouldUpdateSelection) {
 171             selection.endUpdate();
 172         }
 173     }
 174 
 175     @Override
 176     public String getDescription() {
 177         return description;
 178     }
 179 
 180 }