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

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.graphingapi.ui/src/org/eclipse/linuxtools/systemtap/ui/graphingapi/ui/charts/AbstractChartBuilder.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
 * Licensed Material - Property of IBM
 
3
 *
 
4
 * ****-*** 
 
5
 *
 
6
 * (c) Copyright IBM Corp. 2006.  All rights reserved.
 
7
 *
 
8
 * US Government Users Restricted Rights - Use, duplication or
 
9
 * disclosure restricted by GSA ADP Schedule Contract with
 
10
 * IBM Corp.
 
11
 *
 
12
 ****************************************************************
 
13
 */
 
14
package org.eclipse.linuxtools.systemtap.ui.graphingapi.ui.charts;
 
15
 
 
16
import org.eclipse.jface.preference.IPreferenceStore;
 
17
 
 
18
import org.eclipse.linuxtools.internal.systemtap.ui.graphingapi.ui.GraphingAPIUIPlugin;
 
19
import org.eclipse.linuxtools.systemtap.ui.graphingapi.ui.preferences.GraphingAPIPreferenceConstants;
 
20
 
 
21
import org.eclipse.swt.graphics.Color;
 
22
import org.eclipse.swt.layout.FillLayout;
 
23
import org.eclipse.swt.SWT;
 
24
import org.eclipse.swt.widgets.Composite;
 
25
import org.eclipse.linuxtools.systemtap.ui.graphingapi.nonui.adapters.IAdapter;
 
26
import org.eclipse.linuxtools.systemtap.ui.structures.listeners.IUpdateListener;
 
27
import org.eclipse.swt.widgets.Display;
 
28
 
 
29
import org.swtchart.Chart;
 
30
import org.swtchart.ITitle;
 
31
 
 
32
/**
 
33
 * Provides the common members and the framework to build one chart.
 
34
 * 
 
35
 * @author Qi Liang
 
36
 */
 
37
public abstract class AbstractChartBuilder extends Composite implements IUpdateListener{
 
38
 
 
39
        /**
 
40
         * Font name for all titles, labels, and values.
 
41
         */
 
42
        protected final static String FONT_NAME = "MS Sans Serif";
 
43
        protected int maxItems;
 
44
        protected double scale = 1.0;
 
45
 
 
46
        /**
 
47
         * Provides data for chart.
 
48
         */
 
49
        protected IAdapter adapter = null;
 
50
        protected int xseries;
 
51
        protected int[] yseries;
 
52
 
 
53
        protected static final Color WHITE = new Color(Display.getDefault(), 255, 255, 255);
 
54
        protected static final Color BLACK = new Color(Display.getDefault(), 0, 0, 0);
 
55
        protected static final Color RED = new Color(Display.getDefault(), 255, 0, 0);
 
56
 
 
57
        protected static final Color[] COLORS = {
 
58
                                                                                                new Color(Display.getDefault(), 255, 0, 0),
 
59
                                                                                                new Color(Display.getDefault(), 0, 255, 0),
 
60
                                                                                                new Color(Display.getDefault(), 0, 0, 255),
 
61
                                                                                                new Color(Display.getDefault(), 255, 255, 0),
 
62
                                                                                                new Color(Display.getDefault(), 255, 0, 255),
 
63
                                                                                                new Color(Display.getDefault(), 0, 255, 255),
 
64
                                                                                                new Color(Display.getDefault(), 0, 0, 0),
 
65
                                                                                                new Color(Display.getDefault(), 64, 128, 128),
 
66
                                                                                                new Color(Display.getDefault(), 255, 165, 0),
 
67
                                                                                                new Color(Display.getDefault(), 128, 128, 128),
 
68
                                                                                                };
 
69
        /**
 
70
         * Chart instance.
 
71
         */
 
72
        protected Chart chart = null;
 
73
 
 
74
        /**
 
75
         * Chart title.
 
76
         */
 
77
        protected String title = null;
 
78
 
 
79
        /**
 
80
         * Constructs one chart builder and associate it to one data set.
 
81
         * 
 
82
         * @param dataSet
 
83
         *            data set
 
84
         */
 
85
 
 
86
        public AbstractChartBuilder(IAdapter adapter, Composite parent, int style, String title) {
 
87
                super(parent, style);
 
88
                this.adapter = adapter;
 
89
                this.title = title;
 
90
                this.setLayout(new FillLayout());
 
91
                IPreferenceStore store = GraphingAPIUIPlugin.getDefault().getPreferenceStore();
 
92
                maxItems = Math.min(store.getInt(GraphingAPIPreferenceConstants.P_VIEWABLE_DATA_ITEMS),
 
93
                                                                        store.getInt(GraphingAPIPreferenceConstants.P_MAX_DATA_ITEMS));
 
94
        }
 
95
 
 
96
        /**
 
97
         * Builds one chart.
 
98
         */
 
99
        public void build() {
 
100
                createChart();
 
101
                buildPlot();
 
102
                buildLegend();
 
103
                buildTitle();
 
104
                buildXAxis();
 
105
                buildYAxis();
 
106
                buildXSeries();
 
107
                buildYSeries();
 
108
                updateDataSet();
 
109
        }
 
110
 
 
111
        /**
 
112
         * Creates chart instance.
 
113
         */
 
114
        protected void createChart() {
 
115
                this.chart = new Chart(this, getStyle());
 
116
        }
 
117
 
 
118
        /**
 
119
         * Builds plot.
 
120
         */
 
121
        protected void buildPlot() {
 
122
                this.chart.setBackground(WHITE);
 
123
                this.chart.setBackgroundInPlotArea(WHITE);
 
124
        }
 
125
 
 
126
        /**
 
127
         * Builds X axis.
 
128
         */
 
129
        protected void buildXAxis() {
 
130
 
 
131
        }
 
132
 
 
133
        /**
 
134
         * Builds Y axis.
 
135
         */
 
136
        protected void buildYAxis() {
 
137
 
 
138
        }
 
139
 
 
140
        /**
 
141
         * Builds X series.
 
142
         */
 
143
        protected void buildXSeries() {
 
144
 
 
145
        }
 
146
 
 
147
        /**
 
148
         * Builds Y series.
 
149
         */
 
150
        protected void buildYSeries() {
 
151
 
 
152
        }
 
153
 
 
154
        /**
 
155
         * Builds legend.
 
156
         * 
 
157
         */
 
158
        protected void buildLegend() {
 
159
                chart.getLegend().setPosition(SWT.RIGHT);
 
160
        }
 
161
 
 
162
        /**
 
163
         * Builds the chart title.
 
164
         */
 
165
        protected void buildTitle() {
 
166
                ITitle ctitle = chart.getTitle();
 
167
                ctitle.setForeground(BLACK);
 
168
                ctitle.setText(this.title);
 
169
        }
 
170
 
 
171
        /**
 
172
         * Returns the chart instance.
 
173
         * 
 
174
         * @return the chart instance
 
175
         */
 
176
        public Chart getChart() {
 
177
                return chart;
 
178
        }
 
179
 
 
180
        public void updateDataSet() {
 
181
 
 
182
        }
 
183
 
 
184
        public void setScale(double scale) {
 
185
                this.scale = scale;
 
186
                handleUpdateEvent();
 
187
        }
 
188
 
 
189
        protected double getDoubleValue(Object o) {
 
190
                if (o instanceof Integer)
 
191
                        return ((Integer)o).intValue();
 
192
                if (o instanceof Double)
 
193
                        return ((Double)o).doubleValue();
 
194
                return new Double(o.toString()).doubleValue();
 
195
        }
 
196
 
 
197
        public void handleUpdateEvent() {
 
198
                try{
 
199
                        repaint();
 
200
                }catch(Exception e)
 
201
                {
 
202
                        //e.printStackTrace();
 
203
                }
 
204
        }
 
205
 
 
206
        protected void repaint() {
 
207
                getDisplay().syncExec(new Runnable() {
 
208
                        boolean stop = false;
 
209
                        public void run() {
 
210
                                if(stop)
 
211
                                        return;
 
212
                                try {
 
213
                                        updateDataSet();
 
214
                                } catch (Exception e) {
 
215
                                        stop = true;
 
216
                                }
 
217
            }
 
218
                });
 
219
        }
 
220
 
 
221
}