1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.ui.color;
\r
14 import org.eclipse.swt.SWT;
\r
15 import org.eclipse.swt.events.PaintEvent;
\r
16 import org.eclipse.swt.events.PaintListener;
\r
17 import org.eclipse.swt.graphics.GC;
\r
18 import org.eclipse.swt.graphics.Image;
\r
19 import org.eclipse.swt.graphics.Rectangle;
\r
20 import org.eclipse.swt.widgets.Canvas;
\r
21 import org.eclipse.swt.widgets.Composite;
\r
22 import org.eclipse.swt.widgets.Display;
\r
26 * Canvas that shows color gradients
\r
28 * @author Marko Luukkainen
\r
31 public class ColorGradientCanvas extends Canvas{
\r
32 ColorGradient gradient;
\r
35 public ColorGradientCanvas(Composite parent, int style) {
\r
36 super(parent,(style|SWT.BORDER)&(~(SWT.VERTICAL|SWT.HORIZONTAL)));
\r
37 this.style = style & (SWT.VERTICAL | SWT.HORIZONTAL) ;
\r
38 addPaintListener(createPaintListener());
\r
41 public void setGradient(ColorGradient gradient) {
\r
42 this.gradient = gradient;
\r
46 protected PaintListener createPaintListener() {
\r
47 return new PaintListener() {
\r
48 public void paintControl(PaintEvent e) {
\r
50 Rectangle clip = gc.getClipping();
\r
51 paintGradient(gc, clip);
\r
56 protected void paintGradient(GC gc, Rectangle clip) {
\r
57 if (gradient != null) {
\r
58 Image image = gradient.getGradientImage(clip.width,clip.height,ColorGradientCanvas.this.style);
\r
59 gc.drawImage(image, 0, 0);
\r
62 gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
\r
63 gc.fillRectangle(clip);
\r