1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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
.custom
.ScrolledComposite
;
25 import org
.eclipse
.swt
.graphics
.Color
;
26 import org
.eclipse
.swt
.graphics
.GC
;
27 import org
.eclipse
.swt
.graphics
.RGB
;
28 import org
.eclipse
.swt
.graphics
.Rectangle
;
29 import org
.eclipse
.swt
.layout
.GridData
;
30 import org
.eclipse
.swt
.layout
.GridLayout
;
31 import org
.eclipse
.swt
.widgets
.Canvas
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
33 import org
.eclipse
.swt
.widgets
.Control
;
34 import org
.eclipse
.swt
.widgets
.Display
;
35 import org
.eclipse
.swt
.widgets
.Event
;
36 import org
.eclipse
.swt
.widgets
.Group
;
37 import org
.eclipse
.swt
.widgets
.Label
;
38 import org
.eclipse
.swt
.widgets
.Listener
;
39 import org
.eclipse
.swt
.widgets
.Shell
;
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);
98 private void createStatesGroup(Composite composite
) {
99 ScrolledComposite sc
= new ScrolledComposite(composite
, SWT
.V_SCROLL
|SWT
.H_SCROLL
);
100 sc
.setExpandHorizontal(true);
101 sc
.setExpandVertical(true);
102 Group gs
= new Group(sc
, SWT
.H_SCROLL
);
104 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
105 sc
.setLayoutData(gd
);
107 String stateTypeName
= provider
.getStateTypeName();
108 StringBuffer buffer
= new StringBuffer();
109 if (!stateTypeName
.isEmpty()) {
110 buffer
.append(stateTypeName
);
111 buffer
.append(" "); //$NON-NLS-1$
113 buffer
.append(Messages
.TmfTimeLegend_StateTypeName
);
114 gs
.setText(buffer
.toString());
116 GridLayout layout
= new GridLayout();
117 layout
.numColumns
= 2;
118 layout
.marginWidth
= 20;
119 layout
.marginBottom
= 10;
120 gs
.setLayout(layout
);
122 // Go through all the defined pairs of state color and state name and display them.
123 StateItem
[] stateItems
= provider
.getStateTable();
124 for (int i
= 0; i
< stateItems
.length
; i
++) {
125 //Get the color related to the index
126 RGB rgb
= stateItems
[i
].getStateColor();
128 //Get the given name, provided by the interface to the application
129 String stateName
= stateItems
[i
].getStateString();
131 // draw color with name
132 Bar bar
= new Bar(gs
, rgb
);
136 gd
.verticalIndent
= 8;
137 bar
.setLayoutData(gd
);
138 Label name
= new Label(gs
, SWT
.NONE
);
139 name
.setText(stateName
);
141 gd
.horizontalIndent
= 10;
142 gd
.verticalIndent
= 8;
143 name
.setLayoutData(gd
);
145 sc
.setMinSize(gs
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
149 protected void configureShell(Shell shell
) {
150 super.configureShell(shell
);
151 shell
.setText(Messages
.TmfTimeLegend_TRACE_STATES_TITLE
);
155 protected void createButtonsForButtonBar(Composite parent
) {
156 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
,
160 class Bar
extends Canvas
{
161 private final Color color
;
163 public Bar(Composite parent
, RGB rgb
) {
164 super(parent
, SWT
.NONE
);
166 color
= fResourceManager
.createColor(rgb
);
167 addListener(SWT
.Paint
, new Listener() {
169 public void handleEvent(Event event
) {
175 private void draw(GC gc
) {
176 Rectangle r
= getClientArea();
177 gc
.setBackground(color
);
179 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
180 gc
.drawRectangle(0, 0, r
.width
- 1, r
.height
- 1);
184 public void dispose() {