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 public class TimeGraphLegend
extends TitleAreaDialog
{
43 private ITimeGraphPresentationProvider provider
;
44 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
46 public static void open(Shell parent
, ITimeGraphPresentationProvider provider
) {
47 (new TimeGraphLegend(parent
, provider
)).open();
50 public TimeGraphLegend(Shell parent
, ITimeGraphPresentationProvider provider
) {
52 this.provider
= provider
;
53 this.setShellStyle(getShellStyle());
57 protected Control
createDialogArea(Composite parent
) {
58 Composite dlgArea
= (Composite
) super.createDialogArea(parent
);
59 Composite composite
= new Composite(dlgArea
, SWT
.NONE
);
61 GridLayout layout
= new GridLayout();
62 layout
.numColumns
= 2;
63 composite
.setLayout(layout
);
64 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
65 composite
.setLayoutData(gd
);
67 createStatesGroup(composite
);
69 setTitle(Messages
.TmfTimeLegend_LEGEND
);
70 setDialogHelpAvailable(false);
71 setHelpAvailable(false);
76 private void createStatesGroup(Composite composite
) {
77 Group gs
= new Group(composite
, SWT
.NONE
);
78 gs
.setText(provider
.getStateTypeName());
79 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
82 GridLayout layout
= new GridLayout();
83 layout
.numColumns
= 2;
84 layout
.marginWidth
= 20;
85 layout
.marginBottom
= 10;
88 // Go through all the defined pairs of state color and state name and display them.
89 StateItem
[] stateItems
= provider
.getStateTable();
90 for (int i
= 0; i
< stateItems
.length
; i
++) {
91 //Get the color related to the index
92 RGB rgb
= stateItems
[i
].getStateColor();
94 //Get the given name, provided by the interface to the application
95 String stateName
= stateItems
[i
].getStateString();
97 // draw color with name
98 Bar bar
= new Bar(gs
, rgb
);
102 gd
.verticalIndent
= 8;
103 bar
.setLayoutData(gd
);
104 Label name
= new Label(gs
, SWT
.NONE
);
105 name
.setText(stateName
);
107 gd
.horizontalIndent
= 10;
108 gd
.verticalIndent
= 8;
109 name
.setLayoutData(gd
);
114 protected void configureShell(Shell shell
) {
115 super.configureShell(shell
);
116 shell
.setText(Messages
.TmfTimeLegend_TRACE_STATES_TITLE
);
120 protected void createButtonsForButtonBar(Composite parent
) {
121 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
,
125 class Bar
extends Canvas
{
128 public Bar(Composite parent
, RGB rgb
) {
129 super(parent
, SWT
.NONE
);
131 color
= fResourceManager
.createColor(rgb
);
132 addListener(SWT
.Paint
, new Listener() {
134 public void handleEvent(Event event
) {
140 private void draw(GC gc
) {
141 Rectangle r
= getClientArea();
142 gc
.setBackground(color
);
144 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
145 gc
.drawRectangle(0, 0, r
.width
- 1, r
.height
- 1);
149 public void dispose() {