tmf: Update Javadoc throughout tmf.ui
[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 public static void open(Shell parent, ITimeGraphPresentationProvider provider) {
53 (new TimeGraphLegend(parent, provider)).open();
54 }
55
56 public TimeGraphLegend(Shell parent, ITimeGraphPresentationProvider provider) {
57 super(parent);
58 this.provider = provider;
59 this.setShellStyle(getShellStyle());
60 }
61
62 @Override
63 protected Control createDialogArea(Composite parent) {
64 Composite dlgArea = (Composite) super.createDialogArea(parent);
65 Composite composite = new Composite(dlgArea, SWT.NONE);
66
67 GridLayout layout = new GridLayout();
68 layout.numColumns = 2;
69 composite.setLayout(layout);
70 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
71 composite.setLayoutData(gd);
72
73 createStatesGroup(composite);
74
75 setTitle(Messages.TmfTimeLegend_LEGEND);
76 setDialogHelpAvailable(false);
77 setHelpAvailable(false);
78
79 return composite;
80 }
81
82 private void createStatesGroup(Composite composite) {
83 Group gs = new Group(composite, SWT.NONE);
84 gs.setText(provider.getStateTypeName() + " " + Messages.TmfTimeLegend_StateTypeName); //$NON-NLS-1$
85 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
86 gs.setLayoutData(gd);
87
88 GridLayout layout = new GridLayout();
89 layout.numColumns = 2;
90 layout.marginWidth = 20;
91 layout.marginBottom = 10;
92 gs.setLayout(layout);
93
94 // Go through all the defined pairs of state color and state name and display them.
95 StateItem[] stateItems = provider.getStateTable();
96 for (int i = 0; i < stateItems.length; i++) {
97 //Get the color related to the index
98 RGB rgb = stateItems[i].getStateColor();
99
100 //Get the given name, provided by the interface to the application
101 String stateName = stateItems[i].getStateString();
102
103 // draw color with name
104 Bar bar = new Bar(gs, rgb);
105 gd = new GridData();
106 gd.widthHint = 40;
107 gd.heightHint = 20;
108 gd.verticalIndent = 8;
109 bar.setLayoutData(gd);
110 Label name = new Label(gs, SWT.NONE);
111 name.setText(stateName);
112 gd = new GridData();
113 gd.horizontalIndent = 10;
114 gd.verticalIndent = 8;
115 name.setLayoutData(gd);
116 }
117 }
118
119 @Override
120 protected void configureShell(Shell shell) {
121 super.configureShell(shell);
122 shell.setText(Messages.TmfTimeLegend_TRACE_STATES_TITLE);
123 }
124
125 @Override
126 protected void createButtonsForButtonBar(Composite parent) {
127 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
128 true);
129 }
130
131 class Bar extends Canvas {
132 private final Color color;
133
134 public Bar(Composite parent, RGB rgb) {
135 super(parent, SWT.NONE);
136
137 color = fResourceManager.createColor(rgb);
138 addListener(SWT.Paint, new Listener() {
139 @Override
140 public void handleEvent(Event event) {
141 draw(event.gc);
142 }
143 });
144 }
145
146 private void draw(GC gc) {
147 Rectangle r = getClientArea();
148 gc.setBackground(color);
149 gc.fillRectangle(r);
150 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
151 gc.drawRectangle(0, 0, r.width - 1, r.height - 1);
152 }
153
154 @Override
155 public void dispose() {
156 super.dispose();
157 color.dispose();
158 }
159
160 }
161
162 }
This page took 0.040388 seconds and 5 git commands to generate.