]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/color/ColorCanvas.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / color / ColorCanvas.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 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.PaintEvent;
16 import org.eclipse.swt.events.PaintListener;
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.Rectangle;
20 import org.eclipse.swt.widgets.Canvas;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Display;
23
24 /**
25  * 
26  * Canvas that shows color gradients
27  * 
28  * @author Marko Luukkainen
29  *
30  */
31 public class ColorCanvas extends Canvas{
32         Color color;
33         int style;
34         
35         public ColorCanvas(Composite parent, int style) {
36                 // FIXME : use xor operation to get SWT.HORIZONTAL and SWT.VERTICAL
37                 // out of style when it's passed to parent
38                 super(parent,SWT.BORDER);
39                 this.style = style & (SWT.VERTICAL | SWT.HORIZONTAL) ;
40                 addPaintListener(new PaintListener() {
41                 public void paintControl(PaintEvent e) {
42                         GC gc = e.gc;
43                         Rectangle clip = gc.getClipping();
44                         if (color != null) {
45                                 Image image = ColorIconCreator.createImage(color, clip.width,clip.height);
46                                 gc.drawImage(image, 0, 0);
47                                 image.dispose();                
48                         } else {
49                                 gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
50                                 gc.fillRectangle(clip);
51                         }
52                 }
53             });
54         }
55         
56         public void setColor(Color color) {
57                 this.color = color;
58                 this.redraw();
59         }               
60
61 }