1 32 33 package it.businesslogic.ireport.gui.sheet; 34 import java.awt.*; 35 import java.util.*; 36 import javax.swing.*; 37 import javax.swing.border.LineBorder ; 38 import it.businesslogic.ireport.util.I18n; 39 43 public class ColorSelectorPanel extends javax.swing.JPanel { 44 45 private Color color = Color.BLACK; 46 private String value = "[0,0,0]"; 47 48 public ColorSelectorPanel() { 49 initComponents(); 50 applyI18n(); 51 setColor(null); 52 } 53 54 public String getValue() { 55 return value; 56 } 57 58 public void setValue(Object newValue) { 59 60 if (newValue == null){ 61 this.setColor(null); 62 return; 63 } 64 65 Color newColor = null; 66 if (newValue instanceof Color) newColor = (Color)newValue; 67 else newColor = parseColorString(""+newValue); 68 69 if (newColor == null) return; 70 this.setColor(newColor); 71 } 72 73 public Color getColor() { 74 return color; 75 } 76 77 public void setColor(Color color) { 78 this.color = color; 79 if (color == null){ 80 this.value=null; 81 jTextFieldColorValue.setText(""); 82 this.jPanelColor.setBackground(Color.WHITE); 83 this.jPanelColor.setBorder(new LineBorder (Color.LIGHT_GRAY)); 84 this.jPanelColor.invalidate(); 85 this.jPanelColor.updateUI(); 86 } 87 else 88 { 89 this.jPanelColor.setBorder(new LineBorder (Color.BLACK)); 90 this.value = "[" + color.getRed() + "," + color.getGreen() + "," + color.getBlue()+ "]"; 91 this.jPanelColor.setBackground(color ); 92 this.jPanelColor.invalidate(); 93 this.jPanelColor.updateUI(); 94 jTextFieldColorValue.setText( getValue() ); 95 } 96 97 98 fireActionListenerActionPerformed(new java.awt.event.ActionEvent (this,0,"")); 99 } 100 101 public static Color parseColorString(String newValue) 102 { 103 if (newValue == null) return null; 104 105 newValue = newValue.trim(); 106 if (!newValue.startsWith("[") || !newValue.endsWith("]")) 107 { 108 java.awt.Color c = java.awt.Color.getColor(newValue); 110 if (c == null && newValue.startsWith("#") && newValue.length() == 7) 111 { 112 int hr = Integer.parseInt( newValue.substring(1,3), 16); 113 int hg = Integer.parseInt( newValue.substring(3,5), 16); 114 int hb = Integer.parseInt( newValue.substring(5,7), 16); 115 c = new Color(hr,hg,hb); 116 return c; 117 } 118 119 return null; 120 } 121 122 int r = 0; 123 int g = 0; 124 int b = 0; 125 String rgbValues = newValue.substring(1,newValue.length()-1); 126 try { 127 128 StringTokenizer st = new StringTokenizer(rgbValues, ",",false); 129 r = Integer.parseInt( st.nextToken() ); 130 g = Integer.parseInt( st.nextToken() ); 131 b = Integer.parseInt( st.nextToken() ); 132 } catch (Exception ex) { return null; } 133 134 Color c = new Color(r,g,b); 135 return c; 136 137 } 138 139 140 145 private void initComponents() { 147 java.awt.GridBagConstraints gridBagConstraints; 148 149 jPanelColor = new javax.swing.JPanel (); 150 jTextFieldColorValue = new javax.swing.JTextField (); 151 jButtonSelect = new javax.swing.JButton (); 152 153 setLayout(new java.awt.GridBagLayout ()); 154 155 setBackground(new java.awt.Color (255, 255, 255)); 156 setMinimumSize(new java.awt.Dimension (45, 10)); 157 setPreferredSize(new java.awt.Dimension (75, 22)); 158 jPanelColor.setLayout(null); 159 160 jPanelColor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color (0, 0, 0))); 161 jPanelColor.setMaximumSize(new java.awt.Dimension (18, 18)); 162 jPanelColor.setMinimumSize(new java.awt.Dimension (18, 8)); 163 jPanelColor.setPreferredSize(new java.awt.Dimension (18, 18)); 164 gridBagConstraints = new java.awt.GridBagConstraints (); 165 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 166 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 167 gridBagConstraints.weighty = 1.0; 168 gridBagConstraints.insets = new java.awt.Insets (4, 0, 4, 4); 169 add(jPanelColor, gridBagConstraints); 170 171 jTextFieldColorValue.setText("[0,0,0]"); 172 jTextFieldColorValue.setBorder(null); 173 jTextFieldColorValue.setPreferredSize(new java.awt.Dimension (100, 14)); 174 jTextFieldColorValue.addActionListener(new java.awt.event.ActionListener () { 175 public void actionPerformed(java.awt.event.ActionEvent evt) { 176 jTextFieldColorValueActionPerformed(evt); 177 } 178 }); 179 jTextFieldColorValue.addFocusListener(new java.awt.event.FocusAdapter () { 180 public void focusLost(java.awt.event.FocusEvent evt) { 181 jTextFieldColorValueFocusLost(evt); 182 } 183 }); 184 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 187 gridBagConstraints.weightx = 1.0; 188 add(jTextFieldColorValue, gridBagConstraints); 189 190 jButtonSelect.setText("..."); 191 jButtonSelect.setMargin(new java.awt.Insets (0, 0, 0, 0)); 192 jButtonSelect.setMaximumSize(new java.awt.Dimension (19, 19)); 193 jButtonSelect.setMinimumSize(new java.awt.Dimension (19, 10)); 194 jButtonSelect.setPreferredSize(new java.awt.Dimension (19, 22)); 195 jButtonSelect.addActionListener(new java.awt.event.ActionListener () { 196 public void actionPerformed(java.awt.event.ActionEvent evt) { 197 jButtonSelectActionPerformed(evt); 198 } 199 }); 200 201 gridBagConstraints = new java.awt.GridBagConstraints (); 202 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 203 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 204 gridBagConstraints.weighty = 1.0; 205 gridBagConstraints.insets = new java.awt.Insets (0, 4, 0, 0); 206 add(jButtonSelect, gridBagConstraints); 207 208 } 210 private void jTextFieldColorValueFocusLost(java.awt.event.FocusEvent evt) { jTextFieldColorValueActionPerformed(null); 212 } 214 private void jButtonSelectActionPerformed(java.awt.event.ActionEvent evt) { 216 Color c =JColorChooser.showDialog(this,"Pick a color...",this.getColor()); 217 if (c != null) setColor(c); 218 219 } 221 private void jTextFieldColorValueActionPerformed(java.awt.event.ActionEvent evt) { 223 Color newColor = parseColorString(jTextFieldColorValue.getText()); 224 if (newColor == null) 225 { 226 this.jTextFieldColorValue.setText(getValue()); 227 } 228 if (newColor != null) setColor(newColor); 229 230 } 232 236 public synchronized void addActionListener(java.awt.event.ActionListener listener) { 237 238 if (listenerList == null ) { 239 listenerList = new javax.swing.event.EventListenerList (); 240 } 241 listenerList.add (java.awt.event.ActionListener .class, listener); 242 } 243 244 248 public synchronized void removeActionListener(java.awt.event.ActionListener listener) { 249 250 listenerList.remove (java.awt.event.ActionListener .class, listener); 251 } 252 253 258 private void fireActionListenerActionPerformed(java.awt.event.ActionEvent event) { 259 260 if (listenerList == null) return; 261 Object [] listeners = listenerList.getListenerList (); 262 for (int i = listeners.length-2; i>=0; i-=2) { 263 if (listeners[i]==java.awt.event.ActionListener .class) { 264 ((java.awt.event.ActionListener )listeners[i+1]).actionPerformed (event); 265 } 266 } 267 } 268 269 270 private javax.swing.JButton jButtonSelect; 272 private javax.swing.JPanel jPanelColor; 273 private javax.swing.JTextField jTextFieldColorValue; 274 276 public void applyI18n(){ 277 jButtonSelect.setText(I18n.getString("colorSelectorPanel.buttonSelect","...")); 279 } 281 } 282 | Popular Tags |