Skip to content
Snippets Groups Projects
Commit 077799ad authored by Tiziano Munaro's avatar Tiziano Munaro
Browse files

[UI] Introduce utility method for loading FXML layouts

parent 5cebfd73
No related branches found
No related tags found
1 merge request!823437
AF3FXViewPart.java deb9eb2a3edde7deb86bffd046b292f1bf70846a RED
AF3FXViewPart.java 208b300ed4269d3764421a9b515df7356c38208a YELLOW
......@@ -15,6 +15,8 @@
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.common.ui.javafx;
import static org.fortiss.tooling.common.ui.javafx.util.FXMLUtils.load;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
......@@ -25,7 +27,6 @@ import org.eclipse.fx.ui.workbench3.FXViewPart;
import org.eclipse.ui.part.ViewPart;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
......@@ -103,13 +104,10 @@ public abstract class AF3FXViewPart extends FXViewPart implements Initializable
/** {@inheritDoc} */
@Override
protected Scene createFxScene() {
FXMLLoader loader = new FXMLLoader();
loader.setClassLoader(viewerClass.getClassLoader());
loader.setController(this);
if(fXMLLocation != null) {
loader.setLocation(viewerClass.getResource(fXMLLocation));
URL fxmlResource = viewerClass.getResource(fXMLLocation);
try {
root = (Pane)loader.load();
root = load(fxmlResource, this);
} catch(IOException e) {
throw new RuntimeException("Cannot load the resource located at " + fXMLLocation,
e);
......
FXMLUtils.java c788d21167e06365296c4527dba891dde54393cf YELLOW
SceneGraphUtils.java f54304c2eb604934de9afdf9d2a8ca88a762398a GREEN
/*-------------------------------------------------------------------------+
| Copyright 2019 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.common.ui.javafx.util;
import java.io.IOException;
import java.net.URL;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
/**
* Utility methods for handling {@link FXML} layouts.
*
* @author munaro
*/
public class FXMLUtils {
/**
* Loads a JavaFX layout from an {@link FXML} resource and associates it with the given
* controller.
*
* @param fxmlResource
* {@link URL} of the {@link FXML} resource to load.
* @param controller
* Controller to be added to associated with the returned scene.
* @return The loaded object hierarchy. See {@link FXMLLoader} for more information.
* @throws IOException
*/
public static <T> T load(URL fxmlResource, Object controller) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setClassLoader(controller.getClass().getClassLoader());
fxmlLoader.setController(controller);
fxmlLoader.setLocation(fxmlResource);
return fxmlLoader.load();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment