KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > MPrintColor


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-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.print;
15
16 import java.awt.*;
17 import java.util.*;
18
19 import org.compiere.model.*;
20 import org.compiere.util.*;
21
22 /**
23  * AD_PrintColor Print Color Model
24  *
25  * @author Jorg Janke
26  * @version $Id: MPrintColor.java,v 1.11 2003/07/06 18:43:01 jjanke Exp $
27  */

28 public class MPrintColor extends X_AD_PrintColor
29 {
30     /**
31      * Constructor
32      * @param ctx context
33      * @param AD_PrintColor_ID ID
34      */

35     private MPrintColor(Properties ctx, int AD_PrintColor_ID)
36     {
37         super (ctx, AD_PrintColor_ID);
38         if (AD_PrintColor_ID == 0)
39             setIsDefault(false);
40     } // MPrintColor
41

42     /** Color cached */
43     private Color m_cacheColor = null;
44
45     /*************************************************************************/
46
47     /**
48      * Get Color
49      * @return Color
50      */

51     public Color getColor()
52     {
53         if (m_cacheColor != null)
54             return m_cacheColor;
55         String JavaDoc code = getCode();
56         if (code == null || code.equals("."))
57             m_cacheColor = Color.black;
58         try
59         {
60             if (code != null && !code.equals("."))
61             {
62                 int rgba = Integer.parseInt(code);
63                 m_cacheColor = new Color(rgba, false);
64             }
65         }
66         catch (Exception JavaDoc e)
67         {
68             Log.error("MPrintColor.getColor", e);
69         }
70         if (code == null)
71             m_cacheColor = Color.black;
72     // Log.trace(Log.l6_Database, "MPrintColor.getColor " + code, m_cacheColor);
73
return m_cacheColor;
74     } // getColor
75

76     /**
77      * Set Color
78      * @param color Color
79      */

80     public void setColor (Color color)
81     {
82         int rgba = color.getRGB();
83         super.setCode(String.valueOf(rgba));
84     } // setColor
85

86     /**
87      * String Representation
88      * @return info
89      */

90     public String JavaDoc toString()
91     {
92         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("MPrintColor[");
93         sb.append("ID=").append(getID())
94             .append(",Name=").append(getName())
95             .append(",RGB=").append(getCode())
96             .append(",").append(getColor())
97             .append("]");
98         return sb.toString();
99     } // toString
100

101
102     /*************************************************************************/
103
104     /**
105      * Create Color in Database and save
106      * @param color color
107      * @param name name
108      * @return MPrintColor
109      */

110     static MPrintColor create (Color color, String JavaDoc name)
111     {
112         MPrintColor pc = new MPrintColor (Env.getCtx(), 0);
113         pc.setName(name);
114         pc.setColor(color);
115         pc.save();
116         return pc;
117     } // create
118

119     /*************************************************************************/
120
121     public static final Color darkGreen = new Color (0, 128, 0);
122     public static final Color blackGreen = new Color (0, 64, 0);
123
124     public static final Color darkBlue = new Color (0, 0, 128);
125     public static final Color blackBlue = new Color (0, 0, 64);
126
127     public static final Color whiteGray = new Color (224, 224, 224);
128
129     public static final Color brown = new Color (153, 102, 51);
130     public static final Color darkBrown = new Color (102, 51, 0);
131
132     /*************************************************************************/
133
134     /** Cached Colors */
135     static private CCache s_colors = new CCache("printColors", 20);
136
137     /**
138      * Get Color
139      * @param AD_PrintColor_ID id
140      * @return Color
141      */

142     static public MPrintColor get (int AD_PrintColor_ID)
143     {
144         Integer JavaDoc key = new Integer JavaDoc(AD_PrintColor_ID);
145         MPrintColor pc = (MPrintColor)s_colors.get(key);
146         if (pc == null)
147         {
148             pc = new MPrintColor (Env.getCtx(), AD_PrintColor_ID);
149             s_colors.put(key, pc);
150         }
151         return pc;
152     } // get
153

154     /*************************************************************************/
155
156     /**
157      * Create Standard Colors
158      * @param args args
159      */

160     public static void main(String JavaDoc[] args)
161     {
162         org.compiere.Compiere.startupClient();
163         Color[] colors = new Color[]
164             {Color.black, Color.red, Color.green, Color.blue,
165             Color.darkGray, Color.gray, Color.lightGray, Color.white,
166             Color.cyan, Color.magenta, Color.orange, Color.pink, Color.yellow,
167             SystemColor.textHighlight};
168         String JavaDoc[] names = new String JavaDoc[]
169             {"Black", "Red", "Green", "Blue",
170             "Gray dark", "Gray", "Gray light", "White",
171             "Cyan", "Magenta", "Orange", "Pink", "Yellow",
172             "Blue dark"};
173         for (int i = 0; i < colors.length; i++)
174             System.out.println(names[i] + " = " + colors[i] + " RGB=" + colors[i].getRGB()
175                 + " -> " + new Color(colors[i].getRGB(), false)
176                 + " -> " + new Color(colors[i].getRGB(), true));
177 /**
178         // Create Colors
179         for (int i = 0; i < colors.length; i++)
180             create(colors[i], names[i]);
181         create(whiteGray, "Gray white");
182         create(darkGreen, "Green dark");
183         create(blackGreen, "Green black");
184         create(blackBlue, "Blue black");
185         create(brown, "Brown");
186         create(darkBrown, "Brown dark");
187 **/

188
189         // Read All Colors
190
int[] IDs = PO.getAllIDs ("AD_PrintColor", null);
191         for (int i = 0; i < IDs.length; i++)
192         {
193             MPrintColor pc = new MPrintColor(Env.getCtx(), IDs[i]);
194             System.out.println(IDs[i] + ": " + pc + " = " + pc.getColor() + ", RGB=" + pc.getColor().getRGB());
195         }
196     } // main
197
} // MPrintColor
198
Popular Tags