Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Schindl2015-12-01 15:35:54 +0000
committerTom Schindl2015-12-01 15:35:54 +0000
commit1e03475c9dcf09bc18ccd97a86107a3febb169fc (patch)
tree6968a7b24823aa8cbd12308e84b8da36ed84a8e4
parentbe3fc273a0f981f7c16ff28f4e97c6d9bbce0898 (diff)
downloadorg.eclipse.efxclipse-1e03475c9dcf09bc18ccd97a86107a3febb169fc.tar.gz
org.eclipse.efxclipse-1e03475c9dcf09bc18ccd97a86107a3febb169fc.tar.xz
org.eclipse.efxclipse-1e03475c9dcf09bc18ccd97a86107a3febb169fc.zip
Bug 483390 - Add Helper method to create media objects who support any type of URL
-rw-r--r--bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Resource.java19
-rw-r--r--bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java4
-rw-r--r--bundles/runtime/org.eclipse.fx.ui.controls/META-INF/MANIFEST.MF1
-rw-r--r--bundles/runtime/org.eclipse.fx.ui.controls/src/org/eclipse/fx/ui/controls/media/MediaLoader.java78
4 files changed, 92 insertions, 10 deletions
diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Resource.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Resource.java
index 12e8cc9bf..f1bb37547 100644
--- a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Resource.java
+++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Resource.java
@@ -20,13 +20,16 @@ import org.eclipse.jdt.annotation.NonNull;
/**
* Interface wrapping a path allowing to resource temporary resources
*
+ * @param <O>
+ * the resource type
+ *
* @since 2.2.0
*/
-public interface Resource {
+public interface Resource<@NonNull O> {
/**
* @return the path
*/
- public @NonNull Path getPath();
+ public O getNativeResource();
/**
* Release the resource. The semantics what releaseing a resource means
@@ -44,8 +47,8 @@ public interface Resource {
* the path
* @return the resource
*/
- public static Resource createTempResource(@NonNull Path path) {
- return new Resource() {
+ public static Resource<@NonNull Path> createTempResource(@NonNull Path path) {
+ return new Resource<@NonNull Path>() {
@Override
public boolean release() {
@@ -58,7 +61,7 @@ public interface Resource {
}
@Override
- public Path getPath() {
+ public Path getNativeResource() {
return path;
}
};
@@ -71,8 +74,8 @@ public interface Resource {
* the path
* @return the resource
*/
- public static Resource createResource(@NonNull Path path) {
- return new Resource() {
+ public static Resource<@NonNull Path> createResource(@NonNull Path path) {
+ return new Resource<@NonNull Path>() {
@Override
public boolean release() {
@@ -80,7 +83,7 @@ public interface Resource {
}
@Override
- public Path getPath() {
+ public Path getNativeResource() {
return path;
}
};
diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java
index 7e559c2f3..e521253f4 100644
--- a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java
+++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/Util.java
@@ -316,7 +316,7 @@ public class Util {
* @return the path
* @since 2.2.0
*/
- public static Optional<Resource> getLocalPath(@NonNull URL url, boolean copyIfNeeded) {
+ public static Optional<Resource<@NonNull Path>> getLocalPath(@NonNull URL url, boolean copyIfNeeded) {
return lookupServiceList(URLResolver.class)
.stream()
.filter(r -> r.test(url)).findFirst()
@@ -339,7 +339,7 @@ public class Util {
.map(r -> r.resolveToLocalURL(url));
}
- private static Resource copyToTempFile(@NonNull URL url) throws IOException {
+ private static Resource<@NonNull Path> copyToTempFile(@NonNull URL url) throws IOException {
Path path = Files.createTempFile("tmp", Paths.get(url.getPath()).getFileName().toString()); //$NON-NLS-1$
try (InputStream stream = url.openStream()) {
diff --git a/bundles/runtime/org.eclipse.fx.ui.controls/META-INF/MANIFEST.MF b/bundles/runtime/org.eclipse.fx.ui.controls/META-INF/MANIFEST.MF
index 612bb4f0c..6620265e4 100644
--- a/bundles/runtime/org.eclipse.fx.ui.controls/META-INF/MANIFEST.MF
+++ b/bundles/runtime/org.eclipse.fx.ui.controls/META-INF/MANIFEST.MF
@@ -16,6 +16,7 @@ Export-Package: org.eclipse.fx.ui.controls;version="2.2.0",
org.eclipse.fx.ui.controls.image.spi;version="2.2.0",
org.eclipse.fx.ui.controls.list;version="2.2.0",
org.eclipse.fx.ui.controls.markers;version="2.2.0",
+ org.eclipse.fx.ui.controls.media;version="2.2.0",
org.eclipse.fx.ui.controls.paint;version="2.2.0",
org.eclipse.fx.ui.controls.sceneviewer;version="2.2.0",
org.eclipse.fx.ui.controls.stage;version="2.2.0";x-friends:="org.eclipse.fx.ui.workbench.renderers.fx",
diff --git a/bundles/runtime/org.eclipse.fx.ui.controls/src/org/eclipse/fx/ui/controls/media/MediaLoader.java b/bundles/runtime/org.eclipse.fx.ui.controls/src/org/eclipse/fx/ui/controls/media/MediaLoader.java
new file mode 100644
index 000000000..8ebb41e22
--- /dev/null
+++ b/bundles/runtime/org.eclipse.fx.ui.controls/src/org/eclipse/fx/ui/controls/media/MediaLoader.java
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * Copyright (c) 2015 BestSolution.at and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.fx.ui.controls.media;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.file.Path;
+import java.util.Optional;
+
+import org.eclipse.fx.core.Resource;
+import org.eclipse.fx.core.Util;
+import org.eclipse.jdt.annotation.NonNull;
+
+import javafx.scene.media.Media;
+
+/**
+ * Helper to load media files from any URL
+ *
+ * @since 2.2.0
+ */
+public class MediaLoader {
+ private static boolean isSupported(URL url) {
+ if ("http".equals(url.getProtocol()) //$NON-NLS-1$
+ || "https".equals(url.getProtocol()) //$NON-NLS-1$
+ || "file".equals(url.getProtocol())) { //$NON-NLS-1$
+ return true;
+ } else if ("jar".equals(url.getProtocol())) { //$NON-NLS-1$
+ try {
+ if ("file".equals(new URL(url.getFile()).getProtocol())) { //$NON-NLS-1$
+ return true;
+ }
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Create a media instance from the given source
+ *
+ * @param url
+ * the url the media is located at
+ * @return media object created
+ */
+ public static Optional<Media> createMedia(@NonNull URL url) {
+ if (isSupported(url)) {
+ return Optional.of(new Media(url.toExternalForm()));
+ }
+ Optional<URL> localURL = Util.getLocalURL(url);
+
+ if (localURL.isPresent()) {
+ if (isSupported(localURL.get())) {
+ return Optional.of(new Media(localURL.get().toExternalForm()));
+ }
+ }
+
+ Optional<Resource<@NonNull Path>> localPath = Util.getLocalPath(url, false);
+ if( localPath.isPresent() ) {
+ try {
+ return Optional.of(new Media(localPath.get().getNativeResource().toUri().toURL().toExternalForm()));
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ return Optional.empty();
+ }
+
+}

Back to the top