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

[UI] Create utility class for handling graphics in JavaFX-based views

parent 039eaf4e
No related branches found
No related tags found
1 merge request!78[3874] Introduce LWFXEF-based editor for task architectures
GraphicUtils.java 4d471a310a52bda1c090f956dcdbe90775b12cb8 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.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);
}
}
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