jeudi 22 mars 2018

Deserializing JavaFX properties with yWorks and Java Reflection API

I am implementing a process managing tool and I am using (for research purposes) the yWorks library "yFiles for JavaFX".

I am doing fine so far, but that one thing just sets a show stopper for me for 2 days now:

Since im using JavaFX beans and their properties to edit and display my data, I had to implement some kind of custom serializer:

graphMLIOHandler.addHandleSerializationListener(
            (source, hsArgs) -> {
                // Only serialize items that are of the specific type.
                if (hsArgs.getItem() instanceof StringProperty) {
                    //serialize
                } else if (hsArgs.getItem() instanceof IntegerProperty) {
                    //serialize
                } else if (hsArgs.getItem() instanceof DoubleProperty) {
                    //serialize
                } else if (hsArgs.getItem() instanceof SimpleListProperty) {
                    //serialize
                }
            });

But when loading the file i was writing I get a IllegalArgumentException. yFiles wants to invoke a setValue method and is throwing a DeserializationNotSupportedException. So I tried to implement a addHandleDeserializationListener. Didn't work. Then I implemented MarkupExtensions for my Project class. Nothing changed. Giving you guys the code for the latter implementation and hoping that somebody finds what i missed. Im kinda on my last resort here..

CustomGraphController:

public class CustomGraphController extends GraphControl{

   private IGraph graph ;
   private GraphMLIOHandler graphMLIOHandler;

    public CustomGraphController(){
        super();
        graph = this.getGraph();
        graph.setTag(new Project());
    }
}

Project.class:

@GraphML(markupExtensionConverter = ProjectMEC.class)
public class Project {
    SimpleListProperty<ParameterOccurrence> generalParameters;

    public Project(){
        generalParameters = new SimpleListProperty<>(FXCollections.observableArrayList());
    }

    public ObservableList<ParameterOccurrence> getGeneralParameters() {
        return generalParameters;
    }

    public SimpleListProperty<ParameterOccurrence> generalParametersProperty() {
        return generalParameters;
    }

    public void setGeneralParameters(ObservableList<ParameterOccurrence> generalParameters) {
        this.generalParameters.set(generalParameters);
    }
}

ProjectME.class

public class ProjectME extends MarkupExtension {

    ArrayList<ParameterOccurrence> generalParameters;

    public ProjectME() {
        super();
    }

    public ArrayList<ParameterOccurrence> getGeneralParameters() {
        return generalParameters;
    }

    public void setGeneralParameters(ArrayList<ParameterOccurrence> generalParameters) {
        this.generalParameters = generalParameters;
    }


    @Override
    public Object provideValue(ILookup iLookup) {
        Project ret = new Project();
        ret.getGeneralParameters().addAll(generalParameters);
        return ret;
    }
}

ProjectMEC.class:

public class ProjectMEC implements IMarkupExtensionConverter {

    @Override
    public boolean canConvert(IWriteContext iWriteContext, Object o) {
        return o instanceof Project;
    }

    @Override
    public MarkupExtension convert(IWriteContext iWriteContext, Object o) {
        ProjectME extension = new ProjectME();
        ObservableList<ParameterOccurrence> temp = FXCollections.observableArrayList(((Project)o).getGeneralParameters());
        extension.getGeneralParameters().addAll(temp);
        return extension;
    }
}

Stacktrace:

Caused by: com.yworks.yfiles.graphml.DeserializationNotSupportedException: Error parsing property GeneralParameters: argument type mismatch
    at com.yworks.yfiles.graphml.XamlReader.b(Unknown Source)
    at com.yworks.yfiles.graphml.XamlReader.c(Unknown Source)
    ... 83 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.yworks.yfiles.graphml.PropertyInfo.setValue(Unknown Source)
    at com.yworks.yfiles.graphml.Property.setValue(Unknown Source)
    ... 85 more

My guess was that the GeneralParameters property (even though I tryed to implement a addHandleDeserializationListener for SimpleListProperty) somehow need this MarkupExtension. But the stacktrace stayed exactly the same and since the reflection API and the closed source stuff I cant find, where exactly the Exception is thrown...

ANY idea, whats happending?





Aucun commentaire:

Enregistrer un commentaire