KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MColor


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.model;
15
16 import java.awt.*;
17 import java.util.*;
18 import java.sql.*;
19 import java.math.*;
20
21 import org.compiere.util.*;
22 import org.compiere.plaf.*;
23
24 /**
25  * Color Persistent Object Model
26  * (DisplayType=27)
27  *
28  * @author Jorg Janke
29  * @version $Id: MColor.java,v 1.8 2003/07/06 18:39:44 jjanke Exp $
30  */

31 public class MColor extends X_AD_Color
32 {
33     /**
34      * Color Model
35      * @param ctx context
36      * @param AD_Color_ID color
37      */

38     public MColor(Properties ctx, int AD_Color_ID)
39     {
40         super (ctx, AD_Color_ID);
41         if (AD_Color_ID == 0)
42             setName("-/-");
43     } // MColor
44

45     /**
46      * String Representation
47      * @return string
48      */

49     public String JavaDoc toString()
50     {
51         return "MColor[ID=" + getID() + " - " + getName() + "]";
52     } // toString
53

54     /**
55      * Load Special data (images, ..).
56      * To be extended by sub-classes
57      * @param rs result set
58      * @param index zero based index
59      * @return value
60      * @throws SQLException
61      */

62     protected Object JavaDoc loadSpecial (ResultSet rs, int index) throws SQLException
63     {
64         Log.trace(Log.l4_Data, "MColor.loadSpecial", p_info.getColumnName(index));
65         if (index == getColumnIndex("ColorType"))
66             return rs.getString(index+1);
67         return null;
68     } // loadSpecial
69

70
71     /**
72      * Save Special Data.
73      * AD_Image_ID (Background)
74      * @param value value
75      * @param index index
76      * @return SQL code for INSERT VALUES clause
77      */

78     protected String JavaDoc saveNewSpecial (Object JavaDoc value, int index)
79     {
80         String JavaDoc colName = p_info.getColumnName(index);
81         String JavaDoc colValue = value == null ? "null" : value.getClass().toString();
82         Log.trace(Log.l5_DData, "MColor.saveNewSpecial " + colName, colValue);
83         if (value == null)
84             return "NULL";
85         return value.toString();
86     } // saveNewSpecial
87

88     /*************************************************************************/
89
90     /**
91      * Get CompiereColor
92      * @return CompiereColor
93      * @see org.compiere.grid.ed.VColor#getCompiereColor
94      */

95     public CompiereColor getCompiereColor()
96     {
97         if (getID() == 0)
98             return null;
99
100         // Color Type
101
String JavaDoc ColorType = (String JavaDoc)getColorType();
102         if (ColorType == null)
103         {
104             Log.error("MColor.getCompiereColor - No ColorType");
105             return null;
106         }
107         CompiereColor cc = null;
108         //
109
if (ColorType.equals(CompiereColor.TYPE_FLAT))
110         {
111             cc = new CompiereColor(getColor(true), true);
112         }
113         else if (ColorType.equals(CompiereColor.TYPE_GRADIENT))
114         {
115             int RepeatDistance = getRepeatDistance();
116             String JavaDoc StartPoint = getStartPoint();
117             int startPoint = StartPoint == null ? 0 : Integer.parseInt(StartPoint);
118             cc = new CompiereColor(getColor(true), getColor(false), startPoint, RepeatDistance);
119         }
120         else if (ColorType.equals(CompiereColor.TYPE_LINES))
121         {
122             int LineWidth = getLineWidth();
123             int LineDistance = getLineDistance();
124             cc = new CompiereColor(getColor(false), getColor(true), LineWidth, LineDistance);
125         }
126         else if (ColorType.equals(CompiereColor.TYPE_TEXTURE))
127         {
128             int AD_Image_ID = getAD_Image_ID();
129             String JavaDoc url = getURL(AD_Image_ID);
130             if (url == null)
131                 return null;
132             BigDecimal ImageAlpha = getImageAlpha();
133             float compositeAlpha = ImageAlpha == null ? 0.7f : ImageAlpha.floatValue();
134             cc = new CompiereColor(url, getColor(true), compositeAlpha);
135         }
136         return cc;
137     } // getCompiereColor
138

139     /**
140      * Get Color
141      * @param primary true if primary false if secondary
142      * @return Color
143      */

144     private Color getColor (boolean primary)
145     {
146         int red = primary ? getRed() : getRed_1();
147         int green = primary ? getGreen() : getGreen_1();
148         int blue = primary ? getBlue() : getBlue_1();
149         //
150
return new Color (red, green, blue);
151     } // getColor
152

153     /**
154      * Get URL from Image
155      * @param AD_Image_ID image
156      * @return URL as String or null
157      */

158     private String JavaDoc getURL (int AD_Image_ID)
159     {
160         if (AD_Image_ID == 0)
161             return null;
162         //
163
String JavaDoc retValue = null;
164         String JavaDoc sql = "SELECT ImageURL FROM AD_Image WHERE AD_Image_ID=?";
165         try
166         {
167             PreparedStatement pstmt = DB.prepareStatement(sql);
168             pstmt.setInt (1, AD_Image_ID);
169             ResultSet rs = pstmt.executeQuery();
170             if (rs.next())
171             {
172                 retValue = rs.getString(1);
173             }
174             rs.close();
175             pstmt.close();
176         }
177         catch (SQLException e)
178         {
179             Log.error("MColor.getURL", e);
180         }
181         return retValue;
182     } // getURL
183

184 } // MColor
185
Popular Tags