Merge branch 'master' into lttng-kepler
[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.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;
39
40 /**
41 * Legend for the colors used in the time graph view
42 *
43 * @version 1.0
44 * @author Alvaro Sanchez-Leon
45 * @author Patrick Tasse
46 */
47 public class TimeGraphLegend extends TitleAreaDialog {
48
49 private final ITimeGraphPresentationProvider provider;
50 private final LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources());
51
52 /**
53 * Open the time graph legend window
54 *
55 * @param parent
56 * The parent shell
57 * @param provider
58 * The presentation provider
59 */
60 public static void open(Shell parent, ITimeGraphPresentationProvider provider) {
61 (new TimeGraphLegend(parent, provider)).open();
62 }
63
64 /**
65 * Standard constructor
66 *
67 * @param parent
68 * The parent shell
69 * @param provider
70 * The presentation provider
71 */
72 public TimeGraphLegend(Shell parent, ITimeGraphPresentationProvider provider) {
73 super(parent);
74 this.provider = provider;
75 this.setShellStyle(getShellStyle());
76 }
77
78 @Override
79 protected Control createDialogArea(Composite parent) {
80 Composite dlgArea = (Composite) super.createDialogArea(parent);
81 Composite composite = new Composite(dlgArea, SWT.NONE);
82
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);
88
89 createStatesGroup(composite);
90
91 setTitle(Messages.TmfTimeLegend_LEGEND);
92 setDialogHelpAvailable(false);
93 setHelpAvailable(false);
94
95 return composite;
96 }
97
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$
105 }
106 buffer.append(Messages.TmfTimeLegend_StateTypeName);
107 gs.setText(buffer.toString());
108
109 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
110 gs.setLayoutData(gd);
111
112 GridLayout layout = new GridLayout();
113 layout.numColumns = 2;
114 layout.marginWidth = 20;
115 layout.marginBottom = 10;
116 gs.setLayout(layout);
117
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();
123
124 //Get the given name, provided by the interface to the application
125 String stateName = stateItems[i].getStateString();
126
127 // draw color with name
128 Bar bar = new Bar(gs, rgb);
129 gd = new GridData();
130 gd.widthHint = 40;
131 gd.heightHint = 20;
132 gd.verticalIndent = 8;
133 bar.setLayoutData(gd);
134 Label name = new Label(gs, SWT.NONE);
135 name.setText(stateName);
136 gd = new GridData();
137 gd.horizontalIndent = 10;
138 gd.verticalIndent = 8;
139 name.setLayoutData(gd);
140 }
141 }
142
143 @Override
144 protected void configureShell(Shell shell) {
145 super.configureShell(shell);
146 shell.setText(Messages.TmfTimeLegend_TRACE_STATES_TITLE);
147 }
148
149 @Override
150 protected void createButtonsForButtonBar(Composite parent) {
151 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
152 true);
153 }
154
155 class Bar extends Canvas {
156 private final Color color;
157
158 public Bar(Composite parent, RGB rgb) {
159 super(parent, SWT.NONE);
160
161 color = fResourceManager.createColor(rgb);
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.setBackground(color);
173 gc.fillRectangle(r);
174 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
175 gc.drawRectangle(0, 0, r.width - 1, r.height - 1);
176 }
177
178 @Override
179 public void dispose() {
180 super.dispose();
181 color.dispose();
182 }
183
184 }
185
186 }
This page took 0.047678 seconds and 5 git commands to generate.