KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > ed > VColor


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

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 /**
30  * Color Editor.
31  * The editor stores/gets the attributes from the tab.
32  *
33  * @author Jorg Janke
34  * @version $Id: VColor.java,v 1.11 2002/09/02 01:00:57 jjanke Exp $
35  */

36 public class VColor extends CButton
37     implements VEditor, ActionListener
38 {
39     /**
40      * Constructor
41      * @param mTab Tab
42      * @param mandatory mandatory
43      * @param isReadOnly read only
44      */

45     public VColor (MTab mTab, boolean mandatory, boolean isReadOnly)
46     {
47         m_mTab = mTab;
48         setMandatory(mandatory);
49         setReadWrite(!isReadOnly);
50         addActionListener(this);
51     } // VColor
52

53     /**
54      * Dispose
55      */

56     public void dispose()
57     {
58         m_mTab = null;
59     } // dispose
60

61     private MTab m_mTab;
62     private boolean m_mandatory;
63 // private int m_AD_Color_ID = 0;
64
private CompiereColor m_cc = null;
65     private Object JavaDoc m_value;
66
67     /**
68      * Set Mandatory
69      * @param mandatory mandatory
70      */

71     public void setMandatory (boolean mandatory)
72     {
73         m_mandatory = mandatory;
74     } // setMandatory
75

76     /**
77      * Is Mandatory
78      * @return true if Mandatory
79      */

80     public boolean isMandatory()
81     {
82         return m_mandatory;
83     } // isMandatory
84

85     /**
86      * Set Background (nop)
87      * @param error error
88      */

89     public void setBackground (boolean error)
90     {
91     } // setBackground
92

93     /**
94      * Set Value
95      * @param value value
96      */

97     public void setValue (Object JavaDoc value)
98     {
99         Log.trace(Log.l4_Data, "VColor.setValue", value);
100         m_value = value;
101         m_cc = getCompiereColor();
102
103         // Display It
104
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     } // setValue
114

115     /**
116      * GetValue
117      * @return value
118      */

119     public Object JavaDoc getValue()
120     {
121         return m_value;
122     } // getValue
123

124     /**
125      * Get Displayed Value
126      * @return String representation
127      */

128     public String JavaDoc getDisplay()
129     {
130         if (m_cc == null)
131             return "-/-";
132         return " ";
133     } // getDisplay
134

135     /**
136      * Property Change Listener
137      * @param evt event
138      */

139     public void propertyChange (PropertyChangeEvent evt)
140     {
141     // Log.trace(Log.l4_Data, "VColor.propertyChange", evt);
142
if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
143         {
144             setValue(evt.getNewValue());
145             setBackground(false);
146         }
147     } // propertyChange
148

149     /**
150      * Set Field/WindowNo for ValuePreference
151      * @param mField field
152      */

153     public void setField (MField mField)
154     {
155         mField.setOldValue(false); // fire every time
156
} // setField
157

158     /*************************************************************************/
159
160     /**
161      * Load Color from Tab
162      * @return true if loaded
163      * @see org.compiere.model.MColor#getCompiereColor
164      */

165     private CompiereColor getCompiereColor()
166     {
167         Integer JavaDoc AD_Color_ID = (Integer JavaDoc)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         // Color Type
172
String JavaDoc ColorType = (String JavaDoc)m_mTab.getValue("ColorType");
173         if (ColorType == null)
174         {
175             Log.trace(Log.l6_Database, "VColor.getCompiereColor - No ColorType");
176             return null;
177         }
178         //
179
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 JavaDoc StartPoint = (String JavaDoc)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 JavaDoc AD_Image_ID = (Integer JavaDoc)m_mTab.getValue("AD_Image_ID");
202             String JavaDoc 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     } // getCompiereColor
215

216     /**
217      * Get Color from Tab
218      * @param primary true if primary false if secondary
219      * @return Color
220      */

221     private Color getColor (boolean primary)
222     {
223         String JavaDoc 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         //
228
int red = Red == null ? 0 : Red.intValue();
229         int green = Green == null ? 0 : Green.intValue();
230         int blue = Blue == null ? 0 : Blue.intValue();
231         //
232
return new Color (red, green, blue);
233     } // getColor
234

235     /**
236      * Get URL from Image
237      * @param AD_Image_ID image
238      * @return URL as String or null
239      */

240     private String JavaDoc getURL (Integer JavaDoc AD_Image_ID)
241     {
242         if (AD_Image_ID == null || AD_Image_ID.intValue() == 0)
243             return null;
244         //
245
String JavaDoc retValue = null;
246         String JavaDoc 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     } // getURL
265

266     /*************************************************************************/
267
268     /**
269      * Action Listener - Open Dialog
270      * @param e event
271      */

272     public void actionPerformed (ActionEvent e)
273     {
274         // Show Dialog
275
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); // set Button
282
repaint();
283
284         // Update Values
285
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         // URL url = cc.getTextureURL();
308
// m_mTab.setValue("AD_Image_ID");
309
m_mTab.setValue("ImageAlpha", new BigDecimal(cc.getTextureCompositeAlpha()));
310         }
311         m_cc = cc;
312     } // actionPerformed
313

314     /**
315      * Set Color in Tab
316      * @param c Color
317      * @param primary true if primary false if secondary
318      */

319     private void setColor (Color c, boolean primary)
320     {
321         String JavaDoc 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     } // setColor
326

327 } // VColor
328
Popular Tags