~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.lttng.ui/src/org/eclipse/linuxtools/internal/lttng/ui/views/latency/listeners/GraphPaintListener.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2010, 2011 Ericsson
 
3
 * 
 
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
 
8
 * 
 
9
 * Contributors:
 
10
 *   Philippe Sawicki (INF4990.A2010@gmail.com)   - Initial API and implementation
 
11
 *   Mathieu Denis    (mathieu.denis55@gmail.com) - Refactored code
 
12
 *   Bernd Hufmann - Adapted to new model-view-controller design, display improvements
 
13
 *******************************************************************************/
 
14
package org.eclipse.linuxtools.internal.lttng.ui.views.latency.listeners;
 
15
 
 
16
import java.text.DecimalFormat;
 
17
 
 
18
import org.eclipse.linuxtools.internal.lttng.ui.views.latency.AbstractViewer;
 
19
import org.eclipse.linuxtools.internal.lttng.ui.views.latency.GraphViewer;
 
20
import org.eclipse.linuxtools.internal.lttng.ui.views.latency.model.Config;
 
21
import org.eclipse.linuxtools.internal.lttng.ui.views.latency.model.GraphScaledData;
 
22
import org.eclipse.linuxtools.internal.lttng.ui.views.latency.model.IGraphDataModel;
 
23
import org.eclipse.linuxtools.tmf.ui.views.distribution.model.BaseDistributionData;
 
24
import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramUtils;
 
25
 
 
26
/**
 
27
 * <b><u>GraphPaintListener</u></b>
 
28
 * <p>
 
29
 * Graph paint listener.
 
30
 * 
 
31
 * @author Philippe Sawicki
 
32
 */
 
33
public class GraphPaintListener extends AbstractPaintListener {
 
34
 
 
35
    // ------------------------------------------------------------------------
 
36
    // Attributes
 
37
    // ------------------------------------------------------------------------
 
38
 
 
39
    /**
 
40
     * Scaled data from data model
 
41
     */
 
42
    protected GraphScaledData fScaledData;
 
43
 
 
44
    // ------------------------------------------------------------------------
 
45
    // Constructors
 
46
    // ------------------------------------------------------------------------
 
47
 
 
48
    /**
 
49
     * Constructor.
 
50
     * @param viewer
 
51
     *            A reference to the listener's viewer.
 
52
     */
 
53
    public GraphPaintListener(AbstractViewer view) {
 
54
        super(view);
 
55
    }
 
56
 
 
57
    // ------------------------------------------------------------------------
 
58
    // Operations
 
59
    // ------------------------------------------------------------------------
 
60
 
 
61
    /*
 
62
     * (non-Javadoc)
 
63
     * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#scale()
 
64
     */
 
65
    @Override
 
66
    public void scale() {
 
67
         
 
68
         // width of the plot area
 
69
         double width = getWidth();
 
70
         // height of the plot area
 
71
         double height = getHeight();
 
72
 
 
73
         IGraphDataModel model = ((GraphViewer)fViewer).getModel();
 
74
         fScaledData = model.scaleTo((int)width, (int)height, fBarWith);
 
75
 
 
76
         fXMin = fScaledData.getHorFirstBucketTime() > 0 ? fScaledData.getHorFirstBucketTime() : 0;
 
77
         fXMax = 0;
 
78
         if (fScaledData.getHorLastBucket() > 0) {
 
79
             fXMax = fScaledData.getHorBucketEndTime(fScaledData.getHorNbBuckets() - 1);
 
80
         }
 
81
         
 
82
         fYMin = fScaledData.getVerFirstBucketTime() > 0 ? fScaledData.getVerFirstBucketTime() : 0;
 
83
         fYMax = 0;
 
84
         if (fScaledData.getVerLastBucket() > 0) {
 
85
             fYMax = fScaledData.getVerBucketEndTime(fScaledData.getVerNbBuckets() - 1);
 
86
         }
 
87
     }
 
88
    
 
89
    /*
 
90
     * (non-Javadoc)
 
91
     * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#paintContent()
 
92
     */
 
93
    @Override
 
94
    public void paintContent() {
 
95
        if (fXMin >= 0 && fXMax >= 0 && fYMin >= 0 && fYMax >= 0 && fScaledData != null) {
 
96
            
 
97
            fAxisImage.setForeground(fDataColor);
 
98
            fAxisImage.setBackground(fDataColor);
 
99
 
 
100
            double height = getHeight();
 
101
 
 
102
            int xLen = fScaledData.getHorNbBuckets();
 
103
            int yLen = fScaledData.getVerNbBuckets();
 
104
 
 
105
            int barWidth = fScaledData.getBarWidth();
 
106
            
 
107
            for (int i = 0; i < xLen; i++) {
 
108
                for (int j = 0; j < yLen; j++) {
 
109
                    if (fScaledData.getEventCount(i, j) > 0) {
 
110
 
 
111
                        double x = fPadding + i * barWidth + fVerticalAxisOffset + 1;
 
112
                        double y = fPadding + fPaddingTop + height - j * barWidth;
 
113
 
 
114
                        // Avoid over-drawing background area
 
115
                        int yBarWidth = fBarWith;
 
116
                        if (y - yBarWidth < fPadding + fPaddingTop) {
 
117
                            yBarWidth = (int) (y - fPadding - fPaddingTop);
 
118
                        }
 
119
                        int xBarWidth = fBarWith;
 
120
                        if(x + xBarWidth > fClientArea.width - fPadding - fPaddingRight) {
 
121
                            xBarWidth =  (int)(fClientArea.width - fPadding - fPaddingRight - x);    
 
122
                        }
 
123
                        fAxisImage.fillRectangle((int) x, (int) y - (int) yBarWidth, (int) xBarWidth, (int) yBarWidth);
 
124
                    }
 
125
                }
 
126
            }
 
127
            
 
128
            if (fScaledData.isCurrentEventTimeValid()) {
 
129
                // Draw vertical line
 
130
                int index = fScaledData.getHorBucketIndex(fScaledData.getCurrentEventTime());
 
131
 
 
132
                int x = fPadding + index * barWidth + fVerticalAxisOffset + 1;
 
133
                fAxisImage.setForeground(fCurrentEventColor);
 
134
                fAxisImage.setBackground(fCurrentEventColor);
 
135
                fAxisImage.drawLine(x, fPadding + fPaddingTop, x, fClientArea.height - fPadding - fHorizontalAxisYOffset);
 
136
            }
 
137
        }
 
138
    }
 
139
 
 
140
    /*
 
141
     * (non-Javadoc)
 
142
     * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#formatStringForVerticalAxis(long)
 
143
     */
 
144
    @Override
 
145
    public String formatStringForVerticalAxis(long value) {
 
146
        DecimalFormat formatter = new DecimalFormat("0.0E0"); //$NON-NLS-1$
 
147
        return formatter.format(value);
 
148
    }
 
149
    
 
150
    /*
 
151
     * (non-Javadoc)
 
152
     * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#formatStringForHorizontalAxis(long)
 
153
     */
 
154
    @Override
 
155
    public String formatStringForHorizontalAxis(long value) {
 
156
        return HistogramUtils.nanosecondsToString(value);
 
157
    }
 
158
    
 
159
    /*
 
160
     * (non-Javadoc)
 
161
     * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#formatToolTipLabel(int, int)
 
162
     */
 
163
    @Override
 
164
    public String formatToolTipLabel(int x, int y) {
 
165
 
 
166
        int index = getIndexFromHorizontalValue(x);
 
167
        int yIndex = getIndexFromVerticalValue(y);
 
168
 
 
169
        if (index != BaseDistributionData.OUT_OF_RANGE_BUCKET && yIndex != BaseDistributionData.OUT_OF_RANGE_BUCKET) {
 
170
            if (fScaledData.getEventCount(index, yIndex) > 0) {
 
171
                StringBuffer buffer = new StringBuffer();
 
172
                buffer.append("Time Range in s = ["); //$NON-NLS-1$
 
173
                // TODO change Utility
 
174
                long startTime = fScaledData.getHorBucketStartTime(index) > 0 ? fScaledData.getHorBucketStartTime(index) : 0;
 
175
                buffer.append(HistogramUtils.nanosecondsToString(startTime));
 
176
                buffer.append(","); //$NON-NLS-1$
 
177
                buffer.append(HistogramUtils.nanosecondsToString(fScaledData.getHorBucketEndTime(index)));
 
178
                buffer.append("]\n"); //$NON-NLS-1$
 
179
                buffer.append("Latency Range in s = ["); //$NON-NLS-1$
 
180
                long yStartTime = fScaledData.getVerBucketStartTime(yIndex) > 0 ? fScaledData.getVerBucketStartTime(yIndex) : 0;
 
181
                buffer.append(HistogramUtils.nanosecondsToString(yStartTime));
 
182
                buffer.append(","); //$NON-NLS-1$
 
183
                buffer.append(HistogramUtils.nanosecondsToString(fScaledData.getVerBucketEndTime(yIndex)));
 
184
                buffer.append("]\n"); //$NON-NLS-1$
 
185
                buffer.append("Latency count = "); //$NON-NLS-1$
 
186
                buffer.append(fScaledData.getEventCount(index, yIndex));
 
187
                return buffer.toString();
 
188
            }
 
189
        }
 
190
        return ""; //$NON-NLS-1$
 
191
    }
 
192
    
 
193
    public int getIndexFromHorizontalValue(int x) {
 
194
        if (fScaledData != null) {
 
195
            double barWidth = fScaledData.getBarWidth();
 
196
            
 
197
            int index = (int) ((x - fPadding - fVerticalAxisOffset - 1) / barWidth);
 
198
            if ((index >= 0) && (fScaledData.getHorNbBuckets() > index)) {
 
199
                return index;
 
200
            }
 
201
        }
 
202
        return BaseDistributionData.OUT_OF_RANGE_BUCKET;
 
203
    }
 
204
    
 
205
    public int getIndexFromVerticalValue(int y) {
 
206
        if (fScaledData != null) {
 
207
            double barWidth = fScaledData.getBarWidth();
 
208
            double height = getHeight();     // height of the plot area
 
209
            
 
210
            int index = (int) ((height - (y - fPadding - fPaddingTop)) / barWidth);
 
211
            if (index >= 0 && fScaledData.getVerNbBuckets() > index) {
 
212
                return index;
 
213
            }
 
214
        }
 
215
        return BaseDistributionData.OUT_OF_RANGE_BUCKET;
 
216
    }
 
217
 
 
218
    public long getCurrentTimeFromHorizontalValue(int x) {
 
219
        if (fXMin >= 0 && fXMax >= 0) {
 
220
            int index = getIndexFromHorizontalValue(x);
 
221
            if (index != BaseDistributionData.OUT_OF_RANGE_BUCKET) {
 
222
                return fScaledData.getHorBucketStartTime(index);
 
223
            }
 
224
        }
 
225
        return Config.INVALID_EVENT_TIME;
 
226
    }
 
227
}
 
 
b'\\ No newline at end of file'