1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui.color;
15 import java.util.ArrayList;
16 import java.util.Collections;
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;
33 * Widget to create and edit color gradients
35 * @author Marko Luukkainen
38 public class ColorGradientComposite2 extends Composite implements ISelectionChangedListener{
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;
48 private ArrayList<ColorValue> values = new ArrayList<ColorValue>();
50 public ColorGradientComposite2(Composite parent, int style) {
52 GridLayout layout = new GridLayout(2,false);
53 this.setLayout(layout);
54 gradientComposite = new ColorGradientAdjustingCanvas(this,SWT.HORIZONTAL);
56 gradientComposite.addSelectionChangedListener(this);
58 Composite typeComposite = new Composite(this, SWT.NONE);
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) {
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));
74 rgbButton.setText("RGB");
75 hsvButton = new Button(typeComposite,SWT.RADIO);
76 hsvButton.addSelectionListener(new SelectionListener() {
77 public void widgetDefaultSelected(SelectionEvent e) {
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));
87 hsvButton.setText("HSV");
89 Composite buttonComposite = new Composite(this,SWT.NONE);
91 buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
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);
99 Color c = new Color(rgb.red,rgb.green,rgb.blue);
101 if (values.size() == 0) {
103 } else if (values.size() == 1) {
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) {
114 value = (values.get(index+1).getValue()-values.get(index).getValue())*0.5;
115 value += values.get(index).getValue();
117 // add new color to largest gap
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();
129 value = values.get(index).getValue() + r *0.5;
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) {
141 Object obj = ((StructuredSelection)gradientComposite.getSelection()).getFirstElement();
144 if (obj instanceof ColorValue) {
145 RGB rgb = openDialog(((ColorValue)obj).getColor().getRgb());
147 modifyColorValueColor((ColorValue)obj,rgb);
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();
161 if (o instanceof ColorValue) {
168 removeButton.setEnabled(false);
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);
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();
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);
186 if (dialog.open() == org.simantics.utils.ui.color.ColorDialog.OK) {
187 Color color = dialog.getColor();
188 return color.getRgb();
193 public void addColor(Color color, double value) {
194 addColor(new ColorValue(color,value));
197 public void addColor(ColorValue value) {
202 private void updateWidgets() {
203 Collections.sort(values,new ColorValueComparator());
204 gradientComposite.setGradient(new ColorGradient(values,type));
207 public ColorGradient getGradient() {
208 return new ColorGradient(values,type);
211 public void dispose() {
215 public void setGradient(ColorGradient gradient) {
217 type = gradient.getType();
218 for (ColorValue value : gradient.getColorValues())
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);
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);
239 * Enables and disables "Edit color" and "Remove color" buttons depending on selected item
241 public void selectionChanged(SelectionChangedEvent event) {
242 Object obj = ((IStructuredSelection)event.getSelection()).getFirstElement();
244 editButton.setEnabled(false);
245 removeButton.setEnabled(false);
247 editButton.setEnabled(true);
248 int index = values.indexOf(obj);
249 if (index > 0 && index < values.size() -1)
250 removeButton.setEnabled(true);
252 removeButton.setEnabled(false);