Coder Social home page Coder Social logo

viniciusccarvalho / ignite Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gluonhq/ignite

0.0 0.0 0.0 146 KB

Gluon Ignite creates a common abstraction over several popular dependency injection frameworks. With this library, developers can use popular dependency injection frameworks in their JavaFX applications, including inside their FXML controllers.

License: GNU General Public License v3.0

Java 100.00%

ignite's Introduction

Gluon Ignite

xxx xxx xxx xxx

Gluon Ignite allows developers to use popular dependency injection frameworks in their JavaFX applications, including inside their FXML controllers. Gluon Ignite creates a common abstraction over several popular dependency injection frameworks (currently Guice, Spring, and Dagger).

Since v1.1.0 we also added an extended support for Microunaut. Read further for more details.

With full support of JSR-330, Gluon Ignite makes using dependency injection in JavaFX applications trivial. Here is a quick example of creating an application using the Guice framework and Gluon Ignite.

public class GuiceApp extends Application implements ExampleApp {
 
    public static void main(String[] args) {
        launch(args);
    }
 
    private GuiceContext context = new GuiceContext(this, () -> Arrays.asList(new GuiceModule()));
 
    @Inject private FXMLLoader fxmlLoader;
 
    @Override public void start(Stage primaryStage) throws IOException {
        context.init();
        fxmlLoader.setLocation(getViewLocation());
        Parent view = fxmlLoader.load();
 
        primaryStage.setTitle("Guice Example");
        primaryStage.setScene(new Scene(view));
        primaryStage.show();
    }
}
 
class GuiceModule extends AbstractModule {
    @Override protected void configure() {
        bind(Service.class).to(Service.class);
    }
}

By using Gluon Ignite, you not only get dependency injection inside your application class, but also within your FXML controllers too. Even though the sample above shows a Guice context, as mentioned Gluon Ignite also supports other DI frameworks. By using a DaggerContext or SpringContext in your application, it can be easily set up to work with those frameworks instead. Samples of applications are available in the project Wiki

Micronaut support

Ignite implementation for Micronaut provides much deeper integration with Micronaut framework.

Application main class

On top of common, for Ignite, implementation of DIContext interface, Ignite Micronaut provides special implementation of JavaFX Application class, which can to be configured as your main class: com.gluonhq.ignite.micronaut.FXApplication. This makes the code a lot cleaner. As an application developer, you just have to implement an entry point into your application, which will be picked up by Micronaut automatically, since it is a normal Micronaut bean.

Here is a simple example:

import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.runtime.event.annotation.EventListener;
import javafx.scene.Scene;
import javafx.stage.Stage;

import javax.inject.Inject;
import javax.inject.Singleton;

import static com.gluonhq.micronaut.FXApplication.StartEvent;
import static com.gluonhq.micronaut.FXApplication.StopEvent;

@Singleton
public class AppEntryPoint {

    @Inject
    private Object something;

    @EventListener
    void onAppStart(StartEvent event) {
        Stage stage = event.getStage();
        Scene scene = new Scene( new Label("Hello, world!"));
        stage.setScene(scene);
        stage.setTitle("My application");
        stage.show();
    }
    
    @EventListener
    void onAppStop(StopEvent event) {
        //...
    }

}

JavaFX Application Thread

Any bean method can be forced to run on JavaFX Application Thread using OnFxThread annotation

@OnFXThread
void refreshTime() {
    label.setText(timePattern.format(ZonedDateTime.now()));
}

FXML Views

Ignite Micronaut implements support for FXML views based on naming conventions:

  • A simple empty bean inherited from FXMLView defines the view, its name should be exactly the same as corresponding fxml file and should be in the corresponding package. This name usually ends with View but does not have to.
  • A bean named <ViewName>Controller defines a related FXML controller class. As per JavaFX standard, it should be defined within fxml as well. The framework automatically makes it injectable.
  • Optional stylesheet can be provided and should be located in the corresponding folder, next to the fxml file, named as <ViewName>.css

The view loaded by simply injecting the View class, and retrieving its root node using getRoot method. Here is a simple example:

package example.view;

@Singleton
public class ContentView extends BorderPane {

    @Inject
    private NavigationView navigationView;

    @Inject
    private StateView carStateView;
    // view      : example.view.StateView.java
    // controller: example.view.StateController.java
    // fxml      : example/view/StateView.fxml
    // stylesheet: example/view/StateView.css

    @PostConstruct
    private void init() {
        setCenter(navigationView);
        setLeft(carStateView.getRoot());
    }

}

Loading FXML directly

FXML can be loaded using FXMLLoader bean. Controller class will also become injectable in this case.

@Singleton
class SimpleLoad {

    @Inject
    FXMLLoader loader; // Note that the loader can only be used once.
    
    @PostConstruct
    private void init() {
       Node root = loader.load( SimpleLoad.class.getResourceAsStream("view.fxml"));
    }

}

How to use Gluon Ignite

To use Gluon Ignite in your software, simply include it as a dependency in your preferred dependency manager. All artifacts are available in Maven Central:

xxx xxx xxx xxx

Note that ignite-common is automatically included as a dependency for each module, so it is not necessary to include this as a dependency.

ignite's People

Contributors

eugener avatar tiainen avatar erwin1 avatar abhinayagarwal avatar jperedadnr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.