1 19 20 25 26 package org.netbeans.modules.css.visual.ui; 27 28 import org.netbeans.modules.css.visual.model.ColorModel; 29 import java.awt.Color ; 30 import java.awt.Graphics ; 31 import java.awt.Graphics2D ; 32 import java.awt.RenderingHints ; 33 import java.beans.PropertyChangeSupport ; 34 import javax.swing.DefaultComboBoxModel ; 35 import javax.swing.JButton ; 36 import javax.swing.JColorChooser ; 37 import org.openide.util.NbBundle; 38 39 40 48 public class ColorSelectionField extends javax.swing.JPanel { 49 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport (this); 50 ColorModel colorModel = new ColorModel(); 51 52 String currentColor = null; 53 String oldColor = null; 54 55 56 public ColorSelectionField() { 57 initComponents(); 58 initialize(); 59 } 60 61 private void initialize(){ 62 DefaultComboBoxModel colorList = colorModel.getColorList(); 64 colorComboBox.setModel(colorList); 65 colorComboBox.setSelectedIndex(0); 66 } 67 68 private void initComponents() { 70 colorButton = new ColorSelectionButton(); 71 colorComboBox = new javax.swing.JComboBox (); 72 73 setLayout(new java.awt.BorderLayout (3, 0)); 74 75 colorButton.setToolTipText(org.openide.util.NbBundle.getMessage(ColorSelectionField.class, "COLOR_CHOOSER_BTN_LABEL")); 76 colorButton.setPreferredSize(new java.awt.Dimension (20, 20)); 77 colorButton.addActionListener(new java.awt.event.ActionListener () { 78 public void actionPerformed(java.awt.event.ActionEvent 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 () { 89 public void actionPerformed(java.awt.event.ActionEvent evt) { 90 colorComboBoxActionPerformed(evt); 91 } 92 }); 93 colorComboBox.addFocusListener(new java.awt.event.FocusAdapter () { 94 public void focusLost(java.awt.event.FocusEvent evt) { 95 colorComboBoxFocusLost(evt); 96 } 97 }); 98 colorComboBox.addItemListener(new java.awt.event.ItemListener () { 99 public void itemStateChanged(java.awt.event.ItemEvent 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 } 110 private void colorComboBoxItemStateChanged(java.awt.event.ItemEvent evt) { setColor(); 112 } 114 private void colorComboBoxFocusLost(java.awt.event.FocusEvent evt) { setColor(); 116 } 118 private void colorComboBoxActionPerformed(java.awt.event.ActionEvent evt) { setColor(); 120 } 122 private void colorButtonActionPerformed(java.awt.event.ActionEvent evt) { Color 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 } 133 137 public void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 138 propertyChangeSupport.addPropertyChangeListener(l); 139 } 140 141 145 public void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 146 propertyChangeSupport.removePropertyChangeListener(l); 147 } 148 149 private void setColor(){ 150 currentColor = (String )colorComboBox.getSelectedItem(); 151 colorModel.setColor(currentColor); 152 repaint(); 153 propertyChangeSupport.firePropertyChange("color", oldColor, currentColor); oldColor = currentColor; 155 156 } 157 158 public void setColorString(String 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 getColorString(){ 169 return currentColor; 170 } 171 172 private javax.swing.JButton colorButton; 174 private javax.swing.JComboBox colorComboBox; 175 177 class ColorSelectionButton extends JButton { 178 179 public ColorSelectionButton(){ 180 } 182 183 public void paintComponent(Graphics graphics) { 184 super.paintComponent(graphics); 185 Graphics2D g2d = (Graphics2D ) graphics; 186 Color 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 |