KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > visual > model > FontModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * FontModel.java
22  *
23  * Created on October 26, 2004, 1:49 PM
24  */

25
26 package org.netbeans.modules.css.visual.model;
27
28 import java.awt.Font JavaDoc;
29 import java.awt.GraphicsEnvironment JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35 import javax.swing.DefaultComboBoxModel JavaDoc;
36 import javax.swing.DefaultListModel JavaDoc;
37
38 /**
39  * Model for the Font Style Editor data
40  * @author Winston Prakash
41  * @version 1.0
42  */

43 public class FontModel{
44     
45     Map JavaDoc fontFamilyNames = new HashMap JavaDoc();
46     List JavaDoc fontFaceNames = new ArrayList JavaDoc();
47
48     public FontModel(){
49         GraphicsEnvironment JavaDoc ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
50         Font JavaDoc[] fonts = ge.getAllFonts();
51
52         // Process each font
53
for (int i=0; i<fonts.length; i++) {
54             // Get font's family and face
55
String JavaDoc familyName = fonts[i].getFamily();
56             String JavaDoc faceName = fonts[i].getName();
57
58             // Add font to table
59
List JavaDoc list = (List JavaDoc)fontFamilyNames.get(familyName);
60             if (list == null) {
61                 list = new ArrayList JavaDoc();
62                 fontFamilyNames.put(familyName, list);
63
64             }
65             list.add(faceName);
66             fontFaceNames.add(faceName);
67         }
68     }
69
70
71     public DefaultListModel JavaDoc getFontFamilySetList(){
72         return new FontFamilySetList();
73     }
74
75     public DefaultListModel JavaDoc getFontList(){
76         return new FontList();
77     }
78
79     public DefaultListModel JavaDoc getWebFontList(){
80         return new WebFontList();
81     }
82
83     public DefaultListModel JavaDoc getFontFamilyList(){
84         return new FontFamilyList();
85     }
86
87     public DefaultListModel JavaDoc getFontSizeList(){
88         return new FontSizeList();
89     }
90
91     public DefaultComboBoxModel JavaDoc getFontSizeUnitList(){
92         return new FontSizeUnitList();
93     }
94
95     public DefaultComboBoxModel JavaDoc getFontStyleList(){
96         return new FontStyleList();
97     }
98
99     public DefaultComboBoxModel JavaDoc getFontSelectionList(){
100         return new FontSelectionList();
101     }
102
103     public DefaultComboBoxModel JavaDoc getFontWeightList(){
104         return new FontWeightList();
105     }
106
107     public DefaultComboBoxModel JavaDoc getFontVariantList(){
108         return new FontVariantList();
109     }
110
111     public FontSize getFontSize(String JavaDoc fontSizeStr){
112         return new FontSize(fontSizeStr);
113     }
114
115
116     public class FontSize{
117         FontSizeUnitList unitList = new FontSizeUnitList();
118         String JavaDoc fontSizeUnit = null;
119         String JavaDoc fontSize = null;
120         public FontSize(String JavaDoc fontSizeStr){
121             fontSize = fontSizeStr.trim();
122             for(int i=0; i< unitList.getSize(); i++){
123                 String JavaDoc unit = (String JavaDoc)unitList.getElementAt(i);
124                 if(fontSize.endsWith(unit)){
125                     fontSizeUnit = unit;
126                     fontSize = fontSize.replaceAll(unit,"");
127                 }
128             }
129         }
130
131         public String JavaDoc getUnit(){
132             return fontSizeUnit;
133         }
134
135         public String JavaDoc getValue(){
136             if(Utils.isInteger(fontSize)){
137                 return fontSize;
138             }else{
139                 return null;
140             }
141         }
142     }
143
144
145     public Font JavaDoc resolveFont(CssStyleData styleData, Font JavaDoc baseFont) {
146         String JavaDoc fontFamily = styleData.getProperty(CssProperties.FONT_FAMILY);
147         String JavaDoc fontSize = styleData.getProperty(CssProperties.FONT_SIZE);
148         String JavaDoc fontStyle = styleData.getProperty(CssProperties.FONT_STYLE);
149         String JavaDoc fontVariant = styleData.getProperty(CssProperties.FONT_VARIANT);
150         String JavaDoc fontWeight = styleData.getProperty(CssProperties.FONT_WEIGHT);
151         String JavaDoc name = resolveFontName(fontFamily, baseFont.getName());
152         int style = resolveFontStyle(fontStyle,fontWeight, baseFont.getStyle());
153         int size = resolveFontSize(fontSize, baseFont.getSize());
154         return new Font JavaDoc(name, style, size);
155     }
156     
157     private String JavaDoc resolveFontName(String JavaDoc fontFamily, String JavaDoc defName){
158         String JavaDoc fontName = defName;
159         if(fontFamily != null){
160             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(fontFamily.trim(),",");
161             while(st.hasMoreTokens()){
162                 String JavaDoc cssFamilyName = st.nextToken();
163                 cssFamilyName = cssFamilyName.replaceAll("'","");
164                 //Check first in the family names
165
if (fontFamilyNames.containsKey(cssFamilyName)){
166                     List JavaDoc fontFaceList = (List JavaDoc) fontFamilyNames.get(cssFamilyName);
167                     fontName = (String JavaDoc) fontFaceList.get(0);
168                 }else if (fontFaceNames.contains(cssFamilyName)){
169                     fontName = cssFamilyName;
170                 }
171             }
172         }
173         return fontName;
174     }
175     
176     private int resolveFontStyle(String JavaDoc fontStyle, String JavaDoc fontWeight, int defStyle){
177         int style = defStyle;
178         if((fontStyle != null) && (fontStyle.equals("italic") || fontStyle.equals("oblique"))){
179             //System.out.println(fontStyle);
180
style = Font.ITALIC;
181         }
182         if((fontWeight != null) && fontWeight.equals(fontWeight)){
183             style = style | Font.BOLD;
184         }
185         return style;
186     }
187     
188     private int resolveFontSize(String JavaDoc fontSizeStr, int defSize){
189         // XXX convert units to pixels
190
int size = defSize;
191         if(fontSizeStr != null){
192             FontSize fontSize = new FontSize(fontSizeStr);
193             String JavaDoc fontSizeValue = fontSize.getValue();
194             if(Utils.isInteger(fontSizeValue)){
195                 size = Utils.getInteger(fontSizeValue);
196             }else{
197                 if (fontSizeValue.equals("XX-small")) size = 4; //NOI18N
198
if (fontSizeValue.equals("X-small")) size = 6; //NOI18N
199
if (fontSizeValue.equals("small")) size = 8; //NOI18N
200
if (fontSizeValue.equals("medium")) size = 12; //NOI18N
201
if (fontSizeValue.equals("large")) size = 14; //NOI18N
202
if (fontSizeValue.equals("X-large")) size = 16; //NOI18N
203
if (fontSizeValue.equals("XX-large")) size = 20; //NOI18N
204
}
205             
206             if (size < 4) {
207                 size = 4;
208             }else if( size > 72){
209                 size = 72;
210             }
211         }
212         return size;
213     }
214     
215     
216     public static class FontSelectionList extends DefaultComboBoxModel JavaDoc{
217         public FontSelectionList(){
218             addElement(org.openide.util.NbBundle.getMessage(FontModel.class, "FONTS")); //NOI18N
219
addElement(org.openide.util.NbBundle.getMessage(FontModel.class, "FONT_FAMILIES")); //NOI18N
220
addElement(org.openide.util.NbBundle.getMessage(FontModel.class, "WEB_FONTS")); //NOI18N
221
}
222     }
223     
224     public static class FontList extends DefaultListModel JavaDoc{
225         public FontList(){
226             GraphicsEnvironment JavaDoc ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
227             String JavaDoc fontNames[] = ge.getAvailableFontFamilyNames();
228             
229             // Iterate the font names and add to the model
230
for (int i=0; i<fontNames.length; i++) {
231                 addElement(fontNames[i]);
232             }
233         }
234     }
235     
236     public static class WebFontList extends DefaultListModel JavaDoc{
237         public WebFontList(){
238             addElement(CssStyleData.NOT_SET);
239             String JavaDoc[] webFontValues = CssProperties.getWebFontValues();
240             for (int i=0; i<webFontValues.length; i++) {
241                 addElement(webFontValues[i]);
242             }
243         }
244     }
245     
246     public static class FontFamilyList extends DefaultListModel JavaDoc{
247         public FontFamilyList(){
248             addElement(CssStyleData.NOT_SET);
249             String JavaDoc[] FontfamilyValues = CssProperties.getFontFamilyValues();
250             for (int i=0; i<FontfamilyValues.length; i++) {
251                 addElement(FontfamilyValues[i]);
252             }
253         }
254     }
255     
256     public static class FontFamilySetList extends DefaultListModel JavaDoc{
257         public FontFamilySetList(){
258             addElement(CssStyleData.NOT_SET);
259             String JavaDoc[] FontfamilySetValues = CssProperties.getFontFamilySetValues();
260             for (int i=0; i<FontfamilySetValues.length; i++) {
261                 addElement(FontfamilySetValues[i]);
262             }
263         }
264     }
265     
266     public static class FontSizeList extends DefaultListModel JavaDoc{
267         public FontSizeList(){
268             addElement(CssStyleData.NOT_SET);
269             String JavaDoc[] FontSizeValues = CssProperties.getFontSizeValues();
270             for (int i=0; i<FontSizeValues.length; i++) {
271                 addElement(FontSizeValues[i]);
272             }
273         }
274     }
275     
276     public static class FontSizeUnitList extends DefaultComboBoxModel JavaDoc{
277         public FontSizeUnitList(){
278             String JavaDoc[] unitValues = CssProperties.getCssLengthUnits();
279             for(int i=0; i< unitValues.length; i++){
280                 addElement(unitValues[i]);
281             }
282         }
283     }
284     
285     public static class FontStyleList extends DefaultComboBoxModel JavaDoc{
286         public FontStyleList(){
287             String JavaDoc[] propValues = CssProperties.getCssPropertyValues(CssProperties.FONT_STYLE);
288             addElement(CssStyleData.NOT_SET);
289             for(int i=0; i< propValues.length; i++){
290                 addElement(propValues[i]);
291             }
292         }
293     }
294     
295     public static class FontWeightList extends DefaultComboBoxModel JavaDoc{
296         public FontWeightList(){
297             String JavaDoc[] propValues = CssProperties.getCssPropertyValues(CssProperties.FONT_WEIGHT);
298             addElement(CssStyleData.NOT_SET);
299             for(int i=0; i< propValues.length; i++){
300                 addElement(propValues[i]);
301             }
302             setSelectedItem("px");
303         }
304     }
305     
306     public static class FontVariantList extends DefaultComboBoxModel JavaDoc{
307         public FontVariantList(){
308             String JavaDoc[] propValues = CssProperties.getCssPropertyValues(CssProperties.FONT_VARIANT);
309             addElement(CssStyleData.NOT_SET);
310             for(int i=0; i< propValues.length; i++){
311                 addElement(propValues[i]);
312             }
313         }
314     }
315 }
316
Popular Tags