1
/*******************************************************************************
2
* Copyright (c) 2006 IBM Corporation.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
10
*******************************************************************************/
12
package org.eclipse.linuxtools.systemtap.graphingapi.ui.widgets;
14
import java.util.ArrayList;
16
import org.eclipse.linuxtools.systemtap.graphingapi.core.datasets.IDataSet;
17
import org.eclipse.linuxtools.systemtap.graphingapi.core.structures.GraphData;
18
import org.eclipse.linuxtools.systemtap.graphingapi.ui.charts.AbstractChartBuilder;
19
import org.eclipse.linuxtools.systemtap.graphingapi.ui.charts.AbstractChartWithoutAxisBuilder;
20
import org.eclipse.linuxtools.systemtap.graphingapi.ui.wizards.graph.GraphFactory;
21
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.events.SelectionAdapter;
23
import org.eclipse.swt.events.SelectionEvent;
24
import org.eclipse.swt.events.SelectionListener;
25
import org.eclipse.swt.graphics.Font;
26
import org.eclipse.swt.layout.FormAttachment;
27
import org.eclipse.swt.layout.FormData;
28
import org.eclipse.swt.layout.FormLayout;
29
import org.eclipse.swt.widgets.Button;
30
import org.eclipse.swt.widgets.Composite;
31
import org.eclipse.swt.widgets.Label;
32
import org.eclipse.swt.widgets.Scale;
35
* A Composite type to contain a Graph object.
36
* @author Henry Hughes
39
public class GraphComposite extends Composite {
41
* The default constructor: creates an internal composite for the Graph to render on, asks GraphFactory
42
* to create the graph from the given GraphData and DataSet, then initializes all buttons and listeners.
44
public GraphComposite(Composite parent, int style, GraphData gd, IDataSet ds) {
46
FormLayout layout = new FormLayout();
47
layout.marginWidth = 5;
48
layout.marginHeight = 5;
49
this.setLayout(layout);
50
checkOptions = new ArrayList<Button>();
52
label = new Label(this, SWT.HORIZONTAL | SWT.BORDER | SWT.BOLD | SWT.CENTER);
54
label.setText("-"); //$NON-NLS-1$
55
FormData data = new FormData();
56
data.bottom = new FormAttachment(100,-4);
57
data.left = new FormAttachment(0, 2);
59
label.setLayoutData(data);
60
label.setFont(new Font(parent.getDisplay(), "Arial", 10, SWT.BOLD )); //$NON-NLS-1$
61
zoomScale = new Scale(this,SWT.HORIZONTAL);
62
zoomScale.setMinimum(0);
63
zoomScale.setMaximum(14);
64
zoomScale.setIncrement(1);
65
zoomScale.setPageIncrement(1);
66
zoomScale.setSelection(7);
67
zoomScale.setToolTipText("Increase/Decrease the no of X axis ticks"); //$NON-NLS-1$
68
data = new FormData();
69
data.left = new FormAttachment(label,2);
70
data.bottom = new FormAttachment(100, -4);
71
data.right = new FormAttachment(100, -20);
72
zoomScale.setLayoutData(data);
74
pluslabel = new Label(this, SWT.HORIZONTAL | SWT.BOLD | SWT.BORDER | SWT.CENTER);
75
pluslabel.setText("+"); //$NON-NLS-1$
76
pluslabel.setFont(new Font(parent.getDisplay(), "Arial", 10, SWT.BOLD)); //$NON-NLS-1$
77
data = new FormData();
78
data.left = new FormAttachment(zoomScale,2);
79
data.bottom = new FormAttachment(100,-4);
81
pluslabel.setLayoutData(data);
83
builder = GraphFactory.createGraph(this, style,gd, ds);
84
data = new FormData();
85
data.top = new FormAttachment(0,0);
86
data.bottom = new FormAttachment(label,-5);
87
data.right = new FormAttachment(100,0);
88
data.left = new FormAttachment(0,0);
93
builder.setLayoutData(data);
95
zoomScale.addSelectionListener(scaleListener);
97
//The scale zoom scrool doesn't make sense for charts without axis
98
if (builder instanceof AbstractChartWithoutAxisBuilder) {
99
this.configure(false);
104
* Toggles sidebar visible or not visible.
105
* @param withSidebar Enables or disables the sidebar.
107
public void configure(boolean withSidebar) {
108
sidebarVisible = withSidebar;
110
for(Button b:checkOptions) {
111
b.setVisible(sidebarVisible);
114
zoomScale.setVisible(sidebarVisible);
115
label.setVisible(sidebarVisible);
116
pluslabel.setVisible(sidebarVisible);
118
FormData data = new FormData();
119
data.top = new FormAttachment(0,0);
120
data.bottom = (withSidebar ? new FormAttachment(label,-7) : new FormAttachment(100, 0));
121
data.left = new FormAttachment(0,0);
122
data.right = new FormAttachment(100,0);
123
builder.setLayoutData(data);
128
public void addCheckOption(String title, SelectionListener listener) {
129
Button b = new Button(this, SWT.CHECK);
133
if(checkOptions.size() > 0) {
134
old = checkOptions.get(checkOptions.size()-1);
137
FormData data = new FormData();
138
data.bottom = (null != old) ? new FormAttachment(old,0) : new FormAttachment(100, 0);
139
data.right = new FormAttachment(100,0);
141
b.setLayoutData(data);
142
b.addSelectionListener(listener);
146
if(checkOptions.size() == 1) {
147
((FormData)label.getLayoutData()).right = new FormAttachment(b, 0);
148
((FormData)zoomScale.getLayoutData()).right = new FormAttachment(b, 0);
152
public void removeCheckOption(String title) {
153
for(Button b :checkOptions) {
154
if(b.getText().equals(title)) {
155
checkOptions.remove(b);
157
if(checkOptions.size() == 0) {
158
((FormData)label.getLayoutData()).right = new FormAttachment(100, 0);
167
* Returns the graph that is rendering to this composite.
169
public AbstractChartBuilder getCanvas() {
174
* Returns the current sidebar visibility state.
176
public boolean isSidebarVisible() {
177
return sidebarVisible;
181
* Dispose is overriden in order to dispose of the listeners attached to this Composite on disposal.
184
public void dispose() {
185
scaleListener = null;
187
if(null != zoomScale) {
200
* Listeners are below:
201
* scaleListener - detects movement in the Scale widget and rescales the graph on change
202
* titleListener - A SelectionListener for the title button
203
* legendListener - A SelectionListener for the legend checkbox
204
* gridListener - A SelectionListener for the Grid checkbox
205
* normalizeListener - A SelectionListener for the normalization checkbox
206
* propertyChangeListener - Detects changes in user preferences and applies them
208
private SelectionListener scaleListener = new SelectionAdapter() {
210
public void widgetSelected(SelectionEvent e) {
212
Scale scaler = (Scale) e.widget;
213
int index = scaler.getSelection();
214
if(scale != scales[index]) {
215
scale = scales[index];
216
builder.setScale(scale);
221
private boolean sidebarVisible = true;
222
private AbstractChartBuilder builder;
223
private Scale zoomScale;
225
private static final double[] scales = { .0625, .125, .25, .33 , .5, .66, .8, 1.0, 1.25, 1.5, 2, 3, 4, 8, 16 };
227
private Label pluslabel;
229
private ArrayList<Button> checkOptions;