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;
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;
26 * Canvas that shows color gradients
28 * @author Marko Luukkainen
31 public class ColorGradientCanvas extends Canvas{
32 ColorGradient gradient;
35 public ColorGradientCanvas(Composite parent, int style) {
36 super(parent,(style|SWT.BORDER)&(~(SWT.VERTICAL|SWT.HORIZONTAL))|SWT.DOUBLE_BUFFERED);
37 this.style = style & (SWT.VERTICAL | SWT.HORIZONTAL) ;
38 addPaintListener(createPaintListener());
41 public void setGradient(ColorGradient gradient) {
42 this.gradient = gradient;
46 protected PaintListener createPaintListener() {
47 return new PaintListener() {
48 public void paintControl(PaintEvent e) {
50 Rectangle clip = gc.getClipping();
51 paintGradient(gc, clip);
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);
62 gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
63 gc.fillRectangle(clip);