001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * ColorSelectionField.java
044: *
045: * Created on October 18, 2004, 12:34 PM
046: */
047:
048: package org.netbeans.modules.css.visual.ui;
049:
050: import org.netbeans.modules.css.visual.model.ColorModel;
051: import java.awt.Color;
052: import java.awt.Graphics;
053: import java.awt.Graphics2D;
054: import java.awt.RenderingHints;
055: import java.beans.PropertyChangeSupport;
056: import javax.swing.DefaultComboBoxModel;
057: import javax.swing.JButton;
058: import javax.swing.JColorChooser;
059: import org.netbeans.modules.css.visual.model.PropertyData;
060: import org.openide.util.NbBundle;
061:
062: /**
063: * Color Selection text field. Combination of
064: * TextField and a Color button. This button
065: * brings up the Color Chooser Dialog and paints
066: * the selected color in its surface.
067: * @author Winston Prakash
068: * @version 1.0
069: */
070: public class ColorSelectionField extends javax.swing.JPanel {
071: private PropertyChangeSupport propertyChangeSupport;
072: ColorModel colorModel = new ColorModel();
073:
074: String currentColor = null;
075: String oldColor = null;
076:
077: /** Creates new form ColorSelectionField */
078: public ColorSelectionField() {
079: initComponents();
080: initialize();
081: }
082:
083: private void initialize() {
084: // Set the font Variant to the GUI
085: DefaultComboBoxModel colorList = colorModel.getColorList();
086: colorComboBox.setModel(colorList);
087: colorComboBox.setSelectedIndex(0);
088: }
089:
090: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
091: private void initComponents() {
092: colorButton = new ColorSelectionButton();
093: colorComboBox = new javax.swing.JComboBox();
094:
095: setLayout(new java.awt.BorderLayout(3, 0));
096:
097: colorButton.setToolTipText(org.openide.util.NbBundle
098: .getMessage(ColorSelectionField.class,
099: "COLOR_CHOOSER_BTN_LABEL"));
100: colorButton.setPreferredSize(new java.awt.Dimension(20, 20));
101: colorButton
102: .addActionListener(new java.awt.event.ActionListener() {
103: public void actionPerformed(
104: java.awt.event.ActionEvent evt) {
105: colorButtonActionPerformed(evt);
106: }
107: });
108:
109: add(colorButton, java.awt.BorderLayout.EAST);
110: colorButton.getAccessibleContext().setAccessibleName(
111: org.openide.util.NbBundle.getMessage(
112: ColorSelectionField.class,
113: "COLOR_EDITOR_BTN_ACCESS_NAME"));
114: colorButton.getAccessibleContext().setAccessibleDescription(
115: org.openide.util.NbBundle.getMessage(
116: ColorSelectionField.class,
117: "COLOR_EDITOR_BTN_ACCESS_DESC"));
118:
119: colorComboBox.setEditable(true);
120: colorComboBox
121: .addActionListener(new java.awt.event.ActionListener() {
122: public void actionPerformed(
123: java.awt.event.ActionEvent evt) {
124: colorComboBoxActionPerformed(evt);
125: }
126: });
127: colorComboBox
128: .addFocusListener(new java.awt.event.FocusAdapter() {
129: public void focusLost(java.awt.event.FocusEvent evt) {
130: colorComboBoxFocusLost(evt);
131: }
132: });
133: colorComboBox
134: .addItemListener(new java.awt.event.ItemListener() {
135: public void itemStateChanged(
136: java.awt.event.ItemEvent evt) {
137: colorComboBoxItemStateChanged(evt);
138: }
139: });
140:
141: add(colorComboBox, java.awt.BorderLayout.CENTER);
142: colorComboBox.getAccessibleContext().setAccessibleName(
143: org.openide.util.NbBundle.getMessage(
144: ColorSelectionField.class,
145: "COLOR_SELECTION_COMBO_ACCESS_NAME"));
146: colorComboBox.getAccessibleContext().setAccessibleDescription(
147: org.openide.util.NbBundle.getMessage(
148: ColorSelectionField.class,
149: "COLOR_SELECTION_COMBO_ACCESS_Desc"));
150:
151: }// </editor-fold>//GEN-END:initComponents
152:
153: private void colorComboBoxItemStateChanged(
154: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_colorComboBoxItemStateChanged
155: setColor();
156: }//GEN-LAST:event_colorComboBoxItemStateChanged
157:
158: private void colorComboBoxFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_colorComboBoxFocusLost
159: setColor();
160: }//GEN-LAST:event_colorComboBoxFocusLost
161:
162: private void colorComboBoxActionPerformed(
163: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorComboBoxActionPerformed
164: setColor();
165: }//GEN-LAST:event_colorComboBoxActionPerformed
166:
167: private void colorButtonActionPerformed(
168: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorButtonActionPerformed
169: Color newColor = JColorChooser.showDialog(this , NbBundle
170: .getMessage(ColorSelectionField.class,
171: "COLOR_CHOOSER_TITLE"), colorModel.getColor());
172: if (newColor != null) {
173: colorModel.setColor(newColor);
174: colorComboBox.setSelectedItem(colorModel.getHexColor());
175: }
176: }//GEN-LAST:event_colorButtonActionPerformed
177:
178: private void setColor() {
179: PropertyData colorPropertyData = new PropertyData();
180: currentColor = (String) colorComboBox.getSelectedItem();
181: colorPropertyData.setValue(currentColor);
182: colorModel.setColor(currentColor);
183: repaint();
184: firePropertyChange("color", oldColor, colorPropertyData
185: .toString()); //NOI18N
186: oldColor = colorPropertyData.toString();
187:
188: }
189:
190: public void setColorString(String color) {
191: currentColor = color;
192: if ((color == null) || color.equals("")) {
193: colorComboBox.setSelectedIndex(0);
194: } else {
195: colorComboBox.setSelectedItem(currentColor);
196: }
197: repaint();
198: }
199:
200: public String getColorString() {
201: return currentColor;
202: }
203:
204: // Variables declaration - do not modify//GEN-BEGIN:variables
205: private javax.swing.JButton colorButton;
206: private javax.swing.JComboBox colorComboBox;
207:
208: // End of variables declaration//GEN-END:variables
209:
210: class ColorSelectionButton extends JButton {
211:
212: public ColorSelectionButton() {
213: //setPreferredSize(new Dimension(20,20));
214: }
215:
216: public void paintComponent(Graphics graphics) {
217: super .paintComponent(graphics);
218: Graphics2D g2d = (Graphics2D) graphics;
219: Color color = colorModel.getColor();
220: if (color == null)
221: color = Color.BLACK;
222: g2d.setColor(color);
223: int w = getWidth();
224: int h = getHeight();
225: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
226: RenderingHints.VALUE_ANTIALIAS_ON);
227: g2d.fillRoundRect(4, 4, w - 9, h - 9, 5, 5);
228: g2d.setColor(color.darker());
229: g2d.drawRoundRect(4, 4, w - 9, h - 9, 5, 5);
230: }
231:
232: }
233: }
|