Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ff4a9e070159a8bbdd13649ca400276d6b244f55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.opengl.examples;


import java.util.ArrayList;
import java.util.List;

import org.eclipse.opengl.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

class ColorSelectionGroup implements Listener {
	private Color color;
	private Label label;
	private Image image;
	private Button button;
	private List<IColorSelectionListener> listeners = new ArrayList<IColorSelectionListener>();

	/**
	 * Constructor.
	 * 
	 * @param parent the parent composite
	 * @param style style bits to be applied to the color group
	 */
	ColorSelectionGroup(Composite parent, int style) {
		super();
		initColor(parent.getDisplay());

		Composite colorGroup = new Composite(parent, style);
		GridLayout layout = new GridLayout(2, false);
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		colorGroup.setLayout(layout);
//		GridData data = new GridData(GridData.FILL_HORIZONTAL);
//		data.grabExcessHorizontalSpace = true;
//		colorGroup.setLayoutData(data);

		button = new Button(colorGroup, SWT.NONE);
		button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		image = new Image(button.getDisplay(), 12, 12);
		drawButtonImage();
		button.setImage(image);
		button.addListener(SWT.Selection, this);
		button.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent event) {
				if (image != null) image.dispose();
				if (color != null) color.dispose();
			}
		});
		
		label = new Label(colorGroup, SWT.NONE);
		label.setText("Color");
//		data = new GridData(GridData.FILL_HORIZONTAL);
//		data.grabExcessHorizontalSpace = true;
//		label.setLayoutData(data);
	}

	/**
	 * Adds the argument to this group's collection of
	 * color selection listeners.
	 * 
	 * @param listener
	 */
	void addColorSelectionListener(IColorSelectionListener listener) {
		listeners.add(listener);
	}
	
	/**
	 * Draws the colored square on the selection button.
	 */
	void drawButtonImage() {
		GC gc = new GC(image);
		gc.setBackground(color);
		Rectangle bounds = image.getBounds();
		gc.fillRectangle(0, 0, bounds.width, bounds.height);
		gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
		gc.dispose();
	}

	/**
	 * @see org.eclipse.swt.widgets.Listener#handleEvent(Event)
	 */
	public void handleEvent(Event e) {
		Shell shell = button.getShell();
		ColorDialog colorDialog = new ColorDialog(shell);
		colorDialog.setRGB(
			new RGB(color.getRed(), color.getGreen(), color.getBlue()));
		RGB rgb = colorDialog.open();
		if (rgb == null) return;
		setRGB(rgb);
		notifyListeners(rgb);
	}

	/**
	 * Initializes the color by querying for the current color.
	 * 
	 * @param display
	 */
	void initColor(Display display) {
		double[] currentColor = new double[4];
		GL.glGetDoublev(GL.GL_CURRENT_COLOR, currentColor);
		RGB rgb =
			new RGB(
				(int) currentColor[0] * 255,
				(int) currentColor[1] * 255,
				(int) currentColor[2] * 255);
		color = new Color(display, rgb);
	}
	
	/**
	 * Notifies all registered color selection listeners.
	 * 
	 * @param value
	 */
	void notifyListeners(RGB rgb) {
		IColorSelectionListener[] listenersArr =
			listeners.toArray(new IColorSelectionListener[listeners.size()]);
		for (int i = 0; i < listenersArr.length; i++) {
			listenersArr [i].handleColorSelection(rgb);
		}
	}

	/**
	 * Removes the argument from this group's collection of color selection
	 * listeners.  If the argument is not a registered listener then does
	 * nothing.
	 * 
	 * @param listener
	 */
	void removeColorSelectionListener(IColorSelectionListener listener) {
		listeners.remove(listener);
	}

	/**
	 * Sets the text for the group's label.
	 * 
	 * @param text the new label text
	 */
	void setText(String text) {
		label.setText(text);
	}

	/**
	 * Sets the current color.
	 * 
	 * @param rgb the rgb of the new color
	 */
	void setRGB(RGB rgb) {
		Color oldColor = color;
		color = new Color(button.getDisplay(), rgb);
		drawButtonImage();
		button.setImage(image);
		if (oldColor != null) oldColor.dispose();
	}
}

Back to the top