1 14 package org.compiere.grid.ed; 15 16 import java.beans.*; 17 import javax.swing.*; 18 import javax.swing.border.*; 19 import java.awt.*; 20 import java.awt.event.*; 21 import java.math.*; 22 import java.sql.*; 23 24 import org.compiere.model.*; 25 import org.compiere.plaf.*; 26 import org.compiere.swing.*; 27 import org.compiere.util.*; 28 29 36 public class VColor extends CButton 37 implements VEditor, ActionListener 38 { 39 45 public VColor (MTab mTab, boolean mandatory, boolean isReadOnly) 46 { 47 m_mTab = mTab; 48 setMandatory(mandatory); 49 setReadWrite(!isReadOnly); 50 addActionListener(this); 51 } 53 56 public void dispose() 57 { 58 m_mTab = null; 59 } 61 private MTab m_mTab; 62 private boolean m_mandatory; 63 private CompiereColor m_cc = null; 65 private Object m_value; 66 67 71 public void setMandatory (boolean mandatory) 72 { 73 m_mandatory = mandatory; 74 } 76 80 public boolean isMandatory() 81 { 82 return m_mandatory; 83 } 85 89 public void setBackground (boolean error) 90 { 91 } 93 97 public void setValue (Object value) 98 { 99 Log.trace(Log.l4_Data, "VColor.setValue", value); 100 m_value = value; 101 m_cc = getCompiereColor(); 102 103 setText(getDisplay()); 105 if (m_cc != null) 106 setBackgroundColor(m_cc); 107 else 108 { 109 setOpaque(false); 110 putClientProperty(CompierePLAF.BACKGROUND, null); 111 } 112 repaint(); 113 } 115 119 public Object getValue() 120 { 121 return m_value; 122 } 124 128 public String getDisplay() 129 { 130 if (m_cc == null) 131 return "-/-"; 132 return " "; 133 } 135 139 public void propertyChange (PropertyChangeEvent evt) 140 { 141 if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY)) 143 { 144 setValue(evt.getNewValue()); 145 setBackground(false); 146 } 147 } 149 153 public void setField (MField mField) 154 { 155 mField.setOldValue(false); } 158 159 160 165 private CompiereColor getCompiereColor() 166 { 167 Integer AD_Color_ID = (Integer )m_mTab.getValue("AD_Color_ID"); 168 Log.trace(Log.l5_DData, "VColor.getCompiereColor AD_Color_ID", AD_Color_ID); 169 CompiereColor cc = null; 170 171 String ColorType = (String )m_mTab.getValue("ColorType"); 173 if (ColorType == null) 174 { 175 Log.trace(Log.l6_Database, "VColor.getCompiereColor - No ColorType"); 176 return null; 177 } 178 if (ColorType.equals(CompiereColor.TYPE_FLAT)) 180 { 181 cc = new CompiereColor(getColor(true), true); 182 } 183 else if (ColorType.equals(CompiereColor.TYPE_GRADIENT)) 184 { 185 BigDecimal RepeatDistance = (BigDecimal)m_mTab.getValue("RepeatDistance"); 186 String StartPoint = (String )m_mTab.getValue("StartPoint"); 187 int repeatDistance = RepeatDistance == null ? 0 : RepeatDistance.intValue(); 188 int startPoint = StartPoint == null ? 0 : Integer.parseInt(StartPoint); 189 cc = new CompiereColor(getColor(true), getColor(false), startPoint, repeatDistance); 190 } 191 else if (ColorType.equals(CompiereColor.TYPE_LINES)) 192 { 193 BigDecimal LineWidth = (BigDecimal)m_mTab.getValue("LineWidth"); 194 BigDecimal LineDistance = (BigDecimal)m_mTab.getValue("LineDistance"); 195 int lineWidth = LineWidth == null ? 0 : LineWidth.intValue(); 196 int lineDistance = LineDistance == null ? 0 : LineDistance.intValue(); 197 cc = new CompiereColor(getColor(false), getColor(true), lineWidth, lineDistance); 198 } 199 else if (ColorType.equals(CompiereColor.TYPE_TEXTURE)) 200 { 201 Integer AD_Image_ID = (Integer )m_mTab.getValue("AD_Image_ID"); 202 String url = getURL(AD_Image_ID); 203 if (url == null) 204 return null; 205 BigDecimal ImageAlpha = (BigDecimal)m_mTab.getValue("ImageAlpha"); 206 float compositeAlpha = ImageAlpha == null ? 0.7f : ImageAlpha.floatValue(); 207 cc = new CompiereColor(url, getColor(true), compositeAlpha); 208 } 209 else 210 return null; 211 212 Log.trace(Log.l6_Database, "VColor.getCompiereColor", cc); 213 return cc; 214 } 216 221 private Color getColor (boolean primary) 222 { 223 String add = primary ? "" : "_1"; 224 BigDecimal Red = (BigDecimal)m_mTab.getValue("Red" + add); 225 BigDecimal Green = (BigDecimal)m_mTab.getValue("Green" + add); 226 BigDecimal Blue = (BigDecimal)m_mTab.getValue("Blue" + add); 227 int red = Red == null ? 0 : Red.intValue(); 229 int green = Green == null ? 0 : Green.intValue(); 230 int blue = Blue == null ? 0 : Blue.intValue(); 231 return new Color (red, green, blue); 233 } 235 240 private String getURL (Integer AD_Image_ID) 241 { 242 if (AD_Image_ID == null || AD_Image_ID.intValue() == 0) 243 return null; 244 String retValue = null; 246 String sql = "SELECT ImageURL FROM AD_Image WHERE AD_Image_ID=?"; 247 try 248 { 249 PreparedStatement pstmt = DB.prepareStatement(sql); 250 pstmt.setInt (1, AD_Image_ID.intValue()); 251 ResultSet rs = pstmt.executeQuery(); 252 if (rs.next()) 253 { 254 retValue = rs.getString(1); 255 } 256 rs.close(); 257 pstmt.close(); 258 } 259 catch (SQLException e) 260 { 261 Log.error("VColor.getURL", e); 262 } 263 return retValue; 264 } 266 267 268 272 public void actionPerformed (ActionEvent e) 273 { 274 CompiereColor cc = CompiereColorEditor.showDialog((JFrame)Env.getParent(this), m_cc); 276 if (cc == null) 277 { 278 Log.trace(Log.l1_User, "VColor.actionPerformed - no color"); 279 return; 280 } 281 setBackgroundColor(cc); repaint(); 283 284 m_mTab.setValue("ColorType", cc.getType()); 286 if (cc.isFlat()) 287 { 288 setColor (cc.getFlatColor(), true); 289 } 290 else if (cc.isGradient()) 291 { 292 setColor (cc.getGradientUpperColor(), true); 293 setColor (cc.getGradientLowerColor(), false); 294 m_mTab.setValue("RepeatDistance", new BigDecimal(cc.getGradientRepeatDistance())); 295 m_mTab.setValue("StartPoint", String.valueOf(cc.getGradientStartPoint())); 296 } 297 else if (cc.isLine()) 298 { 299 setColor (cc.getLineBackColor(), true); 300 setColor (cc.getLineColor(), false); 301 m_mTab.getValue("LineWidth"); 302 m_mTab.getValue("LineDistance"); 303 } 304 else if (cc.isTexture()) 305 { 306 setColor (cc.getTextureTaintColor(), true); 307 m_mTab.setValue("ImageAlpha", new BigDecimal(cc.getTextureCompositeAlpha())); 310 } 311 m_cc = cc; 312 } 314 319 private void setColor (Color c, boolean primary) 320 { 321 String add = primary ? "" : "_1"; 322 m_mTab.setValue("Red" + add, new BigDecimal(c.getRed())); 323 m_mTab.setValue("Green" + add, new BigDecimal(c.getGreen())); 324 m_mTab.setValue("Blue" + add, new BigDecimal(c.getBlue())); 325 } 327 } | Popular Tags |