View Javadoc

1   /*
2    *  File: ColorSpot.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.ui;
12  
13  import org.eclipse.swt.SWT;
14  import org.eclipse.swt.events.PaintEvent;
15  import org.eclipse.swt.events.PaintListener;
16  import org.eclipse.swt.graphics.Color;
17  import org.eclipse.swt.graphics.GC;
18  import org.eclipse.swt.graphics.Point;
19  import org.eclipse.swt.graphics.RGB;
20  import org.eclipse.swt.widgets.Canvas;
21  import org.eclipse.swt.widgets.Composite;
22  import org.eclipse.swt.widgets.Display;
23  
24  /**
25   * Custom widget showing a colored spot.
26   * 
27   * @author Peter Kliem
28   * @version $Id: SmileyWidget.java 242 2007-02-11 21:05:07Z olk $
29   */
30  public class ColorSpot extends Canvas {
31      /** default prefreed size. */
32      private static final int PREFSIZE_DEFAULT = 10;
33      /** default color. */
34      private static final RGB COLOR_DEFAULT = new RGB(255, 0, 0);
35  
36      /** the preferred size. */
37      private int _preferredSize = PREFSIZE_DEFAULT;
38      /** the color (allocated). */
39      private Color _spotColor;
40      /** the rgb value for the color. */
41      private RGB _spotRGB = COLOR_DEFAULT;
42  
43      /**
44       * Construct a spot.
45       * 
46       * @param parent parent composite
47       * @param style style bits
48       */
49      public ColorSpot(Composite parent, int style) {
50          super(parent, style);
51          addPaintListener(new PaintListener() {
52              public void paintControl(PaintEvent event) {
53                  onPaint(event);
54              }
55          });
56          setBackground(parent.getBackground());
57      }
58  
59      /**
60       * The paint method simply paints a spot on the whole area.
61       * 
62       * @param event paint event
63       */
64      private void onPaint(PaintEvent event) {
65          GC gc = event.gc;
66          int width = getClientArea().width;
67          int height = getClientArea().height;
68  
69          Color bg = gc.getBackground();
70  
71          gc.setBackground(getColorIntern());
72          gc.fillOval(0, 0, width - 1, height - 1);
73  
74          gc.setBackground(bg);
75          gc.drawOval(0, 0, width - 1, height - 1);
76  
77      }
78  
79      /**
80       * Get the allocated color. Allocates the color from the rgb value if not already done.
81       * 
82       * @return the color
83       */
84      private Color getColorIntern() {
85          if (_spotColor != null) {
86              return _spotColor;
87          }
88          _spotColor = new Color(Display.getCurrent(), _spotRGB);
89          return _spotColor;
90      }
91  
92      /**
93       * Get the color of the spot.
94       * 
95       * @return RGB
96       */
97      public RGB getSpotRGB() {
98          return _spotRGB;
99      }
100 
101     /**
102      * Set the color of the spot as RGB. Freeing of the color will be accomplished by the widget.
103      * 
104      * @param spotRGB color as RGB
105      */
106     public void setSpotRGB(RGB spotRGB) {
107         _spotRGB = spotRGB;
108         if (_spotColor != null) {
109             _spotColor.dispose();
110             _spotColor = null;
111         }
112     }
113 
114     /**
115      * {@inheritDoc} Dispose the color.
116      */
117     public void dispose() {
118         super.dispose();
119         if (_spotColor != null) {
120             _spotColor.dispose();
121         }
122     }
123 
124     /**
125      * {@inheritDoc} Uses the hint if set and the preferred size.
126      */
127     public Point computeSize(int whint, int hhint) {
128         if (whint != SWT.DEFAULT && hhint != SWT.DEFAULT) {
129             return new Point(whint, hhint);
130         }
131         if (whint != SWT.DEFAULT) {
132             return new Point(whint, _preferredSize);
133         }
134         if (hhint != SWT.DEFAULT) {
135             return new Point(_preferredSize, hhint);
136         }
137         return new Point(_preferredSize, _preferredSize);
138 
139     }
140 
141     /**
142      * {@inheritDoc} Simply delegates to computeSize(int, int).
143      */
144     public Point computeSize(int hint, int hint2, boolean changed) {
145         return computeSize(hint, hint2);
146     }
147 
148     /**
149      * Get the preferred size.
150      * 
151      * @return preferred size
152      */
153     public int getPreferredSize() {
154         return _preferredSize;
155     }
156 
157     /**
158      * Set the preferred size.
159      * 
160      * @param preferredSize preferred size (width and height)
161      */
162     public void setPreferredSize(int preferredSize) {
163         _preferredSize = preferredSize;
164     }
165 
166 }