1 14 package org.compiere.plaf; 15 16 import java.awt.BorderLayout ; 17 import java.awt.Component ; 18 import java.awt.Dialog ; 19 import java.awt.Dimension ; 20 import java.awt.FlowLayout ; 21 import java.awt.Frame ; 22 import java.awt.Graphics ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.Insets ; 26 import java.awt.Rectangle ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyEditor ; 31 import java.util.ResourceBundle ; 32 33 import javax.swing.BorderFactory ; 34 import javax.swing.JColorChooser ; 35 import javax.swing.JDialog ; 36 37 import org.compiere.swing.CButton; 38 import org.compiere.swing.CComboBox; 39 import org.compiere.swing.CLabel; 40 import org.compiere.swing.CPanel; 41 import org.compiere.swing.CTextField; 42 import org.compiere.util.KeyNamePair; 43 import org.compiere.util.ValueNamePair; 44 45 51 public class CompiereColorEditor extends JDialog  52 implements ActionListener , PropertyEditor  53 { 54 60 public static CompiereColor showDialog (Frame owner, CompiereColor color) 61 { 62 CompiereColorEditor cce = new CompiereColorEditor (owner, color); 63 if (cce.isSaved()) 64 return cce.getColor(); 65 return color; 66 } 68 74 public static CompiereColor showDialog (Dialog owner, CompiereColor color) 75 { 76 CompiereColorEditor cce = new CompiereColorEditor (owner, color); 77 if (cce.isSaved()) 78 return cce.getColor(); 79 return color; 80 } 82 83 84 89 public CompiereColorEditor (Frame owner, CompiereColor color) 90 { 91 super(owner, "", true); 92 init (color); 93 } 95 100 public CompiereColorEditor (Dialog owner, CompiereColor color) 101 { 102 super(owner, "", true); 103 init (color); 104 } 106 110 private void init (CompiereColor color) 111 { 112 try 113 { 114 jbInit(); 115 } 116 catch(Exception e) 117 { 118 e.printStackTrace(); 119 } 120 121 bOK.addActionListener(this); 122 bCancel.addActionListener(this); 123 typeField.addActionListener(this); 124 flatField.addActionListener(this); 125 gradientUpper.addActionListener(this); 126 gradientLower.addActionListener(this); 127 urlField.addActionListener(this); 128 alphaField.addActionListener(this); 129 taintColor.addActionListener(this); 130 lineColor.addActionListener(this); 131 backColor.addActionListener(this); 132 widthField.addActionListener(this); 133 distanceField.addActionListener(this); 134 gradientStartField.addActionListener(this); 135 gradientDistanceField.addActionListener(this); 136 137 if (color == null) 138 setColor (m_cc); 139 else 140 setColor (new CompiereColor(color)); 141 CompierePLAF.showCenterScreen(this); 142 } 144 145 private static ResourceBundle res = ResourceBundle.getBundle("org.compiere.plaf.PlafRes"); 146 private CompiereColor m_cc = null; 147 private boolean m_saved = false; 148 private boolean m_setting = false; 149 150 private CPanel northPanel = new CPanel(); 152 private CPanel southPanel = new CPanel(); 153 private CButton bOK = CompierePLAF.getOKButton(); 154 private CButton bCancel = CompierePLAF.getCancelButton(); 155 private FlowLayout southLayout = new FlowLayout (); 156 private GridBagLayout northLayout = new GridBagLayout (); 157 private CLabel typeLabel = new CLabel(); 158 private CComboBox typeField = new CComboBox(CompiereColor.TYPES); 159 private CButton gradientUpper = new CButton(); 160 private CButton gradientLower = new CButton(); 161 private CLabel urlLabel = new CLabel(); 162 private CTextField urlField = new CTextField(30); 163 private CLabel alphaLabel = new CLabel(); 164 private CTextField alphaField = new CTextField(10); 165 private CButton taintColor = new CButton(); 166 private CButton lineColor = new CButton(); 167 private CButton backColor = new CButton(); 168 private CLabel widthLabel = new CLabel(); 169 private CTextField widthField = new CTextField(10); 170 private CLabel distanceLabel = new CLabel(); 171 private CTextField distanceField = new CTextField(10); 172 private CPanel centerPanel = new CPanel(); 173 private CButton flatField = new CButton(); 174 private CComboBox gradientStartField = new CComboBox(CompiereColor.GRADIENT_SP); 175 private CTextField gradientDistanceField = new CTextField(10); 176 private CLabel gradientStartLabel = new CLabel(); 177 private CLabel gradientDistanceLabel = new CLabel(); 178 179 189 private void jbInit() throws Exception  190 { 191 this.setTitle(res.getString("CompiereColorEditor")); 192 CompiereColor.setBackground (this); 193 southPanel.setLayout(southLayout); 194 southLayout.setAlignment(FlowLayout.RIGHT); 195 northPanel.setLayout(northLayout); 196 typeLabel.setText(res.getString("CompiereType")); 197 gradientUpper.setText(res.getString("GradientUpperColor")); 198 gradientLower.setText(res.getString("GradientLowerColor")); 199 gradientStartLabel.setText(res.getString("GradientStart")); 200 gradientDistanceLabel.setText(res.getString("GradientDistance")); 201 urlLabel.setText(res.getString("TextureURL")); 202 alphaLabel.setText(res.getString("TextureAlpha")); 203 taintColor.setText(res.getString("TextureTaintColor")); 204 lineColor.setText(res.getString("LineColor")); 205 backColor.setText(res.getString("LineBackColor")); 206 widthLabel.setText(res.getString("LineWidth")); 207 distanceLabel.setText(res.getString("LineDistance")); 208 flatField.setText(res.getString("FlatColor")); 209 centerPanel.setBorder(BorderFactory.createRaisedBevelBorder()); 210 centerPanel.setPreferredSize(new Dimension (400, 200)); 211 centerPanel.setOpaque(true); 212 northPanel.setPreferredSize(new Dimension (400, 150)); 213 southPanel.add(bCancel, null); 214 this.getContentPane().add(northPanel, BorderLayout.NORTH); 215 southPanel.add(bOK, null); 216 this.getContentPane().add(southPanel, BorderLayout.SOUTH); 217 this.getContentPane().add(centerPanel, BorderLayout.CENTER); 218 northPanel.add(typeLabel, new GridBagConstraints (0, 0, 1, 1, 0.0, 0.0 219 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 220 northPanel.add(typeField, new GridBagConstraints (1, 0, 1, 1, 0.0, 0.0 221 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 5, 0, 5), 0, 0)); 222 northPanel.add(gradientLower, new GridBagConstraints (1, 2, 1, 1, 0.0, 0.0 223 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets (5, 5, 5, 5), 0, 0)); 224 northPanel.add(urlField, new GridBagConstraints (1, 5, 2, 1, 1.0, 0.0 225 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets (5, 5, 5, 5), 0, 0)); 226 northPanel.add(alphaLabel, new GridBagConstraints (0, 6, 1, 1, 0.0, 0.0 227 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 228 northPanel.add(alphaField, new GridBagConstraints (1, 6, 1, 1, 0.0, 0.0 229 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets (5, 5, 5, 5), 0, 0)); 230 northPanel.add(taintColor, new GridBagConstraints (1, 7, 1, 1, 0.0, 0.0 231 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets (5, 5, 5, 5), 0, 0)); 232 northPanel.add(backColor, new GridBagConstraints (1, 8, 1, 1, 0.0, 0.0 233 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets (5, 5, 5, 5), 0, 0)); 234 northPanel.add(widthLabel, new GridBagConstraints (0, 9, 1, 1, 0.0, 0.0 235 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 236 northPanel.add(widthField, new GridBagConstraints (1, 9, 1, 1, 0.0, 0.0 237 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 238 northPanel.add(distanceLabel, new GridBagConstraints (0, 10, 1, 1, 0.0, 0.0 239 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 240 northPanel.add(distanceField, new GridBagConstraints (1, 10, 1, 1, 0.0, 0.0 241 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 242 northPanel.add(flatField, new GridBagConstraints (1, 1, 1, 1, 0.0, 0.0 243 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets (5, 5, 5, 5), 0, 0)); 244 northPanel.add(gradientStartField, new GridBagConstraints (1, 3, 1, 1, 0.0, 0.0 245 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 246 northPanel.add(gradientDistanceField, new GridBagConstraints (1, 4, 1, 1, 0.0, 0.0 247 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 248 northPanel.add(urlLabel, new GridBagConstraints (0, 5, 1, 1, 0.0, 0.0 249 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 250 northPanel.add(gradientStartLabel, new GridBagConstraints (0, 3, 1, 1, 0.0, 0.0 251 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 252 northPanel.add(gradientDistanceLabel, new GridBagConstraints (0, 4, 1, 1, 0.0, 0.0 253 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 254 northPanel.add(gradientUpper, new GridBagConstraints (0, 2, 1, 1, 0.0, 0.0 255 ,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets (5, 5, 5, 5), 0, 0)); 256 northPanel.add(lineColor, new GridBagConstraints (0, 8, 1, 1, 0.0, 0.0 257 ,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets (5, 5, 5, 5), 0, 0)); 258 } 260 264 public void actionPerformed(ActionEvent e) 265 { 266 if (m_setting) 267 return; 268 if (e.getSource() == bOK) 269 { 270 m_saved = true; 271 dispose(); 272 return; 273 } 274 else if (e.getSource() == bCancel) 275 { 276 dispose(); 277 return; 278 } 279 280 281 try 282 { 283 if (e.getSource() == typeField) 285 cmd_type(); 286 else if (e.getSource() == flatField) 288 m_cc.setFlatColor(JColorChooser.showDialog 289 (this, flatField.getText(), m_cc.getFlatColor())); 290 else if (e.getSource() == gradientUpper) 292 m_cc.setGradientUpperColor(JColorChooser.showDialog 293 (this, gradientUpper.getText(), m_cc.getGradientUpperColor())); 294 else if (e.getSource() == gradientLower) 295 m_cc.setGradientLowerColor(JColorChooser.showDialog 296 (this, gradientLower.getText(), m_cc.getGradientLowerColor())); 297 else if (e.getSource() == gradientStartField) 298 m_cc.setGradientStartPoint 299 (((KeyNamePair)gradientStartField.getSelectedItem()).getKey()); 300 else if (e.getSource() == gradientDistanceField) 301 m_cc.setGradientRepeatDistance(gradientDistanceField.getText()); 302 else if (e.getSource() == urlField) 304 m_cc.setTextureURL(urlField.getText()); 305 else if (e.getSource() == alphaField) 306 m_cc.setTextureCompositeAlpha(alphaField.getText()); 307 else if (e.getSource() == taintColor) 308 m_cc.setTextureTaintColor(JColorChooser.showDialog 309 (this, taintColor.getText(), m_cc.getTextureTaintColor())); 310 else if (e.getSource() == lineColor) 312 m_cc.setLineColor(JColorChooser.showDialog 313 (this, lineColor.getText(), m_cc.getLineColor())); 314 else if (e.getSource() == backColor) 315 m_cc.setLineBackColor(JColorChooser.showDialog 316 (this, backColor.getText(), m_cc.getLineBackColor())); 317 else if (e.getSource() == widthField) 318 m_cc.setLineWidth(widthField.getText()); 319 else if (e.getSource() == distanceField) 320 m_cc.setLineDistance(distanceField.getText()); 321 } 322 catch (Exception ee) 323 { 324 } 325 setColor (m_cc); 326 } 328 331 private void cmd_type() 332 { 333 ValueNamePair vp = (ValueNamePair)typeField.getSelectedItem(); 334 if (vp.getValue().equals(CompiereColor.TYPE_FLAT)) 335 m_cc = new CompiereColor(CompiereColor.TYPE_FLAT); 336 else if (vp.getValue().equals(CompiereColor.TYPE_GRADIENT)) 337 m_cc = new CompiereColor(CompiereColor.TYPE_GRADIENT); 338 else if (vp.getValue().equals(CompiereColor.TYPE_TEXTURE)) 339 m_cc = new CompiereColor(CompiereColor.TYPE_TEXTURE); 340 else if (vp.getValue().equals(CompiereColor.TYPE_LINES)) 341 m_cc = new CompiereColor(CompiereColor.TYPE_LINES); 342 setColor (m_cc); 343 } 345 349 public void setColor (CompiereColor color) 350 { 351 if (color == null && m_cc != null) 352 return; 353 m_cc = color; 356 if (m_cc == null) 357 m_cc = CompierePanelUI.getDefaultBackground(); 358 359 updateFields(); 361 centerPanel.setBackgroundColor(m_cc); 362 centerPanel.repaint(); 363 } 365 368 private void updateFields() 369 { 370 m_setting = true; 371 for (int i = 0; i < CompiereColor.TYPES.length; i++) 373 { 374 if (m_cc.getType().equals(CompiereColor.TYPE_VALUES[i])) 375 { 376 typeField.setSelectedItem(CompiereColor.TYPES[i]); 377 break; 378 } 379 } 380 if (m_cc.isFlat()) 382 { 383 flatField.setVisible(true); 384 gradientUpper.setVisible(false); 385 gradientLower.setVisible(false); 386 gradientStartLabel.setVisible(false); 387 gradientDistanceLabel.setVisible(false); 388 gradientStartField.setVisible(false); 389 gradientDistanceField.setVisible(false); 390 urlLabel.setVisible(false); 391 urlField.setVisible(false); 392 alphaLabel.setVisible(false); 393 alphaField.setVisible(false); 394 taintColor.setVisible(false); 395 lineColor.setVisible(false); 396 backColor.setVisible(false); 397 widthLabel.setVisible(false); 398 widthField.setVisible(false); 399 distanceLabel.setVisible(false); 400 distanceField.setVisible(false); 401 flatField.setBackground(m_cc.getFlatColor()); 403 } 404 else if (m_cc.isGradient()) 405 { 406 flatField.setVisible(false); 407 gradientUpper.setVisible(true); 408 gradientLower.setVisible(true); 409 gradientStartLabel.setVisible(true); 410 gradientDistanceLabel.setVisible(true); 411 gradientStartField.setVisible(true); 412 gradientDistanceField.setVisible(true); 413 urlLabel.setVisible(false); 414 urlField.setVisible(false); 415 alphaLabel.setVisible(false); 416 alphaField.setVisible(false); 417 taintColor.setVisible(false); 418 lineColor.setVisible(false); 419 backColor.setVisible(false); 420 widthLabel.setVisible(false); 421 widthField.setVisible(false); 422 distanceLabel.setVisible(false); 423 distanceField.setVisible(false); 424 gradientUpper.setBackground(m_cc.getGradientUpperColor()); 426 gradientLower.setBackground(m_cc.getGradientLowerColor()); 427 gradientDistanceField.setText(String.valueOf(m_cc.getGradientRepeatDistance())); 428 for (int i = 0; i < CompiereColor.GRADIENT_SP.length; i++) 429 { 430 if (m_cc.getGradientStartPoint() == CompiereColor.GRADIENT_SP_VALUES[i]) 431 { 432 gradientStartField.setSelectedItem(CompiereColor.GRADIENT_SP[i]); 433 break; 434 } 435 } 436 } 437 else if (m_cc.isTexture()) 438 { 439 flatField.setVisible(false); 440 gradientUpper.setVisible(false); 441 gradientLower.setVisible(false); 442 gradientStartLabel.setVisible(false); 443 gradientDistanceLabel.setVisible(false); 444 gradientStartField.setVisible(false); 445 gradientDistanceField.setVisible(false); 446 urlLabel.setVisible(true); 447 urlField.setVisible(true); 448 alphaLabel.setVisible(true); 449 alphaField.setVisible(true); 450 taintColor.setVisible(true); 451 lineColor.setVisible(false); 452 backColor.setVisible(false); 453 widthLabel.setVisible(false); 454 widthField.setVisible(false); 455 distanceLabel.setVisible(false); 456 distanceField.setVisible(false); 457 urlField.setText(m_cc.getTextureURL().toString()); 459 alphaField.setText(String.valueOf(m_cc.getTextureCompositeAlpha())); 460 taintColor.setBackground(m_cc.getTextureTaintColor()); 461 } 462 else if (m_cc.isLine()) 463 { 464 flatField.setVisible(false); 465 gradientUpper.setVisible(false); 466 gradientLower.setVisible(false); 467 gradientStartLabel.setVisible(false); 468 gradientDistanceLabel.setVisible(false); 469 gradientStartField.setVisible(false); 470 gradientDistanceField.setVisible(false); 471 urlLabel.setVisible(false); 472 urlField.setVisible(false); 473 alphaLabel.setVisible(false); 474 alphaField.setVisible(false); 475 taintColor.setVisible(false); 476 lineColor.setVisible(true); 477 backColor.setVisible(true); 478 widthLabel.setVisible(true); 479 widthField.setVisible(true); 480 distanceLabel.setVisible(true); 481 distanceField.setVisible(true); 482 lineColor.setBackground(m_cc.getLineColor()); 484 backColor.setBackground(m_cc.getLineBackColor()); 485 widthField.setText(String.valueOf(m_cc.getLineWidth())); 486 distanceField.setText(String.valueOf(m_cc.getLineDistance())); 487 } 488 m_setting = false; 489 } 491 495 public CompiereColor getColor() 496 { 497 return m_cc; 498 } 500 504 public boolean isSaved() 505 { 506 return m_saved; 507 } 509 510 511 521 public void setValue(Object value) 522 { 523 if (value != null && value instanceof CompiereColor) 524 setColor (new CompiereColor((CompiereColor)value)); 525 else 526 throw new IllegalArgumentException ("CompiereColorEditor.setValue requires CompiereColor"); 527 } 529 535 public Object getValue() 536 { 537 return getColor(); 538 } 540 544 public boolean isPaintable() 545 { 546 return false; 547 } 548 549 564 public void paintValue(Graphics gfx, Rectangle box) 565 { 566 567 throw new java.lang.UnsupportedOperationException ("Method paintValue() not yet implemented."); 568 } 570 581 public String getJavaInitializationString() 582 { 583 return "new CompiereColor()"; 584 } 586 594 public String getAsText() 595 { 596 return m_cc.toString(); 597 } 599 607 public void setAsText(String text) throws java.lang.IllegalArgumentException  608 { 609 throw new java.lang.IllegalArgumentException ("CompiereColorEditor.setAsText not supported"); 610 } 612 623 public String [] getTags() 624 { 625 return null; 626 } 628 642 public Component getCustomEditor() 643 { 644 return this; 645 } 647 651 public boolean supportsCustomEditor() 652 { 653 return true; 654 } 656 665 public void addPropertyChangeListener(PropertyChangeListener listener) 666 { 667 super.addPropertyChangeListener(listener); 668 } 670 674 public void removePropertyChangeListener(PropertyChangeListener listener) 675 { 676 super.removePropertyChangeListener(listener); 677 } 679 } | Popular Tags |