com.contrastsecurity.ide.eclipse.ui.internal.model.VulnerabilityLabelProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.contrastsecurity.ide.eclipse.ui.internal.model.VulnerabilityLabelProvider.java

Source

/*******************************************************************************
 * Copyright (c) 2017 Contrast Security.
 * All rights reserved. 
 * 
 * This program and the accompanying materials are made available under 
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License.
 * 
 * The terms of the GNU GPL version 3 which accompanies this distribution
 * and is available at https://www.gnu.org/licenses/gpl-3.0.en.html
 * 
 * Contributors:
 *     Contrast Security - initial API and implementation
 *******************************************************************************/
package com.contrastsecurity.ide.eclipse.ui.internal.model;

import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Image;

import com.contrastsecurity.ide.eclipse.core.Constants;
import com.contrastsecurity.ide.eclipse.ui.ContrastUIActivator;
import com.contrastsecurity.models.Trace;

public class VulnerabilityLabelProvider extends StyledCellLabelProvider {
    private static final String UNLICENSED_PREFIX = "UNLICENSED - ";

    private Image getImage(Object element, int columnIndex) {
        if (element instanceof Trace) {
            switch (columnIndex) {
            case 0:
                return ContrastUIActivator.getSeverityImage((Trace) element);
            case 3:
                return ContrastUIActivator.getImage("/icons/externalLink.png");

            default:
                break;
            }
        }
        return null;
    }

    private String getText(Object element, int columnIndex) {
        if (element instanceof Trace) {
            switch (columnIndex) {
            case 0:
                if (getImage(element, columnIndex) == null) {
                    return ((Trace) element).getSeverity();
                }
                return null;
            case 1:
                String title = getTitle((Trace) element);
                return title;
            case 2: {
                boolean unlicensed = ((Trace) element).getTitle().contains(Constants.UNLICENSED);
                if (unlicensed) {
                    return "Vulnerability not visible with current license";
                } else {
                    return "View Details";
                }
            }
            default:
                break;
            }

        }
        return null;
    }

    private String getTitle(Trace trace) {
        String title = trace.getTitle();
        int index = title.indexOf(Constants.UNLICENSED);
        if (index > 0) {
            return UNLICENSED_PREFIX + title.substring(0, index);
        }
        return title;
    }

    @Override
    public void update(ViewerCell cell) {
        Object element = cell.getElement();
        if (element instanceof Trace) {
            int index = cell.getColumnIndex();
            switch (index) {
            case 0:
            case 3:
                Image image = getImage(element, index);
                cell.setImage(image);
                break;
            case 1:
                String title = getText(element, index);
                if (title.startsWith(UNLICENSED_PREFIX)) {
                    StyledString text = new StyledString();
                    StyleRange range = new StyleRange(0, UNLICENSED_PREFIX.length(), Constants.UNLICENSED_COLOR,
                            null);
                    text.append(title, StyledString.DECORATIONS_STYLER);
                    StyleRange[] ranges = { range };
                    cell.setStyleRanges(ranges);
                }
                cell.setText(title);
                break;
            case 2:
                String appName = ((Trace) element).getApplication().getName();
                cell.setText(appName);
                break;
            default:
                break;
            }
            if (index == 0) {

            }
        }
        super.update(cell);
    }

}