Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 88b343d5145348de721b5814064c5ef0e8f9e06e (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (c) 2007, 2018 THALES GLOBAL SERVICES.
 * 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.sirius.common.ui.tools.api.profiler.view;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.sirius.common.tools.api.profiler.TimeProfiler2.CompositeTask;
import org.eclipse.sirius.common.tools.api.util.StringUtil;
import org.eclipse.sirius.common.ui.tools.api.util.ImageProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

/**
 * Label provider for the {@link org.eclipse.sirius.common.tools.api.profiler.TimeProfiler2}.
 * 
 * @author ymortier
 */
public class TimeProfiler2ViewLabelProvider extends LabelProvider implements ITableLabelProvider, IFontProvider {

    /** The index of the category column. */
    public static final int CATEGORY_COL = 0;

    /** The index of the task name column. */
    public static final int TASK_NAME_COL = 1;

    /** The index of the time column. */
    public static final int TIME_COL_MS = 2;

    /** The index of the occurences column. */
    public static final int OCCURENCES_COL = 3;

    /** The index of the minimum time. */
    private static final int MINIMUM = 4;

    /** The index of the maximum time. */
    private static final int MAXIMUM = 5;

    /** The index of the average time. */
    private static final int AVERAGE = 6;

    private Map<Boolean, Font> fontCache = new HashMap<>();

    @Override
    public Image getColumnImage(final Object element, final int columnIndex) {
        Image image = null;
        if (element instanceof CompositeTask) {
            final CompositeTask item = (CompositeTask) element;
            switch (columnIndex) {
            case CATEGORY_COL:
                if (item.getProfilerTask().getCategoryImage() != null) {
                    image = ImageProvider.getImageFromPath(item.getProfilerTask().getCategoryImage());
                }
                break;
            case TASK_NAME_COL:
                if (item.getProfilerTask().getTaskImage() != null) {
                    image = ImageProvider.getImageFromPath(item.getProfilerTask().getTaskImage());
                }
                break;
            case TIME_COL_MS:
                break;
            case OCCURENCES_COL:
                break;
            default:
                break;
            }
        }
        return image;
    }

    @Override
    public String getColumnText(final Object element, final int columnIndex) {
        String text = StringUtil.EMPTY_STRING;
        if (element instanceof CompositeTask) {
            final CompositeTask item = (CompositeTask) element;
            switch (columnIndex) {
            case CATEGORY_COL:
                text = item.getProfilerTask().getCategory();
                break;
            case TASK_NAME_COL:
                text = item.getProfilerTask().getName();
                break;
            case TIME_COL_MS:
                text = Long.valueOf(item.getEllapsedTime()).toString();
                break;
            case OCCURENCES_COL:
                text = Integer.valueOf(item.getOccurences()).toString();
                break;
            case MINIMUM:
                text = Long.valueOf(item.getMin()).toString();
                break;
            case MAXIMUM:
                text = Long.valueOf(item.getMax()).toString();
                break;
            case AVERAGE:
                text = Long.valueOf((long) ((double) item.getEllapsedTime() / (double) item.getOccurences())).toString();
                break;
            default:
                break;
            }
        }
        return text;
    }

    @Override
    public Font getFont(final Object element) {
        if (element instanceof CompositeTask) {
            final CompositeTask task = (CompositeTask) element;
            return getFontFromValue(task.getParent() == null);
        }
        return null;
    }

    /**
     * This method helps avoiding memory leaks by keeping track of the already built fonts.
     * 
     * @param bold
     *            : bold of the font
     * @return the default font with the given bold value.
     */
    private Font getFontFromValue(final boolean bold) {
        if (!fontCache.containsKey(Boolean.valueOf(bold))) {
            fontCache.put(Boolean.valueOf(bold), new Font(Display.getDefault(), "ARIAL", 12, !bold ? SWT.NORMAL : SWT.BOLD)); //$NON-NLS-1$
        }
        return fontCache.get(Boolean.valueOf(bold));
    }
}

Back to the top