Esempio n. 1
0
 public Object load(String url) {
   try (InputStream inputStream = SpringFXMLLoader.class.getResourceAsStream(url)) {
     FXMLLoader fxmlLoader = new FXMLLoader();
     fxmlLoader.setControllerFactory(context::getBean);
     return fxmlLoader.load(inputStream);
   } catch (IOException ioexception) {
     LOGGER.error("error loading from url [{}]", url);
     LOGGER.error(ioexception.getMessage());
     throw new RuntimeException("Error loading application");
   }
 }
Esempio n. 2
0
 public Object load(String fxmlName, Class<?> controller) {
   try {
     URL url = controller.getClassLoader().getResource(fxmlName);
     FXMLLoader loader = new FXMLLoader();
     loader.setResources(ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault()));
     loader.setControllerFactory(this);
     return loader.load(url.openStream());
   } catch (IOException ex) {
     throw new RuntimeException(ex);
   }
 }
Esempio n. 3
0
  /**
   * Load a FXML component.
   *
   * <p>The fxml path could be :
   *
   * <ul>
   *   <li>Relative : fxml file will be loaded with the classloader of the given model class
   *   <li>Absolute : fxml file will be loaded with default thread class loader, packages must be
   *       separated by / character
   * </ul>
   *
   * @param model the model that will manage the fxml node
   * @param fxmlPath the fxml string path
   * @param bundlePath the bundle string path
   * @return a FXMLComponent object that wrap a fxml node with its controller
   * @param <M> the model type that will manage this fxml node
   */
  @SuppressWarnings("unchecked")
  public static <M extends Model> FXMLComponentBase loadFXML(
      final M model, final String fxmlPath, final String bundlePath) {

    final FXMLLoader fxmlLoader = new FXMLLoader();

    // Use Custom controller factory to attach the root model to the controller
    fxmlLoader.setControllerFactory(new DefaultFXMLControllerBuilder(model));

    fxmlLoader.setLocation(convertFxmlUrl(model, fxmlPath));

    try {
      if (bundlePath != null) {
        fxmlLoader.setResources(ResourceBundle.getBundle(bundlePath));
      }
    } catch (final MissingResourceException e) {
      LOGGER.log(MISSING_RESOURCE_BUNDLE, e, bundlePath);
    }

    Node node = null;
    boolean error = false;
    try {
      error = fxmlLoader.getLocation() == null;
      if (error) {
        node = TextBuilder.create().text(FXML_ERROR_NODE_LABEL.getText(fxmlPath)).build();
      } else {
        node = (Node) fxmlLoader.load(fxmlLoader.getLocation().openStream());
      }

    } catch (final IOException e) {
      throw new CoreRuntimeException(FXML_NODE_DOESNT_EXIST.getText(fxmlPath), e);
    }

    final FXMLController<M, ?> fxmlController = (FXMLController<M, ?>) fxmlLoader.getController();

    // It's tolerated to have a null controller for an fxml node
    if (fxmlController != null) {
      // The fxml controller must extends AbstractFXMLController
      if (!error && !(fxmlLoader.getController() instanceof AbstractFXMLController)) {
        throw new CoreRuntimeException(
            BAD_FXML_CONTROLLER_ANCESTOR.getText(
                fxmlLoader.getController().getClass().getCanonicalName()));
      }
      // Link the View component with the fxml controller
      fxmlController.setModel(model);
    }

    return new FXMLComponentBase(node, fxmlController);
  }
 private SceneOverloaded initView(String fxml, final Initializable controller) {
   FXMLLoader fxmlLoader;
   try {
     fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
     fxmlLoader.setControllerFactory(
         new Callback<Class<?>, Object>() {
           @Override
           public Object call(Class<?> aClass) {
             return controller;
           }
         });
     AnchorPane page = (AnchorPane) fxmlLoader.load();
     SceneOverloaded scene = new SceneOverloaded(page, controller);
     return scene;
   } catch (IOException exception) {
     logger.error(exception.getMessage(), exception);
     return null;
   }
 }
Esempio n. 5
0
  @Override
  public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    MainController controller = context.getBean(MainController.class);
    loader.setControllerFactory(
        new Callback<Class<?>, Object>() {

          @Override
          public Object call(Class<?> param) {
            return controller;
          }
        });
    Parent root = loader.load(getClass().getResourceAsStream("/fxml/Scene.fxml"));
    Scene scene = new Scene(root);
    scene.getStylesheets().add("/styles/Styles.css");
    String prop = "/application.properties";
    String name = PropertiesUtil.getProperty(prop, "project.name");
    String version = PropertiesUtil.getProperty(prop, "project.version");
    stage.setTitle(name + "    " + version);
    stage.getIcons().add(new Image(getClass().getResourceAsStream("/image/mongodb-logo.png")));
    stage.setScene(scene);
    stage.show();
    stage.setOnCloseRequest(
        new EventHandler<WindowEvent>() {

          @Override
          public void handle(WindowEvent event) {
            Dialog<ButtonType> dialog = new Dialog<>();
            dialog.setContentText("\r\n\r\n是否退出?\r\n\r\n");
            dialog.setTitle("退出提示");
            dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
            dialog
                .showAndWait()
                .ifPresent(
                    res -> {
                      if (res != ButtonType.OK) {
                        event.consume();
                      }
                    });
          }
        });
  }
 private View loadFromFxml(URL fxmlUrl) {
   checkNotNull(fxmlUrl, "FXML URL must not be null");
   try {
     FXMLLoader loader = new FXMLLoader(fxmlUrl, resourceBundle);
     loader.setControllerFactory(viewFactory);
     loader.load();
     Object controller = loader.getController();
     if (controller == null)
       throw new ViewfxException(
           "Failed to load view from FXML file at [%s]. "
               + "Does it declare an fx:controller attribute?",
           fxmlUrl);
     if (!(controller instanceof View))
       throw new ViewfxException(
           "Controller of type [%s] loaded from FXML file at [%s] "
               + "does not implement [%s] as expected.",
           controller.getClass(), fxmlUrl, View.class);
     return (View) controller;
   } catch (IOException ex) {
     throw new ViewfxException(ex, "Failed to load view from FXML file at [%s]", fxmlUrl);
   }
 }
  @Nullable
  protected Node loadFromFXML(@Nonnull String baseName) {
    requireNonBlank(baseName, "Argument 'baseName' must not be blank");
    if (baseName.endsWith(FXML_SUFFIX)) {
      baseName = stripFilenameExtension(baseName);
    }
    baseName = baseName.replace('.', '/');
    String viewName = baseName + FXML_SUFFIX;
    String styleName = baseName + ".css";

    URL viewResource = getResourceAsURL(viewName);
    if (viewResource == null) {
      return null;
    }

    FXMLLoader fxmlLoader = new FXMLLoader(viewResource);
    fxmlLoader.setResources(getApplication().getMessageSource().asResourceBundle());
    fxmlLoader.setBuilderFactory(
        new JavaFXBuilderFactory(getApplication().getApplicationClassLoader().get()));
    fxmlLoader.setClassLoader(getApplication().getApplicationClassLoader().get());
    fxmlLoader.setControllerFactory(klass -> getMvcGroup().getView());

    try {
      fxmlLoader.load();
    } catch (IOException e) {
      throw new GriffonException(e);
    }

    Parent node = fxmlLoader.getRoot();

    URL cssResource = getResourceAsURL(styleName);
    if (cssResource != null) {
      String uriToCss = cssResource.toExternalForm();
      node.getStylesheets().add(uriToCss);
    }

    return node;
  }