diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/.ratings b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/.ratings index cd9b77b00f1d5a5ab51c9a5d37aef27a53e00ed2..6901277b7164e9f08fd7e2dc9484d790a31c53c5 100644 --- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/.ratings +++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/.ratings @@ -1 +1,2 @@ +GraphicUtils.java 4d471a310a52bda1c090f956dcdbe90775b12cb8 GREEN SceneGraphUtils.java f54304c2eb604934de9afdf9d2a8ca88a762398a GREEN diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/GraphicUtils.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/GraphicUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..4d471a310a52bda1c090f956dcdbe90775b12cb8 --- /dev/null +++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/GraphicUtils.java @@ -0,0 +1,67 @@ +/*-------------------------------------------------------------------------+ +| 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.InputStream; +import java.net.URL; +import java.util.HashMap; + +import javafx.scene.image.Image; +import javafx.scene.paint.Color; + +/** + * Utility methods for handling graphics in JavaFX-based views. + * + * @author munaro + */ +public class GraphicUtils { + + /** The image cache of loaded images. */ + private static final HashMap<String, Image> imageCache = new HashMap<>(); + + /** Returns the plugin-local URI string for the given resource. */ + public static String getURIString(String pluginId, String localPath) { + return "platform:/plugin/" + pluginId + localPath; + } + + /** Returns the Java FX Image load from the local path. */ + public static Image getFXImage(String pluginId, String localPath) { + String uri = getURIString(pluginId, localPath); + if(uri == null) { + return null; + } + Image cacheImage = imageCache.get(uri); + if(cacheImage == null) { + InputStream in = null; + try { + in = new URL(uri).openStream(); + } catch(Exception e) { + e.printStackTrace(); + } + if(in != null) { + cacheImage = new Image(in); + } + imageCache.put(uri, cacheImage); + } + return cacheImage; + } + + /** Converts the given SWT color to Java FX color. */ + public static Color convertColor(org.eclipse.swt.graphics.Color swtColor) { + return Color.rgb(swtColor.getRed(), swtColor.getGreen(), swtColor.getBlue(), + swtColor.getAlpha() / 255.0); + } +}