Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Schindl2014-10-22 21:29:09 +0000
committerTom Schindl2014-10-22 21:29:09 +0000
commita4b4ef5f44ccf021f3bebeaff6418b3e87dc54a9 (patch)
tree0d4340c55dd0d8b08e7c5cfa2c6464b937093962
parent63abadea99d55614b7ff09bb0e43f09e555c1a42 (diff)
downloadorg.eclipse.efxclipse-a4b4ef5f44ccf021f3bebeaff6418b3e87dc54a9.tar.gz
org.eclipse.efxclipse-a4b4ef5f44ccf021f3bebeaff6418b3e87dc54a9.tar.xz
org.eclipse.efxclipse-a4b4ef5f44ccf021f3bebeaff6418b3e87dc54a9.zip
Bug 448220 - Using FXMLLoaderFactory with non-file input
-rwxr-xr-xbundles/runtime/org.eclipse.fx.osgi.util/src/org/eclipse/fx/osgi/util/OSGiFXMLLoader.java103
-rwxr-xr-xbundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/FXMLLoaderFactory.java33
-rwxr-xr-xbundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/InjectingFXMLLoader.java53
-rwxr-xr-xbundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/internal/FXMLLoaderSupplier.java16
4 files changed, 176 insertions, 29 deletions
diff --git a/bundles/runtime/org.eclipse.fx.osgi.util/src/org/eclipse/fx/osgi/util/OSGiFXMLLoader.java b/bundles/runtime/org.eclipse.fx.osgi.util/src/org/eclipse/fx/osgi/util/OSGiFXMLLoader.java
index 5c2a24514..37dfa3c08 100755
--- a/bundles/runtime/org.eclipse.fx.osgi.util/src/org/eclipse/fx/osgi/util/OSGiFXMLLoader.java
+++ b/bundles/runtime/org.eclipse.fx.osgi.util/src/org/eclipse/fx/osgi/util/OSGiFXMLLoader.java
@@ -42,7 +42,7 @@ public class OSGiFXMLLoader {
/**
* Load a FXML file relative to the requested
- *
+ *
* @param requester
* the requester
* @param relativeFxmlPath
@@ -68,7 +68,7 @@ public class OSGiFXMLLoader {
/**
* Load a FXML file relative to the requested and provide the constructed
* scene-node and the controller
- *
+ *
* @param requester
* the requester
* @param relativeFxmlPath
@@ -93,7 +93,7 @@ public class OSGiFXMLLoader {
/**
* Load a FXML file relative to the bundle
- *
+ *
* @param bundle
* the bundle
* @param bundleRelativeFxmlPath
@@ -118,7 +118,7 @@ public class OSGiFXMLLoader {
/**
* Load a FXML file relative to the bundle and provide the constructed
* scene-node and the controller
- *
+ *
* @param bundle
* the bundle
* @param bundleRelativeFxmlPath
@@ -144,7 +144,7 @@ public class OSGiFXMLLoader {
/**
* Load using the given classloader
- *
+ *
* @param classloader
* the classloader
* @param url
@@ -165,7 +165,7 @@ public class OSGiFXMLLoader {
/**
* Load using the given classloader and provide the constructed scene-node
* and the controller
- *
+ *
* @param classloader
* the classloader
* @param url
@@ -182,12 +182,12 @@ public class OSGiFXMLLoader {
* @throws IOException
*/
public static <O, C> FXMLData<O, C> loadWithController(@NonNull ClassLoader classloader, @NonNull URL url, @Nullable ResourceBundle resourceBundle, @Nullable BuilderFactory builderFactory) throws IOException {
- return loadWithController(classloader, url, resourceBundle, builderFactory, null);
+ return loadWithController(classloader, url, null, resourceBundle, builderFactory, null);
}
/**
* Load a FXML file relative to the requested
- *
+ *
* @param requester
* the requester
* @param relativeFxmlPath
@@ -214,7 +214,7 @@ public class OSGiFXMLLoader {
/**
* Load a FXML file relative to the requested and provide the constructed
* scene-node and the controller
- *
+ *
* @param requester
* the requester
* @param relativeFxmlPath
@@ -237,12 +237,12 @@ public class OSGiFXMLLoader {
if (url == null) {
throw new IOException("Unable to load '" + relativeFxmlPath + "' relative to '" + requester + "'"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}
- return loadWithController(getClassLoader(requester), url, resourceBundle, builderFactory, controllerFactory);
+ return loadWithController(getClassLoader(requester), url, null, resourceBundle, builderFactory, controllerFactory);
}
/**
* Load a FXML file relative to the bundle
- *
+ *
* @param bundle
* the bundle
* @param bundleRelativeFxmlPath
@@ -269,7 +269,7 @@ public class OSGiFXMLLoader {
/**
* Load a FXML file relative to the bundle and provide the constructed
* scene-node and the controller
- *
+ *
* @param bundle
* the bundle
* @param bundleRelativeFxmlPath
@@ -292,12 +292,12 @@ public class OSGiFXMLLoader {
if (url == null) {
throw new IOException("Unable to load '" + bundleRelativeFxmlPath + "' relative to '" + bundle + "'"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}
- return loadWithController(getClassLoader(bundle), url, resourceBundle, builderFactory, controllerFactory);
+ return loadWithController(getClassLoader(bundle), url, null, resourceBundle, builderFactory, controllerFactory);
}
/**
* Load using the given classloader
- *
+ *
* @param classloader
* the classloader
* @param url
@@ -314,17 +314,69 @@ public class OSGiFXMLLoader {
* @throws IOException
*/
public static <O> O load(@NonNull final ClassLoader classloader, @NonNull URL url, @Nullable ResourceBundle resourceBundle, @Nullable final BuilderFactory builderFactory, @Nullable Callback<Class<?>, Object> controllerFactory) throws IOException {
- return OSGiFXMLLoader.<O, Object> loadWithController(classloader, url, resourceBundle, builderFactory, controllerFactory).node;
+ return OSGiFXMLLoader.<O, Object> loadWithController(classloader, url, null, resourceBundle, builderFactory, controllerFactory).node;
+ }
+
+ /**
+ * Load using the given classloader
+ *
+ * @param classloader
+ * the classloader
+ * @param url
+ * the url of the fxml file
+ * @param resourceBundle
+ * the translations
+ * @param builderFactory
+ * the builder factory
+ * @param controllerFactory
+ * the controller factory
+ * @param <O>
+ * the loaded root object type
+ * @param stream
+ * the input stream to load from
+ * @return the created scene graph
+ * @throws IOException
+ */
+ public static <O> O load(@NonNull final ClassLoader classloader, @Nullable URL url, @NonNull InputStream stream, @Nullable ResourceBundle resourceBundle, @Nullable final BuilderFactory builderFactory, @Nullable Callback<Class<?>, Object> controllerFactory) throws IOException {
+ return OSGiFXMLLoader.<O, Object> loadWithController(classloader, url, stream, resourceBundle, builderFactory, controllerFactory).node;
+ }
+
+ /**
+ * Load using the given classloader and provide the constructed scene-node
+ * and the controller
+ *
+ * @param classloader
+ * the classloader
+ * @param url
+ * the url of the fxml file
+ * @param resourceBundle
+ * the translations
+ * @param builderFactory
+ * the builder factory
+ * @param controllerFactory
+ * the controller factory
+ * @param <O>
+ * the loaded root object type
+ * @param <C>
+ * the loaded controller type
+ * @return the created scene graph
+ * @throws IOException
+ */
+ public static <O, C> FXMLData<O, C> loadWithController(@NonNull final ClassLoader classloader, @Nullable URL url, @Nullable ResourceBundle resourceBundle, @Nullable final BuilderFactory builderFactory, @Nullable Callback<Class<?>, Object> controllerFactory)
+ throws IOException {
+ return loadWithController(classloader, url, null, resourceBundle, builderFactory, controllerFactory);
}
/**
* Load using the given classloader and provide the constructed scene-node
* and the controller
- *
+ *
* @param classloader
* the classloader
* @param url
* the url of the fxml file
+ * @param stream
+ * the input stream
* @param resourceBundle
* the translations
* @param builderFactory
@@ -339,7 +391,8 @@ public class OSGiFXMLLoader {
* @throws IOException
*/
@SuppressWarnings("unchecked")
- public static <O, C> FXMLData<O, C> loadWithController(@NonNull final ClassLoader classloader, @NonNull URL url, @Nullable ResourceBundle resourceBundle, @Nullable final BuilderFactory builderFactory, @Nullable Callback<Class<?>, Object> controllerFactory) throws IOException {
+ public static <O, C> FXMLData<O, C> loadWithController(@NonNull final ClassLoader classloader, @Nullable URL url, @Nullable InputStream stream, @Nullable ResourceBundle resourceBundle, @Nullable final BuilderFactory builderFactory, @Nullable Callback<Class<?>, Object> controllerFactory)
+ throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(url);
loader.setClassLoader(classloader);
@@ -365,18 +418,28 @@ public class OSGiFXMLLoader {
loader.setControllerFactory(controllerFactory);
}
- try (InputStream in = url.openStream()) {
- O value = loader.load(in);
+ if (stream != null) {
+ O value = loader.load(stream);
if (value != null) {
return new FXMLData<O, C>(value, (C) loader.getController());
}
throw new IOException("Unable to construct UI from FXML '" + url + "'"); //$NON-NLS-1$//$NON-NLS-2$
+ } else if (url != null) {
+ try (InputStream in = url.openStream()) {
+ O value = loader.load(in);
+ if (value != null) {
+ return new FXMLData<O, C>(value, (C) loader.getController());
+ }
+ throw new IOException("Unable to construct UI from FXML '" + url + "'"); //$NON-NLS-1$//$NON-NLS-2$
+ }
+ } else {
+ throw new IllegalArgumentException("At least one of the parameters URL or InputStream must be set"); //$NON-NLS-1$
}
}
/**
* Data structured holding data constructed while loading the FXML-Document
- *
+ *
* @param <N>
* the node
* @param <C>
diff --git a/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/FXMLLoaderFactory.java b/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/FXMLLoaderFactory.java
index e3b4a27d6..653f668cc 100755
--- a/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/FXMLLoaderFactory.java
+++ b/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/FXMLLoaderFactory.java
@@ -10,7 +10,11 @@
*******************************************************************************/
package org.eclipse.fx.ui.di;
+import java.io.InputStream;
+import java.net.URL;
+
import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
/**
* Factory to build an {@link FXMLLoader}
@@ -24,7 +28,7 @@ public interface FXMLLoaderFactory {
/**
* Load the fxml file relative to the bundle the loading class is in,
* similar to pure java {@link ClassLoader#getResource(String)}
- *
+ *
* @param relativePath
* the relative path to the bundle classpath
* @return the builder
@@ -35,11 +39,34 @@ public interface FXMLLoaderFactory {
/**
* Load the fxml file relative to the class requesting the loading, similar
* to pure java {@link Class#getResource(String)}
- *
+ *
* @param relativePath
* the relative path from the class
* @return the builder
*/
@NonNull
public <N> FXMLBuilder<N> loadRequestorRelative(@NonNull String relativePath);
-}
+
+ /**
+ * Load the fxml file from the given input stream
+ *
+ * @param stream
+ * the stream
+ * @param path
+ * the path to use for relative lookups like includes, image
+ * sources, ...
+ * @return the builder
+ */
+ @NonNull
+ public <N> FXMLBuilder<N> loadFromInputStream(@NonNull InputStream stream, @Nullable URL path);
+
+ /**
+ * Load the fxml file from the given url
+ *
+ * @param url
+ * the url
+ * @return the builder
+ */
+ @NonNull
+ public <N> FXMLBuilder<N> loadFromURL(@NonNull URL url);
+} \ No newline at end of file
diff --git a/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/InjectingFXMLLoader.java b/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/InjectingFXMLLoader.java
index a7040540d..bd169a5db 100755
--- a/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/InjectingFXMLLoader.java
+++ b/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/InjectingFXMLLoader.java
@@ -11,6 +11,7 @@
package org.eclipse.fx.ui.di;
import java.io.IOException;
+import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ResourceBundle;
@@ -31,7 +32,7 @@ import org.osgi.framework.wiring.BundleWiring;
/**
* Allows to use Eclipse DI inside your controller to access services
- *
+ *
* @param <N>
* the root node
*/
@@ -44,7 +45,7 @@ public abstract class InjectingFXMLLoader<N> implements FXMLBuilder<N> {
/**
* Create a loader which operates relative to the requestor
- *
+ *
* @param context
* the di context
* @param requester
@@ -61,7 +62,7 @@ public abstract class InjectingFXMLLoader<N> implements FXMLBuilder<N> {
/**
* Create a loader which operates relative to the requestor and uses the
* {@link ExtendedFXMLLoader} if requested to do so
- *
+ *
* @param context
* the di context
* @param requester
@@ -106,7 +107,7 @@ public abstract class InjectingFXMLLoader<N> implements FXMLBuilder<N> {
/**
* Create a loader which operates relative to the bundle classpath
- *
+ *
* @param context
* the di context
* @param bundle
@@ -122,7 +123,7 @@ public abstract class InjectingFXMLLoader<N> implements FXMLBuilder<N> {
/**
* Create a loader which operates relative to the bundle classpath and uses
* the {@link ExtendedFXMLLoader} if requested to do so
- *
+ *
* @param context
* the di context
* @param bundle
@@ -191,7 +192,7 @@ public abstract class InjectingFXMLLoader<N> implements FXMLBuilder<N> {
/**
* Create a loader which uses the given classloader
- *
+ *
* @param context
* the di context
* @param classloader
@@ -227,6 +228,46 @@ public abstract class InjectingFXMLLoader<N> implements FXMLBuilder<N> {
};
}
+ /**
+ * Create a loader which uses a give classloader and reads from the stream
+ *
+ * @param context
+ * the di context
+ * @param classloader
+ * the classloader
+ * @param stream
+ * the stream
+ * @param path
+ * the path
+ * @return a loader instance
+ */
+ @NonNull
+ public static <N> InjectingFXMLLoader<N> create(@NonNull final IEclipseContext context, @NonNull final ClassLoader classloader, @NonNull final InputStream stream, @Nullable URL path) {
+ return new InjectingFXMLLoader<N>() {
+
+ @Override
+ public N load() throws IOException {
+ return OSGiFXMLLoader.load(classloader, path, stream, this.resourceBundle, this.builderFactory, new ControllerFactory(context));
+ }
+
+ @Override
+ public <C> Data<N, C> loadWithController() throws IOException {
+ final FXMLData<N, C> d = OSGiFXMLLoader.loadWithController(classloader, path, stream, this.resourceBundle, this.builderFactory, new ControllerFactory(context));
+ return new Data<N, C>() {
+ @Override
+ public C getController() {
+ return d.controller;
+ }
+
+ @Override
+ public N getNode() {
+ return d.node;
+ }
+ };
+ }
+ };
+ }
+
@Override
@NonNull
public InjectingFXMLLoader<N> resourceBundle(@NonNull ResourceBundle resourceBundle) {
diff --git a/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/internal/FXMLLoaderSupplier.java b/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/internal/FXMLLoaderSupplier.java
index da3c9270c..756d59d15 100755
--- a/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/internal/FXMLLoaderSupplier.java
+++ b/bundles/runtime/org.eclipse.fx.ui.di/src/org/eclipse/fx/ui/di/internal/FXMLLoaderSupplier.java
@@ -10,6 +10,9 @@
*******************************************************************************/
package org.eclipse.fx.ui.di.internal;
+import java.io.InputStream;
+import java.net.URL;
+
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.IInjector;
import org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier;
@@ -22,6 +25,7 @@ import org.eclipse.fx.ui.di.FXMLLoader;
import org.eclipse.fx.ui.di.FXMLLoaderFactory;
import org.eclipse.fx.ui.di.InjectingFXMLLoader;
import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
import org.osgi.framework.FrameworkUtil;
/**
@@ -53,6 +57,18 @@ public class FXMLLoaderSupplier extends ExtendedObjectSupplier {
public <N> FXMLBuilder<N> loadBundleRelative(@NonNull String relativePath) {
return InjectingFXMLLoader.create(context, FrameworkUtil.getBundle(requestingClass), relativePath, extended);
}
+
+ @SuppressWarnings("null")
+ @Override
+ public <N> @NonNull FXMLBuilder<N> loadFromInputStream(@NonNull InputStream stream, @Nullable URL path) {
+ return InjectingFXMLLoader.create(context, requestingClass.getClassLoader(), stream, path);
+ }
+
+ @SuppressWarnings("null")
+ @Override
+ public <N> @NonNull FXMLBuilder<N> loadFromURL(@NonNull URL url) {
+ return InjectingFXMLLoader.create(context, requestingClass.getClassLoader(), url);
+ }
};
}

Back to the top