Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 14db96e24d1ee44b3e444bd6f6b9d2e6d7604968 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation.
 * 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:
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 *     Red Hat - Ongoing maintenance
 *******************************************************************************/

package org.eclipse.linuxtools.systemtap.graphing.ui.widgets;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.linuxtools.systemtap.graphing.core.datasets.IDataSet;
import org.eclipse.linuxtools.systemtap.graphing.core.structures.GraphData;
import org.eclipse.linuxtools.systemtap.graphing.ui.charts.AbstractChartBuilder;
import org.eclipse.linuxtools.systemtap.graphing.ui.wizards.graph.GraphFactory;
import org.eclipse.linuxtools.systemtap.structures.listeners.IUpdateListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

/**
 * A Composite type to contain a Graph object.
 */
public class GraphComposite extends Composite {

    private final Composite xControl;
    private final Composite yControl;

    private boolean sidebarVisible = false;
    private AbstractChartBuilder builder;
    private List<Button> checkOptions;
    private Composite checkOptionComp;

    /**
     * The default constructor: creates an internal composite for the Graph to render on, asks GraphFactory
     * to create the graph from the given GraphData and DataSet, then initializes all buttons and listeners.
     * @param parent Parent composite.
     * @param style Style of the widget to create
     * @param gd Graph information.
     * @param ds Data set for the graph.
     */
    public GraphComposite(Composite parent, int style, GraphData gd, IDataSet ds) {
        super(parent, style);
        FormLayout layout = new FormLayout();
        layout.marginWidth = 5;
        layout.marginHeight = 5;
        this.setLayout(layout);
        checkOptions = new ArrayList<>();

        checkOptionComp = new Composite(this, style);
        checkOptionComp.setLayout(new RowLayout(SWT.VERTICAL));
        FormData data = new FormData();
        data.bottom = new FormAttachment(100, 0);
        data.right = new FormAttachment(100, 0);
        checkOptionComp.setLayoutData(data);

        builder = GraphFactory.createGraph(this, style, gd, ds);
        xControl = GraphFactory.createGraphXControl(this, style);
        yControl = GraphFactory.createGraphYControl(this, style);

        if (xControl instanceof IUpdateListener) {
            builder.addUpdateListener((IUpdateListener) xControl);
        }
        if (yControl instanceof IUpdateListener) {
            builder.addUpdateListener((IUpdateListener) yControl);
        }

        configure(true);
        builder.build();
    }

    /**
     * Toggles sidebar visible or not visible.
     * @param withSidebar Enables or disables the sidebar.
     */
    private void configure(boolean withSidebar) {
        sidebarVisible = withSidebar;

        for (Button b : checkOptions) {
            b.setVisible(withSidebar);
        }

        if (xControl != null) {
            xControl.setVisible(withSidebar);
        }
        if (yControl != null) {
            yControl.setVisible(withSidebar);
        }

        FormData data = new FormData();
        data.top = new FormAttachment(0,0);
        data.right = withSidebar ? new FormAttachment(checkOptionComp, 0) : new FormAttachment(100, 0);
        data.bottom = withSidebar && xControl != null ? new FormAttachment(xControl, 0) : new FormAttachment(100, 0);
        data.left = withSidebar && yControl != null ? new FormAttachment(yControl, 0) : new FormAttachment(0, 0);
        builder.setLayoutData(data);
        layout(true, true);
    }

    public void addCheckOption(final String title, final SelectionListener listener) {
        Display.getDefault().syncExec(() -> {
		    Button b = new Button(checkOptionComp, SWT.CHECK);
		    b.setText(title);
		    b.addSelectionListener(listener);
		    checkOptions.add(b);
		    b.setSelection(true);
		    if (sidebarVisible) {
		        configure(true);
		    }
		});
    }

    /**
     * Returns the graph that is rendering to this composite.
     * @return The AbstractChartBuilder to put data into.
     */
    public AbstractChartBuilder getCanvas() {
        return builder;
    }

    /**
     * Set the visibility of the graph's legend.
     * @param visible Set this to <code>true</code> to show the legend,
     * or <code>false</code> to hide it.
     * @since 3.0
     */
    public void setLegendVisible(boolean visible) {
        builder.getChart().getLegend().setVisible(visible);
        builder.handleUpdateEvent();
    }

    /**
     * Set the visibility of the graph's title.
     * @param visible Set this to <code>true</code> to show the title,
     * or <code>false</code> to hide it.
     * @since 3.1
     */
    public void setTitleVisible(boolean visible) {
        builder.getChart().getTitle().setVisible(visible);
        builder.handleUpdateEvent();
    }
}

Back to the top