Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / color / ColorGradientComposite2.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 \r
15 import java.util.ArrayList;\r
16 import java.util.Collections;\r
17 \r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.viewers.ISelectionChangedListener;\r
20 import org.eclipse.jface.viewers.IStructuredSelection;\r
21 import org.eclipse.jface.viewers.SelectionChangedEvent;\r
22 import org.eclipse.jface.viewers.StructuredSelection;\r
23 import org.eclipse.swt.SWT;\r
24 import org.eclipse.swt.events.SelectionEvent;\r
25 import org.eclipse.swt.events.SelectionListener;\r
26 import org.eclipse.swt.graphics.RGB;\r
27 import org.eclipse.swt.layout.FillLayout;\r
28 import org.eclipse.swt.layout.GridLayout;\r
29 import org.eclipse.swt.widgets.Button;\r
30 import org.eclipse.swt.widgets.Composite;\r
31 \r
32 /**\r
33  * Widget to create and edit color gradients\r
34  * \r
35  * @author Marko Luukkainen\r
36  *\r
37  */\r
38 public class ColorGradientComposite2 extends Composite implements ISelectionChangedListener{    \r
39         \r
40         private ColorGradientAdjustingCanvas gradientComposite;\r
41         private Button addButton;\r
42         private Button editButton;\r
43         private Button removeButton;\r
44         private Button rgbButton;\r
45         private Button hsvButton;\r
46         private int type = ColorGradient.RGB;\r
47         \r
48         private ArrayList<ColorValue> values = new ArrayList<ColorValue>();\r
49         \r
50         public ColorGradientComposite2(Composite parent, int style) {\r
51                 super(parent,style);\r
52                 GridLayout layout = new GridLayout(2,false);\r
53             this.setLayout(layout);\r
54             gradientComposite = new ColorGradientAdjustingCanvas(this,SWT.HORIZONTAL);\r
55             \r
56             gradientComposite.addSelectionChangedListener(this);\r
57             \r
58             Composite typeComposite = new Composite(this, SWT.NONE);\r
59             \r
60             typeComposite.setLayout(new GridLayout(1,false));\r
61             rgbButton = new Button(typeComposite,SWT.RADIO);\r
62             rgbButton.setSelection(true);\r
63             rgbButton.addSelectionListener(new SelectionListener() {\r
64                 public void widgetDefaultSelected(SelectionEvent e) {\r
65                         widgetSelected(e);\r
66                 }\r
67                 public void widgetSelected(SelectionEvent e) {\r
68                         rgbButton.setSelection(true);\r
69                         hsvButton.setSelection(false);\r
70                         type = ColorGradient.RGB;\r
71                         gradientComposite.setGradient(new ColorGradient(values,type));\r
72                 }\r
73             });\r
74             rgbButton.setText("RGB");\r
75             hsvButton = new Button(typeComposite,SWT.RADIO);\r
76             hsvButton.addSelectionListener(new SelectionListener() {\r
77                 public void widgetDefaultSelected(SelectionEvent e) {\r
78                         widgetSelected(e);\r
79                 }\r
80                 public void widgetSelected(SelectionEvent e) {\r
81                         hsvButton.setSelection(true);\r
82                         rgbButton.setSelection(false);\r
83                         type = ColorGradient.HSV;\r
84                         gradientComposite.setGradient(new ColorGradient(values,type));\r
85                 }\r
86             });\r
87             hsvButton.setText("HSV"); \r
88             \r
89             Composite buttonComposite = new Composite(this,SWT.NONE);\r
90 \r
91             buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL));\r
92             \r
93             addButton = new Button(buttonComposite,SWT.PUSH);\r
94             addButton.setText("Add new color");\r
95             addButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { \r
96                 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    \r
97                         RGB rgb = openDialog(null);\r
98                 if (rgb != null) {\r
99                     Color c = new Color(rgb.red,rgb.green,rgb.blue);\r
100                     Double value;\r
101                     if (values.size() == 0) {\r
102                         value = 0.0;\r
103                     } else if (values.size() == 1) {\r
104                         value = 100.0;\r
105                     } else {\r
106                         StructuredSelection selection = (StructuredSelection)gradientComposite.getSelection();\r
107                         if (selection.size() == 1) {\r
108                                 // add new color next to the selection\r
109                                 ColorValue v = (ColorValue)selection.getFirstElement();\r
110                                 int index = values.indexOf(v);\r
111                                 if (index == values.size() -1) {\r
112                                         index--;\r
113                                 }\r
114                                 value = (values.get(index+1).getValue()-values.get(index).getValue())*0.5;\r
115                                 value += values.get(index).getValue();\r
116                         } else {\r
117                                 // add new color to largest gap\r
118                                 int index = 0;\r
119                                 double r = 0.0;\r
120                                 for (int i = 0; i < values.size() -1; i++) {\r
121                                         double v1 = values.get(i).getValue();\r
122                                         double v2 = values.get(i+1).getValue();\r
123                                         double vr = v2 -v1;\r
124                                         if (vr > r) {\r
125                                                 r=vr;\r
126                                                 index = i;\r
127                                         }\r
128                                 }\r
129                                 value = values.get(index).getValue() + r *0.5;\r
130                         }\r
131                     }\r
132                     addColor(c,value);\r
133                 }\r
134             }\r
135         });\r
136             editButton = new Button(buttonComposite,SWT.PUSH);\r
137             editButton.setText("Edit color");\r
138             editButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { \r
139                 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    \r
140                 \r
141                         Object obj = ((StructuredSelection)gradientComposite.getSelection()).getFirstElement();\r
142                         if (obj == null)\r
143                                 return;\r
144                         if (obj instanceof ColorValue) {\r
145                                 RGB rgb = openDialog(((ColorValue)obj).getColor().getRgb());\r
146                     if (rgb != null) {\r
147                         modifyColorValueColor((ColorValue)obj,rgb);\r
148                     }\r
149                         }                       \r
150                         \r
151                 }\r
152         });\r
153             editButton.setEnabled(false);\r
154             removeButton = new Button(buttonComposite,SWT.PUSH);\r
155             removeButton.setText("Remove color");\r
156             removeButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { \r
157                 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    \r
158                         Object o = ((StructuredSelection)gradientComposite.getSelection()).getFirstElement();\r
159                         if (o == null)\r
160                                 return;\r
161                         if (o instanceof ColorValue) {\r
162                                 values.remove(o);\r
163                                 updateWidgets();\r
164                         }                       \r
165                         \r
166                 }\r
167         });\r
168             removeButton.setEnabled(false);\r
169             \r
170             GridDataFactory.fillDefaults().span(1, 1).grab(true, false).align(SWT.FILL, SWT.CENTER).hint(SWT.DEFAULT, 32).applyTo(gradientComposite);\r
171             GridDataFactory.fillDefaults().grab(false, true).align(SWT.LEFT, SWT.FILL).applyTo(typeComposite);\r
172             GridDataFactory.fillDefaults().span(1, 1).grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(buttonComposite);  \r
173         }\r
174         \r
175         protected RGB openDialog(RGB selection) {\r
176 //              ColorDialog dialog = new ColorDialog(ColorGradientComposite2.this.getShell(), SWT.NONE);\r
177 //        if (selection != null)\r
178 //              dialog.setRGB(selection);\r
179 //        RGB rgb = dialog.open();\r
180 //        return rgb;\r
181                 org.simantics.utils.ui.color.ColorDialog dialog = new org.simantics.utils.ui.color.ColorDialog(getShell());\r
182                 if (selection != null) {\r
183                         Color color = new Color(selection);\r
184                         dialog.setInitialValue(color);\r
185                 }\r
186                 if (dialog.open() == org.simantics.utils.ui.color.ColorDialog.OK) {\r
187                         Color color = dialog.getColor();\r
188                         return color.getRgb();\r
189                 }\r
190                 return null;\r
191         }\r
192 \r
193         public void addColor(Color color, double value) {\r
194                 addColor(new ColorValue(color,value));\r
195         }\r
196         \r
197         public void addColor(ColorValue value) {\r
198                 values.add(value);\r
199                 updateWidgets();\r
200         }\r
201         \r
202         private void updateWidgets() {\r
203                 Collections.sort(values,new ColorValueComparator());    \r
204                 gradientComposite.setGradient(new ColorGradient(values,type));\r
205         }\r
206 \r
207         public ColorGradient getGradient() {\r
208                 return new ColorGradient(values,type);\r
209         }\r
210 \r
211         public void dispose() {\r
212                 super.dispose();\r
213         }\r
214         \r
215         public void setGradient(ColorGradient gradient) {\r
216                 values.clear();\r
217                 type = gradient.getType();\r
218                 for (ColorValue value : gradient.getColorValues())\r
219                         addColor(value);\r
220                 if (type == ColorGradient.HSV) {\r
221                         rgbButton.setSelection(false);\r
222                         hsvButton.setSelection(true);\r
223                 } else if (type == ColorGradient.RGB) {\r
224                         hsvButton.setSelection(false);\r
225                         rgbButton.setSelection(true);\r
226                 }\r
227                 \r
228         }\r
229         \r
230         private void modifyColorValueColor(ColorValue cValue, RGB rgb) {\r
231                 values.remove(cValue);;\r
232                 Color newColor = new Color(rgb.red,rgb.green,rgb.blue);\r
233                 ColorValue newCValue = new ColorValue(newColor,cValue.getValue());\r
234                 values.add(newCValue);\r
235                 updateWidgets();\r
236         }\r
237         \r
238         /**\r
239          * Enables and disables "Edit color" and "Remove color" buttons depending on selected item\r
240          */\r
241         public void selectionChanged(SelectionChangedEvent event) {\r
242                 Object obj = ((IStructuredSelection)event.getSelection()).getFirstElement();\r
243                 if (obj == null) {\r
244                         editButton.setEnabled(false);\r
245                         removeButton.setEnabled(false);\r
246                 } else {\r
247                         editButton.setEnabled(true);\r
248                         int index = values.indexOf(obj);\r
249                         if (index > 0 && index < values.size() -1)\r
250                                 removeButton.setEnabled(true);\r
251                         else\r
252                                 removeButton.setEnabled(false);\r
253                 }\r
254         }\r
255         \r
256 }\r
257 \r
258         \r