DataFX Controller Questions

484 views
Skip to first unread message

Andrew Ackerman

unread,
Oct 22, 2014, 7:55:59 PM10/22/14
to dataf...@googlegroups.com
Hey all,

I'm new to the DataFX world and still trying to find my way around.  I am currently playing with the Controller API to utilize the ViewContext injection.

Two things I haven't been able to figure out is:

1.  Is there a way to register things to the context before the @PostConstruct is called on the controller?

Something like:

public class test extends Application
{

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        ViewContext<ViewController> c = ViewFactory.getInstance().createByController(ViewController.class);
        c.register("hi", "hello");
    }
   
    @FXMLController("fxml_load_wait.fxml")
    static public class ViewController
    {
        @FXMLViewContext
        ViewContext<?> context;
       
        @PostConstruct
        public void init()
        {
            System.out.println(context.getRegisteredObject("hi"));
        }
    }
   
    public static void main(String[] args)
    {
        launch(args);
    }

}
returns null instead of "hello".

2.  What is the correct way to inject into sub controllers?  For example if the main.fxml has a <fx:include fx:id="subView" source="subView.fxml"> the FXMLLoader will take care of the creating the subView node and subViewController and thus the subViewController will not have any ContextView injected into it.  The only way I can think around this is to not use the fx:include and instead in the main controller class manually create the subView with the ViewFactory and then inject into the context and add the node.  That or flatten the main and subView into one huge fxml and controller.

A third more philosophical question:  If I have two implementation of a services (one production and one test) that take a query and return a result would the "right" thing to do be inject a version of the service into the ViewContext for use by the controller?  Most examples show the ViewContext as more of the Model object and using it this way seem wrong.  (I am also new to dependency injection).

Thanks for any help!
~ Drew

Hendrik Ebbers

unread,
Oct 23, 2014, 3:33:44 AM10/23/14
to dataf...@googlegroups.com
Hi & welcome to DataFX

1. Since DataFX 8 you should not use the Controller API without the Flow. You can use it (as you do) but in that case it's limited. What you want is one of these limitations. When using the Flow API you can inject data in your controller by using @inject. In this case the data will be there when the post construct method is called. The best way is to update your DataFX dependencies to version 8.0 and use the flow / injection API. To do so you only need the io.datafx:injection:8.0 dependency. This will add all the stuff.

You can find tutorials about the flow API here: http://www.guigarage.com/2014/05/datafx-8-0-tutorials/
Even if you have ably one controller you should create a flow that contains only this controller: new Flow(MyController.class). By doing so the DataFX container will manage your controller and inject data automatically. The tutorials only show how to define MVC views in the controller. A good tutorial about injection is currently still missing but you can find examples in our JavaOne slides: http://de.slideshare.net/HendrikEbbers/datafx-8-javaone-2014?ref=http://www.guigarage.com/

2. This should work in the final version of DataFX 8. If you have any problem here please write a short demo and create a issue at bitbucket. I tried this some time ago and it was working :)

Hendrik

Andrew Ackerman

unread,
Oct 23, 2014, 2:00:24 PM10/23/14
to dataf...@googlegroups.com
Hey and thanks for the response!

So using the http://www.guigarage.com/2013/12/datafx-controller-framework-preview/ I think I have a basic handle on @Inject.  A question about this tutorial: Would it be possible to @Inject the DataProvider interface into the controller directly and at runtime specify which implementation of DataProvider to use (for example production vs unit test versions)?

Speaking of the Flow API.  The application that I am trying to make is similar to an IDE opening files.  There is a tree to the left as if you click on a leaf it opens a view in a new tab in the main area.  If it is already opened it just gives focus to the tab.  From the examples I've seen on the Flow API it isn't immediately obvious to me how to do this.  Any suggestions?

~ Drew

Hendrik Ebbers

unread,
Oct 30, 2014, 4:58:39 AM10/30/14
to dataf...@googlegroups.com
About your first question: One of the next DataFX Version (I think it will be 8.2 and the plan is to release it in January) will contain support for TestFX. By doing so you can write tests for a Flow or a controller and inject test implementations. You can find an example in my JavaOne slides about Testing with JavaFX: http://de.slideshare.net/HendrikEbbers/test-driven-development-with-java-fx (the DataFX part starts on slide 34)

About your tree / workbench problem:
The next DataFX Version (8.1 and it will be released next month) will contain a message bus to communicate between different flows. In your example the tree will be one flow and the workbench another one. When clicking on the tree node in the menu you can send a message / update to the workbench flow to trigger a specific action.

Felix

unread,
Nov 12, 2014, 5:39:06 AM11/12/14
to dataf...@googlegroups.com
Hi Hendrik,

On Thursday, October 23, 2014 9:33:44 AM UTC+2, Hendrik Ebbers wrote:
[...]

2. This [injection into subcontrollers] should work in the final version of DataFX 8. [...]

How can I get the jars of the final version?

Unfortunately I have never used gradle so I do not know how to build it from source.
Changing the version in maven pom does work neither.

Felix

Hendrik Ebbers

unread,
Nov 12, 2014, 1:56:19 PM11/12/14
to dataf...@googlegroups.com
They are all published to Maven central. If you use maven or gradle you can define them as dependencies. Otherwise you can download them at maven central: http://search.maven.org/#search%7Cga%7C1%7Cio.datafx

Felix

unread,
Nov 13, 2014, 7:20:52 AM11/13/14
to dataf...@googlegroups.com
Thanks a lot!
The download section of the datafx-website only links to old versions, so I missed it.

Andrews second question was about subcontrollers. I tried using subcontrollers with the example attached to this message (and datafx jars of version 8.0).
The subcontroller does not get initialized. What is my mistake?

Felix
subcontrollers.zip

Tobias Eiss

unread,
Nov 18, 2014, 4:37:56 AM11/18/14
to dataf...@googlegroups.com
I've the same problem!
This would be interesting!

Hendrik Ebbers

unread,
Dec 21, 2014, 9:56:44 AM12/21/14
to dataf...@googlegroups.com
Hi,
sub controllers are still an open point in datafx. There are some hacks how you can use them with datafx but there isn't any official support. That's one of the points that I want to work on in future.

Codeur monsieur

unread,
Aug 23, 2016, 2:53:12 PM8/23/16
to DataFX
Hi Hendrik,

'In the future' you said, we are in 2016.
Have you find a solution for that ?
Thanks

Reply all
Reply to author
Forward
0 new messages