Question

I'm trying to combine CDI (weld-se 2) and JavaFX and I want to annotate my controller class with custom created annotation so this class creation is managed using my factory method. I suppose that is should looks like below but this code is not working. Could you possibly advice what should be changed?

Annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE })
@Qualifier
public @interface FXMLManaged {
}

Factory class:

public class FXMLManagedProducer {
    @Produces @FXMLManaged
    public <T> T getFXMLManagedInstance(Class<T> type) {
        return type.newInstance();
    }
}

Controller class:

@FXMLManaged
public class NewsListView {
}
Was it helpful?

Solution

You seem to mix CDI Extensions with producers. First, if you want to have a producer, the factory method should return a NewsListView, not a generic type. Using @Producer along with a qualifier annotation will bind with the annotated type. So there is no need to annotate NewsListView with @FXMLManaged. Then you inject your NewsListView somewhere in a bean.

Producing the view:

public class FXMLManagedProducer {
    @Produces @FXMLManaged
    public NewsListView getFXMLManagedInstance() {
        return new NewsListView();
    }
}

Using the producer:

public class SomeBean {
    @Inject @FXMLManaged
    NewsListView view;
}

But my guess is that this is not what you are looking for. I think you might want to create a CDI Extension

public class YourExtension implements Extension {

    <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) {
        if(pat.getAnnotatedType().isAnnotationPresent(FXMLManaged.class)) {
            // do your stuff here
        }
    } 
}

This way you would process your annotated NewsListView. You may want to have a look at other methods to hook into the lifecycle, so you can create the bean and inject dependencies if neccessary.

OTHER TIPS

First of all you need to create a Weld-Container to use CDI. Here are some examples:

http://java.dzone.com/articles/fxml-javafx-powered-cdi-jboss http://blog.matthieu.brouillard.fr/2012/08/fxml-javafx-powered-by-cdi-jboss-weld_6.html

There is a work-in-progress CDI API for JavaFX in development. It will be part of DataFX. You can find some news here:

http://www.guigarage.com/2013/05/designing-javafx-business-applications-part-2/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top