Added CDI to the application
This commit is contained in:
49
src/de/geofroggerfx/fx/FxMain.java
Normal file
49
src/de/geofroggerfx/fx/FxMain.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package de.geofroggerfx.fx;
|
||||
|
||||
import de.geofroggerfx.fx.utils.StartupScene;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.stage.Stage;
|
||||
import org.scenicview.ScenicView;
|
||||
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* This class is based on the tutorial
|
||||
* http://blog.matthieu.brouillard.fr/2012/08/fxml-javafx-powered-by-cdi-jboss-weld_6.html
|
||||
*
|
||||
* Thanks to Matthieu BROUILLARD
|
||||
*/
|
||||
public class FxMain {
|
||||
|
||||
@Inject
|
||||
private FXMLLoader fxmlLoader;
|
||||
|
||||
public void start( @Observes @StartupScene Stage stage) throws IOException
|
||||
{
|
||||
try (InputStream fxml = getClass().getResourceAsStream( "geofrogger/geofrogger.fxml" )) {
|
||||
fxmlLoader.setResources(ResourceBundle.getBundle("de.geofroggerfx.fx.geofrogger"));
|
||||
final Parent root = fxmlLoader.load(fxml);
|
||||
final Scene scene = new Scene(root);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
|
||||
scene.setOnKeyPressed(keyEvent -> {
|
||||
if (isScenicViewShortcutPressed(keyEvent)) {
|
||||
ScenicView.show(scene);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isScenicViewShortcutPressed(final KeyEvent keyEvent) {
|
||||
return keyEvent.isAltDown() && keyEvent.isControlDown() && keyEvent.getCode().equals(KeyCode.V);
|
||||
}
|
||||
}
|
||||
@@ -25,38 +25,47 @@
|
||||
*/
|
||||
package de.geofroggerfx.fx;
|
||||
|
||||
import de.geofroggerfx.application.ServiceManager;
|
||||
import de.geofroggerfx.fx.utils.ApplicationParametersProvider;
|
||||
import de.geofroggerfx.fx.utils.StartupScene;
|
||||
import de.geofroggerfx.sql.DatabaseService;
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.stage.Stage;
|
||||
import org.scenicview.ScenicView;
|
||||
import org.jboss.weld.environment.se.Weld;
|
||||
import org.jboss.weld.environment.se.WeldContainer;
|
||||
|
||||
import java.util.ResourceBundle;
|
||||
import javax.enterprise.util.AnnotationLiteral;
|
||||
|
||||
/**
|
||||
* @author Andreas
|
||||
*/
|
||||
public class GeoFroggerFXMain extends Application {
|
||||
|
||||
private Weld weld;
|
||||
private WeldContainer weldContainer;
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
super.init();
|
||||
|
||||
// Init Weld CDI
|
||||
weld = new Weld();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception {
|
||||
loadCustomFonts();
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("geofrogger/geofrogger.fxml"), ResourceBundle.getBundle("de.geofroggerfx.fx.geofrogger"));
|
||||
Parent root = (Parent)fxmlLoader.load();
|
||||
Scene scene = new Scene(root);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
|
||||
scene.setOnKeyPressed(keyEvent -> {
|
||||
if (isScenicViewShortcutPressed(keyEvent)) {
|
||||
ScenicView.show(scene);
|
||||
}
|
||||
});
|
||||
weldContainer = weld.initialize();
|
||||
|
||||
// Make the application parameters injectable with a standard CDI
|
||||
// annotation
|
||||
weldContainer.instance().select(ApplicationParametersProvider.class).get().setParameters(getParameters());
|
||||
|
||||
// Now that JavaFX thread is ready
|
||||
// let's inform whoever cares using standard CDI notification mechanism:
|
||||
// CDI events
|
||||
weldContainer.event().select(Stage.class, new AnnotationLiteral<StartupScene>() {}).fire(stage);
|
||||
}
|
||||
|
||||
private void loadCustomFonts() {
|
||||
@@ -72,13 +81,10 @@ public class GeoFroggerFXMain extends Application {
|
||||
Font.loadFont(GeoFroggerFXMain.class.getResource("/fonts/FiraSansOT-RegularItalic.otf").toExternalForm(), 12);
|
||||
}
|
||||
|
||||
private boolean isScenicViewShortcutPressed(final KeyEvent keyEvent) {
|
||||
return keyEvent.isAltDown() && keyEvent.isControlDown() && keyEvent.getCode().equals(KeyCode.V);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
ServiceManager.getInstance().getDatabaseService().getEntityManager().close();
|
||||
weldContainer.instance().select(DatabaseService.class).get().getEntityManager().close();
|
||||
weld.shutdown();
|
||||
super.stop();
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ import javafx.util.Duration;
|
||||
import jfxtras.labs.map.render.ImageMapMarker;
|
||||
import jfxtras.labs.map.render.MapMarkable;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.net.URL;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Arrays;
|
||||
@@ -97,7 +98,8 @@ public class CacheDetailsController implements Initializable, SessionContextList
|
||||
private WebView shortDescriptionWebView;
|
||||
private WebView longDescriptionWebView;
|
||||
|
||||
private final SessionContext sessionContext = SessionContext.getInstance();
|
||||
@Inject
|
||||
private SessionContext sessionContext;
|
||||
|
||||
/**
|
||||
* Initializes the controller class.
|
||||
|
||||
@@ -142,6 +142,6 @@
|
||||
</TabPane>
|
||||
</children>
|
||||
<stylesheets>
|
||||
<URL value="@../geofrogger.css"/>
|
||||
<URL value="@/de/geofroggerfx/fx/geofrogger.css"/>
|
||||
</stylesheets>
|
||||
</AnchorPane>
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
*/
|
||||
package de.geofroggerfx.fx.cachelist;
|
||||
|
||||
import de.geofroggerfx.application.ServiceManager;
|
||||
import de.geofroggerfx.application.SessionContext;
|
||||
import de.geofroggerfx.application.SessionContextListener;
|
||||
import de.geofroggerfx.fx.components.CacheListCell;
|
||||
@@ -39,10 +38,14 @@ import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.control.Menu;
|
||||
import javafx.scene.control.MenuButton;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.util.Callback;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
@@ -59,8 +62,12 @@ public class CacheListController implements Initializable, SessionContextListene
|
||||
|
||||
private static final String CACHE_LIST_ACTION_ICONS = "cache-list-action-icons";
|
||||
|
||||
private final SessionContext sessionContext = SessionContext.getInstance();
|
||||
private final CacheService cacheService = ServiceManager.getInstance().getCacheService();
|
||||
@Inject
|
||||
private SessionContext sessionContext;
|
||||
|
||||
@Inject
|
||||
private CacheService cacheService;
|
||||
|
||||
private SortingMenuItem currentSortingButton = null;
|
||||
|
||||
@FXML
|
||||
|
||||
@@ -26,6 +26,6 @@
|
||||
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="40.0"/>
|
||||
</children>
|
||||
<stylesheets>
|
||||
<URL value="@../geofrogger.css"/>
|
||||
<URL value="@/de/geofroggerfx/fx/geofrogger.css"/>
|
||||
</stylesheets>
|
||||
</AnchorPane>
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
package de.geofroggerfx.fx.geofrogger;
|
||||
|
||||
import de.geofroggerfx.application.ProgressEvent;
|
||||
import de.geofroggerfx.application.ServiceManager;
|
||||
import de.geofroggerfx.application.SessionContext;
|
||||
import de.geofroggerfx.gpx.GPXReader;
|
||||
import de.geofroggerfx.model.Cache;
|
||||
@@ -45,6 +44,7 @@ import javafx.scene.control.*;
|
||||
import javafx.stage.FileChooser;
|
||||
import org.controlsfx.dialog.Dialog;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
@@ -53,8 +53,8 @@ import java.util.ResourceBundle;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static de.geofroggerfx.service.CacheSortField.*;
|
||||
import static de.geofroggerfx.service.SortDirection.*;
|
||||
import static de.geofroggerfx.service.CacheSortField.NAME;
|
||||
import static de.geofroggerfx.service.SortDirection.ASC;
|
||||
|
||||
/**
|
||||
* FXML Controller class
|
||||
@@ -97,13 +97,19 @@ public class GeofroggerController implements Initializable {
|
||||
+ "\t- H2 1.3.173\n"
|
||||
+ "\t- Icons by http://iconmonstr.com/\n";
|
||||
|
||||
private final SessionContext sessionContext = SessionContext.getInstance();
|
||||
@Inject
|
||||
private SessionContext sessionContext;
|
||||
private final LoadCachesFromFileService loadService = new LoadCachesFromFileService();
|
||||
private final LoadCachesFromDatabaseService loadFromDBService = new LoadCachesFromDatabaseService();
|
||||
|
||||
private final GPXReader gpxReader = ServiceManager.getInstance().getGPXReader();
|
||||
private final CacheService cacheService = ServiceManager.getInstance().getCacheService();
|
||||
private final PluginService pluginService = ServiceManager.getInstance().getPluginService();
|
||||
@Inject
|
||||
private GPXReader gpxReader;
|
||||
|
||||
@Inject
|
||||
private CacheService cacheService;
|
||||
|
||||
@Inject
|
||||
private PluginService pluginService;
|
||||
|
||||
@FXML
|
||||
private Label leftStatus;
|
||||
|
||||
@@ -29,10 +29,10 @@
|
||||
<SplitPane dividerPositions="0.3779342723004695" focusTraversable="true" prefHeight="-1.0" prefWidth="-1.0"
|
||||
VBox.vgrow="ALWAYS">
|
||||
<items>
|
||||
<fx:include source="../cachelist/cache_list.fxml" fx:id="cacheListContent"/>
|
||||
<fx:include source="/de/geofroggerfx/fx/cachelist/cache_list.fxml" fx:id="cacheListContent"/>
|
||||
<ScrollPane fitToHeight="true" fitToWidth="true" pannable="false" prefHeight="-1.0" prefWidth="-1.0">
|
||||
<content>
|
||||
<fx:include source="../cachedetails/cache_details.fxml" fx:id="cacheDetailsContent"/>
|
||||
<fx:include source="/de/geofroggerfx/fx/cachedetails/cache_details.fxml" fx:id="cacheDetailsContent"/>
|
||||
</content>
|
||||
</ScrollPane>
|
||||
</items>
|
||||
@@ -56,6 +56,6 @@
|
||||
</HBox>
|
||||
</children>
|
||||
<stylesheets>
|
||||
<URL value="@../geofrogger.css"/>
|
||||
<URL value="@/de/geofroggerfx/fx/geofrogger.css"/>
|
||||
</stylesheets>
|
||||
</VBox>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package de.geofroggerfx.fx.utils;
|
||||
|
||||
import javafx.application.Application;
|
||||
|
||||
import javax.enterprise.inject.Produces;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
/**
|
||||
* This class is based on the tutorial
|
||||
* http://blog.matthieu.brouillard.fr/2012/08/fxml-javafx-powered-by-cdi-jboss-weld_6.html
|
||||
*
|
||||
* Thanks to Matthieu BROUILLARD
|
||||
*/
|
||||
@Singleton
|
||||
public class ApplicationParametersProvider {
|
||||
private Application.Parameters parameters;
|
||||
|
||||
public void setParameters(Application.Parameters p) {
|
||||
this.parameters = p;
|
||||
}
|
||||
|
||||
public @Produces Application.Parameters getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
}
|
||||
27
src/de/geofroggerfx/fx/utils/FXMLLoaderProducer.java
Normal file
27
src/de/geofroggerfx/fx/utils/FXMLLoaderProducer.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package de.geofroggerfx.fx.utils;
|
||||
|
||||
import javafx.fxml.FXMLLoader;
|
||||
|
||||
import javax.enterprise.inject.Instance;
|
||||
import javax.enterprise.inject.Produces;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* This class is based on the tutorial
|
||||
* http://blog.matthieu.brouillard.fr/2012/08/fxml-javafx-powered-by-cdi-jboss-weld_6.html
|
||||
*
|
||||
* Thanks to Matthieu BROUILLARD
|
||||
*/
|
||||
public class FXMLLoaderProducer {
|
||||
|
||||
@Inject
|
||||
private Instance<Object> instance;
|
||||
|
||||
@Produces
|
||||
public FXMLLoader createLoader()
|
||||
{
|
||||
final FXMLLoader loader = new FXMLLoader();
|
||||
loader.setControllerFactory(param -> instance.select(param).get());
|
||||
return loader;
|
||||
}
|
||||
}
|
||||
19
src/de/geofroggerfx/fx/utils/StartupScene.java
Normal file
19
src/de/geofroggerfx/fx/utils/StartupScene.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package de.geofroggerfx.fx.utils;
|
||||
|
||||
import javax.inject.Qualifier;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* This class is based on the tutorial
|
||||
* http://blog.matthieu.brouillard.fr/2012/08/fxml-javafx-powered-by-cdi-jboss-weld_6.html
|
||||
*
|
||||
* Thanks to Matthieu BROUILLARD
|
||||
*/
|
||||
@Qualifier
|
||||
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface StartupScene {
|
||||
}
|
||||
Reference in New Issue
Block a user