KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.font.*;
18 import java.util.*;
19
20 import org.compiere.model.*;
21 import org.compiere.util.*;
22
23 /**
24  * AD_PrintFont Print Font Model
25  *
26  * @author Jorg Janke
27  * @version $Id: MPrintFont.java,v 1.10 2003/07/06 18:43:01 jjanke Exp $
28  */

29 public class MPrintFont extends X_AD_PrintFont
30 {
31     /**
32      * Constructor
33      * @param ctx context
34      * @param AD_PrintFont_ID ID
35      */

36     private MPrintFont(Properties ctx, int AD_PrintFont_ID)
37     {
38         super (ctx, AD_PrintFont_ID);
39         if (AD_PrintFont_ID == 0)
40             setIsDefault(false);
41     } // MPrintFont
42

43     /** Font cached */
44     private Font m_cacheFont = null;
45
46     /*************************************************************************/
47
48     /**
49      * Get Font
50      * @return Font
51      */

52     public Font getFont()
53     {
54         if (m_cacheFont != null)
55             return m_cacheFont;
56         String JavaDoc code = (String JavaDoc)getValue("Code");
57         if (code == null || code.equals("."))
58             m_cacheFont = new Font (null);
59         try
60         {
61             if (code != null && !code.equals("."))
62             // fontfamilyname-style-pointsize
63
m_cacheFont = Font.decode(code);
64         }
65         catch (Exception JavaDoc e)
66         {
67             Log.error("MPrintFont.getFont", e);
68         }
69         if (code == null)
70             m_cacheFont = new Font (null); // family=dialog,name=Dialog,style=plain,size=12
71
// Log.trace(Log.l6_Database, "MPrintFont.getFont " + code, m_cacheFont);
72
return m_cacheFont;
73     } // getFont
74

75     /**
76      * Set Font
77      * @param font Font
78      */

79     public void setFont (Font font)
80     {
81         // fontfamilyname-style-pointsize
82
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
83         sb.append(font.getFamily()).append("-");
84         int style = font.getStyle();
85         if (style == Font.PLAIN)
86             sb.append("PLAIN");
87         else if (style == Font.BOLD)
88             sb.append("BOLD");
89         else if (style == Font.ITALIC)
90             sb.append("ITALIC");
91         else if (style == (Font.BOLD + Font.ITALIC))
92             sb.append("BOLDITALIC");
93         sb.append("-").append(font.getSize());
94         setCode(sb.toString());
95     } // setFont
96

97     /*************************************************************************/
98
99     /**
100      * Create Font in Database and save
101      * @param font font
102      * @return PrintFont
103      */

104     static MPrintFont create (Font font)
105     {
106         MPrintFont pf = new MPrintFont(Env.getCtx(), 0);
107         StringBuffer JavaDoc name = new StringBuffer JavaDoc (font.getName());
108         if (font.isBold())
109             name.append(" bold");
110         if (font.isItalic())
111             name.append(" italic");
112         name.append(" ").append(font.getSize());
113         pf.setName(name.toString());
114         pf.setFont(font);
115         pf.save();
116         return pf;
117     } // create
118

119     /**
120      * String Representation
121      * @return info
122      */

123     public String JavaDoc toString()
124     {
125         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("MPrintFont[");
126         sb.append("ID=").append(getID())
127             .append(",Name=").append(getName())
128             .append("PSName=").append(getFont().getPSName())
129             .append(getFont())
130             .append("]");
131         return sb.toString();
132     } // toString
133

134     /**
135      * Get PostScript Level 2 definition.
136      * e.g. /dialog 12 selectfont
137      * @return PostScript command
138      */

139     public String JavaDoc toPS()
140     {
141         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("/");
142         sb.append(getFont().getPSName());
143         if (getFont().isBold())
144             sb.append(" Bold");
145         if (getFont().isItalic())
146             sb.append(" Italic");
147         sb.append(" ").append(getFont().getSize())
148             .append(" selectfont");
149         return sb.toString();
150     } // toPS
151

152     /**
153      * Dump Font
154      * @param font font
155      */

156     static void dump (Font font)
157     {
158         System.out.println("Family=" + font.getFamily());
159         System.out.println("FontName=" + font.getFontName());
160         System.out.println("Name=" + font.getName());
161         System.out.println("PSName=" + font.getPSName());
162         System.out.println("Style=" + font.getStyle());
163         System.out.println("Size=" + font.getSize());
164         System.out.println("Attributes:");
165         Map map = font.getAttributes();
166         Iterator keys = map.keySet().iterator();
167         while (keys.hasNext())
168         {
169             Object JavaDoc key = keys.next();
170             Object JavaDoc value = map.get(key);
171             System.out.println(" - " + key + "=" + value);
172         }
173         System.out.println(font);
174     } // dump
175

176     /*************************************************************************/
177
178     /** Cached Fonts */
179     static private CCache s_fonts = new CCache("printFonts", 20);
180
181     /**
182      * Get Font
183      * @param AD_PrintFont_ID id
184      * @return Font
185      */

186     static public MPrintFont get (int AD_PrintFont_ID)
187     {
188         Integer JavaDoc key = new Integer JavaDoc(AD_PrintFont_ID);
189         MPrintFont pf = (MPrintFont)s_fonts.get(key);
190         if (pf == null)
191         {
192             pf = new MPrintFont (Env.getCtx(), AD_PrintFont_ID);
193             s_fonts.put(key, pf);
194         }
195         return pf;
196     } // get
197

198     /*************************************************************************/
199
200     /**
201      * Seed Fonts
202      * @param args args
203      */

204     public static void main(String JavaDoc[] args)
205     {
206         System.out.println("Available Fonts:");
207         String JavaDoc[] family = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
208         for (int i = 0; i < family.length; i++)
209             System.out.println(" - " + family[i]);
210
211         org.compiere.Compiere.startupClient();
212         MPrintFont pf = new MPrintFont(Env.getCtx(), 100);
213         dump( pf.getFont() );
214
215         String JavaDoc[] systemLocical = new String JavaDoc[] {"Dialog", "DialogInput", "Monospaced", "Serif", "SansSerif"};
216         for (int i = 0; i < systemLocical.length; i++)
217         {
218         // create(new Font(systemLocical[i], Font.BOLD, 13));
219
// create(new Font(systemLocical[i], Font.PLAIN, 11));
220
// create(new Font(systemLocical[i], Font.BOLD, 11));
221
// create(new Font(systemLocical[i], Font.ITALIC, 11));
222
// create(new Font(systemLocical[i], Font.PLAIN, 10));
223
// create(new Font(systemLocical[i], Font.BOLD, 10));
224
// create(new Font(systemLocical[i], Font.ITALIC, 10));
225
// create(new Font(systemLocical[i], Font.PLAIN, 9));
226
// create(new Font(systemLocical[i], Font.BOLD, 9));
227
// create(new Font(systemLocical[i], Font.ITALIC, 9));
228
// create(new Font(systemLocical[i], Font.PLAIN, 8));
229
// create(new Font(systemLocical[i], Font.BOLD, 8));
230
// create(new Font(systemLocical[i], Font.ITALIC, 8));
231
}
232
233         // Read All Fonts
234
int[] IDs = PO.getAllIDs ("AD_PrintFont", null);
235         for (int i = 0; i < IDs.length; i++)
236         {
237             pf = new MPrintFont(Env.getCtx(), IDs[i]);
238             System.out.println(IDs[i] + " = " + pf.getFont());
239         }
240
241     } // main
242
} // MPrintFont
243
Popular Tags