I have a series of popups where I edit or view specific objects. I use these popups for editing various objects that are handled by and ORM (ORMLite), what I am trying to achieve is to have a generic/abstract class that implements similar behaviours through encapsulating methods. As I don't want to have the same FXML for all of the
popup dialogs what I came up with was to create a "template" FXML, load it through loadFXML() function provided by Griffon and store it in a Node object to be the root of the created Scene at the Abstract Class. I am familiar with dependency injection, but I am not aware of the AST of the framework so my Abstract class is able to call loadFromFXML() within the Abstract class I created.
I post my code here:
Concrete class implementing the abstract ViewPopUp class I created:
#ArtifactProviderFor(GriffonView.class)
public class VerConductoresView extends AbstractViewPopUp<ObservablePlanilla> {
private VerConductoresController controller;
private ConductoresModel model;
VerConductoresView() {
super(ObservablePlanilla.class, Conductor.class);
nodeM = new GridPane();
super.setController(controller);
}
#Override
public void initUI() {
Stage stage = (Stage) getApplication()
.createApplicationContainer(Collections.<String,Object>emptyMap());
stage.setTitle(getApplication().getConfiguration().getAsString("application.title"));
stage.setScene(init());
stage.sizeToScene();
getApplication().getWindowManager().attach("ver-conductores", stage);
}
}
Abstract view PopUp I created:
public abstract class AbstractViewPopUp<T> extends AbstractJavaFXGriffonView {
protected Class klazz;
protected Class<T> klazz2;
protected Scene viewScene;
protected ViewControllerPopUp viewController;
protected TableView tableView;
protected GridPane gridPane;
protected String[] ignoredNames;
protected String[] columnNames;
protected IModel<T> viewModel;
protected Node nodeM;
#MVCMember
public void setController(ViewControllerPopUp controller) {
this.viewController = controller;
}
AbstractViewPopUp(Class<T> k1, Class k2, Node node){
klazz = k2;
klazz2 = k1;
nodeM = node;
nodeM = loadFromFXML("com.softgan.viewPopUp");
nodeM = node;
}
AbstractViewPopUp(Class<T> k1, Class k2){
klazz = k2;
klazz2 = k1;
nodeM = loadFromFXML("com.softgan.viewPopUp");
}
protected Scene init() {
Scene scene = new Scene(new Group());
if (nodeM instanceof Parent) {
scene.setRoot((Parent) nodeM);
} else {
((Group) scene.getRoot()).getChildren().addAll(nodeM);
}
connectActions(nodeM, viewController);
connectMessageSource(nodeM);
return scene;
}
}
I want to load the FXML through the Abstract class and then store it so the concrete class can access the loaded FXML so I am able to manipulate its contents, adding labels and textfields dynamically. The problem seems to be that loadFromFXML is throwing a NullPointerException as it is not able to resolve the FXML file from the resources. I already tried to use an AST transformation to make it resources aware, but it seems to not be a valid approach as Guice is not able to resolve, I think, the ResourceHandler.
EDIT
This is the Stacktrace I am getting:
[griffon-pool-1-thread-2] WARN org.codehaus.griffon.runtime.core.controller.AbstractActionManager - An exception occurred when executing com.softgan.ConductoresController.view
griffon.exceptions.InstanceMethodInvocationException: An error occurred while invoking instance method com.softgan.ConductoresController.view()
at griffon.util.GriffonClassUtils.invokeExactInstanceMethod(GriffonClassUtils.java:3186)
Caused by: griffon.exceptions.GriffonException: An error occurred while executing a task inside the UI thread
at com.softgan.ConductoresController.view(ConductoresController.java:122)
at griffon.util.MethodUtils.invokeExactMethod(MethodUtils.java:407)
at griffon.util.MethodUtils.invokeExactMethod(MethodUtils.java:356)
at griffon.util.GriffonClassUtils.invokeExactInstanceMethod(GriffonClassUtils.java:3182)
Caused by: java.util.concurrent.ExecutionException: griffon.exceptions.InstanceNotFoundException: Could not find an instance of type com.softgan.VerConductoresView
... 4 more
Caused by: griffon.exceptions.InstanceNotFoundException: Could not find an instance of type com.softgan.VerConductoresView
Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors:
1) Error injecting constructor, java.lang.NullPointerException
at com.softgan.VerConductoresView.<init>(VerConductoresView.java:31)
while locating com.softgan.VerConductoresView
1 error
at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1053)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1086)
Caused by: java.lang.NullPointerException
at com.softgan.AbstractViewPopUp.<init>(AbstractViewPopUp.java:72)
at com.softgan.VerConductoresView.<init>(VerConductoresView.java:31)
at com.softgan.VerConductoresView$$FastClassByGuice$$d0c2bde8.newInstance(<generated>)
at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114)
at com.google.inject.internal.ConstructorInjector.access$000(ConstructorInjector.java:32)
at com.google.inject.internal.ConstructorInjector$1.call(ConstructorInjector.java:98)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:112)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:120)
at com.google.inject.internal.ProvisionListenerStackCallback.provision(ProvisionListenerStackCallback.java:66)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:93)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1050)
... 1 more
[griffon-pool-1-thread-2] ERROR griffon.core.GriffonExceptionHandler - Uncaught Exception. Stacktrace was sanitized. Set System property 'griffon.full.stacktrace' to 'true' for full report.
griffon.exceptions.InstanceMethodInvocationException: An error occurred while invoking instance method com.softgan.ConductoresController.view()
at griffon.util.GriffonClassUtils.invokeExactInstanceMethod(GriffonClassUtils.java:3186)
Caused by: griffon.exceptions.GriffonException: An error occurred while executing a task inside the UI thread
at com.softgan.ConductoresController.view(ConductoresController.java:122)
at griffon.util.MethodUtils.invokeExactMethod(MethodUtils.java:407)
at griffon.util.MethodUtils.invokeExactMethod(MethodUtils.java:356)
at griffon.util.GriffonClassUtils.invokeExactInstanceMethod(GriffonClassUtils.java:3182)
Caused by: java.util.concurrent.ExecutionException: griffon.exceptions.InstanceNotFoundException: Could not find an instance of type com.softgan.VerConductoresView
... 4 more
Caused by: griffon.exceptions.InstanceNotFoundException: Could not find an instance of type com.softgan.VerConductoresView
Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors:
1) Error injecting constructor, java.lang.NullPointerException
at com.softgan.VerConductoresView.<init>(VerConductoresView.java:31)
while locating com.softgan.VerConductoresView
1 error
at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1053)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1086)
Caused by: java.lang.NullPointerException
at com.softgan.AbstractViewPopUp.<init>(AbstractViewPopUp.java:72)
at com.softgan.VerConductoresView.<init>(VerConductoresView.java:31)
at com.softgan.VerConductoresView$$FastClassByGuice$$d0c2bde8.newInstance(<generated>)
at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114)
at com.google.inject.internal.ConstructorInjector.access$000(ConstructorInjector.java:32)
at com.google.inject.internal.ConstructorInjector$1.call(ConstructorInjector.java:98)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:112)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:120)
at com.google.inject.internal.ProvisionListenerStackCallback.provision(ProvisionListenerStackCallback.java:66)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:93)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1050)
... 1 more
UPDATE
I already found what the problem was. The constructor was not aware of the loadFromFXML method as in the constructor of the view the UI has not been loaded yet. What I did was simply put the loadFromFXML() inside the init() method of the Abstract Class and call it directly from the Concrete View Class. I found out this by calling the loadFromFXML from the initUI method, which is where the UI can be accessed.
AST transformations only apply if you're compiling Groovy code, which may not be what you're doing. The loadFromFXML() method expects a resource to be available on the classpath by matching the given argument using the following value transformation
arg.replaceAll('.', '/') + ".fxml"
This means your code resolves "com.softgan.viewPopUp" to "com/softgan/viewPopUp.fxml". Does that file exist in src/main/resources or griffon-app/resources?
Related
Picocli has to introspect the command tree. Doing so it needs to load the domain object classes for every Command which slows down the jvm startup.
What options are there to avoid this startup lag? One solution I've come up with is described in https://github.com/remkop/picocli/issues/482:
I am using reflection to postpone any class loading until after the command is selected. This way only the command classes themselves are loaded and finally the classes which implement the single command requested by the user:
abstract class BaseCommand implements Runnable {
interface CommandExecutor {
Object doExecute() throws Exception;
}
// find the CommandExecutor declared at the BaseCommand subclass.
protected Object executeReflectively() throws Exception {
Class<?> innerClass = getExecutorInnerClass();
Constructor<?> ctor = innerClass.getDeclaredConstructor(getClass());
CommandExecutor exec = (CommandExecutor) ctor.newInstance(this);
return exec.doExecute();
}
private Class<?> getExecutorInnerClass() throws ClassNotFoundException {
return getClass().getClassLoader().loadClass(getClass().getName() + "$Executor");
}
public void run() {
try {
executeReflectively();
} catch(...){
/// usual stuff
}
}
}
A concrete commend class:
#Command(...)
final class CopyProfile extends BaseCommand {
#Option String source;
#Option String dest;
// class must NOT be static and must be called "Executor"
public class Executor implements CommandExecutor {
#Override
public Object doExecute() throws Exception {
// you can basically wrap your original run() with this boilerplate
// all the CopyProfile's field are in scope!
FileUtils.copy(source, dest);
}
}
}
It seems like https://github.com/remkop/picocli/issues/500 may provide the ultimate solution to this. What are the other options until then?
UPDATE February 2020:
Upgrading to a recent version of picocli should fix this issue.
From the picocli 4.2.0 release notes:
From this release, subcommands are not instantiated until they are matched on the command line. This should improve the startup time for applications with subcommands that do a lot of initialization when they are instantiated.
An alternative that doesn’t require any code changes is to use GraalVM to compile your picocli-based application to a native image.
This article shows how to do this and the resulting startup time is 3 milliseconds.
I want to load Spring initial context inside a AWS lambda handler class. This class is the starting point of my application. I did it in the below way.
#SpringBootApplication
public class LambdaFunctionHandler implements RequestHandler<KinesisEvent, Object> {
#Override
public Object handleRequest(KinesisEvent input, Context context) {
AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(LambdaFunctionHandler.class);
LambdaFunctionHandler lambdaHandlerBean = appContext.getBean(LambdaFunctionHandler.class);
// some business logic call
return null;
}
}
This is working fine but I'm getting warning on appContext that it should be closed as it is resource leak. this can be fixed by calling appContext.close() but my doubt is whether this way of initializing Spring application context in a non main method is correct ? Most recommended way to do in a main method is like below
SpringApplication app = new SpringApplication(LambdaFunctionHandler.class);
ConfigurableApplicationContext context = app.run(args);
LambdaFunctionHandler lambdaFunctionHandler =
context.getBean(LambdaFunctionHandler.class);
But I don't have the value to replace the args in my case. can anyone suggest the right way of doing this
You can simple class with main method for #SpringBootApplication and
use CommandLineRunner for loading AWS lambda handler. Just implement the run to load the bean
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/CommandLineRunner.html
I'm calling a method to load a window by passing some parameters to a specific internal method of this window, but I've got this exception:
GRAVE: null
javafx.fxml.LoadException: Controller value already specified.
unknown path:12
here is my method
public void openDef(String sys, String comp, String code) throws Exception {
Stage defStage = new Stage();
FXMLLoader loader = new FXMLLoader();
DefTableController cont = new DefTableController();//calling class controller
loader.setController(cont);
Parent frame = loader.load(getClass().getResource("defTable.fxml").openStream());
cont.getSysChoice(sys, comp, code);//call the method by passing parameters
Scene sceneDef = new Scene(frame);
defStage.setTitle("DĂ©faillance du " + comp);
defStage.setScene(sceneDef);
defStage.show();
}
I don't understand why it consider that the controller is already set? and how to fix that ?
thank you
Remove the fx:controller attribute from the FXML file. That attribute is an instruction to the FXMLLoader to create a new controller: since you have already set one by calling setController it is contradictory.
JavaFX Error: Controller value already specified
This guy answered it ^ Props to him!
Probably I miss somehting out, but I'm struggeling to find a solution how I can pass dependencies like an instance of my event bus and some service interfaces to my javafx application.
I got an UI-Init class which does not much more than starting the application and receiving some dependencies for the UI like an eventBus:
public class Frontend {
public Frontend(MBassador<EventBase> eventBus) {
Application.launch(AppGui.class);
}
My AppGui class extends Application and loads an FXML:
public class AppGui extends Application {
private Stage primaryStage;
private GridPane rootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("RootLayout.fxml"));
rootLayout = (GridPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
scene.setFill(null);
primaryStage.setScene(scene);
RootLayoutController rootController = loader.getController();
rootController.init(/*here I would like to inject my eventBus*/);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
Now, how can I pass my eventBus and other service interfaces to this controller? I've read about using DI frameworks like guice (How can JavaFX controllers access other services?) or afterburner.fx to use it. But even if I use guice for the rest of my application, somehow I need to pass the Injector instance to the JavaFX application?.
But Application.launch(AppGui.class); is static and internally creates an AppGui instance on the javafx thread, which I don't get access to. So how I can inject dependencies to my AppGui object without using static variables?
Here is what I do:
The Application class has a couple of lifecycle callbacks, init() and stop().
From the Javadocs:
public void init() throws java.lang.Exception
The application initialization method. This method is called immediately after the Application class is loaded and constructed. An application may override this method to perform initialization prior to the actual starting of the application.
public void stop() throws java.lang.Exception
This method is called when the application should stop, and provides a convenient place to prepare for application exit and destroy resources.
Also from the Javadocs, the lifecycle:
Constructs an instance of the specified Application class
Calls the init() method
Calls the start(javafx.stage.Stage) method
Waits for the application to finish, which happens when either of the following occur:
the application calls Platform.exit()
the last window has been closed and the implicitExit attribute on Platform is true
Calls the stop() method
I start the IoC container in init() and stop it in stop(). Now my Application class has a reference to the IoC container and can supply the first controller with its dependencies.
As a matter of fact, I let the IoC framework manage the controllers. I set them to the loaded FXML using FXMLLoader.setController(), instead of specifying them with fx:controller.
You can pass a static reference to your application class before you call launch(). Something like:
public class Frontend {
public Frontend(MBassador<EventBase> eventBus) {
AppGui.setEventBus(eventBus);
Application.launch(AppGui.class);
}
}
public class AppGui extends Application {
private static MBassador<EventBase> eventBus;
public static void setEventBus(MBassador<EventBase> eventBus) {
this.eventBus = eventBus;
}
private MBassador<EventBase> eventBus;
#Override
public void init() {
if (AppGui.eventBus == null) {
throw new IllegalStateException();
// or however you want to handle that state
} else {
this.eventBus = AppGui.eventBus;
AppGui.eventBus = null;
}
}
}
Whether you keep and use the static reference, or you copy the static reference to a local reference is up to you and the design of your application. If you expect to instantiate more than one copy of AppGui, you may need the local reference.
No idea if this is thread safe (probably not). The advice from #Nikos and #James_D is solid and preferred... but sometimes you just need a hack. :) YMMV
I currently have an app that has many activities and needs to have a way of maintaining state between these activities.
I use the Application class to do this, declaring my global variables and using getters and setters to interact with my activities.
I was hoping to place a few custom methods in there, so that when I want to do a common task like, for instance, display an error message, I can declare the method in my application class and call it from any activity that uses it
EscarApplication application = (EscarApplication) this.getApplication();
EscarApplication being the name of my application class above.
I have tried to include this method in my application class:
public void showError(String title, String message) {
Log.i("Application level",message);
this.alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
return;
}
});
alertDialog.show();
}
In the hope that I can call this method from activity without having to redeclare it, but when I call it using something like below I get an null pointer exception:
Visit.this.application.showError("Update error", "An error has occurred while trying to communicate with the server");
Visit being the name of my current activity above.
Should this work, or can I only use getters and setters to change global vars in an Application Class.
Stack Trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{escar.beedge/escar.beedge.HomeScreen}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$2100(ActivityThread.java:116)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4203)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(375): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:460)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:238)
at escar.beedge.EscarApplication.showError(EscarApplication.java:98)
at escar.beedge.HomeScreen.onCreate(HomeScreen.java:30)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
The dialog is declared as such in the application class:
AlertDialog alertDialog;
Created in that same class:
alertDialog = new AlertDialog.Builder(this).create();
and the method to call it in that class is as follows:
public void showError(String title, String message) {
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
return;
}
});
alertDialog.show();
}
And finally, it is called from an activity like so:
EscarApplication application;
application = (EscarApplication) this.getApplication();
application.showError("test", "display this message");
If you need to maintain state between activities, then use a service. Anything else is a hack
Someone correct me if Im wrong, but an Application class wont be able to execute view related objects as they need to be bound to a view which needs to be bound to an activity.
In that sense, you could use your Application class to implement a static method that customises the dialog
public static void setDialog(String title, String message,AlertDialog alertDialog){
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
return;
}
});
}
but you would have to create the dialog and call the show method on the activities themselves (actually maybe even the button to be set in the dialog would need to be created on the activity)
Another option could be to implement a class that extends the AlertDialog class and whose button is pre-set to the behavior you want.
You could use the Singleton pattern.
I'm looking to achieve something similar to you.
I haven't found an official answer, but it looks like you shouldn't be using the application context for Toast and Dialogs. Instead, try using the context of an Activity like so :
// From inside your activity
Dialog dialog = new Dialog(this);
instead of this:
// From inside your Application instance
Dialog dialog = new Dialog(getApplicationContext());
Read this :
Android: ProgressDialog.show() crashes with getApplicationContext