aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/SpringFXMLLoader.java
blob: cb1f510b0ad4f7f4cd623b78fb4f909ffe3fd308 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package at.ac.tuwien.sepm.assignment.groupphase.util;

import java.io.IOException;
import java.io.InputStream;
import javafx.fxml.FXMLLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * A Spring Based FXMLLoader.
 *
 * <p>Provides the possibility to load FXML-Files and wrap them for convenient access to the loaded
 * class as well as the spring loaded controller.
 */
@Component
public class SpringFXMLLoader {

    private Logger LOG = LoggerFactory.getLogger(SpringFXMLLoader.class);

    private ApplicationContext applicationContext;

    @Autowired
    public SpringFXMLLoader(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    private FXMLLoader getFXMLLoader() {
        final var fxmlLoader = new FXMLLoader();
        fxmlLoader.setControllerFactory(applicationContext::getBean);
        return fxmlLoader;
    }

    /**
     * Load and return an object from an FXML-File.
     *
     * @param inputStream the input stream of the FXML-File
     * @param loadType the class of the object to load
     * @param <TLoad> the loaded object
     * @return the loaded object
     * @throws IOException if the resource could not be loaded
     */
    public synchronized <TLoad> TLoad load(InputStream inputStream, Class<TLoad> loadType)
            throws IOException {
        LOG.trace(
                "Loading object of type {} from fxml resource {}",
                loadType.getCanonicalName(),
                inputStream);
        return this.getFXMLLoader().load(inputStream);
    }

    /**
     * Load and return an object from an FXML-File.
     *
     * @param pathToFXMLFile path to the FXML-File
     * @param loadType the class of the object to load
     * @param <TLoad> the loaded object
     * @return the loaded object
     * @throws IOException if the resource could not be loaded
     */
    public <TLoad> TLoad load(String pathToFXMLFile, Class<TLoad> loadType) throws IOException {
        return this.load(SpringFXMLLoader.class.getResourceAsStream(pathToFXMLFile), loadType);
    }

    /**
     * Load and return an object from an FXML-File.
     *
     * @param inputStream the input stream of the FXML-File
     * @return the loaded object
     * @throws IOException if the resource could not be loaded
     */
    public Object load(InputStream inputStream) throws IOException {
        return this.load(inputStream, Object.class);
    }

    /**
     * Load and return an object from an FXML-File.
     *
     * @param pathToFXMLFile path to the FXML-File
     * @return the loaded object
     * @throws IOException if the resource could not be loaded
     */
    public Object load(String pathToFXMLFile) throws IOException {
        return this.load(SpringFXMLLoader.class.getResourceAsStream(pathToFXMLFile));
    }

    /**
     * Load and wrap an object and the declared controller from an FXML-File.
     *
     * @param inputStream the input stream of the FXML-File
     * @param loadType the class of the object to load
     * @param controllerType the class of the declared controller of the loaded object
     * @param <TLoad> the loaded object
     * @param <TController> the controller of the loaded object
     * @return a wrapper object containing the loaded object and its declared controller
     * @throws IOException if the resource could not be loaded
     * @see FXMLWrapper
     */
    public synchronized <TLoad, TController> FXMLWrapper<TLoad, TController> loadAndWrap(
            InputStream inputStream, Class<TLoad> loadType, Class<TController> controllerType)
            throws IOException {
        final var fxmlLoader = this.getFXMLLoader();
        LOG.trace(
                "Loading and wrapping object of type {} with controller of type {} from fxml resource {}",
                loadType.getCanonicalName(),
                controllerType.getCanonicalName(),
                inputStream);
        return new FXMLWrapper<>(fxmlLoader.load(inputStream), fxmlLoader.getController());
    }

    /**
     * Load and wrap an object and the declared controller from an FXML-File.
     *
     * @param pathToFXMLFile path to the FXML-File
     * @param loadType the class of the object to load
     * @param controllerType the class of the declared controller of the loaded object
     * @param <TLoad> the loaded object
     * @param <TController> the controller of the loaded object
     * @return a wrapper object containing the loaded object and its declared controller
     * @throws IOException if the resource could not be loaded
     * @see FXMLWrapper
     */
    public synchronized <TLoad, TController> FXMLWrapper<TLoad, TController> loadAndWrap(
            String pathToFXMLFile, Class<TLoad> loadType, Class<TController> controllerType)
            throws IOException {
        return this.loadAndWrap(
                SpringFXMLLoader.class.getResourceAsStream(pathToFXMLFile),
                loadType,
                controllerType);
    }

    /**
     * Load and wrap an object and the declared controller from an FXML-File.
     *
     * @param inputStream the input stream of the FXML-File
     * @param controllerType the class of the declared controller of the loaded object
     * @param <TController> the controller of the loaded object
     * @return a wrapper object containing the loaded object and its declared controller
     * @throws IOException if the resource could not be loaded
     * @see FXMLWrapper
     */
    public <TController> FXMLWrapper<Object, TController> loadAndWrap(
            InputStream inputStream, Class<TController> controllerType) throws IOException {
        return this.loadAndWrap(inputStream, Object.class, controllerType);
    }

    /**
     * Load and wrap an object and the declared controller from an FXML-File.
     *
     * @param pathToFXMLFile path to the FXML-File
     * @param controllerType the class of the declared controller of the loaded object
     * @param <TController> the controller of the loaded object
     * @return a wrapper object containing the loaded object and its declared controller
     * @throws IOException if the resource could not be loaded
     * @see FXMLWrapper
     */
    public <TController> FXMLWrapper<Object, TController> loadAndWrap(
            String pathToFXMLFile, Class<TController> controllerType) throws IOException {
        return this.loadAndWrap(pathToFXMLFile, Object.class, controllerType);
    }

    /**
     * Load and wrap an object and the declared controller from an FXML-File.
     *
     * @param inputStream the input stream of the FXML-File
     * @return a wrapper object containing the loaded object and its declared controller
     * @throws IOException if the resource could not be loaded
     * @see FXMLWrapper
     */
    public FXMLWrapper<Object, Object> loadAndWrap(InputStream inputStream) throws IOException {
        return this.loadAndWrap(inputStream, Object.class, Object.class);
    }

    /**
     * Load and wrap an object and the declared controller from an FXML-File.
     *
     * @param pathToFXMLFile path to the FXML-File
     * @return a wrapper object containing the loaded object and its declared controller
     * @throws IOException if the resource could not be loaded
     * @see FXMLWrapper
     */
    public FXMLWrapper<Object, Object> loadAndWrap(String pathToFXMLFile) throws IOException {
        return this.loadAndWrap(pathToFXMLFile, Object.class, Object.class);
    }

    /**
     * A wrapper for loading FXML-files and wrapping the loaded object as well as the controller.
     *
     * @param <TLoad> the loaded object
     * @param <TController> the controller of the loaded object
     */
    public class FXMLWrapper<TLoad, TController> {

        public TLoad getLoadedObject() {
            return loadedObject;
        }

        public TController getController() {
            return controller;
        }

        private final TLoad loadedObject;
        private final TController controller;

        private FXMLWrapper(TLoad loadedObject, TController controller) {
            this.loadedObject = loadedObject;
            this.controller = controller;
        }
    }
}