001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.client.tide.javafx;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    
026    import javafx.fxml.FXMLLoader;
027    import javafx.util.Callback;
028    
029    import javax.inject.Named;
030    
031    import org.granite.client.tide.Context;
032    
033    /**
034     * @author William DRAI
035     */
036    public class TideFXMLLoader {
037    
038        public static Object load(final Context context, String url, Class<?> controllerClass) throws IOException {
039            InputStream fxmlStream = null;
040            try {
041                fxmlStream = controllerClass.getResourceAsStream(url);
042                FXMLLoader loader = new FXMLLoader();
043                loader.setLocation(controllerClass.getResource(url));
044                loader.setControllerFactory(new ControllerFactory(context));
045                    loader.getNamespace().putAll(context.allByAnnotatedWith(Named.class));
046                
047                return loader.load(fxmlStream);
048            }
049            finally {
050                if (fxmlStream != null) {
051                    fxmlStream.close();
052                }
053            }
054        }
055            
056        public static Object load(String url, final Object controller) throws IOException {
057            InputStream fxmlStream = null;
058            try {
059                fxmlStream = controller.getClass().getResourceAsStream(url);
060                FXMLLoader loader = new FXMLLoader();
061                loader.setLocation(controller.getClass().getResource(url));
062                loader.setControllerFactory(new Callback<Class<?>, Object>() {
063                                    @Override
064                                    public Object call(Class<?> type) {
065                                            if (type.isInstance(controller))
066                                                    return controller;
067                                            try {
068                                                    return type.newInstance();
069                                            }
070                                            catch (Exception e) {
071                                                    throw new RuntimeException("Could not instantiate controller of class " + type);
072                                            }
073                                    }
074                });
075                return loader.load(fxmlStream);
076            }
077            finally {
078                if (fxmlStream != null) {
079                    fxmlStream.close();
080                }
081            }
082        }
083        
084        public static class ControllerFactory implements Callback<Class<?>, Object> {
085            
086            private final Context context;
087            
088            public ControllerFactory(Context context) {
089                    this.context = context;
090            }
091            
092            @Override
093            public Object call(Class<?> type) {
094                    return context.byType(type);
095            }
096        }
097    }