KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > ColorEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.editors.text;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.DisposeEvent;
15 import org.eclipse.swt.events.DisposeListener;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.graphics.RGB;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.ColorDialog;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
29
30 import org.eclipse.jface.resource.JFaceResources;
31
32 /**
33  * A "button" of a certain color determined by the color picker.
34  *
35  * @since 2.1
36  */

37 class ColorEditor {
38     
39     /** The extent. */
40     private Point fExtent;
41     /** The image for the push button. */
42     private Image fImage;
43     /** The current RGB color value. */
44     private RGB fColorValue;
45     /** The current color. */
46     private Color fColor;
47     /** The image push button which open the color dialog. */
48     private Button fButton;
49     
50     /**
51      * Creates and returns a new color editor.
52      *
53      * @param parent the parent composite of this color editor
54      */

55     public ColorEditor(Composite parent) {
56         
57         fButton= new Button(parent, SWT.PUSH);
58         fExtent= computeImageSize(parent);
59         fImage= new Image(parent.getDisplay(), fExtent.x, fExtent.y);
60         
61         GC gc= new GC(fImage);
62         gc.setBackground(fButton.getBackground());
63         gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
64         gc.dispose();
65         
66         fButton.setImage(fImage);
67         fButton.addSelectionListener(new SelectionAdapter() {
68             public void widgetSelected(SelectionEvent event) {
69                 ColorDialog colorDialog= new ColorDialog(fButton.getShell());
70                 colorDialog.setRGB(fColorValue);
71                 RGB newColor = colorDialog.open();
72                 if (newColor != null) {
73                     fColorValue= newColor;
74                     updateColorImage();
75                 }
76             }
77         });
78         
79         fButton.addDisposeListener(new DisposeListener() {
80             public void widgetDisposed(DisposeEvent event) {
81                 if (fImage != null) {
82                     fImage.dispose();
83                     fImage= null;
84                 }
85                 if (fColor != null) {
86                     fColor.dispose();
87                     fColor= null;
88                 }
89             }
90         });
91     }
92     
93     /**
94      * Returns the current RGB color value.
95      *
96      * @return an rgb with the current color value
97      */

98     public RGB getColorValue() {
99         return fColorValue;
100     }
101     
102     /**
103      * Sets the current RGB color value.
104      *
105      * @param rgb the new value for the rgb color value
106      */

107     public void setColorValue(RGB rgb) {
108         fColorValue= rgb;
109         updateColorImage();
110     }
111     
112     /**
113      * Returns the image push button.
114      *
115      * @return the button which shows the current color as image
116      */

117     public Button getButton() {
118         return fButton;
119     }
120     
121     /**
122      * Updates the color of the button image.
123      */

124     protected void updateColorImage() {
125         
126         Display display= fButton.getDisplay();
127         
128         GC gc= new GC(fImage);
129         gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
130         gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
131         
132         if (fColor != null)
133             fColor.dispose();
134             
135         fColor= new Color(display, fColorValue);
136         gc.setBackground(fColor);
137         gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
138         gc.dispose();
139         
140         fButton.setImage(fImage);
141     }
142     
143     
144     /**
145      * Computes the size for the image.
146      *
147      * @param window the window on which to render the image
148      * @return the point with the image size
149      */

150     protected Point computeImageSize(Control window) {
151         GC gc= new GC(window);
152         Font f= JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT);
153         gc.setFont(f);
154         int height= gc.getFontMetrics().getHeight();
155         gc.dispose();
156         Point p= new Point(height * 3 - 6, height);
157         return p;
158     }
159 }
160
Popular Tags