Source Code Cross Referenced for ColorCellEditor.java in  » IDE-Eclipse » jface » org » eclipse » jface » viewers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » jface » org.eclipse.jface.viewers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 2006 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.jface.viewers;
011:
012:        import org.eclipse.swt.SWT;
013:        import org.eclipse.swt.custom.TableTree;
014:        import org.eclipse.swt.graphics.Color;
015:        import org.eclipse.swt.graphics.FontMetrics;
016:        import org.eclipse.swt.graphics.GC;
017:        import org.eclipse.swt.graphics.Image;
018:        import org.eclipse.swt.graphics.ImageData;
019:        import org.eclipse.swt.graphics.PaletteData;
020:        import org.eclipse.swt.graphics.Point;
021:        import org.eclipse.swt.graphics.RGB;
022:        import org.eclipse.swt.graphics.Rectangle;
023:        import org.eclipse.swt.widgets.ColorDialog;
024:        import org.eclipse.swt.widgets.Composite;
025:        import org.eclipse.swt.widgets.Control;
026:        import org.eclipse.swt.widgets.Label;
027:        import org.eclipse.swt.widgets.Layout;
028:        import org.eclipse.swt.widgets.Table;
029:        import org.eclipse.swt.widgets.Tree;
030:
031:        /**
032:         * A cell editor that manages a color field.
033:         * The cell editor's value is the color (an SWT <code>RBG</code>).
034:         * <p>
035:         * This class may be instantiated; it is not intended to be subclassed.
036:         * </p>
037:         */
038:        public class ColorCellEditor extends DialogCellEditor {
039:
040:            /**
041:             * The default extent in pixels.
042:             */
043:            private static final int DEFAULT_EXTENT = 16;
044:
045:            /**
046:             * Gap between between image and text in pixels.
047:             */
048:            private static final int GAP = 6;
049:
050:            /**
051:             * The composite widget containing the color and RGB label widgets
052:             */
053:            private Composite composite;
054:
055:            /**
056:             * The label widget showing the current color.
057:             */
058:            private Label colorLabel;
059:
060:            /**
061:             * The label widget showing the RGB values.
062:             */
063:            private Label rgbLabel;
064:
065:            /**
066:             * The image.
067:             */
068:            private Image image;
069:
070:            /**
071:             * Internal class for laying out this cell editor.
072:             */
073:            private class ColorCellLayout extends Layout {
074:                public Point computeSize(Composite editor, int wHint,
075:                        int hHint, boolean force) {
076:                    if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
077:                        return new Point(wHint, hHint);
078:                    }
079:                    Point colorSize = colorLabel.computeSize(SWT.DEFAULT,
080:                            SWT.DEFAULT, force);
081:                    Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT,
082:                            SWT.DEFAULT, force);
083:                    return new Point(colorSize.x + GAP + rgbSize.x, Math.max(
084:                            colorSize.y, rgbSize.y));
085:                }
086:
087:                public void layout(Composite editor, boolean force) {
088:                    Rectangle bounds = editor.getClientArea();
089:                    Point colorSize = colorLabel.computeSize(SWT.DEFAULT,
090:                            SWT.DEFAULT, force);
091:                    Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT,
092:                            SWT.DEFAULT, force);
093:                    int ty = (bounds.height - rgbSize.y) / 2;
094:                    if (ty < 0) {
095:                        ty = 0;
096:                    }
097:                    colorLabel.setBounds(-1, 0, colorSize.x, colorSize.y);
098:                    rgbLabel.setBounds(colorSize.x + GAP - 1, ty, bounds.width
099:                            - colorSize.x - GAP, bounds.height);
100:                }
101:            }
102:
103:            /**
104:             * Creates a new color cell editor parented under the given control.
105:             * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no 
106:             * validator.
107:             *
108:             * @param parent the parent control
109:             */
110:            public ColorCellEditor(Composite parent) {
111:                this (parent, SWT.NONE);
112:            }
113:
114:            /**
115:             * Creates a new color cell editor parented under the given control.
116:             * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no 
117:             * validator.
118:             *
119:             * @param parent the parent control
120:             * @param style the style bits
121:             * @since 2.1
122:             */
123:            public ColorCellEditor(Composite parent, int style) {
124:                super (parent, style);
125:                doSetValue(new RGB(0, 0, 0));
126:            }
127:
128:            /**
129:             * Creates and returns the color image data for the given control
130:             * and RGB value. The image's size is either the control's item extent 
131:             * or the cell editor's default extent, which is 16 pixels square.
132:             *
133:             * @param w the control
134:             * @param color the color
135:             */
136:            private ImageData createColorImage(Control w, RGB color) {
137:
138:                GC gc = new GC(w);
139:                FontMetrics fm = gc.getFontMetrics();
140:                int size = fm.getAscent();
141:                gc.dispose();
142:
143:                int indent = 6;
144:                int extent = DEFAULT_EXTENT;
145:                if (w instanceof  Table) {
146:                    extent = ((Table) w).getItemHeight() - 1;
147:                } else if (w instanceof  Tree) {
148:                    extent = ((Tree) w).getItemHeight() - 1;
149:                } else if (w instanceof  TableTree) {
150:                    extent = ((TableTree) w).getItemHeight() - 1;
151:                }
152:
153:                if (size > extent) {
154:                    size = extent;
155:                }
156:
157:                int width = indent + size;
158:                int height = extent;
159:
160:                int xoffset = indent;
161:                int yoffset = (height - size) / 2;
162:
163:                RGB black = new RGB(0, 0, 0);
164:                PaletteData dataPalette = new PaletteData(new RGB[] { black,
165:                        black, color });
166:                ImageData data = new ImageData(width, height, 4, dataPalette);
167:                data.transparentPixel = 0;
168:
169:                int end = size - 1;
170:                for (int y = 0; y < size; y++) {
171:                    for (int x = 0; x < size; x++) {
172:                        if (x == 0 || y == 0 || x == end || y == end) {
173:                            data.setPixel(x + xoffset, y + yoffset, 1);
174:                        } else {
175:                            data.setPixel(x + xoffset, y + yoffset, 2);
176:                        }
177:                    }
178:                }
179:
180:                return data;
181:            }
182:
183:            /* (non-Javadoc)
184:             * Method declared on DialogCellEditor.
185:             */
186:            protected Control createContents(Composite cell) {
187:                Color bg = cell.getBackground();
188:                composite = new Composite(cell, getStyle());
189:                composite.setBackground(bg);
190:                composite.setLayout(new ColorCellLayout());
191:                colorLabel = new Label(composite, SWT.LEFT);
192:                colorLabel.setBackground(bg);
193:                rgbLabel = new Label(composite, SWT.LEFT);
194:                rgbLabel.setBackground(bg);
195:                rgbLabel.setFont(cell.getFont());
196:                return composite;
197:            }
198:
199:            /* (non-Javadoc)
200:             * Method declared on CellEditor.
201:             */
202:            public void dispose() {
203:                if (image != null) {
204:                    image.dispose();
205:                    image = null;
206:                }
207:                super .dispose();
208:            }
209:
210:            /* (non-Javadoc)
211:             * Method declared on DialogCellEditor.
212:             */
213:            protected Object openDialogBox(Control cellEditorWindow) {
214:                ColorDialog dialog = new ColorDialog(cellEditorWindow
215:                        .getShell());
216:                Object value = getValue();
217:                if (value != null) {
218:                    dialog.setRGB((RGB) value);
219:                }
220:                value = dialog.open();
221:                return dialog.getRGB();
222:            }
223:
224:            /* (non-Javadoc)
225:             * Method declared on DialogCellEditor.
226:             */
227:            protected void updateContents(Object value) {
228:                RGB rgb = (RGB) value;
229:                // XXX: We don't have a value the first time this method is called".
230:                if (rgb == null) {
231:                    rgb = new RGB(0, 0, 0);
232:                }
233:                // XXX: Workaround for 1FMQ0P3: SWT:ALL - TableItem.setImage doesn't work if using the identical image."
234:                if (image != null) {
235:                    image.dispose();
236:                }
237:
238:                ImageData id = createColorImage(colorLabel.getParent()
239:                        .getParent(), rgb);
240:                ImageData mask = id.getTransparencyMask();
241:                image = new Image(colorLabel.getDisplay(), id, mask);
242:                colorLabel.setImage(image);
243:
244:                rgbLabel
245:                        .setText("(" + rgb.red + "," + rgb.green + "," + rgb.blue + ")");//$NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
246:            }
247:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.