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.job.atomic.ToggleFxRootJob; 35 import com.oracle.javafx.scenebuilder.kit.editor.job.atomic.ModifyFxControllerJob; 36 import com.oracle.javafx.scenebuilder.kit.editor.EditorController; 37 import com.oracle.javafx.scenebuilder.kit.editor.i18n.I18N; 38 import com.oracle.javafx.scenebuilder.kit.editor.job.atomic.RemoveObjectJob; 39 import com.oracle.javafx.scenebuilder.kit.editor.selection.AbstractSelectionGroup; 40 import com.oracle.javafx.scenebuilder.kit.editor.selection.ObjectSelectionGroup; 41 import com.oracle.javafx.scenebuilder.kit.editor.selection.Selection; 42 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument; 43 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMFxIdIndex; 44 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance; 45 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject; 46 import java.util.ArrayList; 47 import java.util.List; 48 49 /** 50 * 51 */ 52 public class TrimSelectionJob extends BatchSelectionJob { 53 54 public TrimSelectionJob(EditorController editorController) { 55 super(editorController); 56 } 57 58 @Override 59 protected List<Job> makeSubJobs() { 60 final List<Job> result = new ArrayList<>(); 61 62 if (canTrim()) { 63 64 final Selection selection = getEditorController().getSelection(); 65 assert selection.getGroup() instanceof ObjectSelectionGroup; // Because (1) 66 final ObjectSelectionGroup osg = (ObjectSelectionGroup) selection.getGroup(); 67 assert osg.getItems().size() == 1; 68 final FXOMObject oldRoot = getEditorController().getFxomDocument().getFxomRoot(); 69 final FXOMObject candidateRoot = osg.getItems().iterator().next(); 70 71 /* 72 * This job is composed of subjobs: 73 * 0) Remove fx:controller/fx:root (if defined) from the old root object if any 74 * 1) Unselect the candidate 75 * => ClearSelectionJob 76 * 2) Disconnect the candidate from its existing parent 77 * => DeleteObjectJob 78 * 3) Set the candidate as the root of the document 79 * => SetDocumentRootJob 80 * 4) Add fx:controller/fx:root (if defined) to the new root object 81 */ 82 assert oldRoot instanceof FXOMInstance; 83 boolean isFxRoot = ((FXOMInstance) oldRoot).isFxRoot(); 84 final String fxController = oldRoot.getFxController(); 85 // First remove the fx:controller/fx:root from the old root object 86 if (isFxRoot) { 87 final ToggleFxRootJob fxRootJob = new ToggleFxRootJob(getEditorController()); 88 result.add(fxRootJob); 89 } 90 if (fxController != null) { 91 final ModifyFxControllerJob fxControllerJob 92 = new ModifyFxControllerJob(oldRoot, null, getEditorController()); 93 result.add(fxControllerJob); 94 } 95 96 final Job deleteNewRoot = new RemoveObjectJob(candidateRoot, getEditorController()); 97 result.add(deleteNewRoot); 98 99 final Job setDocumentRoot = new SetDocumentRootJob(candidateRoot, getEditorController()); 100 result.add(setDocumentRoot); 101 102 // Finally add the fx:controller/fx:root to the new root object 103 if (isFxRoot) { 104 final ToggleFxRootJob fxRootJob = new ToggleFxRootJob(getEditorController()); 105 result.add(fxRootJob); 106 } 107 if (fxController != null) { 108 final ModifyFxControllerJob fxControllerJob 109 = new ModifyFxControllerJob(candidateRoot, fxController, getEditorController()); 110 result.add(fxControllerJob); 111 } 112 } 113 114 return result; 115 } 116 117 @Override 118 protected String makeDescription() { 119 return I18N.getString("label.action.edit.trim"); 120 } 121 122 @Override 123 protected AbstractSelectionGroup getNewSelectionGroup() { 124 // Selection unchanged 125 return getOldSelectionGroup(); 126 } 127 128 private boolean canTrim() { 129 final Selection selection = getEditorController().getSelection(); 130 final boolean result; 131 132 if (selection.getGroup() instanceof ObjectSelectionGroup) { 133 final ObjectSelectionGroup osg = (ObjectSelectionGroup) selection.getGroup(); 134 if (osg.getItems().size() == 1) { 135 // We can trim if: 136 // - object is an FXOMInstance 137 // - object is not already the root 138 // - object is self contained 139 final FXOMObject fxomObject = osg.getItems().iterator().next(); 140 if (fxomObject instanceof FXOMInstance) { 141 final FXOMDocument fxomDocument = fxomObject.getFxomDocument(); 142 result = (fxomObject != fxomDocument.getFxomRoot()) 143 && FXOMFxIdIndex.isSelfContainedObject(fxomObject); 144 } else { 145 result = false; 146 } 147 } else { 148 // Cannot trim when multiple objects are selected 149 result = false; 150 } 151 } else { 152 // selection.getGroup() instanceof GridSelectionGroup 153 // => cannot trim a selected row/column in a grid pane 154 result = false; 155 } 156 157 return result; 158 } 159 }