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());
53 * Open the time graph legend window
58 * The presentation provider
60 public static void open(Shell parent
, ITimeGraphPresentationProvider provider
) {
61 (new TimeGraphLegend(parent
, provider
)).open();
65 * Standard constructor
70 * The presentation provider
72 public TimeGraphLegend(Shell parent
, ITimeGraphPresentationProvider provider
) {
74 this.provider
= provider
;
75 this.setShellStyle(getShellStyle());
79 protected Control
createDialogArea(Composite parent
) {
80 Composite dlgArea
= (Composite
) super.createDialogArea(parent
);
81 Composite composite
= new Composite(dlgArea
, SWT
.NONE
);
83 GridLayout layout
= new GridLayout();
84 layout
.numColumns
= 2;
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);
98 private void createStatesGroup(Composite composite
) {
99 Group gs
= new Group(composite
, SWT
.NONE
);
100 String stateTypeName
= provider
.getStateTypeName();
101 StringBuffer buffer
= new StringBuffer();
102 if (!stateTypeName
.isEmpty()) {
103 buffer
.append(stateTypeName
);
104 buffer
.append(" "); //$NON-NLS-1$
106 buffer
.append(Messages
.TmfTimeLegend_StateTypeName
);
107 gs
.setText(buffer
.toString());
109 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
110 gs
.setLayoutData(gd
);
112 GridLayout layout
= new GridLayout();
113 layout
.numColumns
= 2;
114 layout
.marginWidth
= 20;
115 layout
.marginBottom
= 10;
116 gs
.setLayout(layout
);
118 // Go through all the defined pairs of state color and state name and display them.
119 StateItem
[] stateItems
= provider
.getStateTable();
120 for (int i
= 0; i
< stateItems
.length
; i
++) {
121 //Get the color related to the index
122 RGB rgb
= stateItems
[i
].getStateColor();
124 //Get the given name, provided by the interface to the application
125 String stateName
= stateItems
[i
].getStateString();
127 // draw color with name
128 Bar bar
= new Bar(gs
, rgb
);
132 gd
.verticalIndent
= 8;
133 bar
.setLayoutData(gd
);
134 Label name
= new Label(gs
, SWT
.NONE
);
135 name
.setText(stateName
);
137 gd
.horizontalIndent
= 10;
138 gd
.verticalIndent
= 8;
139 name
.setLayoutData(gd
);
144 protected void configureShell(Shell shell
) {
145 super.configureShell(shell
);
146 shell
.setText(Messages
.TmfTimeLegend_TRACE_STATES_TITLE
);
150 protected void createButtonsForButtonBar(Composite parent
) {
151 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
,
155 class Bar
extends Canvas
{
156 private final Color color
;
158 public Bar(Composite parent
, RGB rgb
) {
159 super(parent
, SWT
.NONE
);
161 color
= fResourceManager
.createColor(rgb
);
162 addListener(SWT
.Paint
, new Listener() {
164 public void handleEvent(Event event
) {
170 private void draw(GC gc
) {
171 Rectangle r
= getClientArea();
172 gc
.setBackground(color
);
174 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
175 gc
.drawRectangle(0, 0, r
.width
- 1, r
.height
- 1);
179 public void dispose() {