com.lmpessoa.sonarview.ui.Images.java Source code

Java tutorial

Introduction

Here is the source code for com.lmpessoa.sonarview.ui.Images.java

Source

/**
 * SonarView - A SonarQube/Eclipse integration plugin
 * Copyright (c) 2016 Leonardo Pessoa
 * http://lmpessoa.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.lmpessoa.sonarview.ui;

import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;

import com.lmpessoa.sonarview.Plugin;

public final class Images {
    private static final URL BASE_URL = Plugin.getDefault().getBundle().getEntry("/icons/");
    ///
    public static final Image IMG_SEVERITY_BLOCKER = createImage("severity/blocker.png");
    public static final Image IMG_SEVERITY_CRITICAL = createImage("severity/critical.png");
    public static final Image IMG_SEVERITY_MAJOR = createImage("severity/major.png");
    public static final Image IMG_SEVERITY_MINOR = createImage("severity/minor.png");
    public static final Image IMG_SEVERITY_INFO = createImage("severity/info.png");
    public static final Image IMG_SEVERITY_UNKNOWN = createImage("severity/unknown.png");
    ///
    public static final Image IMG_ACTION_REFRESH = createImage("actions/refresh_tab.gif");
    public static final Image IMG_ACTION_LINK = createImage("actions/link_obj.gif");

    private Images() {
    }

    private static URL getUrl(String key) throws MalformedURLException {
        return new URL(BASE_URL, key);
    }

    private static Image createImage(String key) {
        createImageDescriptor(key);
        ImageRegistry imageRegistry = getImageRegistry();
        return imageRegistry != null ? imageRegistry.get(key) : null;
    }

    private static ImageDescriptor createImageDescriptor(String key) {
        ImageRegistry imageRegistry = getImageRegistry();
        if (imageRegistry != null) {
            ImageDescriptor imageDescriptor = imageRegistry.getDescriptor(key);
            if (imageDescriptor == null) {
                try {
                    imageDescriptor = ImageDescriptor.createFromURL(getUrl(key));
                } catch (MalformedURLException e) {
                    imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
                }
                imageRegistry.put(key, imageDescriptor);
            }
            return imageDescriptor;
        }
        return null;
    }

    private static ImageRegistry getImageRegistry() {
        // "org.eclipse.swt.SWTError: Invalid thread access" might be thrown during unit tests
        if (PlatformUI.isWorkbenchRunning()) {
            return Plugin.getDefault().getImageRegistry();
        }
        return null;
    }

    public static Image getMarkerImage(String level) {
        try {
            Field field = Images.class.getField("IMG_SEVERITY_" + level);
            return (Image) field.get(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return IMG_SEVERITY_UNKNOWN;
    }
}