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.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.FXOMDocument; 40 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMNodes; 41 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject; 42 import com.oracle.javafx.scenebuilder.kit.metadata.util.DesignHierarchyMask; 43 import java.io.File; 44 import java.io.IOException; 45 import java.util.ArrayList; 46 import java.util.List; 47 48 /** 49 * 50 */ 51 public class ImportFileJob extends BatchSelectionJob { 52 53 private final File file; 54 private FXOMObject newObject, targetObject; 55 56 public ImportFileJob(File file, EditorController editorController) { 57 super(editorController); 58 59 assert file != null; 60 this.file = file; 61 } 62 63 public FXOMObject getTargetObject() { 64 return targetObject; 65 } 66 67 @Override 68 protected List<Job> makeSubJobs() { 69 final List<Job> result = new ArrayList<>(); 70 71 final FXOMDocument targetDocument = getEditorController().getFxomDocument(); 72 73 try { 74 newObject = FXOMNodes.newObject(targetDocument, file); 75 76 // newObject is null when file is empty 77 if (newObject != null) { 78 // If the document is empty (root object is null), then we 79 // insert the new object as root. 80 // Otherwise, we insert the new object under the common parent 81 // of the selected objects. 82 final FXOMObject rootObject = targetDocument.getFxomRoot(); 83 84 if (rootObject == null) { 85 result.add(new SetDocumentRootJob(newObject, getEditorController())); 86 } else { 87 final Selection selection = getEditorController().getSelection(); 88 if (selection.isEmpty() || selection.isSelected(rootObject)) { 89 // No selection or root is selected -> we insert below root 90 targetObject = rootObject; 91 } else { 92 // Let's use the common parent of the selected objects. 93 // It might be null if selection holds some non FXOMObject entries 94 targetObject = selection.getAncestor(); 95 } 96 // Build InsertAsSubComponent jobs 97 final DesignHierarchyMask targetMask = new DesignHierarchyMask(targetObject); 98 if (targetMask.isAcceptingSubComponent(newObject)) { 99 result.add(new InsertAsSubComponentJob( 100 newObject, 101 targetObject, 102 targetMask.getSubComponentCount(), 103 getEditorController())); 104 } 105 } 106 } 107 } catch (IOException ex) { 108 } 109 110 return result; 111 } 112 113 @Override 114 protected String makeDescription() { 115 return I18N.getString("import.from.file", file.getName()); 116 } 117 118 @Override 119 protected AbstractSelectionGroup getNewSelectionGroup() { 120 final List<FXOMObject> fxomObjects = new ArrayList<>(); 121 fxomObjects.add(newObject); 122 return new ObjectSelectionGroup(fxomObjects, newObject, null); 123 } 124 }