Commit | Line | Data |
---|---|---|
fb5cad3d PT |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2009, 2012 Ericsson.\r | |
013a5f1c | 3 | *\r |
fb5cad3d PT |
4 | * All rights reserved. This program and the accompanying materials are\r |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
013a5f1c | 8 | *\r |
fb5cad3d PT |
9 | * Contributors:\r |
10 | * Alvaro Sanchez-Leon - Initial API and implementation\r | |
11 | * Patrick Tasse - Refactoring\r | |
12 | *******************************************************************************/\r | |
13 | \r | |
14 | package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.dialogs;\r | |
15 | \r | |
16 | import org.eclipse.jface.dialogs.IDialogConstants;\r | |
17 | import org.eclipse.jface.dialogs.TitleAreaDialog;\r | |
b83af2c3 PT |
18 | import org.eclipse.jface.resource.JFaceResources;\r |
19 | import org.eclipse.jface.resource.LocalResourceManager;\r | |
fb5cad3d | 20 | import org.eclipse.linuxtools.internal.tmf.ui.Messages;\r |
b83af2c3 PT |
21 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider;\r |
22 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.StateItem;\r | |
fb5cad3d PT |
23 | import org.eclipse.swt.SWT;\r |
24 | import org.eclipse.swt.graphics.Color;\r | |
25 | import org.eclipse.swt.graphics.GC;\r | |
b83af2c3 | 26 | import org.eclipse.swt.graphics.RGB;\r |
fb5cad3d PT |
27 | import org.eclipse.swt.graphics.Rectangle;\r |
28 | import org.eclipse.swt.layout.GridData;\r | |
29 | import org.eclipse.swt.layout.GridLayout;\r | |
30 | import org.eclipse.swt.widgets.Canvas;\r | |
31 | import org.eclipse.swt.widgets.Composite;\r | |
32 | import org.eclipse.swt.widgets.Control;\r | |
b83af2c3 | 33 | import org.eclipse.swt.widgets.Display;\r |
fb5cad3d PT |
34 | import org.eclipse.swt.widgets.Event;\r |
35 | import org.eclipse.swt.widgets.Group;\r | |
36 | import org.eclipse.swt.widgets.Label;\r | |
37 | import org.eclipse.swt.widgets.Listener;\r | |
38 | import org.eclipse.swt.widgets.Shell;\r | |
39 | \r | |
013a5f1c AM |
40 | /**\r |
41 | * Legend for the colors used in the time graph view\r | |
42 | *\r | |
43 | * @version 1.0\r | |
44 | * @author Alvaro Sanchez-Leon\r | |
45 | * @author Patrick Tasse\r | |
46 | */\r | |
fb5cad3d PT |
47 | public class TimeGraphLegend extends TitleAreaDialog {\r |
48 | \r | |
013a5f1c AM |
49 | private final ITimeGraphPresentationProvider provider;\r |
50 | private final LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources());\r | |
fb5cad3d | 51 | \r |
b83af2c3 PT |
52 | public static void open(Shell parent, ITimeGraphPresentationProvider provider) {\r |
53 | (new TimeGraphLegend(parent, provider)).open();\r | |
fb5cad3d PT |
54 | }\r |
55 | \r | |
b83af2c3 | 56 | public TimeGraphLegend(Shell parent, ITimeGraphPresentationProvider provider) {\r |
fb5cad3d | 57 | super(parent);\r |
b83af2c3 PT |
58 | this.provider = provider;\r |
59 | this.setShellStyle(getShellStyle());\r | |
fb5cad3d PT |
60 | }\r |
61 | \r | |
62 | @Override\r | |
63 | protected Control createDialogArea(Composite parent) {\r | |
64 | Composite dlgArea = (Composite) super.createDialogArea(parent);\r | |
65 | Composite composite = new Composite(dlgArea, SWT.NONE);\r | |
66 | \r | |
67 | GridLayout layout = new GridLayout();\r | |
68 | layout.numColumns = 2;\r | |
69 | composite.setLayout(layout);\r | |
70 | GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);\r | |
71 | composite.setLayoutData(gd);\r | |
72 | \r | |
b83af2c3 | 73 | createStatesGroup(composite);\r |
fb5cad3d | 74 | \r |
b83af2c3 | 75 | setTitle(Messages.TmfTimeLegend_LEGEND);\r |
fb5cad3d PT |
76 | setDialogHelpAvailable(false);\r |
77 | setHelpAvailable(false);\r | |
78 | \r | |
79 | return composite;\r | |
80 | }\r | |
81 | \r | |
b83af2c3 | 82 | private void createStatesGroup(Composite composite) {\r |
fb5cad3d | 83 | Group gs = new Group(composite, SWT.NONE);\r |
05df727a | 84 | gs.setText(provider.getStateTypeName() + " " + Messages.TmfTimeLegend_StateTypeName); //$NON-NLS-1$\r |
fb5cad3d PT |
85 | GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);\r |
86 | gs.setLayoutData(gd);\r | |
87 | \r | |
88 | GridLayout layout = new GridLayout();\r | |
89 | layout.numColumns = 2;\r | |
90 | layout.marginWidth = 20;\r | |
91 | layout.marginBottom = 10;\r | |
92 | gs.setLayout(layout);\r | |
93 | \r | |
013a5f1c | 94 | // Go through all the defined pairs of state color and state name and display them.\r |
b83af2c3 PT |
95 | StateItem[] stateItems = provider.getStateTable();\r |
96 | for (int i = 0; i < stateItems.length; i++) {\r | |
97 | //Get the color related to the index\r | |
98 | RGB rgb = stateItems[i].getStateColor();\r | |
99 | \r | |
fb5cad3d | 100 | //Get the given name, provided by the interface to the application\r |
b83af2c3 PT |
101 | String stateName = stateItems[i].getStateString();\r |
102 | \r | |
103 | // draw color with name\r | |
104 | Bar bar = new Bar(gs, rgb);\r | |
105 | gd = new GridData();\r | |
106 | gd.widthHint = 40;\r | |
107 | gd.heightHint = 20;\r | |
108 | gd.verticalIndent = 8;\r | |
109 | bar.setLayoutData(gd);\r | |
110 | Label name = new Label(gs, SWT.NONE);\r | |
111 | name.setText(stateName);\r | |
112 | gd = new GridData();\r | |
113 | gd.horizontalIndent = 10;\r | |
114 | gd.verticalIndent = 8;\r | |
115 | name.setLayoutData(gd);\r | |
fb5cad3d PT |
116 | }\r |
117 | }\r | |
118 | \r | |
119 | @Override\r | |
120 | protected void configureShell(Shell shell) {\r | |
121 | super.configureShell(shell);\r | |
b83af2c3 | 122 | shell.setText(Messages.TmfTimeLegend_TRACE_STATES_TITLE);\r |
fb5cad3d PT |
123 | }\r |
124 | \r | |
125 | @Override\r | |
126 | protected void createButtonsForButtonBar(Composite parent) {\r | |
127 | createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,\r | |
128 | true);\r | |
129 | }\r | |
130 | \r | |
131 | class Bar extends Canvas {\r | |
013a5f1c | 132 | private final Color color;\r |
fb5cad3d | 133 | \r |
b83af2c3 | 134 | public Bar(Composite parent, RGB rgb) {\r |
fb5cad3d PT |
135 | super(parent, SWT.NONE);\r |
136 | \r | |
b83af2c3 | 137 | color = fResourceManager.createColor(rgb);\r |
fb5cad3d PT |
138 | addListener(SWT.Paint, new Listener() {\r |
139 | @Override\r | |
140 | public void handleEvent(Event event) {\r | |
141 | draw(event.gc);\r | |
142 | }\r | |
143 | });\r | |
144 | }\r | |
145 | \r | |
146 | private void draw(GC gc) {\r | |
147 | Rectangle r = getClientArea();\r | |
148 | gc.setBackground(color);\r | |
149 | gc.fillRectangle(r);\r | |
b83af2c3 | 150 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));\r |
fb5cad3d PT |
151 | gc.drawRectangle(0, 0, r.width - 1, r.height - 1);\r |
152 | }\r | |
fb5cad3d | 153 | \r |
b83af2c3 PT |
154 | @Override\r |
155 | public void dispose() {\r | |
156 | super.dispose();\r | |
157 | color.dispose();\r | |
fb5cad3d PT |
158 | }\r |
159 | \r | |
fb5cad3d PT |
160 | }\r |
161 | \r | |
fb5cad3d | 162 | }\r |