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
.jface
.resource
.JFaceResources
;
19 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
20 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
21 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
22 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.graphics
.Color
;
25 import org
.eclipse
.swt
.graphics
.GC
;
26 import org
.eclipse
.swt
.graphics
.RGB
;
27 import org
.eclipse
.swt
.graphics
.Rectangle
;
28 import org
.eclipse
.swt
.layout
.GridData
;
29 import org
.eclipse
.swt
.layout
.GridLayout
;
30 import org
.eclipse
.swt
.widgets
.Canvas
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.Control
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.eclipse
.swt
.widgets
.Event
;
35 import org
.eclipse
.swt
.widgets
.Group
;
36 import org
.eclipse
.swt
.widgets
.Label
;
37 import org
.eclipse
.swt
.widgets
.Listener
;
38 import org
.eclipse
.swt
.widgets
.Shell
;
41 * Legend for the colors used in the time graph view
44 * @author Alvaro Sanchez-Leon
45 * @author Patrick Tasse
47 public class TimeGraphLegend
extends TitleAreaDialog
{
49 private final ITimeGraphPresentationProvider provider
;
50 private final LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
52 public static void open(Shell parent
, ITimeGraphPresentationProvider provider
) {
53 (new TimeGraphLegend(parent
, provider
)).open();
56 public TimeGraphLegend(Shell parent
, ITimeGraphPresentationProvider provider
) {
58 this.provider
= provider
;
59 this.setShellStyle(getShellStyle());
63 protected Control
createDialogArea(Composite parent
) {
64 Composite dlgArea
= (Composite
) super.createDialogArea(parent
);
65 Composite composite
= new Composite(dlgArea
, SWT
.NONE
);
67 GridLayout layout
= new GridLayout();
68 layout
.numColumns
= 2;
69 composite
.setLayout(layout
);
70 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
71 composite
.setLayoutData(gd
);
73 createStatesGroup(composite
);
75 setTitle(Messages
.TmfTimeLegend_LEGEND
);
76 setDialogHelpAvailable(false);
77 setHelpAvailable(false);
82 private void createStatesGroup(Composite composite
) {
83 Group gs
= new Group(composite
, SWT
.NONE
);
84 gs
.setText(provider
.getStateTypeName() + " " + Messages
.TmfTimeLegend_StateTypeName
); //$NON-NLS-1$
85 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
88 GridLayout layout
= new GridLayout();
89 layout
.numColumns
= 2;
90 layout
.marginWidth
= 20;
91 layout
.marginBottom
= 10;
94 // Go through all the defined pairs of state color and state name and display them.
95 StateItem
[] stateItems
= provider
.getStateTable();
96 for (int i
= 0; i
< stateItems
.length
; i
++) {
97 //Get the color related to the index
98 RGB rgb
= stateItems
[i
].getStateColor();
100 //Get the given name, provided by the interface to the application
101 String stateName
= stateItems
[i
].getStateString();
103 // draw color with name
104 Bar bar
= new Bar(gs
, rgb
);
108 gd
.verticalIndent
= 8;
109 bar
.setLayoutData(gd
);
110 Label name
= new Label(gs
, SWT
.NONE
);
111 name
.setText(stateName
);
113 gd
.horizontalIndent
= 10;
114 gd
.verticalIndent
= 8;
115 name
.setLayoutData(gd
);
120 protected void configureShell(Shell shell
) {
121 super.configureShell(shell
);
122 shell
.setText(Messages
.TmfTimeLegend_TRACE_STATES_TITLE
);
126 protected void createButtonsForButtonBar(Composite parent
) {
127 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
,
131 class Bar
extends Canvas
{
132 private final Color color
;
134 public Bar(Composite parent
, RGB rgb
) {
135 super(parent
, SWT
.NONE
);
137 color
= fResourceManager
.createColor(rgb
);
138 addListener(SWT
.Paint
, new Listener() {
140 public void handleEvent(Event event
) {
146 private void draw(GC gc
) {
147 Rectangle r
= getClientArea();
148 gc
.setBackground(color
);
150 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
151 gc
.drawRectangle(0, 0, r
.width
- 1, r
.height
- 1);
155 public void dispose() {