Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / color / ColorGradientComposite.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 import java.util.HashMap;\r
18 \r
19 import org.eclipse.jface.layout.GridDataFactory;\r
20 import org.eclipse.jface.util.IPropertyChangeListener;\r
21 import org.eclipse.jface.util.PropertyChangeEvent;\r
22 import org.eclipse.jface.viewers.CellEditor;\r
23 import org.eclipse.jface.viewers.ColorCellEditor;\r
24 import org.eclipse.jface.viewers.ICellEditorListener;\r
25 import org.eclipse.jface.viewers.ICellModifier;\r
26 import org.eclipse.jface.viewers.ILabelProviderListener;\r
27 import org.eclipse.jface.viewers.ISelectionChangedListener;\r
28 import org.eclipse.jface.viewers.IStructuredContentProvider;\r
29 import org.eclipse.jface.viewers.IStructuredSelection;\r
30 import org.eclipse.jface.viewers.ITableLabelProvider;\r
31 import org.eclipse.jface.viewers.SelectionChangedEvent;\r
32 import org.eclipse.jface.viewers.StructuredSelection;\r
33 import org.eclipse.jface.viewers.TableViewer;\r
34 import org.eclipse.jface.viewers.TextCellEditor;\r
35 import org.eclipse.jface.viewers.Viewer;\r
36 import org.eclipse.swt.SWT;\r
37 import org.eclipse.swt.events.SelectionEvent;\r
38 import org.eclipse.swt.events.SelectionListener;\r
39 import org.eclipse.swt.graphics.Image;\r
40 import org.eclipse.swt.graphics.RGB;\r
41 import org.eclipse.swt.layout.FillLayout;\r
42 import org.eclipse.swt.layout.GridLayout;\r
43 import org.eclipse.swt.widgets.Button;\r
44 import org.eclipse.swt.widgets.ColorDialog;\r
45 import org.eclipse.swt.widgets.Composite;\r
46 import org.eclipse.swt.widgets.Table;\r
47 import org.eclipse.swt.widgets.TableColumn;\r
48 import org.eclipse.swt.widgets.TableItem;\r
49 \r
50 /**\r
51  * Widget to create and edit color gradients\r
52  * \r
53  * @author Marko Luukkainen\r
54  *\r
55  */\r
56 public class ColorGradientComposite extends Composite implements ISelectionChangedListener, ICellEditorListener, IPropertyChangeListener{\r
57         private static int ICON_WIDTH = 36;\r
58         private static int ICON_HEIGHT = 14;\r
59         \r
60         private static final String COLOR_ID = "Color";\r
61         private static final String VALUE_ID = "Value";\r
62         \r
63         \r
64         private ColorGradientCanvas gradientComposite;\r
65         private TableViewer viewer;\r
66         private Button addButton;\r
67         private Button editButton;\r
68         private Button removeButton;\r
69         private Button rgbButton;\r
70         private Button hsvButton;\r
71         private int type = ColorGradient.RGB;\r
72         \r
73         private ArrayList<ColorValue> values = new ArrayList<ColorValue>();\r
74         private HashMap<ColorValue,Image> images = new HashMap<ColorValue,Image>();\r
75         \r
76         public ColorGradientComposite(Composite parent, int style) {\r
77                 super(parent,style);\r
78                 GridLayout layout = new GridLayout(2,false);\r
79             this.setLayout(layout);\r
80             gradientComposite = new ColorGradientCanvas(this,SWT.HORIZONTAL);\r
81             \r
82            \r
83             \r
84 //          Group typeComposite = new Group(this,SWT.NONE);\r
85 //          typeComposite.setText("Interpolation");\r
86             Composite typeComposite = new Composite(this, SWT.NONE);\r
87             \r
88             typeComposite.setLayout(new GridLayout(1,false));\r
89             rgbButton = new Button(typeComposite,SWT.RADIO);\r
90             rgbButton.setSelection(true);\r
91             rgbButton.addSelectionListener(new SelectionListener() {\r
92                 public void widgetDefaultSelected(SelectionEvent e) {\r
93                         widgetSelected(e);\r
94                 }\r
95                 public void widgetSelected(SelectionEvent e) {\r
96                         rgbButton.setSelection(true);\r
97                         hsvButton.setSelection(false);\r
98                         type = ColorGradient.RGB;\r
99                         gradientComposite.setGradient(new ColorGradient(values,type));\r
100                 }\r
101             });\r
102             rgbButton.setText("RGB");\r
103             hsvButton = new Button(typeComposite,SWT.RADIO);\r
104             hsvButton.addSelectionListener(new SelectionListener() {\r
105                 public void widgetDefaultSelected(SelectionEvent e) {\r
106                         widgetSelected(e);\r
107                 }\r
108                 public void widgetSelected(SelectionEvent e) {\r
109                         hsvButton.setSelection(true);\r
110                         rgbButton.setSelection(false);\r
111                         type = ColorGradient.HSV;\r
112                         gradientComposite.setGradient(new ColorGradient(values,type));\r
113                 }\r
114             });\r
115             hsvButton.setText("HSV");\r
116            \r
117             viewer = new TableViewer(this,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
118             final Table table = viewer.getTable();\r
119             \r
120             table.setLinesVisible(true);\r
121             table.setHeaderVisible(true);\r
122             \r
123             viewer.addSelectionChangedListener(this);\r
124             viewer.addPostSelectionChangedListener(this);\r
125             \r
126             TableColumn column = new TableColumn(table,SWT.RIGHT);\r
127             column.setText("Color");\r
128             column.setWidth(60);\r
129             column.setMoveable(false);\r
130             column.setResizable(false);\r
131             column = new TableColumn(table,SWT.LEFT);\r
132             column.setText("Value");\r
133             column.setWidth(300);\r
134             column.setMoveable(false);\r
135             \r
136             viewer.setColumnProperties(new String[]{COLOR_ID,VALUE_ID});\r
137             \r
138             viewer.setLabelProvider(new TableLabelProvider());\r
139             viewer.setContentProvider(new TableContentProvider());\r
140             viewer.setInput(values);\r
141             \r
142             final ColorCellEditor colorEditor = new ColorCellEditor(table);\r
143             final TextCellEditor valueEditor = new TextCellEditor(table);\r
144             colorEditor.addListener(this);\r
145             valueEditor.addListener(this);\r
146             valueEditor.addPropertyChangeListener(this);\r
147             viewer.setCellEditors(new CellEditor[]{colorEditor,valueEditor});\r
148             \r
149             viewer.setCellModifier(new TableCellEditorModifier());    \r
150             \r
151             Composite buttonComposite = new Composite(this,SWT.NONE);\r
152 \r
153             buttonComposite.setLayout(new FillLayout(SWT.VERTICAL));\r
154             \r
155             addButton = new Button(buttonComposite,SWT.PUSH);\r
156             addButton.setText("Add new color");\r
157             addButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { \r
158                 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    \r
159                         ColorDialog dialog = new ColorDialog(ColorGradientComposite.this.getShell(), SWT.NONE);\r
160                 \r
161                 RGB rgb = dialog.open();\r
162                 if (rgb != null) {\r
163                     Color c = new Color(rgb.red,rgb.green,rgb.blue);\r
164                     Double value;\r
165                     if (values.size() == 0) {\r
166                         value = 0.0;\r
167                     } else if (values.size() == 1) {\r
168                         value = 100.0;\r
169                     } else {\r
170                         StructuredSelection selection = (StructuredSelection)viewer.getSelection();\r
171                         if (selection.size() == 1) {\r
172                                 // add new color next to the selection\r
173                                 ColorValue v = (ColorValue)selection.getFirstElement();\r
174                                 int index = values.indexOf(v);\r
175                                 if (index == values.size() -1) {\r
176                                         index--;\r
177                                 }\r
178                                 value = (values.get(index+1).getValue()-values.get(index).getValue())*0.5;\r
179                                 value += values.get(index).getValue();\r
180                         } else {\r
181                                 // add new color to largest gap\r
182                                 int index = 0;\r
183                                 double r = 0.0;\r
184                                 for (int i = 0; i < values.size() -1; i++) {\r
185                                         double v1 = values.get(i).getValue();\r
186                                         double v2 = values.get(i+1).getValue();\r
187                                         double vr = v2 -v1;\r
188                                         if (vr > r) {\r
189                                                 r=vr;\r
190                                                 index = i;\r
191                                         }\r
192                                 }\r
193                                 value = values.get(index).getValue() + r *0.5;\r
194                         }\r
195                     }\r
196                     addColor(c,value);\r
197                 }\r
198             }\r
199         });\r
200             editButton = new Button(buttonComposite,SWT.PUSH);\r
201             editButton.setText("Edit color");\r
202             editButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { \r
203                 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    \r
204                         TableItem selection[] = table.getSelection();\r
205                         if (selection.length > 0) {\r
206                                 TableItem selected = selection[0];\r
207                                 Object obj = selected.getData();\r
208                                 if (obj instanceof ColorValue) {\r
209                                         ColorDialog dialog = new ColorDialog(ColorGradientComposite.this.getShell(), SWT.NONE);\r
210                                         dialog.setRGB(getRGB(((ColorValue)obj).getColor()));\r
211                                         RGB rgb = dialog.open();\r
212                         if (rgb != null) {\r
213                                 modifyColorValueColor((ColorValue)obj,rgb);\r
214                         }\r
215                                 }                       \r
216                         }\r
217                 }\r
218         });\r
219             editButton.setEnabled(false);\r
220             removeButton = new Button(buttonComposite,SWT.PUSH);\r
221             removeButton.setText("Remove color");\r
222             removeButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { \r
223                 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    \r
224                         TableItem selection[] = table.getSelection();\r
225                         if (selection.length > 0) {\r
226                                 TableItem selected = selection[0];\r
227                                 Object o = selected.getData();\r
228                                 if (o instanceof ColorValue) {\r
229                                         Image i = images.get(o);\r
230                                         i.dispose();\r
231                                         images.remove(o);\r
232                                         values.remove(o);\r
233                                         updateWidgets();\r
234                                 }                       \r
235                         }\r
236                 }\r
237         });\r
238             removeButton.setEnabled(false);\r
239             \r
240             GridDataFactory.fillDefaults().span(1, 1).grab(true, false).align(SWT.FILL, SWT.CENTER).hint(SWT.DEFAULT, 32).applyTo(gradientComposite);\r
241             GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).hint(SWT.DEFAULT, 100).applyTo(table);\r
242             GridDataFactory.fillDefaults().grab(false, true).align(SWT.LEFT, SWT.FILL).applyTo(typeComposite);\r
243             GridDataFactory.fillDefaults().span(1, 1).grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(buttonComposite);\r
244             \r
245             \r
246         }\r
247 \r
248         public void addColor(Color color, double value) {\r
249                 addColor(new ColorValue(color,value));\r
250         }\r
251         \r
252         public void addColor(ColorValue value) {\r
253                 values.add(value);\r
254                 Image image = ColorIconCreator.createImage(value.getColor(), ICON_WIDTH, ICON_HEIGHT, SWT.BORDER);\r
255                 images.put(value,image);\r
256                 updateWidgets();\r
257                 //viewer.refresh(true);\r
258         }\r
259         \r
260         private void updateWidgets() {\r
261                 Collections.sort(values,new ColorValueComparator());    \r
262                 gradientComposite.setGradient(new ColorGradient(values,type));\r
263                 viewer.refresh();\r
264                 \r
265         }\r
266 \r
267         public ColorGradient getGradient() {\r
268                 return new ColorGradient(values,type);\r
269         }\r
270 \r
271         public void dispose() {\r
272                 for (Image i : images.values())\r
273                         i.dispose();\r
274                 super.dispose();\r
275         }\r
276         \r
277         public void setGradient(ColorGradient gradient) {\r
278                 for (Image i : images.values())\r
279                         i.dispose();\r
280                 values.clear();\r
281                 images.clear();\r
282                 type = gradient.getType();\r
283                 for (ColorValue value : gradient.getColorValues())\r
284                         addColor(value);\r
285                 if (type == ColorGradient.HSV) {\r
286                         rgbButton.setSelection(false);\r
287                         hsvButton.setSelection(true);\r
288                 } else if (type == ColorGradient.RGB) {\r
289                         hsvButton.setSelection(false);\r
290                         rgbButton.setSelection(true);\r
291                 }\r
292                 \r
293         }\r
294         \r
295         private RGB getRGB(Color color) {\r
296                 RGB rgb = new RGB(color.getR(),color.getG(),color.getB());\r
297                 return rgb;\r
298         }\r
299         \r
300         private void modifyColorValueValue(ColorValue cValue, String string) {\r
301                 try {\r
302                         double d = Double.parseDouble(string);\r
303                         values.remove(cValue);\r
304                         Image image = images.get(cValue);\r
305                         images.remove(cValue);\r
306                         ColorValue newCValue = new ColorValue(cValue.getColor(),d);\r
307                         values.add(newCValue);\r
308                         images.put(newCValue,image);\r
309                         updateWidgets();\r
310                         \r
311                 } catch (NumberFormatException e) {\r
312                         return;\r
313                 }\r
314         }\r
315         \r
316         private void modifyColorValueColor(ColorValue cValue, RGB rgb) {\r
317                 values.remove(cValue);\r
318                 Image oldImage = images.get(cValue);\r
319                 oldImage.dispose();\r
320                 images.remove(cValue);\r
321                 \r
322                 Color newColor = new Color(rgb.red,rgb.green,rgb.blue);\r
323                 ColorValue newCValue = new ColorValue(newColor,cValue.getValue());\r
324                 values.add(newCValue);\r
325                 Image newImage = ColorIconCreator.createImage(newColor, ICON_WIDTH, ICON_HEIGHT, SWT.BORDER);\r
326                 images.put(newCValue,newImage);\r
327                 updateWidgets();\r
328         }\r
329         \r
330         /**\r
331          * Enables and disables "Edit color" and "Remove color" buttons depending on selected item\r
332          */\r
333         public void selectionChanged(SelectionChangedEvent event) {\r
334                 Object obj = ((IStructuredSelection)event.getSelection()).getFirstElement();\r
335                 if (obj == null) {\r
336                         editButton.setEnabled(false);\r
337                         removeButton.setEnabled(false);\r
338                 } else {\r
339                         editButton.setEnabled(true);\r
340                         removeButton.setEnabled(true);\r
341                 }\r
342         }\r
343         \r
344         /*\r
345          * FIXME :\r
346          * If item is opened for editing selectionChanged won't get null selection and\r
347          * "Edit" & "Remove" buttons are enabled when there's no valid selection\r
348          * \r
349          * There seems to be no way to get event when cellEditor is activated...\r
350          */\r
351         \r
352         public void applyEditorValue() {\r
353                 // TODO Auto-generated method stub\r
354                 editButton.setEnabled(false);\r
355                 removeButton.setEnabled(false); \r
356         }\r
357         \r
358         public void cancelEditor() {\r
359                 // TODO Auto-generated method stub\r
360                 editButton.setEnabled(false);\r
361                 removeButton.setEnabled(false); \r
362         }\r
363         \r
364         public void editorValueChanged(boolean oldValidState, boolean newValidState) {\r
365                 // TODO Auto-generated method stub\r
366                 editButton.setEnabled(false);\r
367                 removeButton.setEnabled(false); \r
368         }\r
369         \r
370         public void propertyChange(PropertyChangeEvent event) {\r
371                 editButton.setEnabled(false);\r
372                 removeButton.setEnabled(false); \r
373                 \r
374         }\r
375         /**\r
376          * @author Marko Luukkainen\r
377          *\r
378          */\r
379         private class TableLabelProvider implements ITableLabelProvider {\r
380                 private ArrayList<ILabelProviderListener> listeners = new ArrayList<ILabelProviderListener>();\r
381                                 \r
382                 /* (non-Javadoc)\r
383                  * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()\r
384                  */\r
385                 public void dispose() {\r
386                                                 \r
387                 }\r
388                 /* (non-Javadoc)\r
389                  * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)\r
390                  */\r
391                 public Image getColumnImage(Object element, int columnIndex) {\r
392                         //System.out.println("TableLabelProvider.getColumnImage() " + element + " , " + columnIndex);\r
393                         ColorValue value = (ColorValue)element;\r
394                         if (columnIndex == 0)\r
395                                 return images.get(value);\r
396                         return null;\r
397                 }\r
398                 /* (non-Javadoc)\r
399                  * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)\r
400                  */\r
401                 public String getColumnText(Object element, int columnIndex) {\r
402                         //System.out.println("TableLabelProvider.getColumnText() " + element + " , " + columnIndex);\r
403                         ColorValue value = (ColorValue)element;\r
404                         if (columnIndex == 1)\r
405                                 return Double.toString(value.getValue());\r
406                         return null;\r
407                 }\r
408                 /* (non-Javadoc)\r
409                  * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)\r
410                  */\r
411                 public boolean isLabelProperty(Object element, String property) {\r
412                         return true;\r
413                 }\r
414                 /* (non-Javadoc)\r
415                  * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)\r
416                  */\r
417                 public void addListener(ILabelProviderListener listener) {\r
418                         if(!listeners.contains(listener))\r
419                                 listeners.add(listener);                        \r
420                 }\r
421                 /* (non-Javadoc)\r
422                  * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)\r
423                  */\r
424                 public void removeListener(ILabelProviderListener listener) {\r
425                         listeners.remove(listener);\r
426                 }\r
427                 \r
428                 \r
429         }\r
430         \r
431         /**\r
432          * @author Marko Luukkainen\r
433          *\r
434          */\r
435         private class TableContentProvider implements IStructuredContentProvider {\r
436                 public void dispose() {\r
437                         \r
438                 }\r
439                 public Object[] getElements(Object inputElement) {\r
440                         ColorValue valueArray[] = new ColorValue[values.size()];\r
441                         // values.toArray(valueArray);\r
442             // to get correct ordering (smallest last)\r
443             // we must reverse the order of the list\r
444             for (int i = 0; i < values.size(); i++) {\r
445                             valueArray[i] = values.get(values.size() - 1 -i);\r
446             }\r
447             //System.out.println("TableContentProvider.getElements() : array size " + valueArray.length);\r
448                         return valueArray;\r
449                 }\r
450                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {\r
451                         //System.out.println("TableContentProvider.inputChanged()");\r
452                         \r
453                 }\r
454         }\r
455         \r
456         /**\r
457          * @author Marko Luukkainen\r
458          *\r
459          */\r
460         private class TableCellEditorModifier implements ICellModifier {\r
461                 public boolean canModify(Object element, String property) {\r
462                         //System.out.println("TableCellEditorModifier.canModify()" + element + " , " + property);\r
463                         if (property == VALUE_ID)\r
464                                 return true;\r
465                         // this is commented out because it's hard so select the row when both rows have editors\r
466                         // now the color can be changed with "Edit Color" button\r
467                         //if (property == COLOR_ID)\r
468                         //      return true;\r
469                         return false;\r
470                 }\r
471                 public Object getValue(Object element, String property) {\r
472                         //System.out.println("TableCellEditorModifier.getValue()" + element + " , " + property);\r
473                         if (property == VALUE_ID)\r
474                                 return Double.toString(((ColorValue)element).getValue());\r
475                         if (property == COLOR_ID) \r
476                                 return getRGB(((ColorValue)element).getColor());\r
477                         return null;\r
478                         \r
479                 }\r
480                 public void modify(Object element, String property, Object value) {\r
481                         TableItem item = (TableItem)element;\r
482                         Object obj = item.getData();\r
483                         //System.out.println("TableCellEditorModifier.modify()" + element + " , " + property + " , " + value);\r
484                         if (property == VALUE_ID)\r
485                                 modifyColorValueValue((ColorValue)obj,(String)value);\r
486                         if (property == COLOR_ID)\r
487                                 modifyColorValueColor((ColorValue)obj,(RGB)value);\r
488                 }\r
489                 \r
490         }\r
491 }\r
492 \r
493         \r