001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * -------------------
028 * SwtPaintCanvas.java
029 * -------------------
030 * (C) Copyright 2000-2007, by Object Refinery Limited.
031 *
032 * Original Author: Henry Proudhon (henry.proudhon AT ensmp.fr);
033 * Contributor(s): David Gilbert (for Object Refinery Limited);
034 *
035 * Changes
036 * -------
037 * 04-Aug-2006 : New class (HP);
038 * 25-Oct-2007 : Fixed Eclipse warnings (DG);
039 *
040 */
041
042 package org.jfree.experimental.swt;
043
044 import org.eclipse.swt.SWT;
045 import org.eclipse.swt.events.PaintEvent;
046 import org.eclipse.swt.events.PaintListener;
047 import org.eclipse.swt.graphics.Color;
048 import org.eclipse.swt.widgets.Canvas;
049 import org.eclipse.swt.widgets.Composite;
050
051 /**
052 * A paint canvas.
053 */
054 public class SWTPaintCanvas extends Canvas {
055 private Color myColor;
056
057 /**
058 * Creates a new instance.
059 *
060 * @param parent the parent.
061 * @param style the style.
062 * @param color the color.
063 */
064 public SWTPaintCanvas(Composite parent, int style, Color color) {
065 this(parent, style);
066 this.setColor(color);
067 }
068
069 /**
070 * Creates a new instance.
071 *
072 * @param parent the parent.
073 * @param style the style.
074 */
075 public SWTPaintCanvas(Composite parent, int style) {
076 super(parent, style);
077 addPaintListener(new PaintListener() {
078 public void paintControl(PaintEvent e) {
079 e.gc.setForeground(e.gc.getDevice().getSystemColor(
080 SWT.COLOR_BLACK));
081 e.gc.setBackground(SWTPaintCanvas.this.myColor);
082 e.gc.fillRectangle(getClientArea());
083 e.gc.drawRectangle(getClientArea().x, getClientArea().y,
084 getClientArea().width - 1, getClientArea().height - 1);
085 }
086 });
087 }
088
089 /**
090 * Sets the color.
091 *
092 * @param color the color.
093 */
094 public void setColor(Color color) {
095 if (this.myColor != null) {
096 this.myColor.dispose();
097 }
098 //this.myColor = new Color( getDisplay(), color.getRGB() );
099 this.myColor = color;
100 }
101
102 /**
103 * Returns the color.
104 *
105 * @return The color.
106 */
107 public Color getColor() {
108 return this.myColor;
109 }
110
111 /**
112 * Overridden to do nothing.
113 *
114 * @param c the color.
115 */
116 public void setBackground(Color c) {
117 return;
118 }
119
120 /**
121 * Overridden to do nothing.
122 *
123 * @param c the color.
124 */
125 public void setForeground(Color c) {
126 return;
127 }
128
129 /**
130 * Frees resources.
131 */
132 public void dispose() {
133 this.myColor.dispose();
134 }
135 }