From 7710e6ed3de37ae6b3f48ae500cce863caf531c5 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Wed, 4 May 2016 16:04:01 -0400 Subject: [PATCH] analysis.lami: Add toolbar to custom charts For now only a close button action is present. This will allow removing a single chart, and not all of them. Bug: 493941 Change-Id: I24c8ee2d64bc9e8da93788f34f380bda4e7a88b8 Signed-off-by: Jonathan Rajotte Reviewed-on: https://git.eclipse.org/r/72244 Reviewed-by: Hudson CI Reviewed-by: Patrick Tasse Tested-by: Patrick Tasse Reviewed-by: Alexandre Montplaisir --- .../lami/ui/viewers/LamiXYChartViewer.java | 103 ++++++++++++++++++ .../analysis/lami/ui/viewers/Messages.java | 6 +- .../lami/ui/viewers/messages.properties | 3 +- 3 files changed, 109 insertions(+), 3 deletions(-) diff --git a/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/LamiXYChartViewer.java b/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/LamiXYChartViewer.java index 4a0a776a4a..eb4f839dca 100644 --- a/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/LamiXYChartViewer.java +++ b/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/LamiXYChartViewer.java @@ -23,15 +23,21 @@ import java.util.function.ToDoubleFunction; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.ToolBar; +import org.eclipse.swt.widgets.ToolItem; import org.eclipse.tracecompass.common.core.format.DecimalUnitFormat; import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.aspect.LamiTableEntryAspect; import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.module.LamiChartModel; @@ -41,6 +47,8 @@ import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.module.L import org.eclipse.tracecompass.internal.provisional.analysis.lami.ui.signals.LamiSelectionUpdateSignal; import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; import org.eclipse.tracecompass.tmf.ui.viewers.TmfViewer; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; import org.swtchart.Chart; import org.swtchart.ITitle; @@ -162,6 +170,8 @@ public abstract class LamiXYChartViewer extends TmfViewer implements ILamiViewer private boolean fSelected; private Set fSelection; + private final ToolBar fToolBar; + /** * Creates a Viewer instance based on SWTChart. * @@ -278,6 +288,8 @@ public abstract class LamiXYChartViewer extends TmfViewer implements ILamiViewer /* Refresh the titles to fit the current chart size */ refreshDisplayTitles(); + fToolBar = createChartToolBar(); + fChart.addDisposeListener(e -> { /* Dispose resources of this class */ LamiXYChartViewer.super.dispose(); @@ -395,6 +407,13 @@ public abstract class LamiXYChartViewer extends TmfViewer implements ILamiViewer return fChart; } + /** + * @return the toolBar + */ + public ToolBar getToolBar() { + return fToolBar; + } + /** * Is a selection made in the chart. * @@ -592,4 +611,88 @@ public abstract class LamiXYChartViewer extends TmfViewer implements ILamiViewer redraw(); } + + /** + * Create a tool bar on top right of the chart. Contained actions: + *
    + *
  • Dispose the current viewer, also known as "Close the chart"
  • + *
+ * + * This tool bar should only appear when the mouse enters the composite. + * + * @return the tool bar + */ + protected ToolBar createChartToolBar() { + Image removeImage = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ELCL_REMOVE); + ToolBar toolBar = new ToolBar(getChart(), SWT.HORIZONTAL); + + /* Default state */ + toolBar.moveAbove(null); + toolBar.setVisible(false); + + /* + * Close chart button + */ + ToolItem closeButton = new ToolItem(toolBar, SWT.PUSH); + closeButton.setImage(removeImage); + closeButton.setToolTipText(Messages.LamiXYChartViewer_CloseChartToolTip); + closeButton.addSelectionListener(new SelectionListener() { + @Override + public void widgetSelected(@Nullable SelectionEvent e) { + Composite parent = getParent(); + dispose(); + parent.layout(); + } + + @Override + public void widgetDefaultSelected(@Nullable SelectionEvent e) { + } + }); + + toolBar.pack(); + toolBar.setLocation(new Point(getChart().getSize().x - toolBar.getSize().x, 0)); + + /* Visibility toggle filter */ + Listener toolBarVisibilityToggleListener = e -> { + if (e.widget instanceof Control) { + Control control = (Control) e.widget; + Point display = control.toDisplay(e.x, e.y); + Point location = getChart().getParent().toControl(display); + + /* + * Only set to visible if we are at the right location, in the + * right shell. + */ + boolean visible = getChart().getBounds().contains(location) && + control.getShell().equals(getChart().getShell()); + getToolBar().setVisible(visible); + } + }; + + /* Filter to make sure we hide the toolbar if we exit the window */ + Listener hideToolBarListener = (e -> getToolBar().setVisible(false)); + + /* + * Add the filters to the main Display, and remove them when we dispose + * the chart. + */ + Display display = getChart().getDisplay(); + display.addFilter(SWT.MouseEnter, toolBarVisibilityToggleListener); + display.addFilter(SWT.MouseExit, hideToolBarListener); + + getChart().addDisposeListener(e -> { + display.removeFilter(SWT.MouseEnter, toolBarVisibilityToggleListener); + display.removeFilter(SWT.MouseExit, hideToolBarListener); + }); + + /* Reposition the tool bar on resize */ + getChart().addListener(SWT.Resize, new Listener() { + @Override + public void handleEvent(@Nullable Event event) { + toolBar.setLocation(new Point(getChart().getSize().x - toolBar.getSize().x, 0)); + } + }); + + return toolBar; + } } diff --git a/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/Messages.java b/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/Messages.java index 3a0e7909e3..6461df7195 100644 --- a/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/Messages.java +++ b/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/Messages.java @@ -23,10 +23,12 @@ public class Messages extends NLS { private static final String BUNDLE_NAME = Messages.class.getPackage().getName() + ".messages"; //$NON-NLS-1$ - public static String LamiViewer_DefaultValueName; - public static String LamiScatterViewer_by; + public static String LamiXYChartViewer_CloseChartToolTip; + + public static String LamiViewer_DefaultValueName; + static { NLS.initializeMessages(BUNDLE_NAME, Messages.class); } diff --git a/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/messages.properties b/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/messages.properties index deaeae33d6..163eb5dbe9 100644 --- a/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/messages.properties +++ b/analysis/org.eclipse.tracecompass.analysis.lami.ui/src/org/eclipse/tracecompass/internal/provisional/analysis/lami/ui/viewers/messages.properties @@ -7,6 +7,7 @@ # http://www.eclipse.org/legal/epl-v10.html ############################################################################### -LamiViewer_DefaultValueName = Value LamiScatterViewer_by = by +LamiXYChartViewer_CloseChartToolTip = Close chart +LamiViewer_DefaultValueName = Value -- 2.34.1