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

[UI] Unify `SceneGraphUtils` and `FXMLUtils` in `JavaFXUtils`

parent e8b8b7da
No related branches found
No related tags found
1 merge request!823437
AF3FXViewPart.java d849a51e96181629b6adab841410aa1b1a10dbe7 GREEN
AF3FXViewPart.java 67cc6f943239c695bf2965502ccc05091f3dd346 GREEN
......@@ -15,7 +15,7 @@
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.common.ui.javafx;
import static org.fortiss.tooling.common.ui.javafx.util.FXMLUtils.load;
import static org.fortiss.tooling.common.ui.javafx.util.JavaFXUtils.loadFXML;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
......@@ -39,7 +39,8 @@ import javafx.scene.layout.Pane;
*
* @author diewald
*/
public abstract class AF3FXViewPart extends FXViewPart implements ICompositeFXController, Initializable {
public abstract class AF3FXViewPart extends FXViewPart
implements ICompositeFXController, Initializable {
/** For later use, if we need to define different styles. */
private String cssLocation;
......@@ -65,7 +66,8 @@ public abstract class AF3FXViewPart extends FXViewPart implements ICompositeFXCo
* style sheet file path for the appearance settings relative to the source
* (resource) folders. Pass {@code null} if the OS-native look shall be used.
* @param containments
* Optional {@link ICompositeFXController}s to be added to a container node within the view
* Optional {@link ICompositeFXController}s to be added to a container node within
* the view
* part.
* @throws Exception
* if the given FXML file path is not given or if it cannot be found in any of the
......@@ -85,7 +87,7 @@ public abstract class AF3FXViewPart extends FXViewPart implements ICompositeFXCo
if(getFXMLLocation() != null) {
URL fxmlResource = viewerClass.getResource(getFXMLLocation());
try {
root = load(fxmlResource, this);
root = loadFXML(fxmlResource, this);
} catch(IOException e) {
throw new RuntimeException(
"Cannot load the resource located at " + getFXMLLocation(), e);
......
CompositeFXControllerBase.java 5983c46c93c3cdc303c47784861be314810239dc GREEN
CompositeFXControllerBase.java 7b4d3b7511e4ef146528cb2095856e4aa43e965f GREEN
ICompositeFXController.java d2c239d857aa1b125097fe0feb8ebb7dfe7f7072 GREEN
......@@ -15,7 +15,7 @@
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.common.ui.javafx.layout;
import static org.fortiss.tooling.common.ui.javafx.util.FXMLUtils.load;
import static org.fortiss.tooling.common.ui.javafx.util.JavaFXUtils.loadFXML;
import java.io.IOException;
import java.net.URL;
......@@ -40,7 +40,7 @@ public abstract class CompositeFXControllerBase implements ICompositeFXControlle
/** Constructor. */
public CompositeFXControllerBase(CompositeFXControllerBase... containments) throws IOException {
URL fxmlResource = getClass().getResource(getFXMLLocation());
layout = load(fxmlResource, this);
layout = loadFXML(fxmlResource, this);
addContainments(containments);
}
......
FXMLUtils.java 4f62cc98d504b978cc8480432c1f53d0fa6fefcf RED
JavaFXUtils.java db3cf28289109ffec64c8f96c7b2de779a977b3b 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
*/
// TODO(AD): Please rename to JFXUtils or JavaFXUtils. Please unify with SceneGraphUtils.
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();
}
}
......@@ -15,10 +15,14 @@
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.common.ui.javafx.util;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import java.util.Queue;
import java.util.function.Predicate;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
......@@ -28,14 +32,35 @@ import javafx.scene.Scene;
*
* @author hoelzl
* @author diewald
* @author munaro
*/
public final class SceneGraphUtils {
public final class JavaFXUtils {
/** Prevent instantiation. */
private SceneGraphUtils() {
private JavaFXUtils() {
// Nothing to do
}
/**
* 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 loadFXML(URL fxmlResource, Object controller) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setClassLoader(controller.getClass().getClassLoader());
fxmlLoader.setController(controller);
fxmlLoader.setLocation(fxmlResource);
return fxmlLoader.load();
}
/**
* Searches the scene graph hierarchy for the nearest parent node of the given class.
*
......
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