Skip to content

Commit

Permalink
Merge pull request #146 from sialcasa/#136_unify_cdi_guice
Browse files Browse the repository at this point in the history
#136 unify cdi guice
  • Loading branch information
manuel-mauky committed Oct 17, 2014
2 parents 278efd1 + 7334878 commit afba4d1
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public static void main(String...args){
private Repository repository;

@Override
public void init() throws Exception {
super.init();

public void initMvvmfx() throws Exception {
int numberOfContacts = 30;
for(int i=0 ; i<numberOfContacts ; i++){
repository.save(ContactFactory.createRandomContact());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
******************************************************************************/
package de.saxsys.mvvmfx.cdi;

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

Expand All @@ -35,7 +36,7 @@
*
* @author manuel.mauky
*/
public abstract class MvvmfxCdiApplication extends Application {
public abstract class MvvmfxCdiApplication extends Application implements MvvmfxApplication {


private final BeanManager beanManager;
Expand Down Expand Up @@ -71,12 +72,6 @@ public final void start(Stage primaryStage) throws Exception {
startMvvmfx(primaryStage);
}

/**
* Override this method with your application startup logic.
* <p/>
* This method is a wrapper method for javafx's {@link javafx.application.Application#start(javafx.stage.Stage)}.
*/
public abstract void startMvvmfx(Stage primaryStage) throws Exception;

/**
* This method is called when the javafx application is initialized. See {@link javafx.application.Application#init()}
Expand All @@ -90,7 +85,7 @@ public final void start(Stage primaryStage) throws Exception {
*/
@SuppressWarnings("unchecked")
@Override
public void init() throws Exception {
public final void init() throws Exception {
ctx = beanManager.createCreationalContext(null);
injectionTarget = beanManager.createInjectionTarget(
beanManager.createAnnotatedType((Class<MvvmfxCdiApplication>) this.getClass()));
Expand All @@ -99,6 +94,8 @@ public void init() throws Exception {
injectionTarget.postConstruct(this);

producer.setApplicationParameters(getParameters());

initMvvmfx();
}


Expand All @@ -113,7 +110,8 @@ public void init() throws Exception {
* @throws Exception
*/
@Override
public void stop() throws Exception {
public final void stop() throws Exception {
stopMvvmfx();

injectionTarget.preDestroy(this);
injectionTarget.dispose(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public class IntegrationTest {

@Before
public void setup(){
MyApp.wasPostConstructCalled=false;
MyApp.wasPreDestroyCalled=false;
MyApp.wasPostConstructCalled = false;
MyApp.wasPreDestroyCalled = false;
MyApp.wasInitCalled = false;
MyApp.wasStopCalled = false;
}

@Test
Expand All @@ -22,6 +24,8 @@ public void test(){
assertThat(MyApp.wasPostConstructCalled).isTrue();
assertThat(MyApp.wasPreDestroyCalled).isTrue();

assertThat(MyApp.wasInitCalled).isTrue();
assertThat(MyApp.wasStopCalled).isTrue();

assertThat(MyApp.viewTuple).isNotNull();
assertThat(MyApp.stage).isNotNull();
Expand Down
14 changes: 14 additions & 0 deletions mvvmfx-cdi/src/test/java/de/saxsys/mvvmfx/cdi/it/MyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class MyApp extends MvvmfxCdiApplication {
static boolean wasPreDestroyCalled = false;
static boolean wasPostConstructCalled = false;

static boolean wasInitCalled = false;
static boolean wasStopCalled = false;



static ViewTuple<MyView, MyViewModel> viewTuple;
Expand All @@ -36,4 +39,15 @@ public void postConstruct(){
public void preDestroy(){
wasPreDestroyCalled = true;
}


@Override
public void initMvvmfx() throws Exception {
wasInitCalled = true;
}

@Override
public void stopMvvmfx() throws Exception {
wasStopCalled = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package de.saxsys.mvvmfx.guice;

import java.util.List;

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

Expand All @@ -34,7 +36,7 @@
*
* @author manuel.mauky
*/
public abstract class MvvmfxGuiceApplication extends GuiceApplication {
public abstract class MvvmfxGuiceApplication extends GuiceApplication implements MvvmfxApplication{


@Inject
Expand All @@ -50,19 +52,20 @@ public final void init(List<Module> modules) throws Exception {
@Override
protected void configure() {
bind(HostServices.class).toProvider(new Provider<HostServices>() {
@Override public HostServices get() {
@Override
public HostServices get() {
return getHostServices();
}
});

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

bind(Parameters.class).toProvider(new Provider<Parameters> (){
bind(Parameters.class).toProvider(new Provider<Parameters>() {
@Override
public Parameters get() {
return getParameters();
Expand All @@ -73,6 +76,7 @@ public Parameters get() {


this.initGuiceModules(modules);
this.initMvvmfx();
}

/**
Expand All @@ -86,14 +90,12 @@ public final void start(Stage stage) throws Exception {

this.startMvvmfx(stage);
}

/**
* Override this method with your application startup logic.
* <p/>
* This method is a wrapper method for javafx's {@link javafx.application.Application#start(javafx.stage.Stage)}.
*/
public abstract void startMvvmfx(Stage stage) throws Exception;


@Override
public final void stop() throws Exception {
stopMvvmfx();
}

/**
* Configure the guice modules.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@

import static org.assertj.core.api.Assertions.*;

import org.junit.Before;
import org.junit.Test;

import de.saxsys.mvvmfx.guice.internal.GuiceInjector;

public class IntegrationTest {

@Before
public void setup(){
MyApp.wasInitCalled = false;
MyApp.wasStopCalled = false;
}

@Test
public void testInjectionOfPrimaryStage(){
MyApp.main("test");


assertThat(MyApp.wasInitCalled).isTrue();
assertThat(MyApp.wasStopCalled).isTrue();

assertThat(MyApp.stage).isNotNull();
assertThat(MyApp.parameters).isNotNull();
assertThat(MyApp.parameters.getUnnamed()).contains("test");
Expand Down
14 changes: 14 additions & 0 deletions mvvmfx-guice/src/test/java/de/saxsys/mvvmfx/guice/it/MyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@


public class MyApp extends MvvmfxGuiceApplication {


static boolean wasInitCalled = false;
static boolean wasStopCalled = false;

public static void main(String...args){
launch(args);
Expand Down Expand Up @@ -47,4 +51,14 @@ public void startMvvmfx(Stage stage) throws Exception {
// we can't shutdown the application in the test case so we need to do it here.
Platform.exit();
}

@Override
public void initMvvmfx() throws Exception {
wasInitCalled = true;
}

@Override
public void stopMvvmfx() throws Exception {
wasStopCalled = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.saxsys.mvvmfx.internal;

import javafx.application.Application;
import javafx.stage.Stage;

/**
* This interface defines a common set of methods that the root application classes of extensions of mvvmfx should
* implement.
*
* The interface is intended as an internal helper to unify the API of the official mvvmFX extensions (Guice and CDI at the
* moment). It is <strong>not</strong> intended to be used for other use cases.
*
* A mvvmfx extension has to do the following:
* <ol>
* <li>Overwrite {@link Application#init()} method with <string>final</string>. In this method the extension can do some
* bootstrapping (if needed).</li>
* <li>Call {@link #initMvvmfx()} as last step in the overwritten init method. The contract is that the
* {@link #initMvvmfx()} is called when the basic container bootstrapping is done so that the user can do her own
* initialization in this method.</li>
* <li>Implement {@link Application#start(Stage)} method. In this method own startup logic can be done (if needed).</li>
* <li>Call {@link #startMvvmfx(Stage)} as last step in the overwritten start method. Pass the stage instance from the
* original {@link Application#start(Stage)} method to {@link #startMvvmfx(Stage)}.</li>
* <li>Implement {@link Application#stop()} method. In this method own shutdown logic can be done (if needed).</li>
* <li>Call {@link #stopMvvmfx()} as last step in the overwritten stop method.</li>
* </ol>
*
*/
public interface MvvmfxApplication {

/**
* This method is called when the javafx application is initialized. See
* {@link javafx.application.Application#init()} for more details.
*
* @throws Exception
*/
default void initMvvmfx() throws Exception {
}

/**
* Override this method with your application startup logic.
* <p/>
* This method is a wrapper method for javafx's {@link javafx.application.Application#start(javafx.stage.Stage)}.
*/
void startMvvmfx(Stage stage) throws Exception;

/**
* This method is called when the application should stop. See {@link javafx.application.Application#stop()} for
* more details.
*
* @throws Exception
*/
default void stopMvvmfx() throws Exception {
}

}

0 comments on commit afba4d1

Please sign in to comment.