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.panel.util; 33 34 import com.oracle.javafx.scenebuilder.kit.editor.EditorController; 35 import java.io.IOException; 36 import java.net.URL; 37 import java.util.ResourceBundle; 38 import javafx.fxml.FXMLLoader; 39 import javafx.scene.Parent; 40 41 /** 42 * AbstractFxmlPanelController is the abstract base class for all the 43 * panel controller which build their UI components from an FXML file. 44 * 45 * Subclasses should provide a {@link AbstractFxmlPanelController#controllerDidLoadFxml() } 46 * method in charge of finishing the initialization of the UI components 47 * loaded from the FXML file. 48 * 49 * 50 */ 51 public abstract class AbstractFxmlPanelController extends AbstractPanelController { 52 53 private final URL fxmlURL; 54 private final ResourceBundle resources; 55 56 /** 57 * Base constructor for invocation by the subclasses. 58 * 59 * @param fxmlURL the URL of the FXML file to be loaded (cannot be null) 60 * @param editorController the editor controller (cannot be null) 61 */ 62 protected AbstractFxmlPanelController(URL fxmlURL, ResourceBundle resources, EditorController editorController) { 63 super(editorController); 64 this.fxmlURL = fxmlURL; 65 this.resources = resources; 66 assert fxmlURL != null : "Check the name of the FXML file used by " 67 + getClass().getSimpleName(); 68 } 69 70 /* 71 * AbstractPanelController 72 */ 73 74 /** 75 * This implementation loads the FXML file using the URL passed to 76 * {@link AbstractFxmlPanelController}. 77 * Subclass implementation should make sure that this method can be invoked 78 * outside of the JavaFX thread 79 */ 80 @Override 81 protected void makePanel() { 82 final FXMLLoader loader = new FXMLLoader(); 83 84 loader.setController(this); 85 loader.setLocation(fxmlURL); 86 loader.setResources(resources); 87 try { 88 setPanelRoot((Parent)loader.load()); 89 controllerDidLoadFxml(); 90 } catch (RuntimeException | IOException x) { 91 System.out.println("loader.getController()=" + loader.getController()); 92 System.out.println("loader.getLocation()=" + loader.getLocation()); 93 throw new RuntimeException("Failed to load " + fxmlURL.getFile(), x); //NOI18N 94 } 95 } 96 97 /* 98 * Protected 99 */ 100 101 /** 102 * Called by {@link AbstractFxmlPanelController#makePanel() } after 103 * the FXML file has been successfully loaded. 104 * Warning : this routine may be invoked outside of the event thread. 105 */ 106 protected abstract void controllerDidLoadFxml(); 107 108 // Note : remember that here: 109 // 1) getHost() might be null 110 // 2) getPanelRoot().getScene() might be null 111 // 3) getPanelRoot().getScene().getWindow() might be null 112 113 }