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