Commit | Line | Data |
---|---|---|
4208b510 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015, 2016 EfficiOS Inc., Alexandre Montplaisir | |
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 | ||
10 | package org.eclipse.tracecompass.internal.provisional.analysis.lami.ui.views; | |
11 | ||
12 | import org.eclipse.jdt.annotation.Nullable; | |
13 | import org.eclipse.jface.action.Action; | |
14 | import org.eclipse.jface.resource.ImageDescriptor; | |
15 | import org.eclipse.swt.widgets.Composite; | |
16 | import org.eclipse.tracecompass.internal.analysis.lami.ui.Activator; | |
a805f10b GB |
17 | import org.eclipse.tracecompass.internal.provisional.analysis.lami.ui.viewers.LamiTableViewer; |
18 | import org.eclipse.tracecompass.internal.provisional.tmf.chart.core.chart.ChartData; | |
19 | import org.eclipse.tracecompass.internal.provisional.tmf.chart.core.chart.ChartModel; | |
20 | import org.eclipse.tracecompass.internal.provisional.tmf.chart.core.chart.ChartType; | |
21 | import org.eclipse.tracecompass.internal.provisional.tmf.chart.ui.chart.IChartViewer; | |
22 | import org.eclipse.tracecompass.tmf.ui.viewers.TmfViewer; | |
4208b510 AM |
23 | |
24 | /** | |
25 | * Control for Lami viewers. | |
26 | * | |
27 | * Since viewers can be disposed, the "viewer control" will remain and be ready | |
28 | * to re-instantiate the viewer if required to. | |
29 | * | |
30 | * @author Alexandre Montplaisir | |
ea82729d | 31 | * @since 1.1 |
4208b510 AM |
32 | */ |
33 | public final class LamiViewerControl { | |
34 | ||
35 | private final Action fToggleAction; | |
36 | ||
a805f10b | 37 | private @Nullable TmfViewer fViewer; |
4208b510 AM |
38 | |
39 | /** | |
40 | * Build a new control for a Lami table viewer. | |
41 | * | |
42 | * @param parent | |
43 | * The parent composite | |
3f506e25 JR |
44 | * @param page |
45 | * The {@link LamiReportViewTabPage} page parent | |
4208b510 | 46 | */ |
3f506e25 | 47 | public LamiViewerControl(Composite parent, LamiReportViewTabPage page) { |
4208b510 AM |
48 | fToggleAction = new Action() { |
49 | @Override | |
50 | public void run() { | |
a805f10b | 51 | TmfViewer viewer = fViewer; |
4208b510 | 52 | if (viewer == null) { |
a805f10b | 53 | fViewer = LamiTableViewer.createLamiTable(parent, page); |
4208b510 AM |
54 | } else { |
55 | viewer.dispose(); | |
56 | fViewer = null; | |
57 | } | |
58 | parent.layout(); | |
59 | } | |
60 | }; | |
61 | fToggleAction.setText(Messages.LamiReportView_ActivateTableAction_ButtonName); | |
62 | fToggleAction.setToolTipText(Messages.LamiReportView_ActivateTableAction_ButtonTooltip); | |
63 | fToggleAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath("icons/table.gif")); //$NON-NLS-1$ | |
64 | } | |
65 | ||
66 | /** | |
67 | * Build a new control for a graph viewer. | |
68 | * | |
69 | * @param parent | |
70 | * The parent composite | |
a805f10b | 71 | * @param data |
3f506e25 | 72 | * The {@link LamiReportViewTabPage} parent page |
a805f10b | 73 | * @param model |
4208b510 AM |
74 | * The graph model |
75 | */ | |
a805f10b | 76 | public LamiViewerControl(Composite parent, ChartData data, ChartModel model) { |
4208b510 AM |
77 | fToggleAction = new Action() { |
78 | @Override | |
79 | public void run() { | |
a805f10b | 80 | TmfViewer viewer = fViewer; |
4208b510 | 81 | if (viewer == null) { |
a805f10b | 82 | fViewer = (TmfViewer) IChartViewer.createChart(parent, data, model); |
4208b510 AM |
83 | } else { |
84 | viewer.dispose(); | |
85 | fViewer = null; | |
86 | } | |
87 | parent.layout(); | |
88 | } | |
89 | }; | |
a805f10b | 90 | fToggleAction.setText(Messages.LamiReportView_ToggleAction_ButtonNamePrefix + ' ' + model.getTitle()); |
4208b510 | 91 | fToggleAction.setToolTipText(Messages.LamiReportView_ToggleAction_ButtonTooltip); |
a805f10b | 92 | fToggleAction.setImageDescriptor(getIconForGraphType(model.getChartType())); |
4208b510 AM |
93 | } |
94 | ||
95 | /** | |
96 | * Get the viewer of this control. Returns null if the viewer is current | |
97 | * disposed. | |
98 | * | |
99 | * @return The viewer | |
100 | */ | |
a805f10b | 101 | public @Nullable TmfViewer getViewer() { |
4208b510 AM |
102 | return fViewer; |
103 | } | |
104 | ||
105 | /** | |
106 | * Get the toggle action that shows/hide this control's viewer. | |
107 | * | |
108 | * @return The toggle action | |
109 | */ | |
110 | public Action getToggleAction() { | |
111 | return fToggleAction; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Explicitly dispose this control's viewer. | |
116 | */ | |
117 | public void dispose() { | |
118 | if (fViewer != null) { | |
119 | fViewer.dispose(); | |
120 | } | |
121 | } | |
122 | ||
a805f10b GB |
123 | private static @Nullable ImageDescriptor getIconForGraphType(ChartType chartType) { |
124 | switch (chartType) { | |
4208b510 AM |
125 | case BAR_CHART: |
126 | return Activator.getDefault().getImageDescripterFromPath("icons/histogram.gif"); //$NON-NLS-1$ | |
127 | case PIE_CHART: | |
a805f10b | 128 | case SCATTER_CHART: |
4208b510 AM |
129 | default: |
130 | // FIXME Use other icons | |
131 | return Activator.getDefault().getImageDescripterFromPath("icons/histogram.gif"); //$NON-NLS-1$ | |
132 | } | |
133 | } | |
134 | ||
135 | } |