Skip to content

Commit

Permalink
re-implement guice-support without fx-guice dependency #490
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-mauky committed Nov 14, 2018
1 parent 1be13d7 commit 4adfb6d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 55 deletions.
7 changes: 6 additions & 1 deletion examples/mini-examples/welcome-example/pom.xml
Expand Up @@ -38,6 +38,11 @@
<groupId>de.saxsys</groupId>
<artifactId>mvvmfx-guice</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.2</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -52,7 +57,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>12.0</version>
<version>27.0-jre</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
8 changes: 4 additions & 4 deletions mvvmfx-guice/pom.xml
Expand Up @@ -49,13 +49,13 @@
<scope>provided</scope>
</dependency>


<dependency>
<groupId>com.cathive.fx</groupId>
<artifactId>fx-guice</artifactId>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>[3.0,)</version>
<scope>provided</scope>
</dependency>


<!-- Testing Frameworks -->
<dependency>
<groupId>org.assertj</groupId>
Expand Down
Expand Up @@ -15,67 +15,65 @@
******************************************************************************/
package de.saxsys.mvvmfx.guice;

import java.util.List;

import de.saxsys.mvvmfx.internal.MvvmfxApplication;
import javafx.application.HostServices;
import javafx.stage.Stage;

import com.cathive.fx.guice.GuiceApplication;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;

import de.saxsys.mvvmfx.MvvmFX;
import de.saxsys.mvvmfx.guice.internal.GuiceInjector;
import de.saxsys.mvvmfx.guice.internal.MvvmfxModule;
import de.saxsys.mvvmfx.internal.MvvmfxApplication;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

/**
* This class has to be extended by the user to build a javafx application powered by Guice.
*
* @author manuel.mauky
*/
public abstract class MvvmfxGuiceApplication extends GuiceApplication implements MvvmfxApplication {


@Inject
private GuiceInjector guiceInjector;
public abstract class MvvmfxGuiceApplication extends Application implements MvvmfxApplication {

private Stage primaryStage;

@Override
public final void init(List<Module> modules) throws Exception {


/**
* This method is called by the javafx runtime when the application is initialized.
* See {@link Application#init()} for more details.
* <p>
* In this method the initialization of the guice container is done.
* For this reason, this method is marked as final and cannot be overwritten by users.
* <p>
* Please use {@link #initMvvmfx()} for your own initialization logic.
*
* @throws Exception
*/
public final void init() throws Exception {
List<Module> modules = new ArrayList<>();

modules.add(new MvvmfxModule());

modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(HostServices.class).toProvider(new Provider<HostServices>() {
@Override
public HostServices get() {
return getHostServices();
}
});
bind(HostServices.class).toProvider(MvvmfxGuiceApplication.this::getHostServices);

bind(Stage.class).toProvider(new Provider<Stage>() {
@Override
public Stage get() {
return primaryStage;
}
});
bind(Stage.class).toProvider(() -> primaryStage);

bind(Parameters.class).toProvider(new Provider<Parameters>() {
@Override
public Parameters get() {
return getParameters();
}
});
bind(Parameters.class).toProvider(MvvmfxGuiceApplication.this::getParameters);
}
});


this.initGuiceModules(modules);

final Injector injector = Guice.createInjector(modules);
MvvmFX.setCustomDependencyInjector(injector::getInstance);

injector.injectMembers(this);

this.initMvvmfx();
}

Expand All @@ -86,8 +84,7 @@ public Parameters get() {
*/
public final void start(Stage stage) throws Exception {
this.primaryStage = stage;
MvvmFX.setCustomDependencyInjector(guiceInjector);


this.startMvvmfx(stage);
}

Expand All @@ -97,7 +94,8 @@ public final void stop() throws Exception {
}

/**
* Configure the guice modules.
* Configure your guice modules. Use the given list and
* add your modules to it.
*
* @param modules
* module list
Expand Down
Expand Up @@ -16,8 +16,6 @@
package de.saxsys.mvvmfx.guice.internal;

import com.google.inject.AbstractModule;
import com.google.inject.Provider;

import de.saxsys.mvvmfx.MvvmFX;
import de.saxsys.mvvmfx.utils.notifications.NotificationCenter;

Expand All @@ -29,13 +27,6 @@
public class MvvmfxModule extends AbstractModule {
@Override
protected void configure() {
bind(NotificationCenter.class).toProvider(new Provider<NotificationCenter>() {
@Override
public NotificationCenter get() {
return MvvmFX.getNotificationCenter();
}
});


bind(NotificationCenter.class).toProvider(MvvmFX::getNotificationCenter);
}
}

0 comments on commit 4adfb6d

Please sign in to comment.