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