bae10b93586e926a5e33e6ca20b87cda1fb5de3e
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / color / ColorComposite.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.ui.color;\r
13 \r
14 import org.eclipse.swt.SWT;\r
15 import org.eclipse.swt.events.MouseEvent;\r
16 import org.eclipse.swt.events.MouseListener;\r
17 import org.eclipse.swt.graphics.Rectangle;\r
18 import org.eclipse.swt.layout.GridData;\r
19 import org.eclipse.swt.layout.GridLayout;\r
20 import org.eclipse.swt.widgets.Canvas;\r
21 import org.eclipse.swt.widgets.Composite;\r
22 import org.eclipse.swt.widgets.Label;\r
23 import org.eclipse.swt.widgets.Text;\r
24 \r
25 /**\r
26  * \r
27  * Widget to edit colors\r
28  * \r
29  * @author Marko Luukkainen\r
30  *\r
31  */\r
32 public class ColorComposite extends Composite{\r
33         \r
34         \r
35         private Color color = new Color(255,255,255);\r
36         private ColorGradient rGradient;\r
37         private ColorGradient gGradient;\r
38         private ColorGradient bGradient;\r
39         private ColorGradient colorGradient;\r
40         \r
41         Text rText;\r
42         Text gText;\r
43         Text bText;\r
44         ColorGradientCanvas rCanvas;\r
45         ColorGradientCanvas gCanvas;\r
46         ColorGradientCanvas bCanvas;\r
47         ColorGradientCanvas colorCanvas;\r
48         \r
49         \r
50         public ColorComposite(Composite parent, int style) {\r
51                 super(parent,style);\r
52                 updateGradients();\r
53                 \r
54                 GridLayout layout = new GridLayout();\r
55                 layout.makeColumnsEqualWidth = false;\r
56                 layout.numColumns = 3;\r
57                 this.setLayout(layout);\r
58                 \r
59                 colorCanvas = new ColorGradientCanvas(this,SWT.BORDER|SWT.HORIZONTAL);\r
60                 GridData colorCanvasData = new GridData();\r
61                 colorCanvasData.horizontalSpan = 3;\r
62                 colorCanvasData.heightHint = 32;\r
63                 colorCanvasData.widthHint = 600;\r
64                 colorCanvasData.grabExcessHorizontalSpace = true;\r
65                 colorCanvas.setLayoutData(colorCanvasData);\r
66                 \r
67                 //layout data used with labels\r
68                 GridData labelData = new GridData();\r
69                 labelData.widthHint = 30;\r
70                 //layout data used with gradients\r
71                 GridData gradientData = new GridData();\r
72                 gradientData.grabExcessHorizontalSpace = true;\r
73                 gradientData.heightHint = 16;\r
74                 gradientData.widthHint = 500;\r
75                 //layout data used with texts\r
76                 GridData textData = new GridData();\r
77                 textData.widthHint = 30;\r
78                 \r
79                 \r
80                 //Composite rComposite = new Composite(this,SWT.NONE);\r
81                 //Layout rLayout = new FillLayout(SWT.HORIZONTAL);\r
82                 //rComposite.setLayout(rLayout);\r
83                 Label rLabel = new Label(this,SWT.NONE);\r
84                 rLabel.setText("Red");\r
85                 rLabel.setLayoutData(labelData);\r
86                 rCanvas = new ColorGradientCanvas(this,SWT.BORDER|SWT.HORIZONTAL);\r
87                 rCanvas.setLayoutData(gradientData);\r
88                 rText = new Text(this,SWT.BORDER);\r
89                 rText.setLayoutData(textData);\r
90                 rCanvas.addMouseListener(new ColorSelectListener(rCanvas) {\r
91                         void setValue(int value) {\r
92                                 Color newColor = new Color(value,color.getG(),color.getB());\r
93                                 updateColor(newColor);\r
94                         };\r
95                 });\r
96                 \r
97                 //Composite gComposite = new Composite(this,SWT.NONE);\r
98                 //Layout gLayout = new FillLayout(SWT.HORIZONTAL);\r
99                 //gComposite.setLayout(gLayout);\r
100                 Label gLabel = new Label(this,SWT.NONE);\r
101                 gLabel.setText("Green");\r
102                 gLabel.setLayoutData(labelData);\r
103                 gCanvas = new ColorGradientCanvas(this,SWT.BORDER|SWT.HORIZONTAL);\r
104                 gCanvas.setLayoutData(gradientData);\r
105                 gText = new Text(this,SWT.BORDER);\r
106                 gText.setLayoutData(textData);\r
107                 gCanvas.addMouseListener(new ColorSelectListener(gCanvas) {\r
108                         void setValue(int value) {\r
109                                 Color newColor = new Color(color.getR(),value,color.getB());\r
110                                 updateColor(newColor);\r
111                         };\r
112                 });\r
113                 \r
114                 //Composite bComposite = new Composite(this,SWT.NONE);\r
115                 //Layout bLayout = new FillLayout(SWT.HORIZONTAL);\r
116                 //bComposite.setLayout(bLayout);\r
117                 Label bLabel = new Label(this,SWT.NONE);\r
118                 bLabel.setText("Blue");\r
119                 bLabel.setLayoutData(labelData);\r
120                 bCanvas = new ColorGradientCanvas(this,SWT.BORDER|SWT.HORIZONTAL);\r
121                 bCanvas.setLayoutData(gradientData);\r
122                 bText = new Text(this,SWT.BORDER);\r
123                 bText.setLayoutData(textData);\r
124                 bCanvas.addMouseListener(new ColorSelectListener(bCanvas) {\r
125                         void setValue(int value) {\r
126                                 Color newColor = new Color(color.getR(),color.getG(),value);\r
127                                 updateColor(newColor);\r
128                         };\r
129                 });\r
130                 updateWidgets();\r
131                 \r
132         }\r
133         \r
134         private abstract class ColorSelectListener implements MouseListener {\r
135                 private Canvas canvas;\r
136                 \r
137                 public ColorSelectListener(Canvas canvas) {\r
138                         this.canvas = canvas;\r
139                 }\r
140                 public void mouseDoubleClick(MouseEvent e) {\r
141                                                 \r
142                 }\r
143                 public void mouseDown(MouseEvent e) {\r
144                         Rectangle r = this.canvas.getBounds();\r
145                         double d = (double)e.x / (double)r.width;\r
146                         int value = (int)(d * 255);\r
147                         if (value < 0)\r
148                                 value = 0;\r
149                         if (value > 255)\r
150                                 value = 255;\r
151                         setValue(value);\r
152                         \r
153                 }\r
154                 public void mouseUp(MouseEvent e) {\r
155                         \r
156                 }\r
157                 \r
158                 abstract void setValue(int value);\r
159         }\r
160         \r
161         private void updateColor(Color color) {\r
162                 this.color = color;\r
163                 updateWidgets();\r
164         }\r
165         \r
166         private void updateGradients() {\r
167                 rGradient = new ColorGradient(new ColorValue[]{\r
168                                         new ColorValue(new Color(0,color.getG(),color.getB()),0.0),\r
169                                         new ColorValue(new Color(255,color.getG(),color.getB()),1.0)});\r
170                 gGradient = new ColorGradient(new ColorValue[]{\r
171                                 new ColorValue(new Color(color.getR(),0,color.getB()),0.0),\r
172                                 new ColorValue(new Color(color.getR(),255,color.getB()),1.0)});\r
173                 bGradient = new ColorGradient(new ColorValue[]{\r
174                                 new ColorValue(new Color(color.getR(),color.getG(),0),0.0),\r
175                                 new ColorValue(new Color(color.getR(),color.getG(),255),1.0)});\r
176                 colorGradient = new ColorGradient(new ColorValue[]{\r
177                                 new ColorValue(new Color(color.getR(),color.getG(),color.getB()),0.0)});\r
178         \r
179         }\r
180         \r
181         private void updateWidgets() {\r
182                 updateGradients();\r
183                 rCanvas.setGradient(rGradient);\r
184                 gCanvas.setGradient(gGradient);\r
185                 bCanvas.setGradient(bGradient);\r
186                 colorCanvas.setGradient(colorGradient);\r
187                 rText.setText(Integer.toString(color.getR()));\r
188                 gText.setText(Integer.toString(color.getG()));\r
189                 bText.setText(Integer.toString(color.getB()));  \r
190         }\r
191 }\r