001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * FontModel.java
044: *
045: * Created on October 26, 2004, 1:49 PM
046: */
047:
048: package org.netbeans.modules.css.visual.model;
049:
050: import org.netbeans.modules.css.editor.model.CssRuleContent;
051: import java.awt.Font;
052: import java.awt.GraphicsEnvironment;
053: import java.util.ArrayList;
054: import java.util.HashMap;
055: import java.util.List;
056: import java.util.Map;
057: import java.util.StringTokenizer;
058: import javax.swing.DefaultComboBoxModel;
059: import javax.swing.DefaultListModel;
060:
061: /**
062: * Model for the Font Style Editor data
063: * @author Winston Prakash
064: * @version 1.0
065: */
066: public class FontModel {
067:
068: Map<String, List<String>> fontFamilyNames = new HashMap<String, List<String>>();
069: List<String> fontFaceNames = new ArrayList<String>();
070:
071: public FontModel() {
072: GraphicsEnvironment ge = GraphicsEnvironment
073: .getLocalGraphicsEnvironment();
074: Font[] fonts = ge.getAllFonts();
075:
076: // Process each font
077: for (int i = 0; i < fonts.length; i++) {
078: // Get font's family and face
079: String familyName = fonts[i].getFamily();
080: String faceName = fonts[i].getName();
081:
082: // Add font to table
083: List<String> list = fontFamilyNames.get(familyName);
084: if (list == null) {
085: list = new ArrayList<String>();
086: fontFamilyNames.put(familyName, list);
087:
088: }
089: list.add(faceName);
090: fontFaceNames.add(faceName);
091: }
092: }
093:
094: public DefaultListModel getFontFamilySetList() {
095: return new FontFamilySetList();
096: }
097:
098: public DefaultListModel getFontList() {
099: return new FontList();
100: }
101:
102: public DefaultListModel getWebFontList() {
103: return new WebFontList();
104: }
105:
106: public DefaultListModel getFontFamilyList() {
107: return new FontFamilyList();
108: }
109:
110: public DefaultListModel getFontSizeList() {
111: return new FontSizeList();
112: }
113:
114: public DefaultComboBoxModel getFontSizeUnitList() {
115: return new FontSizeUnitList();
116: }
117:
118: public DefaultComboBoxModel getFontStyleList() {
119: return new FontStyleList();
120: }
121:
122: public DefaultComboBoxModel getFontSelectionList() {
123: return new FontSelectionList();
124: }
125:
126: public DefaultComboBoxModel getFontWeightList() {
127: return new FontWeightList();
128: }
129:
130: public DefaultComboBoxModel getFontVariantList() {
131: return new FontVariantList();
132: }
133:
134: public FontSize getFontSize(String fontSizeStr) {
135: return new FontSize(fontSizeStr);
136: }
137:
138: public class FontSize {
139: FontSizeUnitList unitList = new FontSizeUnitList();
140: String fontSizeUnit = null;
141: String fontSize = null;
142:
143: public FontSize(String fontSizeStr) {
144: fontSize = fontSizeStr.trim();
145: for (int i = 0; i < unitList.getSize(); i++) {
146: String unit = (String) unitList.getElementAt(i);
147: if (fontSize.endsWith(unit)) {
148: fontSizeUnit = unit;
149: fontSize = fontSize.replaceAll(unit, "");
150: }
151: }
152: }
153:
154: public String getUnit() {
155: return fontSizeUnit;
156: }
157:
158: public String getValue() {
159: if (Utils.isInteger(fontSize)) {
160: return fontSize;
161: } else {
162: return null;
163: }
164: }
165: }
166:
167: public Font resolveFont(CssRuleContent styleData, Font baseFont) {
168: String fontFamily = styleData
169: .getProperty(CssProperties.FONT_FAMILY);
170: String fontSize = styleData
171: .getProperty(CssProperties.FONT_SIZE);
172: String fontStyle = styleData
173: .getProperty(CssProperties.FONT_STYLE);
174: String fontVariant = styleData
175: .getProperty(CssProperties.FONT_VARIANT);
176: String fontWeight = styleData
177: .getProperty(CssProperties.FONT_WEIGHT);
178: String name = resolveFontName(fontFamily, baseFont.getName());
179: int style = resolveFontStyle(fontStyle, fontWeight, baseFont
180: .getStyle());
181: int size = resolveFontSize(fontSize, baseFont.getSize());
182: return new Font(name, style, size);
183: }
184:
185: private String resolveFontName(String fontFamily, String defName) {
186: String fontName = defName;
187: if (fontFamily != null) {
188: StringTokenizer st = new StringTokenizer(fontFamily.trim(),
189: ",");
190: while (st.hasMoreTokens()) {
191: String cssFamilyName = st.nextToken();
192: cssFamilyName = cssFamilyName.replaceAll("'", "");
193: //Check first in the family names
194: if (fontFamilyNames.containsKey(cssFamilyName)) {
195: List fontFaceList = (List) fontFamilyNames
196: .get(cssFamilyName);
197: fontName = (String) fontFaceList.get(0);
198: } else if (fontFaceNames.contains(cssFamilyName)) {
199: fontName = cssFamilyName;
200: }
201: }
202: }
203: return fontName;
204: }
205:
206: private int resolveFontStyle(String fontStyle, String fontWeight,
207: int defStyle) {
208: int style = defStyle;
209: if ((fontStyle != null)
210: && (fontStyle.equals("italic") || fontStyle
211: .equals("oblique"))) {
212: //System.out.println(fontStyle);
213: style = Font.ITALIC;
214: }
215: if ((fontWeight != null) && fontWeight.equals(fontWeight)) {
216: style = style | Font.BOLD;
217: }
218: return style;
219: }
220:
221: private int resolveFontSize(String fontSizeStr, int defSize) {
222: // XXX convert units to pixels
223: int size = defSize;
224: if (fontSizeStr != null) {
225: FontSize fontSize = new FontSize(fontSizeStr);
226: String fontSizeValue = fontSize.getValue();
227: if (Utils.isInteger(fontSizeValue)) {
228: size = Utils.getInteger(fontSizeValue);
229: } else {
230: if (fontSizeValue.equals("XX-small"))
231: size = 4; //NOI18N
232: if (fontSizeValue.equals("X-small"))
233: size = 6; //NOI18N
234: if (fontSizeValue.equals("small"))
235: size = 8; //NOI18N
236: if (fontSizeValue.equals("medium"))
237: size = 12; //NOI18N
238: if (fontSizeValue.equals("large"))
239: size = 14; //NOI18N
240: if (fontSizeValue.equals("X-large"))
241: size = 16; //NOI18N
242: if (fontSizeValue.equals("XX-large"))
243: size = 20; //NOI18N
244: }
245:
246: if (size < 4) {
247: size = 4;
248: } else if (size > 72) {
249: size = 72;
250: }
251: }
252: return size;
253: }
254:
255: public static class FontSelectionList extends DefaultComboBoxModel {
256: public FontSelectionList() {
257: addElement(org.openide.util.NbBundle.getMessage(
258: FontModel.class, "FONTS")); //NOI18N
259: addElement(org.openide.util.NbBundle.getMessage(
260: FontModel.class, "FONT_FAMILIES")); //NOI18N
261: addElement(org.openide.util.NbBundle.getMessage(
262: FontModel.class, "WEB_FONTS")); //NOI18N
263: }
264: }
265:
266: public static class FontList extends DefaultListModel {
267: public FontList() {
268: GraphicsEnvironment ge = GraphicsEnvironment
269: .getLocalGraphicsEnvironment();
270: String fontNames[] = ge.getAvailableFontFamilyNames();
271:
272: // Iterate the font names and add to the model
273: for (int i = 0; i < fontNames.length; i++) {
274: addElement(fontNames[i]);
275: }
276: }
277: }
278:
279: public static class WebFontList extends DefaultListModel {
280: public WebFontList() {
281: addElement(CssRuleContent.NOT_SET);
282: String[] webFontValues = CssProperties.getWebFontValues();
283: for (int i = 0; i < webFontValues.length; i++) {
284: addElement(webFontValues[i]);
285: }
286: }
287: }
288:
289: public static class FontFamilyList extends DefaultListModel {
290: public FontFamilyList() {
291: addElement(CssRuleContent.NOT_SET);
292: String[] FontfamilyValues = CssProperties
293: .getFontFamilyValues();
294: for (int i = 0; i < FontfamilyValues.length; i++) {
295: addElement(FontfamilyValues[i]);
296: }
297: }
298: }
299:
300: public static class FontFamilySetList extends DefaultListModel {
301: public FontFamilySetList() {
302: addElement(CssRuleContent.NOT_SET);
303: String[] FontfamilySetValues = CssProperties
304: .getFontFamilySetValues();
305: for (int i = 0; i < FontfamilySetValues.length; i++) {
306: addElement(FontfamilySetValues[i]);
307: }
308: }
309: }
310:
311: public static class FontSizeList extends DefaultListModel {
312: public FontSizeList() {
313: addElement(CssRuleContent.NOT_SET);
314: String[] FontSizeValues = CssProperties.getFontSizeValues();
315: for (int i = 0; i < FontSizeValues.length; i++) {
316: addElement(FontSizeValues[i]);
317: }
318: }
319: }
320:
321: public static class FontSizeUnitList extends DefaultComboBoxModel {
322: public FontSizeUnitList() {
323: String[] unitValues = CssProperties.getCssLengthUnits();
324: for (int i = 0; i < unitValues.length; i++) {
325: addElement(unitValues[i]);
326: }
327: }
328: }
329:
330: public static class FontStyleList extends DefaultComboBoxModel {
331: public FontStyleList() {
332: String[] propValues = CssProperties
333: .getCssPropertyValues(CssProperties.FONT_STYLE);
334: addElement(CssRuleContent.NOT_SET);
335: for (int i = 0; i < propValues.length; i++) {
336: addElement(propValues[i]);
337: }
338: }
339: }
340:
341: public static class FontWeightList extends DefaultComboBoxModel {
342: public FontWeightList() {
343: String[] propValues = CssProperties
344: .getCssPropertyValues(CssProperties.FONT_WEIGHT);
345: addElement(CssRuleContent.NOT_SET);
346: for (int i = 0; i < propValues.length; i++) {
347: addElement(propValues[i]);
348: }
349: setSelectedItem("px");
350: }
351: }
352:
353: public static class FontVariantList extends DefaultComboBoxModel {
354: public FontVariantList() {
355: String[] propValues = CssProperties
356: .getCssPropertyValues(CssProperties.FONT_VARIANT);
357: addElement(CssRuleContent.NOT_SET);
358: for (int i = 0; i < propValues.length; i++) {
359: addElement(propValues[i]);
360: }
361: }
362: }
363: }
|