KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > visual > ui > ColorSelectionField


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ColorSelectionField.java
22  *
23  * Created on October 18, 2004, 12:34 PM
24  */

25
26 package org.netbeans.modules.css.visual.ui;
27
28 import org.netbeans.modules.css.visual.model.ColorModel;
29 import java.awt.Color JavaDoc;
30 import java.awt.Graphics JavaDoc;
31 import java.awt.Graphics2D JavaDoc;
32 import java.awt.RenderingHints JavaDoc;
33 import java.beans.PropertyChangeSupport JavaDoc;
34 import javax.swing.DefaultComboBoxModel JavaDoc;
35 import javax.swing.JButton JavaDoc;
36 import javax.swing.JColorChooser JavaDoc;
37 import org.openide.util.NbBundle;
38
39
40 /**
41  * Color Selection text field. Combination of
42  * TextField and a Color button. This button
43  * brings up the Color Chooser Dialog and paints
44  * the selected color in its surface.
45  * @author Winston Prakash
46  * @version 1.0
47  */

48 public class ColorSelectionField extends javax.swing.JPanel JavaDoc {
49     private PropertyChangeSupport JavaDoc propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
50     ColorModel colorModel = new ColorModel();
51
52     String JavaDoc currentColor = null;
53     String JavaDoc oldColor = null;
54
55     /** Creates new form ColorSelectionField */
56     public ColorSelectionField() {
57         initComponents();
58         initialize();
59     }
60
61     private void initialize(){
62         // Set the font Variant to the GUI
63
DefaultComboBoxModel JavaDoc colorList = colorModel.getColorList();
64         colorComboBox.setModel(colorList);
65         colorComboBox.setSelectedIndex(0);
66     }
67
68     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
69
private void initComponents() {
70         colorButton = new ColorSelectionButton();
71         colorComboBox = new javax.swing.JComboBox JavaDoc();
72
73         setLayout(new java.awt.BorderLayout JavaDoc(3, 0));
74
75         colorButton.setToolTipText(org.openide.util.NbBundle.getMessage(ColorSelectionField.class, "COLOR_CHOOSER_BTN_LABEL"));
76         colorButton.setPreferredSize(new java.awt.Dimension JavaDoc(20, 20));
77         colorButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
78             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
79                 colorButtonActionPerformed(evt);
80             }
81         });
82
83         add(colorButton, java.awt.BorderLayout.EAST);
84         colorButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ColorSelectionField.class, "COLOR_EDITOR_BTN_ACCESS_NAME"));
85         colorButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ColorSelectionField.class, "COLOR_EDITOR_BTN_ACCESS_DESC"));
86
87         colorComboBox.setEditable(true);
88         colorComboBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
89             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
90                 colorComboBoxActionPerformed(evt);
91             }
92         });
93         colorComboBox.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
94             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
95                 colorComboBoxFocusLost(evt);
96             }
97         });
98         colorComboBox.addItemListener(new java.awt.event.ItemListener JavaDoc() {
99             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
100                 colorComboBoxItemStateChanged(evt);
101             }
102         });
103
104         add(colorComboBox, java.awt.BorderLayout.CENTER);
105         colorComboBox.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ColorSelectionField.class, "COLOR_SELECTION_COMBO_ACCESS_NAME"));
106         colorComboBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ColorSelectionField.class, "COLOR_SELECTION_COMBO_ACCESS_Desc"));
107
108     }// </editor-fold>//GEN-END:initComponents
109

110     private void colorComboBoxItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_colorComboBoxItemStateChanged
111
setColor();
112     }//GEN-LAST:event_colorComboBoxItemStateChanged
113

114     private void colorComboBoxFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_colorComboBoxFocusLost
115
setColor();
116     }//GEN-LAST:event_colorComboBoxFocusLost
117

118     private void colorComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_colorComboBoxActionPerformed
119
setColor();
120     }//GEN-LAST:event_colorComboBoxActionPerformed
121

122     private void colorButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_colorButtonActionPerformed
123
Color JavaDoc newColor = JColorChooser.showDialog(
124         this,
125         NbBundle.getMessage(ColorSelectionField.class, "COLOR_CHOOSER_TITLE"),
126         colorModel.getColor());
127         if(newColor != null){
128             colorModel.setColor(newColor);
129             colorComboBox.setSelectedItem(colorModel.getHexColor());
130         }
131     }//GEN-LAST:event_colorButtonActionPerformed
132

133     /**
134      * Adds a PropertyChangeListener to the listener list.
135      * @param l The listener to add.
136      */

137     public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
138         propertyChangeSupport.addPropertyChangeListener(l);
139     }
140     
141     /**
142      * Removes a PropertyChangeListener from the listener list.
143      * @param l The listener to remove.
144      */

145     public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
146         propertyChangeSupport.removePropertyChangeListener(l);
147     }
148     
149     private void setColor(){
150         currentColor = (String JavaDoc)colorComboBox.getSelectedItem();
151         colorModel.setColor(currentColor);
152         repaint();
153         propertyChangeSupport.firePropertyChange("color", oldColor, currentColor); //NOI18N
154
oldColor = currentColor;
155         
156     }
157     
158     public void setColorString(String JavaDoc color){
159         currentColor = color;
160         if((color == null) || color.equals("")){
161             colorComboBox.setSelectedIndex(0);
162         }else{
163             colorComboBox.setSelectedItem(currentColor);
164         }
165         repaint();
166     }
167     
168     public String JavaDoc getColorString(){
169         return currentColor;
170     }
171     
172     // Variables declaration - do not modify//GEN-BEGIN:variables
173
private javax.swing.JButton JavaDoc colorButton;
174     private javax.swing.JComboBox JavaDoc colorComboBox;
175     // End of variables declaration//GEN-END:variables
176

177     class ColorSelectionButton extends JButton JavaDoc{
178         
179         public ColorSelectionButton(){
180             //setPreferredSize(new Dimension(20,20));
181
}
182         
183         public void paintComponent(Graphics JavaDoc graphics) {
184             super.paintComponent(graphics);
185             Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) graphics;
186             Color JavaDoc color = colorModel.getColor();
187             if(color == null) color = Color.BLACK;
188             g2d.setColor(color);
189             int w = getWidth();
190             int h = getHeight();
191             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
192             g2d.fillRoundRect(4,4,w-9,h-9,5,5);
193             g2d.setColor(color.darker());
194             g2d.drawRoundRect(4,4,w-9,h-9,5,5);
195         }
196         
197     }
198 }
199
Popular Tags