Commit | Line | Data |
---|---|---|
6151d86c PT |
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; | |
941f3750 | 24 | import org.eclipse.swt.custom.ScrolledComposite; |
6151d86c PT |
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; | |
40 | ||
41 | /** | |
42 | * Legend for the colors used in the time graph view | |
43 | * | |
44 | * @version 1.0 | |
45 | * @author Alvaro Sanchez-Leon | |
46 | * @author Patrick Tasse | |
47 | */ | |
48 | public class TimeGraphLegend extends TitleAreaDialog { | |
49 | ||
50 | private final ITimeGraphPresentationProvider provider; | |
51 | private final LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources()); | |
52 | ||
53 | /** | |
54 | * Open the time graph legend window | |
55 | * | |
56 | * @param parent | |
57 | * The parent shell | |
58 | * @param provider | |
59 | * The presentation provider | |
60 | */ | |
61 | public static void open(Shell parent, ITimeGraphPresentationProvider provider) { | |
62 | (new TimeGraphLegend(parent, provider)).open(); | |
63 | } | |
64 | ||
65 | /** | |
66 | * Standard constructor | |
67 | * | |
68 | * @param parent | |
69 | * The parent shell | |
70 | * @param provider | |
71 | * The presentation provider | |
72 | */ | |
73 | public TimeGraphLegend(Shell parent, ITimeGraphPresentationProvider provider) { | |
74 | super(parent); | |
75 | this.provider = provider; | |
941f3750 | 76 | this.setShellStyle(getShellStyle() | SWT.RESIZE); |
6151d86c PT |
77 | } |
78 | ||
79 | @Override | |
80 | protected Control createDialogArea(Composite parent) { | |
81 | Composite dlgArea = (Composite) super.createDialogArea(parent); | |
82 | Composite composite = new Composite(dlgArea, SWT.NONE); | |
83 | ||
84 | GridLayout layout = new GridLayout(); | |
6151d86c PT |
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) { | |
941f3750 XR |
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); | |
103 | sc.setContent(gs); | |
104 | GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); | |
105 | sc.setLayoutData(gd); | |
106 | ||
6151d86c PT |
107 | String stateTypeName = provider.getStateTypeName(); |
108 | StringBuffer buffer = new StringBuffer(); | |
109 | if (!stateTypeName.isEmpty()) { | |
110 | buffer.append(stateTypeName); | |
111 | buffer.append(" "); //$NON-NLS-1$ | |
112 | } | |
113 | buffer.append(Messages.TmfTimeLegend_StateTypeName); | |
114 | gs.setText(buffer.toString()); | |
115 | ||
6151d86c PT |
116 | GridLayout layout = new GridLayout(); |
117 | layout.numColumns = 2; | |
118 | layout.marginWidth = 20; | |
119 | layout.marginBottom = 10; | |
120 | gs.setLayout(layout); | |
121 | ||
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(); | |
127 | ||
128 | //Get the given name, provided by the interface to the application | |
129 | String stateName = stateItems[i].getStateString(); | |
130 | ||
131 | // draw color with name | |
132 | Bar bar = new Bar(gs, rgb); | |
133 | gd = new GridData(); | |
134 | gd.widthHint = 40; | |
135 | gd.heightHint = 20; | |
136 | gd.verticalIndent = 8; | |
137 | bar.setLayoutData(gd); | |
138 | Label name = new Label(gs, SWT.NONE); | |
139 | name.setText(stateName); | |
140 | gd = new GridData(); | |
141 | gd.horizontalIndent = 10; | |
142 | gd.verticalIndent = 8; | |
143 | name.setLayoutData(gd); | |
144 | } | |
941f3750 | 145 | sc.setMinSize(gs.computeSize(SWT.DEFAULT, SWT.DEFAULT)); |
6151d86c PT |
146 | } |
147 | ||
148 | @Override | |
149 | protected void configureShell(Shell shell) { | |
150 | super.configureShell(shell); | |
151 | shell.setText(Messages.TmfTimeLegend_TRACE_STATES_TITLE); | |
152 | } | |
153 | ||
154 | @Override | |
155 | protected void createButtonsForButtonBar(Composite parent) { | |
156 | createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, | |
157 | true); | |
158 | } | |
159 | ||
160 | class Bar extends Canvas { | |
161 | private final Color color; | |
162 | ||
163 | public Bar(Composite parent, RGB rgb) { | |
164 | super(parent, SWT.NONE); | |
165 | ||
166 | color = fResourceManager.createColor(rgb); | |
167 | addListener(SWT.Paint, new Listener() { | |
168 | @Override | |
169 | public void handleEvent(Event event) { | |
170 | draw(event.gc); | |
171 | } | |
172 | }); | |
173 | } | |
174 | ||
175 | private void draw(GC gc) { | |
176 | Rectangle r = getClientArea(); | |
177 | gc.setBackground(color); | |
178 | gc.fillRectangle(r); | |
179 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); | |
180 | gc.drawRectangle(0, 0, r.width - 1, r.height - 1); | |
181 | } | |
182 | ||
183 | @Override | |
184 | public void dispose() { | |
185 | super.dispose(); | |
186 | color.dispose(); | |
187 | } | |
188 | ||
189 | } | |
190 | ||
191 | } |