| 1 | /*******************************************************************************\r |
| 2 | * Copyright (c) 2010 Ericsson\r |
| 3 | * \r |
| 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 |
| 8 | * \r |
| 9 | * Contributors:\r |
| 10 | * Patrick Tasse - Initial API and implementation\r |
| 11 | *******************************************************************************/\r |
| 12 | \r |
| 13 | package org.eclipse.linuxtools.tmf.ui.views.colors;\r |
| 14 | \r |
| 15 | import org.eclipse.jface.dialogs.Dialog;\r |
| 16 | import org.eclipse.linuxtools.internal.tmf.ui.Messages;\r |
| 17 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphColorScheme;\r |
| 18 | import org.eclipse.swt.SWT;\r |
| 19 | import org.eclipse.swt.events.MouseAdapter;\r |
| 20 | import org.eclipse.swt.events.MouseEvent;\r |
| 21 | import org.eclipse.swt.events.PaintEvent;\r |
| 22 | import org.eclipse.swt.events.PaintListener;\r |
| 23 | import org.eclipse.swt.graphics.Color;\r |
| 24 | import org.eclipse.swt.graphics.GC;\r |
| 25 | import org.eclipse.swt.graphics.Point;\r |
| 26 | import org.eclipse.swt.graphics.Rectangle;\r |
| 27 | import org.eclipse.swt.layout.GridData;\r |
| 28 | import org.eclipse.swt.layout.GridLayout;\r |
| 29 | import org.eclipse.swt.widgets.Canvas;\r |
| 30 | import org.eclipse.swt.widgets.Composite;\r |
| 31 | import org.eclipse.swt.widgets.Control;\r |
| 32 | import org.eclipse.swt.widgets.Display;\r |
| 33 | import org.eclipse.swt.widgets.Shell;\r |
| 34 | \r |
| 35 | public class TickColorDialog extends Dialog {\r |
| 36 | \r |
| 37 | int selectedIndex = 0;\r |
| 38 | Composite colorComposite;\r |
| 39 | \r |
| 40 | TimeGraphColorScheme traceColorScheme = new TimeGraphColorScheme();\r |
| 41 | \r |
| 42 | protected TickColorDialog(Shell shell) {\r |
| 43 | super(shell);\r |
| 44 | setShellStyle(getShellStyle() | SWT.MAX);\r |
| 45 | }\r |
| 46 | \r |
| 47 | /* (non-Javadoc)\r |
| 48 | * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)\r |
| 49 | */\r |
| 50 | @Override\r |
| 51 | protected Control createDialogArea(Composite parent) {\r |
| 52 | getShell().setText(Messages.TickColorDialog_TickColorDialogTitle);\r |
| 53 | //getShell().setMinimumSize(getShell().computeSize(500, 200));\r |
| 54 | Composite composite = (Composite) super.createDialogArea(parent);\r |
| 55 | colorComposite = new Composite(composite, SWT.NONE);\r |
| 56 | colorComposite.setLayout(new GridLayout(4, false));\r |
| 57 | \r |
| 58 | for (int i = 0; i < 16; i++) {\r |
| 59 | TickColorCanvas tickColorCanvas = new TickColorCanvas(colorComposite, SWT.NONE);\r |
| 60 | tickColorCanvas.setColorIndex(i);\r |
| 61 | }\r |
| 62 | \r |
| 63 | return composite;\r |
| 64 | }\r |
| 65 | \r |
| 66 | public void setColorIndex(int colorIndex) {\r |
| 67 | selectedIndex = colorIndex;\r |
| 68 | }\r |
| 69 | \r |
| 70 | public int getColorIndex() {\r |
| 71 | return selectedIndex;\r |
| 72 | }\r |
| 73 | \r |
| 74 | private class TickColorCanvas extends Canvas {\r |
| 75 | int colorIndex;\r |
| 76 | \r |
| 77 | public TickColorCanvas(Composite parent, int style) {\r |
| 78 | super(parent, style);\r |
| 79 | \r |
| 80 | GridData gd = new GridData(SWT.CENTER, SWT.FILL, true, false);\r |
| 81 | gd.widthHint = 40;\r |
| 82 | gd.heightHint = 25;\r |
| 83 | setLayoutData(gd);\r |
| 84 | setBackground(traceColorScheme.getBkColor(false, false, false));\r |
| 85 | \r |
| 86 | addPaintListener(new PaintListener() {\r |
| 87 | @Override\r |
| 88 | public void paintControl(PaintEvent e) {\r |
| 89 | e.gc.setForeground(traceColorScheme.getColor(TimeGraphColorScheme.MID_LINE));\r |
| 90 | int midy = e.y + e.height / 2;\r |
| 91 | e.gc.drawLine(e.x, midy, e.x + e.width, midy);\r |
| 92 | int midx = e.x + e.width / 2;\r |
| 93 | Rectangle rect = new Rectangle(midx - 10, e.y + 3, 0, e.height - 6);\r |
| 94 | for (int i = 1; i <= 3; i++) {\r |
| 95 | rect.x += i;\r |
| 96 | rect.width = i;\r |
| 97 | e.gc.setBackground(traceColorScheme.getColor(colorIndex));\r |
| 98 | e.gc.fillRectangle(rect);\r |
| 99 | }\r |
| 100 | for (int i = 3; i > 0; i--) {\r |
| 101 | rect.x += i + 2;\r |
| 102 | rect.width = i;\r |
| 103 | e.gc.setBackground(traceColorScheme.getColor(colorIndex));\r |
| 104 | e.gc.fillRectangle(rect);\r |
| 105 | }\r |
| 106 | if (selectedIndex == colorIndex) {\r |
| 107 | Color borderColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);\r |
| 108 | Point p = TickColorCanvas.this.getSize();\r |
| 109 | rect = new Rectangle(0, 0, p.x - 1, p.y - 1);\r |
| 110 | GC gc = e.gc;\r |
| 111 | gc.setForeground(borderColor);\r |
| 112 | gc.drawRectangle(rect);\r |
| 113 | }\r |
| 114 | }});\r |
| 115 | \r |
| 116 | addMouseListener(new MouseAdapter() {\r |
| 117 | @Override\r |
| 118 | public void mouseUp(MouseEvent e) {\r |
| 119 | selectedIndex = colorIndex;\r |
| 120 | colorComposite.redraw(0, 0, colorComposite.getBounds().width, colorComposite.getBounds().height, true);\r |
| 121 | }});\r |
| 122 | }\r |
| 123 | \r |
| 124 | public void setColorIndex(int index) {\r |
| 125 | colorIndex = index;\r |
| 126 | }\r |
| 127 | }\r |
| 128 | \r |
| 129 | \r |
| 130 | }\r |