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 com.oracle.javafx.scenebuilder.kit.editor.i18n.I18N;
  36 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument;
  37 import java.io.IOException;
  38 import java.net.URL;
  39 import javafx.fxml.FXMLLoader;
  40 import javafx.scene.Parent;
  41 
  42 /**
  43  * AbstractFxmlController is the abstract base class for all the
  44  * controller which build their UI components from an FXML file.
  45  *
  46  * Subclasses should provide a {@link AbstractFxmlPanelController#controllerDidLoadFxml() }
  47  * method in charge of finishing the initialization of the UI components
  48  * loaded from the FXML file.
  49  *
  50  *
  51  */
  52 public abstract class AbstractFxmlController extends AbstractPanelController {
  53 
  54     private final URL fxmlURL;
  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 AbstractFxmlController(URL fxmlURL, EditorController editorController) {
  63         super(editorController);
  64         this.fxmlURL = fxmlURL;
  65         assert fxmlURL != null : "Check the name of the FXML file used by "
  66                 + getClass().getSimpleName();
  67     }
  68 
  69     /*
  70      * AbstractPanelController
  71      */
  72 
  73     /**
  74      * This implementation loads the FXML file using the URL passed to
  75      * {@link AbstractFxmlPanelController}.
  76      * Subclass implementation should make sure that this method can be invoked
  77      * outside of the JavaFX thread
  78      */
  79     @Override
  80     protected void makePanel() {
  81         final FXMLLoader loader = new FXMLLoader();
  82 
  83         loader.setController(this);
  84         loader.setLocation(fxmlURL);
  85         loader.setResources(I18N.getBundle());
  86         try {
  87             setPanelRoot((Parent)loader.load());
  88             controllerDidLoadFxml();
  89         } catch (RuntimeException | IOException x) {
  90             System.out.println("loader.getController()=" + loader.getController());
  91             System.out.println("loader.getLocation()=" + loader.getLocation());
  92             throw new RuntimeException("Failed to load " + fxmlURL.getFile(), x); //NOI18N
  93         }
  94     }
  95 
  96     /*
  97      * Protected
  98      */
  99 
 100     /**
 101      * Called by {@link AbstractFxmlPanelController#makePanel() } after
 102      * the FXML file has been successfully loaded.
 103      * Warning : this routine may be invoked outside of the event thread.
 104      */
 105     protected abstract void controllerDidLoadFxml();
 106 
 107         // Note : remember that here:
 108         // 1) getHost() might be null
 109         // 2) getPanelRoot().getScene() might be null
 110         // 3) getPanelRoot().getScene().getWindow() might be null
 111 
 112     @Override
 113     protected void fxomDocumentDidChange(FXOMDocument oldDocument) {
 114         // Ignored
 115     }
 116 
 117     @Override
 118     protected void sceneGraphRevisionDidChange() {
 119         // Ignored
 120     }
 121 
 122     @Override
 123     protected void cssRevisionDidChange() {
 124         // Ignored
 125     }
 126 
 127     @Override
 128     protected void jobManagerRevisionDidChange() {
 129         // Ignored
 130     }
 131 
 132     @Override
 133     protected void editorSelectionDidChange() {
 134         // Ignored
 135     }
 136 }