Java Resource Get getResource(Class controllerClass)

Here you can find the source of getResource(Class controllerClass)

Description

Find the .fxml resource associated with a given controller class by location and naming conventions.

License

Open Source License

Parameter

Parameter Description
controllerClass The controller class whose resource we wish to locate

Return

The URL for the resource associated with the given controller class.

Declaration

public static URL getResource(Class<?> controllerClass) 

Method Source Code


//package com.java2s;
/*/*from  w  ww .  j  av  a2s.c o  m*/
 * Copyright (C) 2018 Scientific Analysis Instruments Limited <contact@saiman.co.uk>
 *          ______         ___      ___________
 *       ,'========\     ,'===\    /========== \
 *      /== \___/== \  ,'==.== \   \__/== \___\/
 *     /==_/____\__\/,'==__|== |     /==  /
 *     \========`. ,'========= |    /==  /
 *   ___`-___)== ,'== \____|== |   /==  /
 *  /== \__.-==,'==  ,'    |== '__/==  /_
 *  \======== /==  ,'      |== ========= \
 *   \_____\.-\__\/        \__\\________\/
 *
 * This file is part of uk.co.saiman.fx.
 *
 * uk.co.saiman.fx is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * uk.co.saiman.fx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.URL;

public class Main {
    private static final String CONTROLLER_STRING = "Controller";

    /**
     * Find the {@code .fxml} resource associated with a given controller class by
     * location and naming conventions. The location of the file is assumed to be
     * the same package as the controller class. The name of the file is determined
     * according to the convention described by {@link #getResourceName(Class)}.
     * 
     * @param controllerClass The controller class whose resource we wish to locate
     * @return The URL for the resource associated with the given controller class.
     */
    public static URL getResource(Class<?> controllerClass) {
        return getResource(controllerClass, getResourceName(controllerClass));
    }

    /**
     * Find the {@code .fxml} resource for a given controller class by location
     * conventions. The location of the file is assumed to be the same package as
     * the controller class.
     * 
     * @param controllerClass The controller class whose resource we wish to locate
     * @param resourceName    The name of the resource file
     * @return The URL for the resource associated with the given controller class.
     */
    public static URL getResource(Class<?> controllerClass, String resourceName) {
        String resourceLocation = "/" + controllerClass.getPackage().getName().replace('.', '/') + "/"
                + resourceName + ".fxml";

        return controllerClass.getResource(resourceLocation);
    }

    /**
     * Find the name of the {@code .fxml} resource associated with a given
     * controller class by convention. The name of the file is assumed to be
     * {@code [classname].fxml}, or if {@code [classname]} takes the form
     * {@code [classnameprefix]Controller}, the name of the file is assumed to be
     * {@code [classnameprefix].fxml}.
     * 
     * @param controllerClass The controller class whose resource we wish to locate
     * @return The URL for the resource associated with the given controller class.
     */
    public static String getResourceName(Class<?> controllerClass) {
        String resourceName = controllerClass.getSimpleName();

        if (resourceName.endsWith(CONTROLLER_STRING)) {
            resourceName = resourceName.substring(0, resourceName.length() - CONTROLLER_STRING.length());
        }

        return resourceName;
    }
}

Related

  1. getResource(Class _class, String resource)
  2. getResource(Class baseclass, String name)
  3. getResource(Class c, String name)
  4. getResource(Class clazz, String resource)
  5. getResource(Class testClass, String resource)
  6. getResource(final Class clazz, final String res)
  7. getResource(final Class aClass, final String aName)
  8. getResource(final String name)