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.i18n.I18N;
  36 import com.oracle.javafx.scenebuilder.kit.editor.selection.AbstractSelectionGroup;
  37 import com.oracle.javafx.scenebuilder.kit.editor.selection.ObjectSelectionGroup;
  38 import com.oracle.javafx.scenebuilder.kit.editor.selection.Selection;
  39 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMCollection;
  40 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument;
  41 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance;
  42 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject;
  43 import com.oracle.javafx.scenebuilder.kit.metadata.util.ClipboardDecoder;
  44 import com.oracle.javafx.scenebuilder.kit.metadata.util.DesignHierarchyMask;
  45 import com.oracle.javafx.scenebuilder.kit.metadata.util.DesignHierarchyMask.Accessory;
  46 
  47 import java.util.ArrayList;
  48 import java.util.List;
  49 
  50 import javafx.scene.input.Clipboard;
  51 
  52 /**
  53  *
  54  */
  55 public class PasteIntoJob extends BatchSelectionJob {
  56 
  57     private List<FXOMObject> newObjects;
  58 
  59     public PasteIntoJob(EditorController editorController) {
  60         super(editorController);
  61     }
  62 
  63     @Override
  64     protected List<Job> makeSubJobs() {
  65         final List<Job> result = new ArrayList<>();
  66 
  67         final FXOMDocument fxomDocument = getEditorController().getFxomDocument();
  68         if (fxomDocument != null) {
  69 
  70             // Retrieve the FXOMObjects from the clipboard
  71             final ClipboardDecoder clipboardDecoder
  72                     = new ClipboardDecoder(Clipboard.getSystemClipboard());
  73             newObjects = clipboardDecoder.decode(fxomDocument);
  74             assert newObjects != null; // But possible empty
  75 
  76             // Retrieve the target FXOMObject
  77             final Selection selection = getEditorController().getSelection();
  78             if (selection.getGroup() instanceof ObjectSelectionGroup) {
  79                 final ObjectSelectionGroup osg = (ObjectSelectionGroup) selection.getGroup();
  80                 // Single target selection
  81                 if (osg.getItems().size() == 1) {
  82                     final FXOMObject targetObject = osg.getItems().iterator().next();
  83 
  84                     // Build InsertAsSubComponent jobs
  85                     final DesignHierarchyMask targetMask = new DesignHierarchyMask(targetObject);
  86                     if (targetMask.isAcceptingSubComponent(newObjects)) {
  87                         for (FXOMObject newObject : newObjects) {
  88                             final InsertAsSubComponentJob subJob = new InsertAsSubComponentJob(
  89                                     newObject,
  90                                     targetObject,
  91                                     targetMask.getSubComponentCount(),
  92                                     getEditorController());
  93                             result.add(0, subJob);
  94                         }
  95                     } // Build InsertAsAccessory jobs for single source selection
  96                     else if (newObjects.size() == 1) {
  97                         final FXOMObject newObject = newObjects.get(0);
  98                         final Accessory[] accessories = {Accessory.CONTENT,
  99                             Accessory.CONTEXT_MENU, Accessory.GRAPHIC,
 100                             Accessory.TOOLTIP};
 101                         for (Accessory a : accessories) {
 102                             if (targetMask.isAcceptingAccessory(a, newObject)
 103                                     && targetMask.getAccessory(a) == null) {
 104                                 final InsertAsAccessoryJob subJob = new InsertAsAccessoryJob(
 105                                         newObject, targetObject, a,
 106                                         getEditorController());
 107                                 result.add(subJob);
 108                                 break;
 109                             }
 110                         }
 111                     }
 112                 }
 113             }
 114         }
 115         return result;
 116     }
 117 
 118     @Override
 119     protected String makeDescription() {
 120         final String result;
 121 
 122         if (newObjects.size() == 1) {
 123             result = makeSingleSelectionDescription();
 124         } else {
 125             result = makeMultipleSelectionDescription();
 126         }
 127 
 128         return result;
 129     }
 130 
 131     @Override
 132     protected AbstractSelectionGroup getNewSelectionGroup() {
 133         assert newObjects != null; // But possibly empty
 134         if (newObjects.isEmpty()) {
 135             return null;
 136         } else {
 137             return new ObjectSelectionGroup(newObjects, newObjects.iterator().next(), null);
 138         }
 139     }
 140 
 141     private String makeSingleSelectionDescription() {
 142         final String result;
 143 
 144         assert newObjects.size() == 1;
 145         final FXOMObject newObject = newObjects.get(0);
 146         if (newObject instanceof FXOMInstance) {
 147             final Object sceneGraphObject = newObject.getSceneGraphObject();
 148             if (sceneGraphObject != null) {
 149                 result = I18N.getString("label.action.edit.paste.into.1", sceneGraphObject.getClass().getSimpleName());
 150             } else {
 151                 result = I18N.getString("label.action.edit.paste.into.unresolved");
 152             }
 153         } else if (newObject instanceof FXOMCollection) {
 154             result = I18N.getString("label.action.edit.paste.into.collection");
 155         } else {
 156             assert false;
 157             result = I18N.getString("label.action.edit.paste.into.1", newObject.getClass().getSimpleName());
 158         }
 159 
 160         return result;
 161     }
 162 
 163     private String makeMultipleSelectionDescription() {
 164         final int objectCount = newObjects.size();
 165         return I18N.getString("label.action.edit.paste.into.n", objectCount);
 166     }
 167 }