]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/color/ColorGradientCanvas.java
11ba7642b6240b9883a968d02c31d62ad68fac22
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / color / ColorGradientCanvas.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 ColorGradientCanvas extends Canvas{
32         ColorGradient gradient;
33         int style;
34         
35         public ColorGradientCanvas(Composite parent, int style) {
36                 super(parent,(style|SWT.BORDER)&(~(SWT.VERTICAL|SWT.HORIZONTAL)));
37                 this.style = style & (SWT.VERTICAL | SWT.HORIZONTAL) ;
38                 addPaintListener(createPaintListener());
39         }
40         
41         public void setGradient(ColorGradient gradient) {
42                 this.gradient = gradient;
43                 this.redraw();
44         }       
45         
46         protected PaintListener createPaintListener() {
47                 return new PaintListener() {
48                 public void paintControl(PaintEvent e) {
49                         GC gc = e.gc;
50                         Rectangle clip = gc.getClipping();
51                         paintGradient(gc, clip);
52                 }
53             };
54         }
55         
56         protected void paintGradient(GC gc, Rectangle clip) {
57                 if (gradient != null) {
58                         Image image = gradient.getGradientImage(clip.width,clip.height,ColorGradientCanvas.this.style);
59                         gc.drawImage(image, 0, 0);
60                         image.dispose();                
61                 } else {
62                         gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
63                         gc.fillRectangle(clip);
64                 }
65         }
66
67 }