Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d9d43e15da7df58fa723855fa7fc2b75288c1691 (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
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.editors.text;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

import org.eclipse.jface.resource.JFaceResources;

/**
 * A "button" of a certain color determined by the color picker.
 * 
 * @since 2.1
 */
class ColorEditor {
	
	/** The extent. */
	private Point fExtent;
	/** The image for the push button. */
	private Image fImage;
	/** The current RGB color value. */
	private RGB fColorValue;
	/** The current color. */
	private Color fColor;
	/** The image push button which open the color dialog. */
	private Button fButton;
	
	/**
	 * Creates and returns a new color editor.
	 * 
	 * @param parent the parent composite of this color editor
	 */
	public ColorEditor(Composite parent) {
		
		fButton= new Button(parent, SWT.PUSH);
		fExtent= computeImageSize(parent);
		fImage= new Image(parent.getDisplay(), fExtent.x, fExtent.y);
		
		GC gc= new GC(fImage);
		gc.setBackground(fButton.getBackground());
		gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
		gc.dispose();
		
		fButton.setImage(fImage);
		fButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				ColorDialog colorDialog= new ColorDialog(fButton.getShell());
				colorDialog.setRGB(fColorValue);
				RGB newColor = colorDialog.open();
				if (newColor != null) {
					fColorValue= newColor;
					updateColorImage();
				}
			}
		});
		
		fButton.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent event) {
				if (fImage != null)  {
					fImage.dispose();
					fImage= null;
				}
				if (fColor != null) {
					fColor.dispose();
					fColor= null;
				}
			}
		});
	}
	
	/**
	 * Returns the current RGB color value.
	 * 
	 * @return an rgb with the current color value
	 */
	public RGB getColorValue() {
		return fColorValue;
	}
	
	/**
	 * Sets the current RGB color value.
	 * 
	 * @param rgb the new value for the rgb color value
	 */
	public void setColorValue(RGB rgb) {
		fColorValue= rgb;
		updateColorImage();
	}
	
	/**
	 * Returns the image push button.
	 * 
	 * @return the button which shows the current color as image
	 */
	public Button getButton() {
		return fButton;
	}
	
	/**
	 * Updates the color of the button image.
	 */
	protected void updateColorImage() {
		
		Display display= fButton.getDisplay();
		
		GC gc= new GC(fImage);
		gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
		gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
		
		if (fColor != null)
			fColor.dispose();
			
		fColor= new Color(display, fColorValue);
		gc.setBackground(fColor);
		gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
		gc.dispose();
		
		fButton.setImage(fImage);
	}
	
	
	/**
	 * Computes the size for the image.
	 * 
	 * @param window the window on which to render the image
	 * @return the point with the image size
	 */
	protected Point computeImageSize(Control window) {
		GC gc= new GC(window);
		Font f= JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT);
		gc.setFont(f);
		int height= gc.getFontMetrics().getHeight();
		gc.dispose();
		Point p= new Point(height * 3 - 6, height);
		return p;
	}
}

Back to the top