├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── pom.xml └── src └── main └── java └── com └── tangorabox └── reactivedesk ├── FXMLView.java ├── ReactiveApplication.java ├── ReactiveModel.java └── internal ├── FXMLInflater.java ├── ReactiveDeskModule.java └── ReactiveInjector.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | dependency-reduced-pom.xml 8 | buildNumber.properties 9 | .mvn/timing.properties 10 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 11 | .mvn/wrapper/maven-wrapper.jar 12 | /.idea/ 13 | *.iml 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | languague: java 2 | addons: 3 | sonarcloud: 4 | organization: "tangorabox" 5 | 6 | script: 7 | # the following command line builds the project and then execute the SonarCloud analysis 8 | - mvn clean install sonar:sonar -Dsonar.login=${SONAR_TOKEN} 9 | 10 | 11 | cache: 12 | directories: 13 | - '$HOME/.m2/repository' 14 | - '$HOME/.sonar/cache' -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ReactiveDeskFX (JavaFX and Google Guice MVVM Pattern micro-framework) 2 | 3 | JavaFX micro-framework to develop very fast JavaFX components with minimal code following MVVM architecture pattern with passive view. 4 | 5 | FXML auto-loading with Google Guice dependency Injection. 6 | 7 | [![License: LGPL v3](https://img.shields.io/badge/License-LGPLv3-blue.svg)](https://opensource.org/licenses/LGPL-3.0) 8 | [![Build Status](https://travis-ci.com/TangoraBox/ReactiveDeskFX.svg?branch=master)](https://travis-ci.com/TangoraBox/ReactiveDeskFX) 9 | [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=com.tangorabox%3Areactive-desk-fx&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.tangorabox%3Areactive-desk-fx) 10 | 11 | --- 12 | 13 | ## Installation 14 | 15 | This library is compatible with java 11 or higher version, lower versions are not supported 16 | 17 | ### Maven artifact 18 | 19 | ```xml 20 | 21 | com.tangorabox 22 | reactive-desk-fx 23 | 1.0.2 24 | 25 | ``` 26 | 27 | ## Features 28 | 29 | - An a 'ready to go' `ReactiveApplication` class that extends `Application` to auto-instance the Guice Injector, simply extend it and override the `startReactiveApp(Stage primaryStage)` to start your JavaFX Application with Guice Injection. @Inject attributes of your Application class are ready to use in startReactiveApp method call 30 | 31 | - Automatic FXML file loading (your component must be inyected, don't use `new`) 32 | 33 | - `ReactiveModel` combines multiple JavaFX properties in only one model which has invalidation events 34 | 35 | - Injection of custom components inside your view (through @Inject) 36 | 37 | - Convention over Configuration: you don't need to specify the component of .fxml, simply place .fxml with the same name in the same location of your component (you must set up maven accord for that) 38 | 39 | - Optional tag `fxml:controller´ in your .fxml (only needed if you want IDE features) 40 | 41 | --- 42 | 43 | ## Example project 44 | 45 | If you want to see how to code with this framework please visit my [ResumeFX project](https://github.com/TangoraBox/ResumeFX) witch uses that. 46 | 47 | --- 48 | 49 | ## Usage Steps 50 | 1. .fxml file must have `` tag (`fx:controller` attribute it's optional) 51 | 2. View class must extend the class of your top node in level hierarchy of your .fxml (`type` attribute of `fx:root`) 52 | 3. View class must be annotated with `@FXMLView` (`com.tangorabox.reactivedesk.FXMLView`) 53 | 4. After injection logic can be done in `@FML void initialize()` method as usually in JavaFX (or implementing `Initializable` interface) 54 | 55 | ### Maven configuration for nested .fxml 56 | .fxml files must be placed at the same level of it's .java component, If you want place .fxml in the same package, not in resources folder, 57 | you must set up maven accordingly: 58 | 59 | ```xml 60 | 61 | 62 | 63 | src/main/resources 64 | 65 | 66 | ${project.build.sourceDirectory} 67 | 68 | **/*.java 69 | 70 | 71 | 72 | 73 | 74 | 75 | src/test/resources 76 | 77 | 78 | ${project.build.testSourceDirectory} 79 | 80 | **/*.java 81 | 82 | 83 | 84 | 85 | 86 | ``` 87 | --- 88 | 89 | ### Limitations 90 | When custom component is included inside another .fxml, there should be no customization properties in the tag, or fxml loading will crash on runtime. 91 | 92 | #### Non compliant example: 93 | 94 | ```xml 95 | 96 | ``` 97 | 98 | #### Compliant example 99 | 100 | ```xml 101 | 102 | 103 | 104 | ``` 105 | 106 | As you can see, restrict size values through a wrapping Parent, or set this in fxml or your CustomComponent, or in the view code. 107 | 108 | If you want to set the text "hello world" please do it programmatically in your view code. 109 | 110 | ## Inspiration Projects 111 | 112 | - [Afterburner.fx](https://github.com/AdamBien/afterburner.fx): I think that it's FXMLView class generate multiple empty useless classes in projects, one for each view 113 | 114 | - [JuiceFx](https://github.com/garzy/JuiceFX): my fork of Afterburner.fx to add @Guice injection features 115 | 116 | - [mvvmFX](https://github.com/sialcasa/mvvmFX): interesting alternative but seems to have problems with java11+ 117 | 118 | - [fx-guice](https://github.com/cathive/fx-guice): very similar to my project, and I could have used it, but it seems the project is very old and is dead 119 | 120 | 121 | ## Contributing 122 | 123 | > If you want to contribute to upgrade this project with new features or fixing bugs, you're welcome, please make a pull request. 124 | 125 | --- 126 | 127 | ## Team 128 | 129 | 130 | | **GaRzY** | 131 | | :---: 132 | | [![GaRzY](https://avatars0.githubusercontent.com/u/10849239?s=200)](https://github.com/garzy) 133 | | `github.com/garzy` | 134 | 135 | 136 | --- 137 | 138 | ## Support 139 | 140 | Reach out to me at one of the following places! 141 | 142 | - Mail to [info@tangorabox.com](mailto:info@tangorabox.com) 143 | - Twitter at `@garzydj` 144 | 145 | --- 146 | 147 | 148 | ## License 149 | 150 | [![License: LGPL v3](https://img.shields.io/badge/License-LGPLv3-blue.svg)](https://opensource.org/licenses/LGPL-3.0) 151 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | 6 | com.tangorabox 7 | reactive-desk-fx 8 | 1.0.3-SNAPSHOT 9 | reactive-desk-fx 10 | 11 | https://github.com/TangoraBox/ReactiveDeskFX 12 | JavaFX micro-framework that follows MVVM Pattern with Google Guice dependency Injection 13 | 14 | 15 | UTF-8 16 | UTF-8 17 | 11 18 | 2021 19 | 20 | 21 | 22 | ReactiveDeskFX by Tangorabox.com 23 | https://tangorabox.com 24 | 25 | 26 | 27 | 28 | GaRzY 29 | info@tangorabox.com 30 | https://github.com/garzy 31 | GaRzY 32 | 33 | Founder 34 | Maintainer 35 | 36 | 37 | 38 | 39 | 40 | https://github.com/TangoraBox/ReactiveDeskFX/issues 41 | GitHub Issues 42 | 43 | 44 | 45 | 46 | GNU LGPL v3 47 | https://www.gnu.org/licenses/lgpl-3.0.txt 48 | 49 | 50 | 51 | 52 | 53 | 54 | ossrh 55 | https://oss.sonatype.org/content/repositories/snapshots 56 | 57 | 58 | ossrh 59 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 60 | 61 | 62 | 63 | 64 | 65 | scm:git:git://github.com/TangoraBox/ReactiveDeskFX.git 66 | scm:git:git@github.com:TangoraBox/ReactiveDeskFX.git 67 | https://github.com/TangoraBox/ReactiveDeskFX 68 | HEAD 69 | 70 | 71 | 72 | 73 | 74 | javafx-base 75 | org.openjfx 76 | 11.0.2 77 | 78 | 79 | 80 | org.openjfx 81 | javafx-graphics 82 | 11.0.2 83 | 84 | 85 | 86 | org.openjfx 87 | javafx-fxml 88 | 11.0.2 89 | 90 | 91 | 92 | com.google.inject 93 | guice 94 | 5.0.1 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | org.apache.maven.plugins 103 | maven-compiler-plugin 104 | 3.8.1 105 | 106 | 11 107 | 11 108 | 109 | 110 | 111 | maven-deploy-plugin 112 | 2.8.2 113 | 114 | 115 | default-deploy 116 | deploy 117 | 118 | deploy 119 | 120 | 121 | 122 | 123 | 124 | org.apache.maven.plugins 125 | maven-release-plugin 126 | 2.5.3 127 | 128 | true 129 | false 130 | forked-path 131 | -Dgpg.passphrase=${gpg.passphrase} 132 | 133 | 134 | 135 | org.apache.maven.scm 136 | maven-scm-provider-gitexe 137 | 1.9.4 138 | 139 | 140 | 141 | 142 | org.sonatype.plugins 143 | nexus-staging-maven-plugin 144 | 1.6.8 145 | true 146 | 147 | ossrh 148 | https://oss.sonatype.org/ 149 | true 150 | 151 | 152 | 153 | org.apache.maven.plugins 154 | maven-source-plugin 155 | 3.1.0 156 | 157 | 158 | attach-sources 159 | 160 | jar 161 | 162 | 163 | 164 | 165 | 166 | org.apache.maven.plugins 167 | maven-javadoc-plugin 168 | 3.1.1 169 | 170 | 171 | attach-javadocs 172 | 173 | jar 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | release-sign-artifacts 185 | 186 | 187 | performRelease 188 | true 189 | 190 | 191 | 192 | 193 | 194 | org.apache.maven.plugins 195 | maven-gpg-plugin 196 | 1.6 197 | 198 | 199 | sign-artifacts 200 | verify 201 | 202 | sign 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | -------------------------------------------------------------------------------- /src/main/java/com/tangorabox/reactivedesk/FXMLView.java: -------------------------------------------------------------------------------- 1 | package com.tangorabox.reactivedesk; 2 | 3 | 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Target({ElementType.TYPE}) 10 | @Retention(RetentionPolicy.RUNTIME) 11 | public @interface FXMLView { 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/tangorabox/reactivedesk/ReactiveApplication.java: -------------------------------------------------------------------------------- 1 | package com.tangorabox.reactivedesk; 2 | 3 | import com.google.inject.Injector; 4 | import com.google.inject.Module; 5 | import com.tangorabox.reactivedesk.internal.ReactiveInjector; 6 | import javafx.application.Application; 7 | import javafx.stage.Stage; 8 | 9 | import java.util.Collection; 10 | import java.util.Collections; 11 | 12 | public abstract class ReactiveApplication extends Application { 13 | 14 | @Override 15 | public final void start(Stage primaryStage) throws Exception { 16 | Injector injector = ReactiveInjector.createInjector(getApplicationModules(primaryStage)); 17 | injector.injectMembers(this); 18 | startReactiveApp(primaryStage); 19 | } 20 | 21 | protected Collection getApplicationModules(Stage primaryStage) { 22 | return Collections.emptyList(); 23 | } 24 | 25 | protected abstract void startReactiveApp(Stage primaryStage); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/tangorabox/reactivedesk/ReactiveModel.java: -------------------------------------------------------------------------------- 1 | package com.tangorabox.reactivedesk; 2 | 3 | import javafx.beans.InvalidationListener; 4 | import javafx.beans.Observable; 5 | import javafx.beans.value.ChangeListener; 6 | import javafx.beans.value.ObservableValueBase; 7 | 8 | import java.util.*; 9 | 10 | public class ReactiveModel extends ObservableValueBase implements InvalidationListener { 11 | 12 | public static final String ERROR_MESSAGE = "Model hasn't observable properties, set it first!!"; 13 | private Observable[] observableProperties = null; 14 | private final List invalidationListeners = new LinkedList<>(); 15 | 16 | public ReactiveModel() { 17 | super(); 18 | } 19 | 20 | protected void setObservableProperties(Observable... properties) { 21 | if (this.observableProperties != null) { 22 | throw new IllegalStateException("Model yet initialized, can't set properties again"); 23 | } 24 | this.observableProperties = Objects.requireNonNull(properties, "Properties cannot be null!"); 25 | for (Observable property : properties) { 26 | property.addListener(this); 27 | } 28 | } 29 | 30 | @Override 31 | public void invalidated(Observable observable) { 32 | fireValueChangedEvent(); 33 | } 34 | 35 | @Override 36 | public void addListener(InvalidationListener listener) { 37 | super.addListener(listener); 38 | invalidationListeners.add(listener); 39 | } 40 | 41 | @Override 42 | public void removeListener(InvalidationListener listener) { 43 | super.removeListener(listener); 44 | invalidationListeners.remove(listener); 45 | } 46 | 47 | @Override 48 | public void addListener(ChangeListener listener) { 49 | throw new UnsupportedOperationException("Only invalidation listener supported"); 50 | } 51 | 52 | @Override 53 | public T getValue() { 54 | return (T) this; 55 | } 56 | 57 | public Observable[] getObservableProperties() { 58 | return Objects.requireNonNull(observableProperties, ERROR_MESSAGE); 59 | } 60 | 61 | @Override 62 | public boolean equals(Object o) { 63 | if (this == o) { 64 | return true; 65 | } 66 | if (o == null || getClass() != o.getClass()) { 67 | return false; 68 | } 69 | ReactiveModel that = (ReactiveModel) o; 70 | return Arrays.equals(observableProperties, that.observableProperties); 71 | } 72 | 73 | @Override 74 | public int hashCode() { 75 | return Arrays.hashCode(observableProperties); 76 | } 77 | 78 | public void dispose() { 79 | for (Observable observableProperty : getObservableProperties()) { 80 | observableProperty.removeListener(this); 81 | } 82 | observableProperties = null; 83 | 84 | List listCopy = new ArrayList<>(invalidationListeners); 85 | for (InvalidationListener invalidationListener : listCopy) { 86 | removeListener(invalidationListener); 87 | } 88 | listCopy.clear(); 89 | invalidationListeners.clear(); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/tangorabox/reactivedesk/internal/FXMLInflater.java: -------------------------------------------------------------------------------- 1 | package com.tangorabox.reactivedesk.internal; 2 | 3 | import javafx.fxml.FXMLLoader; 4 | import javafx.scene.Node; 5 | 6 | import javax.inject.Inject; 7 | import javax.inject.Provider; 8 | import java.io.*; 9 | import java.net.URL; 10 | import java.nio.charset.StandardCharsets; 11 | import java.util.MissingResourceException; 12 | import java.util.Optional; 13 | import java.util.ResourceBundle; 14 | 15 | public class FXMLInflater { 16 | 17 | private final Provider fxmlLoaderProvider; 18 | 19 | public static void inflate(Node component) { 20 | FXMLInflater inflater = new FXMLInflater(FXMLLoader::new); 21 | inflater.loadNestedFXMLOfComponent(component); 22 | } 23 | 24 | @Inject 25 | FXMLInflater(Provider fxmlLoaderProvider) { 26 | super(); 27 | this.fxmlLoaderProvider = fxmlLoaderProvider; 28 | } 29 | 30 | 31 | public URL getNestedFXMLOfComponent(Node component) { 32 | Class componentClass = component.getClass(); 33 | String className = componentClass.getName().replace('.', '/'); 34 | String baseName = className + ".fxml"; 35 | return componentClass.getClassLoader().getResource(baseName); 36 | } 37 | 38 | public void loadNestedFXMLOfComponent(Node component) { 39 | FXMLLoader loader = fxmlLoaderProvider.get(); 40 | URL xmlLocation = getNestedFXMLOfComponent(component); 41 | if (xmlLocation == null) { 42 | return; 43 | } 44 | loadFXMLForComponent(xmlLocation, component, loader); 45 | } 46 | 47 | private Optional getNestedResourceBundleOfComponent(Node component) { 48 | try { 49 | return Optional.of(ResourceBundle.getBundle(component.getClass().getName())); 50 | } catch (MissingResourceException ex) { 51 | return Optional.empty(); 52 | } 53 | } 54 | 55 | private void loadFXMLForComponent(URL xmlLocation, Node component, FXMLLoader loader) { 56 | loader.setLocation(xmlLocation); 57 | loader.setRoot(component); 58 | loader.setController(component); 59 | getNestedResourceBundleOfComponent(component) 60 | .ifPresent(loader::setResources); 61 | try { 62 | InputStream preloadedXML = preloadAndFilterController(xmlLocation); 63 | loader.load(preloadedXML); 64 | } catch (IOException ex) { 65 | throw new UncheckedIOException("fxml load error:", ex); 66 | } 67 | } 68 | 69 | private InputStream preloadAndFilterController(URL xmlLocation) throws IOException { 70 | try (InputStream inputStream = xmlLocation.openStream()) { 71 | String xmlString = convertToString(inputStream); 72 | String xmlFilteredString = xmlString.replaceAll("fx:controller.*=.*\".*?\"", ""); 73 | return new ByteArrayInputStream(xmlFilteredString.getBytes(StandardCharsets.UTF_8)); 74 | } 75 | } 76 | 77 | private String convertToString(InputStream inputStream) throws IOException { 78 | ByteArrayOutputStream result = new ByteArrayOutputStream(); 79 | byte[] buffer = new byte[1024]; 80 | int length; 81 | while ((length = inputStream.read(buffer)) != -1) { 82 | result.write(buffer, 0, length); 83 | } 84 | return result.toString(StandardCharsets.UTF_8.name()); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/com/tangorabox/reactivedesk/internal/ReactiveDeskModule.java: -------------------------------------------------------------------------------- 1 | package com.tangorabox.reactivedesk.internal; 2 | 3 | import com.google.inject.AbstractModule; 4 | import com.google.inject.Injector; 5 | import com.google.inject.Provides; 6 | import com.google.inject.TypeLiteral; 7 | import com.google.inject.matcher.AbstractMatcher; 8 | import com.google.inject.spi.InjectionListener; 9 | import com.google.inject.spi.TypeEncounter; 10 | import com.google.inject.spi.TypeListener; 11 | import com.tangorabox.reactivedesk.FXMLView; 12 | import javafx.fxml.FXMLLoader; 13 | import javafx.fxml.JavaFXBuilderFactory; 14 | import javafx.scene.Node; 15 | 16 | import java.util.Objects; 17 | 18 | class ReactiveDeskModule extends AbstractModule { 19 | 20 | private Injector injector; 21 | private final JavaFXBuilderFactory defaultJavaFXBuilderFactory = new JavaFXBuilderFactory(); 22 | private final ClassLoader classLoader; 23 | 24 | public ReactiveDeskModule(ClassLoader classLoader) { 25 | super(); 26 | this.classLoader = classLoader; 27 | } 28 | 29 | void setInjector(Injector injector) { 30 | this.injector = Objects.requireNonNull(injector); 31 | } 32 | 33 | @Override 34 | protected void configure() { 35 | bindFXMLComponentListener(); 36 | } 37 | 38 | private void bindFXMLComponentListener() { 39 | bindListener(new AbstractMatcher<>() { 40 | @Override 41 | public boolean matches(TypeLiteral typeLiteral) { 42 | return Node.class.isAssignableFrom(typeLiteral.getRawType()) 43 | && typeLiteral.getRawType().isAnnotationPresent(FXMLView.class); 44 | } 45 | }, fxmlComponentTypeListener()); 46 | } 47 | 48 | @SuppressWarnings("unchecked") 49 | private TypeListener fxmlComponentTypeListener() { 50 | FXMLInflater loader = new FXMLInflater(this::provideFXMLLoader); 51 | return new TypeListener() { 52 | @Override 53 | public void hear(TypeLiteral typeLiteral, TypeEncounter typeEncounter) { 54 | ((TypeEncounter) typeEncounter).register((InjectionListener) loader::loadNestedFXMLOfComponent); 55 | } 56 | }; 57 | } 58 | 59 | @Provides 60 | protected FXMLLoader provideFXMLLoader() { 61 | FXMLLoader loader = new FXMLLoader(); 62 | loader.setControllerFactory(controllerClazz -> { 63 | if (injector != null) { 64 | return injector.getInstance(controllerClazz); 65 | } 66 | throw new IllegalStateException("Guice Injector cannot be null!!"); 67 | }); 68 | loader.setBuilderFactory(type -> { 69 | if (type.isAnnotationPresent(FXMLView.class)) { 70 | return () -> injector.getInstance(type); 71 | } 72 | return defaultJavaFXBuilderFactory.getBuilder(type); 73 | }); 74 | if (classLoader != null) { 75 | loader.setClassLoader(classLoader); 76 | } 77 | return loader; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/tangorabox/reactivedesk/internal/ReactiveInjector.java: -------------------------------------------------------------------------------- 1 | package com.tangorabox.reactivedesk.internal; 2 | 3 | import com.google.inject.Guice; 4 | import com.google.inject.Injector; 5 | import com.google.inject.Module; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.List; 11 | 12 | public class ReactiveInjector { 13 | 14 | private ReactiveInjector(){} 15 | 16 | public static Injector createInjector(Module... guiceModules) { 17 | return createInjector(Arrays.asList(guiceModules)); 18 | } 19 | 20 | public static Injector createInjector(ClassLoader classLoader, Module... guiceModules) { 21 | return createInjector(classLoader, Arrays.asList(guiceModules)); 22 | } 23 | 24 | public static Injector createInjector(Collection guiceModules) { 25 | return createInjector(null, guiceModules); 26 | } 27 | 28 | public static Injector createInjector(ClassLoader classLoader, Collection guiceModules) { 29 | ReactiveDeskModule reactiveDeskModule = new ReactiveDeskModule(classLoader); 30 | List modules = new ArrayList<>(); 31 | modules.add(reactiveDeskModule); 32 | if (guiceModules != null) { 33 | modules.addAll(guiceModules); 34 | } 35 | Injector injector = Guice.createInjector(modules); 36 | reactiveDeskModule.setInjector(injector); 37 | return injector; 38 | } 39 | } 40 | --------------------------------------------------------------------------------