import java.util.Collections;\r
import java.util.HashMap;\r
\r
+import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.util.IPropertyChangeListener;\r
import org.eclipse.jface.util.PropertyChangeEvent;\r
import org.eclipse.jface.viewers.CellEditor;\r
import org.eclipse.jface.viewers.IStructuredSelection;\r
import org.eclipse.jface.viewers.ITableLabelProvider;\r
import org.eclipse.jface.viewers.SelectionChangedEvent;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
import org.eclipse.jface.viewers.TableViewer;\r
import org.eclipse.jface.viewers.TextCellEditor;\r
import org.eclipse.jface.viewers.Viewer;\r
import org.eclipse.swt.graphics.Image;\r
import org.eclipse.swt.graphics.RGB;\r
import org.eclipse.swt.layout.FillLayout;\r
-import org.eclipse.swt.layout.FormAttachment;\r
-import org.eclipse.swt.layout.FormData;\r
-import org.eclipse.swt.layout.FormLayout;\r
+import org.eclipse.swt.layout.GridLayout;\r
import org.eclipse.swt.widgets.Button;\r
import org.eclipse.swt.widgets.ColorDialog;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.swt.widgets.Group;\r
-import org.eclipse.swt.widgets.Label;\r
import org.eclipse.swt.widgets.Table;\r
import org.eclipse.swt.widgets.TableColumn;\r
import org.eclipse.swt.widgets.TableItem;\r
\r
\r
private ColorGradientCanvas gradientComposite;\r
- //private Table table;\r
private TableViewer viewer;\r
private Button addButton;\r
private Button editButton;\r
private int type = ColorGradient.RGB;\r
\r
private ArrayList<ColorValue> values = new ArrayList<ColorValue>();\r
- //private ArrayList<Image> images = new ArrayList<Image>();\r
- //private HashMap<TableItem,ColorValue> items = new HashMap<TableItem,ColorValue>();\r
private HashMap<ColorValue,Image> images = new HashMap<ColorValue,Image>();\r
\r
- \r
- \r
public ColorGradientComposite(Composite parent, int style) {\r
super(parent,style);\r
- FormLayout layout = new FormLayout();\r
+ GridLayout layout = new GridLayout(2,false);\r
this.setLayout(layout);\r
gradientComposite = new ColorGradientCanvas(this,SWT.HORIZONTAL);\r
\r
- Group typeComposite = new Group(this,SWT.NONE);\r
- typeComposite.setText("Color interpolation type");\r
+ \r
+ \r
+// Group typeComposite = new Group(this,SWT.NONE);\r
+// typeComposite.setText("Interpolation");\r
+ Composite typeComposite = new Composite(this, SWT.NONE);\r
\r
- typeComposite.setLayout(new FillLayout(SWT.HORIZONTAL));\r
- Label rgbLabel = new Label(typeComposite,SWT.NONE);\r
- rgbLabel.setText("RGB");\r
+ typeComposite.setLayout(new GridLayout(1,false));\r
rgbButton = new Button(typeComposite,SWT.RADIO);\r
rgbButton.setSelection(true);\r
rgbButton.addSelectionListener(new SelectionListener() {\r
gradientComposite.setGradient(new ColorGradient(values,type));\r
}\r
});\r
- Label hsvLabel = new Label(typeComposite,SWT.NONE);\r
- hsvLabel.setText("HSV");\r
+ rgbButton.setText("RGB");\r
hsvButton = new Button(typeComposite,SWT.RADIO);\r
hsvButton.addSelectionListener(new SelectionListener() {\r
public void widgetDefaultSelected(SelectionEvent e) {\r
gradientComposite.setGradient(new ColorGradient(values,type));\r
}\r
});\r
+ hsvButton.setText("HSV");\r
+ \r
viewer = new TableViewer(this,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
- //table = new Table(this,SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);\r
final Table table = viewer.getTable();\r
+ \r
table.setLinesVisible(true);\r
table.setHeaderVisible(true);\r
\r
\r
TableColumn column = new TableColumn(table,SWT.RIGHT);\r
column.setText("Color");\r
- column.setWidth(40);\r
+ column.setWidth(60);\r
column.setMoveable(false);\r
column.setResizable(false);\r
column = new TableColumn(table,SWT.LEFT);\r
\r
viewer.setCellModifier(new TableCellEditorModifier()); \r
\r
- \r
Composite buttonComposite = new Composite(this,SWT.NONE);\r
- \r
- \r
- buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL));\r
+\r
+ buttonComposite.setLayout(new FillLayout(SWT.VERTICAL));\r
\r
addButton = new Button(buttonComposite,SWT.PUSH);\r
addButton.setText("Add new color");\r
} else if (values.size() == 1) {\r
value = 100.0;\r
} else {\r
- // FIXME : insert some logic here\r
- value = 100.0 * Math.random();\r
+ StructuredSelection selection = (StructuredSelection)viewer.getSelection();\r
+ if (selection.size() == 1) {\r
+ // add new color next to the selection\r
+ ColorValue v = (ColorValue)selection.getFirstElement();\r
+ int index = values.indexOf(v);\r
+ if (index == values.size() -1) {\r
+ index--;\r
+ }\r
+ value = (values.get(index+1).getValue()-values.get(index).getValue())*0.5;\r
+ value += values.get(index).getValue();\r
+ } else {\r
+ // add new color to largest gap\r
+ int index = 0;\r
+ double r = 0.0;\r
+ for (int i = 0; i < values.size() -1; i++) {\r
+ double v1 = values.get(i).getValue();\r
+ double v2 = values.get(i+1).getValue();\r
+ double vr = v2 -v1;\r
+ if (vr > r) {\r
+ r=vr;\r
+ index = i;\r
+ }\r
+ }\r
+ value = values.get(index).getValue() + r *0.5;\r
+ }\r
}\r
- \r
addColor(c,value);\r
}\r
}\r
});\r
removeButton.setEnabled(false);\r
\r
- FormData gradientData = new FormData();\r
- gradientData.top = new FormAttachment(0,0);\r
- gradientData.left = new FormAttachment(0,0);\r
- gradientData.right = new FormAttachment(100,0);\r
- gradientData.height = 40;\r
- gradientComposite.setLayoutData(gradientData);\r
- \r
- FormData typeData = new FormData(); \r
- typeData.left = new FormAttachment(0,0);\r
- typeData.right = new FormAttachment(100,0);\r
- typeData.top = new FormAttachment(gradientComposite,0,SWT.BOTTOM);\r
- typeData.height = 20;\r
- typeComposite.setLayoutData(typeData);\r
- \r
- FormData buttonData = new FormData(); \r
- buttonData.left = new FormAttachment(0,0);\r
- buttonData.right = new FormAttachment(100,0);\r
- buttonData.bottom = new FormAttachment(100,0);\r
- buttonData.height = 30;\r
- buttonComposite.setLayoutData(buttonData);\r
+ GridDataFactory.fillDefaults().span(1, 1).grab(true, false).align(SWT.FILL, SWT.CENTER).hint(SWT.DEFAULT, 32).applyTo(gradientComposite);\r
+ GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).hint(SWT.DEFAULT, 100).applyTo(table);\r
+ GridDataFactory.fillDefaults().grab(false, true).align(SWT.LEFT, SWT.FILL).applyTo(typeComposite);\r
+ GridDataFactory.fillDefaults().span(1, 1).grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(buttonComposite);\r
\r
- FormData viewerData = new FormData(); \r
- viewerData.top = new FormAttachment(typeComposite,0,SWT.BOTTOM);\r
- viewerData.left = new FormAttachment(0,0);\r
- viewerData.right = new FormAttachment(100,0);\r
- viewerData.bottom = new FormAttachment(buttonComposite,0,SWT.TOP);\r
- table.setLayoutData(viewerData);\r
\r
- \r
- }\r
- /*\r
- private void addColorValueToTable(ColorValue value) {\r
- TableItem item = new TableItem(table,SWT.NONE);\r
- item.setText(Double.toString(value.getValue()));\r
- Image image = ColorIconCreator.createImage(value.getColor(), 14);\r
- item.setImage(image);\r
- images.add(image);\r
- items.put(item,value);\r
- }\r
- */\r
- /*\r
- private void sortItems() {\r
- for (Image i : images)\r
- i.dispose();\r
- for (TableItem item : table.getItems())\r
- if(!item.isDisposed())\r
- item.dispose();\r
- items.clear();\r
- Collections.sort(values,new ColorValueComparator());\r
- for (ColorValue value : values) {\r
- addColorValueToTable(value);\r
- }\r
}\r
- */\r
+\r
public void addColor(Color color, double value) {\r
addColor(new ColorValue(color,value));\r
}\r
\r
public void addColor(ColorValue value) {\r
values.add(value);\r
- Image image = ColorIconCreator.createImage(value.getColor(), ICON_WIDTH, ICON_HEIGHT);\r
+ Image image = ColorIconCreator.createImage(value.getColor(), ICON_WIDTH, ICON_HEIGHT, SWT.BORDER);\r
images.put(value,image);\r
updateWidgets();\r
//viewer.refresh(true);\r
viewer.refresh();\r
\r
}\r
- /*\r
- public void addColor(ColorValue value) {\r
- values.add(value);\r
- sortItems();\r
- // sortItem creates new menuitems, which makes selectedItem disposed\r
- for (TableItem item : items.keySet() ) {\r
- if (items.get(item) == value) {\r
- gradientComposite.setGradient(new ColorGradient(values,type));\r
- return;\r
- }\r
- \r
- }\r
- gradientComposite.setGradient(new ColorGradient(values,type));\r
- }\r
- */\r
+\r
public ColorGradient getGradient() {\r
return new ColorGradient(values,type);\r
}\r
- /*\r
- public void dispose() {\r
- for (Image i : images)\r
- i.dispose();\r
- if (!table.isDisposed()) {\r
- for (TableItem item : table.getItems())\r
- if(!item.isDisposed())\r
- item.dispose();\r
- table.dispose();\r
- }\r
- }\r
- */\r
+\r
public void dispose() {\r
for (Image i : images.values())\r
i.dispose();\r
type = gradient.getType();\r
for (ColorValue value : gradient.getColorValues())\r
addColor(value);\r
+ if (type == ColorGradient.HSV) {\r
+ rgbButton.setSelection(false);\r
+ hsvButton.setSelection(true);\r
+ } else if (type == ColorGradient.RGB) {\r
+ hsvButton.setSelection(false);\r
+ rgbButton.setSelection(true);\r
+ }\r
\r
}\r
\r
- \r
-// private org.eclipse.swt.graphics.Color getSWTColor(Color color){\r
-// org.eclipse.swt.graphics.Color swtColor = new org.eclipse.swt.graphics.Color(Display.getCurrent(), color.getR(), color.getG(), color.getB()); \r
-// return swtColor;\r
-// }\r
- \r
private RGB getRGB(Color color) {\r
RGB rgb = new RGB(color.getR(),color.getG(),color.getB());\r
return rgb;\r
Color newColor = new Color(rgb.red,rgb.green,rgb.blue);\r
ColorValue newCValue = new ColorValue(newColor,cValue.getValue());\r
values.add(newCValue);\r
- Image newImage = ColorIconCreator.createImage(newColor, ICON_WIDTH, ICON_HEIGHT);\r
+ Image newImage = ColorIconCreator.createImage(newColor, ICON_WIDTH, ICON_HEIGHT, SWT.BORDER);\r
images.put(newCValue,newImage);\r
updateWidgets();\r
}\r