1 /*
   2  * Copyright (c) 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.job.atomic.ModifyObjectJob;
  35 import com.oracle.javafx.scenebuilder.kit.editor.EditorController;
  36 import com.oracle.javafx.scenebuilder.kit.editor.i18n.I18N;
  37 import com.oracle.javafx.scenebuilder.kit.editor.selection.GridSelectionGroup;
  38 import com.oracle.javafx.scenebuilder.kit.editor.selection.ObjectSelectionGroup;
  39 import com.oracle.javafx.scenebuilder.kit.editor.selection.Selection;
  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.ValuePropertyMetadata;
  43 import com.oracle.javafx.scenebuilder.kit.metadata.util.DesignHierarchyMask;
  44 import java.util.ArrayList;
  45 import java.util.HashSet;
  46 import java.util.List;
  47 import java.util.Set;
  48 
  49 /**
  50  *
  51  */
  52 public class ModifySelectionJob extends BatchDocumentJob {
  53 
  54     protected final ValuePropertyMetadata propertyMetadata;
  55     protected final Object newValue;
  56 
  57     public ModifySelectionJob(ValuePropertyMetadata propertyMetadata,
  58             Object newValue, EditorController editorController) {
  59         super(editorController);
  60         this.propertyMetadata = propertyMetadata;
  61         this.newValue = newValue;
  62     }
  63 
  64     @Override
  65     protected List<Job> makeSubJobs() {
  66 
  67         final List<Job> result = new ArrayList<>();
  68         final Set<FXOMInstance> candidates = new HashSet<>();
  69         final Selection selection = getEditorController().getSelection();
  70         if (selection.getGroup() instanceof ObjectSelectionGroup) {
  71             final ObjectSelectionGroup osg = (ObjectSelectionGroup) selection.getGroup();
  72             for (FXOMObject fxomObject : osg.getItems()) {
  73                 if (fxomObject instanceof FXOMInstance) {
  74                     candidates.add((FXOMInstance) fxomObject);
  75                 }
  76             }
  77         } else if (selection.getGroup() instanceof GridSelectionGroup) {
  78             final GridSelectionGroup gsg = (GridSelectionGroup) selection.getGroup();
  79             final DesignHierarchyMask mask = new DesignHierarchyMask(gsg.getAncestor());
  80             for (int index : gsg.getIndexes()) {
  81                 FXOMObject constraints = null;
  82                 switch (gsg.getType()) {
  83                     case COLUMN:
  84                         constraints = mask.getColumnConstraintsAtIndex(index);
  85                         break;
  86                     case ROW:
  87                         constraints = mask.getRowConstraintsAtIndex(index);
  88                         break;
  89                     default:
  90                         assert false;
  91                         break;
  92                 }
  93                 assert constraints instanceof FXOMInstance;
  94                 candidates.add((FXOMInstance) constraints);
  95             }
  96         } else {
  97             assert selection.getGroup() == null : "Add implementation for " + selection.getGroup();
  98         }
  99 
 100         // Add ModifyObject jobs
 101         for (FXOMInstance fxomInstance : candidates) {
 102             final ModifyObjectJob subJob = new ModifyObjectJob(
 103                     fxomInstance, propertyMetadata, newValue, getEditorController());
 104             if (subJob.isExecutable()) {
 105                 result.add(subJob);
 106             }
 107         }
 108 
 109         return result;
 110     }
 111 
 112     @Override
 113     protected String makeDescription() {
 114         final String result;
 115         final List<Job> subJobs = getSubJobs();
 116         final int subJobCount = subJobs.size();
 117 
 118         switch (subJobCount) {
 119             case 0:
 120                 result = "Unexecutable Set"; //NOI18N
 121                 break;
 122             case 1: // Single selection
 123                 result = subJobs.get(0).getDescription();
 124                 break;
 125             default:
 126                 result = I18N.getString("label.action.edit.set.n",
 127                         propertyMetadata.getName().toString(),
 128                         subJobCount);
 129                 break;
 130         }
 131 
 132         return result;
 133     }
 134 }