1
2
3
4
5
6
7
8
9
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
26
27
28
29
30 public class ColorSpot extends Canvas {
31
32 private static final int PREFSIZE_DEFAULT = 10;
33
34 private static final RGB COLOR_DEFAULT = new RGB(255, 0, 0);
35
36
37 private int _preferredSize = PREFSIZE_DEFAULT;
38
39 private Color _spotColor;
40
41 private RGB _spotRGB = COLOR_DEFAULT;
42
43
44
45
46
47
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
61
62
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
81
82
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
94
95
96
97 public RGB getSpotRGB() {
98 return _spotRGB;
99 }
100
101
102
103
104
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
116
117 public void dispose() {
118 super.dispose();
119 if (_spotColor != null) {
120 _spotColor.dispose();
121 }
122 }
123
124
125
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
143
144 public Point computeSize(int hint, int hint2, boolean changed) {
145 return computeSize(hint, hint2);
146 }
147
148
149
150
151
152
153 public int getPreferredSize() {
154 return _preferredSize;
155 }
156
157
158
159
160
161
162 public void setPreferredSize(int preferredSize) {
163 _preferredSize = preferredSize;
164 }
165
166 }