Skip to content

Commit

Permalink
Fix for issue #1 "Add possibility to use different CDI contexts for F…
Browse files Browse the repository at this point in the history
…XMLView".
  • Loading branch information
dlemmermann committed Jan 30, 2020
1 parent c056b8b commit 5d08b8e
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/main/java/com/airhacks/afterburner/views/FXMLView.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* limitations under the License.
* #L%
*/

import com.airhacks.afterburner.injection.Injector;
import com.airhacks.afterburner.injection.PresenterFactory;
import javafx.application.Platform;
Expand Down Expand Up @@ -60,7 +61,8 @@ public abstract class FXMLView extends StackPane {
protected FXMLLoader fxmlLoader;
protected String bundleName;
protected ResourceBundle bundle;
protected final Function<String, Object> injectionContext;
protected PresenterFactory presenterFactory;
protected Function<String, Object> injectionContext;
protected URL resource;
protected static Executor FX_PLATFORM_EXECUTOR = Platform::runLater;

Expand All @@ -71,16 +73,38 @@ public abstract class FXMLView extends StackPane {
* context.
*/
public FXMLView() {
this(f -> null);
this(null, f -> null);
}

/**
* Constructs the view lazily (fxml is not loaded) with empty injection
* context.
*/
public FXMLView(PresenterFactory factory) {
this(factory, f -> null);
}

/**
* Constructs a new FXML view.
*
* @param injectionContext the function is used as a injection source.
* Values matching for the keys are going to be used for injection into the
* corresponding presenter.
* Values matching for the keys are going to be used for injection into the
* corresponding presenter.
*/
public FXMLView(Function<String, Object> injectionContext) {
this(null, injectionContext);
}

/**
* Constructs a new FXML view.
*
* @param presenterFactory an explicit presenter factory, needed when working with different contexts in the same application
* @param injectionContext the function is used as a injection source.
* Values matching for the keys are going to be used for injection into the
* corresponding presenter.
*/
public FXMLView(PresenterFactory presenterFactory, Function<String, Object> injectionContext) {
this.presenterFactory = presenterFactory;
this.injectionContext = injectionContext;
this.init(getFXMLName());
}
Expand All @@ -106,6 +130,10 @@ FXMLLoader loadSynchronously(final URL resource, ResourceBundle bundle, final St
}

PresenterFactory discover() {
if (presenterFactory != null) {
return presenterFactory;
}

Iterable<PresenterFactory> discoveredFactories = PresenterFactory.discover();
List<PresenterFactory> factories = StreamSupport.stream(discoveredFactories.spliterator(), false).
collect(Collectors.toList());
Expand Down Expand Up @@ -159,7 +187,6 @@ public void getView(Consumer<Parent> consumer) {
* Creates the view asynchronously using an internal thread pool and passes
* the parent node within the UI Thread.
*
*
* @param consumer - an object interested in received the Parent as callback
*/
public void getViewAsync(Consumer<Parent> consumer) {
Expand Down Expand Up @@ -199,7 +226,6 @@ String getStyleSheetName() {
}

/**
*
* @return the name of the fxml file derived from the FXML view. e.g. The
* name for the AirhacksView is going to be airhacks.fxml.
*/
Expand Down Expand Up @@ -253,18 +279,16 @@ public void getPresenter(Consumer<Object> presenterConsumer) {
}

/**
*
* @param lowercase indicates whether the simple class name should be
* converted to lowercase of left unchanged
* @param ending the suffix to append
* converted to lowercase of left unchanged
* @param ending the suffix to append
* @return the conventional name with stripped ending
*/
protected String getConventionalName(boolean lowercase, String ending) {
return getConventionalName(lowercase) + ending;
}

/**
*
* @param lowercase indicates whether the simple class name should be
* @return the name of the view without the "View" prefix.
*/
Expand Down Expand Up @@ -299,7 +323,6 @@ public static ResourceBundle getResourceBundle(String name) {
}

/**
*
* @return an existing resource bundle, or null
*/
public ResourceBundle getResourceBundle() {
Expand All @@ -317,7 +340,6 @@ static ExecutorService getExecutorService() {
}

/**
*
* @param t exception to report
* @return nothing
*/
Expand Down

0 comments on commit 5d08b8e

Please sign in to comment.