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