tmf: Bug 490400: Leaking widgets due to incorrect cleanup in dispose()
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / widgets / timegraph / dialogs / TimeGraphLegend.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2016 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.tracecompass.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.swt.SWT;
21 import org.eclipse.swt.custom.ScrolledComposite;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.GC;
24 import org.eclipse.swt.graphics.RGB;
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.Display;
32 import org.eclipse.swt.widgets.Event;
33 import org.eclipse.swt.widgets.Group;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Listener;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
38 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider;
39 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem;
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;
76 this.setShellStyle(getShellStyle() | SWT.RESIZE);
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();
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 composite.addDisposeListener((e) -> {
96 fResourceManager.dispose();
97 });
98 return composite;
99 }
100
101 private void createStatesGroup(Composite composite) {
102 ScrolledComposite sc = new ScrolledComposite(composite, SWT.V_SCROLL|SWT.H_SCROLL);
103 sc.setExpandHorizontal(true);
104 sc.setExpandVertical(true);
105 Group gs = new Group(sc, SWT.H_SCROLL);
106 sc.setContent(gs);
107 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
108 sc.setLayoutData(gd);
109
110 String stateTypeName = provider.getStateTypeName();
111 StringBuffer buffer = new StringBuffer();
112 if (!stateTypeName.isEmpty()) {
113 buffer.append(stateTypeName);
114 buffer.append(" "); //$NON-NLS-1$
115 }
116 buffer.append(Messages.TmfTimeLegend_StateTypeName);
117 gs.setText(buffer.toString());
118
119 GridLayout layout = new GridLayout();
120 layout.numColumns = 2;
121 layout.marginWidth = 20;
122 layout.marginBottom = 10;
123 gs.setLayout(layout);
124
125 // Go through all the defined pairs of state color and state name and display them.
126 StateItem[] stateItems = provider.getStateTable();
127 for (int i = 0; i < stateItems.length; i++) {
128 //Get the color related to the index
129 RGB rgb = stateItems[i].getStateColor();
130
131 //Get the given name, provided by the interface to the application
132 String stateName = stateItems[i].getStateString();
133
134 // draw color with name
135 Bar bar = new Bar(gs, rgb);
136 gd = new GridData();
137 gd.widthHint = 40;
138 gd.heightHint = 20;
139 gd.verticalIndent = 8;
140 bar.setLayoutData(gd);
141 Label name = new Label(gs, SWT.NONE);
142 name.setText(stateName);
143 gd = new GridData();
144 gd.horizontalIndent = 10;
145 gd.verticalIndent = 8;
146 name.setLayoutData(gd);
147 }
148 sc.setMinSize(gs.computeSize(SWT.DEFAULT, SWT.DEFAULT));
149 }
150
151 @Override
152 protected void configureShell(Shell shell) {
153 super.configureShell(shell);
154 shell.setText(Messages.TmfTimeLegend_TRACE_STATES_TITLE);
155 }
156
157 @Override
158 protected void createButtonsForButtonBar(Composite parent) {
159 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
160 true);
161 }
162
163 class Bar extends Canvas {
164 private final Color color;
165
166 public Bar(Composite parent, RGB rgb) {
167 super(parent, SWT.NONE);
168
169 color = fResourceManager.createColor(rgb);
170 addListener(SWT.Paint, new Listener() {
171 @Override
172 public void handleEvent(Event event) {
173 draw(event.gc);
174 }
175 });
176 }
177
178 private void draw(GC gc) {
179 Rectangle r = getClientArea();
180 gc.setBackground(color);
181 gc.fillRectangle(r);
182 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
183 gc.drawRectangle(0, 0, r.width - 1, r.height - 1);
184 }
185 }
186
187 }
This page took 0.035705 seconds and 5 git commands to generate.