1 /*******************************************************************************
2 * Copyright (c) 2009, 2016 Ericsson.
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
10 * Alvaro Sanchez-Leon - Initial API and implementation
11 * Patrick Tasse - Refactoring
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.dialogs
;
16 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
17 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
18 import org
.eclipse
.jface
.resource
.JFaceResources
;
19 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
22 import org
.eclipse
.swt
.graphics
.Color
;
23 import org
.eclipse
.swt
.graphics
.GC
;
24 import org
.eclipse
.swt
.graphics
.RGB
;
25 import org
.eclipse
.swt
.graphics
.Rectangle
;
26 import org
.eclipse
.swt
.layout
.GridData
;
27 import org
.eclipse
.swt
.layout
.GridLayout
;
28 import org
.eclipse
.swt
.widgets
.Canvas
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Control
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.swt
.widgets
.Event
;
33 import org
.eclipse
.swt
.widgets
.Group
;
34 import org
.eclipse
.swt
.widgets
.Label
;
35 import org
.eclipse
.swt
.widgets
.Listener
;
36 import org
.eclipse
.swt
.widgets
.Shell
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
39 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
42 * Legend for the colors used in the time graph view
45 * @author Alvaro Sanchez-Leon
46 * @author Patrick Tasse
48 public class TimeGraphLegend
extends TitleAreaDialog
{
50 private final ITimeGraphPresentationProvider provider
;
51 private final LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
54 * Open the time graph legend window
59 * The presentation provider
61 public static void open(Shell parent
, ITimeGraphPresentationProvider provider
) {
62 (new TimeGraphLegend(parent
, provider
)).open();
66 * Standard constructor
71 * The presentation provider
73 public TimeGraphLegend(Shell parent
, ITimeGraphPresentationProvider provider
) {
75 this.provider
= provider
;
76 this.setShellStyle(getShellStyle() | SWT
.RESIZE
);
80 protected Control
createDialogArea(Composite parent
) {
81 Composite dlgArea
= (Composite
) super.createDialogArea(parent
);
82 Composite composite
= new Composite(dlgArea
, SWT
.NONE
);
84 GridLayout layout
= new GridLayout();
85 composite
.setLayout(layout
);
86 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
87 composite
.setLayoutData(gd
);
89 createStatesGroup(composite
);
91 setTitle(Messages
.TmfTimeLegend_LEGEND
);
92 setDialogHelpAvailable(false);
93 setHelpAvailable(false);
95 composite
.addDisposeListener((e
) -> {
96 fResourceManager
.dispose();
101 private void createStatesGroup(Composite composite
) {
102 ScrolledComposite sc
= new ScrolledComposite(composite
, SWT
.V_SCROLL
|SWT
.H_SCROLL
);
103 sc
.setExpandHorizontal(true);
104 sc
.setExpandVertical(true);
105 Group gs
= new Group(sc
, SWT
.H_SCROLL
);
107 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
108 sc
.setLayoutData(gd
);
110 String stateTypeName
= provider
.getStateTypeName();
111 StringBuffer buffer
= new StringBuffer();
112 if (!stateTypeName
.isEmpty()) {
113 buffer
.append(stateTypeName
);
114 buffer
.append(" "); //$NON-NLS-1$
116 buffer
.append(Messages
.TmfTimeLegend_StateTypeName
);
117 gs
.setText(buffer
.toString());
119 GridLayout layout
= new GridLayout();
120 layout
.numColumns
= 2;
121 layout
.marginWidth
= 20;
122 layout
.marginBottom
= 10;
123 gs
.setLayout(layout
);
125 // Go through all the defined pairs of state color and state name and display them.
126 StateItem
[] stateItems
= provider
.getStateTable();
127 for (int i
= 0; i
< stateItems
.length
; i
++) {
128 //Get the color related to the index
129 RGB rgb
= stateItems
[i
].getStateColor();
131 //Get the given name, provided by the interface to the application
132 String stateName
= stateItems
[i
].getStateString();
134 // draw color with name
135 Bar bar
= new Bar(gs
, rgb
);
139 gd
.verticalIndent
= 8;
140 bar
.setLayoutData(gd
);
141 Label name
= new Label(gs
, SWT
.NONE
);
142 name
.setText(stateName
);
144 gd
.horizontalIndent
= 10;
145 gd
.verticalIndent
= 8;
146 name
.setLayoutData(gd
);
148 sc
.setMinSize(gs
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
152 protected void configureShell(Shell shell
) {
153 super.configureShell(shell
);
154 shell
.setText(Messages
.TmfTimeLegend_TRACE_STATES_TITLE
);
158 protected void createButtonsForButtonBar(Composite parent
) {
159 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
,
163 class Bar
extends Canvas
{
164 private final Color color
;
166 public Bar(Composite parent
, RGB rgb
) {
167 super(parent
, SWT
.NONE
);
169 color
= fResourceManager
.createColor(rgb
);
170 addListener(SWT
.Paint
, new Listener() {
172 public void handleEvent(Event event
) {
178 private void draw(GC gc
) {
179 Rectangle r
= getClientArea();
180 gc
.setBackground(color
);
182 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
183 gc
.drawRectangle(0, 0, r
.width
- 1, r
.height
- 1);