Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2d311c293fafabb5bdc2a5f030d8d60800fb30d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*******************************************************************************
 * Copyright (c) 2017 Obeo.
 * 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:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.eef.ide.ui.internal;

import java.net.MalformedURLException;
import java.text.MessageFormat;
import java.util.Optional;

import org.eclipse.core.runtime.Path;
import org.eclipse.eef.ide.ui.internal.resource.FileProvider;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;

/**
 * Utility class to manage images.
 *
 * @author mbats
 */
public final class EEFImageUtils {
	/**
	 * The constructor.
	 */
	private EEFImageUtils() {
		// prevent instantiation
	}

	/**
	 * Retrieve an image from a string path as '/resource/folder/image.png'.
	 *
	 * @param imgPath
	 *            The image path
	 * @return The image
	 */
	public static Optional<Image> getImage(String imgPath) {
		return FileProvider.getDefault().getFile(new Path(imgPath)).flatMap(imageFile -> {
			Optional<Image> optionalImage = Optional.empty();

			if (imageFile.exists() && imageFile.canRead()) {
				try {
					ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(imageFile.toURI().toURL());
					optionalImage = Optional.ofNullable(ExtendedImageRegistry.INSTANCE.getImage(imageDescriptor));
				} catch (MalformedURLException e) {
					EEFIdeUiPlugin.INSTANCE.log(e);
				}
			} else {
				String message = MessageFormat.format(Messages.EEFIdeUiPlugin_fileNotFound, imgPath);
				EEFIdeUiPlugin.getPlugin().error(message);
			}

			return optionalImage;
		});
	}

	/**
	 * Retrieve an image descriptor from a string path as '/resource/folder/image.png'.
	 *
	 * @param imgPath
	 *            The image path
	 * @return The image descriptor
	 */
	public static Optional<ImageDescriptor> getImageDescriptor(String imgPath) {
		return FileProvider.getDefault().getFile(new Path(imgPath)).flatMap(imageFile -> {
			Optional<ImageDescriptor> optionalImageDescriptor = Optional.empty();

			if (imageFile.exists() && imageFile.canRead()) {
				try {
					optionalImageDescriptor = Optional.of(ImageDescriptor.createFromURL(imageFile.toURI().toURL()));
				} catch (MalformedURLException e) {
					EEFIdeUiPlugin.INSTANCE.log(e);
				}
			} else {
				String message = MessageFormat.format(Messages.EEFIdeUiPlugin_fileNotFound, imgPath);
				EEFIdeUiPlugin.getPlugin().error(message);
			}

			return optionalImageDescriptor;
		});
	}
}

Back to the top