2 * Copyright (C) 2017 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 package org
.lttng
.scope
.tmf2
.views
.ui
.jfx
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStream
;
14 import java
.util
.HashMap
;
17 import org
.eclipse
.jdt
.annotation
.Nullable
;
19 import javafx
.scene
.image
.Image
;
22 * Factory for JavaFX {@link Image}s. This will allow caching the Image objects,
23 * allowing any class to re-use already read images.
25 * @author Alexandre Montplaisir
26 * @noreference This cache is only valid for classes within the same jar/plugin.
27 * The resource paths would not work from different plugins.
29 public final class JfxImageFactory
{
31 private static final JfxImageFactory INSTANCE
= new JfxImageFactory();
33 private JfxImageFactory() {}
36 * Get the singleton instance of this factory.
38 * @return The instance
40 public static JfxImageFactory
instance() {
44 private final Map
<String
, Image
> fImages
= new HashMap
<>();
47 * Get the {@link Image} for a given path within the jar's resources.
50 * The path to the image resource. It should be a standard
51 * .gif/.png/.jpg etc. file.
52 * @return The corresponding Image.
54 public synchronized @Nullable Image
getImageFromResource(String resourcePath
) {
55 Image image
= fImages
.get(resourcePath
);
57 try (InputStream is
= getClass().getResourceAsStream(resourcePath
)) {
59 /* The image was not found, the path is invalid */
62 image
= new Image(is
);
63 } catch (IOException e
) {
66 fImages
.put(resourcePath
, image
);
This page took 0.043626 seconds and 5 git commands to generate.