Bug 378401: Implementation of time graph widget.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / dialogs / TimeGraphLegend.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson.
3 *
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
8 *
9 * Contributors:
10 * Alvaro Sanchez-Leon - Initial API and implementation
11 * Patrick Tasse - Refactoring
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.dialogs;
15
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;
36
37
38 public class TimeGraphLegend extends TitleAreaDialog {
39
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 };
44
45 protected TimeGraphColorScheme colors;
46 private ITimeGraphProvider ifUtil;
47
48 public static void open(Shell parent, ITimeGraphProvider rifUtil) {
49 (new TimeGraphLegend(parent, rifUtil)).open();
50 }
51
52 public TimeGraphLegend(Shell parent, ITimeGraphProvider rifUtil) {
53 super(parent);
54 colors = new TimeGraphColorScheme();
55 this.ifUtil = rifUtil;
56 }
57
58 @Override
59 protected Control createDialogArea(Composite parent) {
60 Composite dlgArea = (Composite) super.createDialogArea(parent);
61 Composite composite = new Composite(dlgArea, SWT.NONE);
62
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);
68
69 createThreadStatesGroup(composite);
70
71 setMessage(Messages.TmfTimeLegend_LEGEND);
72 setTitle(Messages.TmfTimeLegend_TRACE_STATES_TITLE);
73 setDialogHelpAvailable(false);
74 setHelpAvailable(false);
75
76 return composite;
77 }
78
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);
83 gs.setLayoutData(gd);
84
85 GridLayout layout = new GridLayout();
86 layout.numColumns = 2;
87 layout.marginWidth = 20;
88 layout.marginBottom = 10;
89 gs.setLayout(layout);
90
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);
101 gd = new GridData();
102 gd.widthHint = 40;
103 gd.heightHint = 20;
104 gd.verticalIndent = 8;
105 bar.setLayoutData(gd);
106 Label name = new Label(gs, SWT.NONE);
107 name.setText(stateName);
108 gd = new GridData();
109 gd.horizontalIndent = 10;
110 gd.verticalIndent = 8;
111 name.setLayoutData(gd);
112 }
113 }
114 }
115
116 @Override
117 protected void configureShell(Shell shell) {
118 super.configureShell(shell);
119 shell.setText(Messages.TmfTimeLegend_WINDOW_TITLE);
120 }
121
122 @Override
123 protected void createButtonsForButtonBar(Composite parent) {
124 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
125 true);
126 }
127
128 class Bar extends Canvas {
129 private Color color;
130
131 public Bar(Composite parent, int colorIdx) {
132 super(parent, SWT.NONE);
133
134 color = colors.getColor(colorIdx);
135 addListener(SWT.Paint, new Listener() {
136 @Override
137 public void handleEvent(Event event) {
138 draw(event.gc);
139 }
140 });
141 }
142
143 private void draw(GC gc) {
144 Rectangle r = getClientArea();
145 gc.setBackground(color);
146 gc.fillRectangle(r);
147 gc.setForeground(colors.getColor(TimeGraphColorScheme.BLACK));
148 gc.drawRectangle(0, 0, r.width - 1, r.height - 1);
149 }
150 }
151
152 class Arrow extends Canvas {
153 public final static int HEIGHT = 12;
154 public final static int DX = 3;
155
156 private Color color;
157
158 public Arrow(Composite parent, int colorIdx) {
159 super(parent, SWT.NONE);
160
161 color = colors.getColor(colorIdx);
162 addListener(SWT.Paint, new Listener() {
163 @Override
164 public void handleEvent(Event event) {
165 draw(event.gc);
166 }
167 });
168 }
169
170 private void draw(GC gc) {
171 Rectangle r = getClientArea();
172 gc.setForeground(color);
173
174 int y0, y1;
175 if (r.height > HEIGHT) {
176 y0 = (r.height - HEIGHT) / 2;
177 y1 = y0 + HEIGHT;
178 } else {
179 y0 = 0;
180 y1 = r.height;
181 }
182
183 gc.drawLine(DX, y0, DX, y1);
184
185 gc.drawLine(0, y0 + 3, DX, y0);
186 gc.drawLine(2 * DX, y0 + 3, DX, y0);
187 }
188 }
189
190 class Mark extends Canvas {
191 public final static int DX = 3;
192
193 private Color color;
194
195 public Mark(Composite parent, int colorIdx) {
196 super(parent, SWT.NONE);
197
198 color = colors.getColor(colorIdx);
199 addListener(SWT.Paint, new Listener() {
200 @Override
201 public void handleEvent(Event event) {
202 draw(event.gc);
203 }
204 });
205 }
206
207 private void draw(GC gc) {
208 Rectangle r = getClientArea();
209 gc.setBackground(color);
210
211 int y = (r.height - DX) / 2;
212 int c[] = { 0, y, DX, y + DX, 2 * DX, y };
213 gc.fillPolygon(c);
214 }
215 }
216 }
This page took 0.03575 seconds and 5 git commands to generate.