1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 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
.linuxtools
.tmf
.ui
.widgets
.timegraph
.dialogs
;
16 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
17 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
18 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
19 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphProvider
;
20 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphProvider
.StateColor
;
21 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphColorScheme
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.graphics
.Color
;
24 import org
.eclipse
.swt
.graphics
.GC
;
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
.Event
;
32 import org
.eclipse
.swt
.widgets
.Group
;
33 import org
.eclipse
.swt
.widgets
.Label
;
34 import org
.eclipse
.swt
.widgets
.Listener
;
35 import org
.eclipse
.swt
.widgets
.Shell
;
38 public class TimeGraphLegend
extends TitleAreaDialog
{
40 public static final int interactionColors
[] = {
41 TimeGraphColorScheme
.TI_START_THREAD
,
42 TimeGraphColorScheme
.TI_NOTIFY_JOINED
, TimeGraphColorScheme
.TI_NOTIFY
,
43 TimeGraphColorScheme
.TI_INTERRUPT
, TimeGraphColorScheme
.TI_HANDOFF_LOCK
};
45 protected TimeGraphColorScheme colors
;
46 private ITimeGraphProvider ifUtil
;
48 public static void open(Shell parent
, ITimeGraphProvider rifUtil
) {
49 (new TimeGraphLegend(parent
, rifUtil
)).open();
52 public TimeGraphLegend(Shell parent
, ITimeGraphProvider rifUtil
) {
54 colors
= new TimeGraphColorScheme();
55 this.ifUtil
= rifUtil
;
59 protected Control
createDialogArea(Composite parent
) {
60 Composite dlgArea
= (Composite
) super.createDialogArea(parent
);
61 Composite composite
= new Composite(dlgArea
, SWT
.NONE
);
63 GridLayout layout
= new GridLayout();
64 layout
.numColumns
= 2;
65 composite
.setLayout(layout
);
66 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
67 composite
.setLayoutData(gd
);
69 createThreadStatesGroup(composite
);
71 setMessage(Messages
.TmfTimeLegend_LEGEND
);
72 setTitle(Messages
.TmfTimeLegend_TRACE_STATES_TITLE
);
73 setDialogHelpAvailable(false);
74 setHelpAvailable(false);
79 private void createThreadStatesGroup(Composite composite
) {
80 Group gs
= new Group(composite
, SWT
.NONE
);
81 gs
.setText(Messages
.TmfTimeLegend_TRACE_STATES
);
82 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
85 GridLayout layout
= new GridLayout();
86 layout
.numColumns
= 2;
87 layout
.marginWidth
= 20;
88 layout
.marginBottom
= 10;
91 // Go through all the defined colors and only add the ones you need.
92 // This will not handle several colors assigned to a color, we have
93 // 16 mil colors, and should not pick two to mean the same thing.
94 for (int i
= 0; i
< TimeGraphColorScheme
.getStateColors().length
; i
++) {
95 //Get the color enum related to the index
96 StateColor stateColor
= TimeGraphColorScheme
.getStateColors()[i
];
97 //Get the given name, provided by the interface to the application
98 String stateName
= ifUtil
.getStateName(stateColor
);
99 if( stateName
!= "Not mapped" ) { //$NON-NLS-1$
100 Bar bar
= new Bar(gs
, i
);
104 gd
.verticalIndent
= 8;
105 bar
.setLayoutData(gd
);
106 Label name
= new Label(gs
, SWT
.NONE
);
107 name
.setText(stateName
);
109 gd
.horizontalIndent
= 10;
110 gd
.verticalIndent
= 8;
111 name
.setLayoutData(gd
);
117 protected void configureShell(Shell shell
) {
118 super.configureShell(shell
);
119 shell
.setText(Messages
.TmfTimeLegend_WINDOW_TITLE
);
123 protected void createButtonsForButtonBar(Composite parent
) {
124 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
,
128 class Bar
extends Canvas
{
131 public Bar(Composite parent
, int colorIdx
) {
132 super(parent
, SWT
.NONE
);
134 color
= colors
.getColor(colorIdx
);
135 addListener(SWT
.Paint
, new Listener() {
137 public void handleEvent(Event event
) {
143 private void draw(GC gc
) {
144 Rectangle r
= getClientArea();
145 gc
.setBackground(color
);
147 gc
.setForeground(colors
.getColor(TimeGraphColorScheme
.BLACK
));
148 gc
.drawRectangle(0, 0, r
.width
- 1, r
.height
- 1);
152 class Arrow
extends Canvas
{
153 public final static int HEIGHT
= 12;
154 public final static int DX
= 3;
158 public Arrow(Composite parent
, int colorIdx
) {
159 super(parent
, SWT
.NONE
);
161 color
= colors
.getColor(colorIdx
);
162 addListener(SWT
.Paint
, new Listener() {
164 public void handleEvent(Event event
) {
170 private void draw(GC gc
) {
171 Rectangle r
= getClientArea();
172 gc
.setForeground(color
);
175 if (r
.height
> HEIGHT
) {
176 y0
= (r
.height
- HEIGHT
) / 2;
183 gc
.drawLine(DX
, y0
, DX
, y1
);
185 gc
.drawLine(0, y0
+ 3, DX
, y0
);
186 gc
.drawLine(2 * DX
, y0
+ 3, DX
, y0
);
190 class Mark
extends Canvas
{
191 public final static int DX
= 3;
195 public Mark(Composite parent
, int colorIdx
) {
196 super(parent
, SWT
.NONE
);
198 color
= colors
.getColor(colorIdx
);
199 addListener(SWT
.Paint
, new Listener() {
201 public void handleEvent(Event event
) {
207 private void draw(GC gc
) {
208 Rectangle r
= getClientArea();
209 gc
.setBackground(color
);
211 int y
= (r
.height
- DX
) / 2;
212 int c
[] = { 0, y
, DX
, y
+ DX
, 2 * DX
, y
};