| 1 |
/* |
| 2 |
* @(#) FontNameConverter.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2003 JIDE Software. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import java.awt.*; |
| 9 |
|
| 10 |
/** |
| 11 |
* Converter which converts Font Name String to String and converts it back. It's almost the same as StringConverter |
| 12 |
* except if user types in a string which cannot find in font list on your computer, it will return null in fromString |
| 13 |
* and return "" in toString. |
| 14 |
*/ |
| 15 |
public class FontNameConverter implements ObjectConverter { |
| 16 |
private boolean _ensureFontExistence = false; |
| 17 |
/** |
| 18 |
* ConverterContext for a font name. |
| 19 |
*/ |
| 20 |
public static ConverterContext CONTEXT = new ConverterContext("FontName"); |
| 21 |
|
| 22 |
public String toString(Object object, ConverterContext context) { |
| 23 |
if (object == null) { |
| 24 |
return ""; |
| 25 |
} |
| 26 |
else { |
| 27 |
if (isEnsureFontExistence()) { |
| 28 |
String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); |
| 29 |
for (String fontName : fontNames) { // check font if it is available |
| 30 |
if (fontName.equals(object)) { |
| 31 |
return fontName; |
| 32 |
} |
| 33 |
} |
| 34 |
return ""; |
| 35 |
} |
| 36 |
else if (object instanceof String) { |
| 37 |
return (String) object; |
| 38 |
} |
| 39 |
else { |
| 40 |
return ""; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public boolean supportToString(Object object, ConverterContext context) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
public Object fromString(String string, ConverterContext context) { |
| 50 |
if (string.length() == 0) { |
| 51 |
return null; |
| 52 |
} |
| 53 |
else { |
| 54 |
if (isEnsureFontExistence()) { |
| 55 |
String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); |
| 56 |
for (String fontName : fontNames) { // check font if it is available |
| 57 |
if (fontName.equals(string)) { |
| 58 |
return string; |
| 59 |
} |
| 60 |
} |
| 61 |
return null; |
| 62 |
} |
| 63 |
else { |
| 64 |
return string; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public boolean supportFromString(String string, ConverterContext context) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Checks if the FontNameConverter ensures the font exists on your OS. It is false by default. Setting it to true |
| 75 |
* will slow down the performance as it takes time to check whether the font actually exists. |
| 76 |
* |
| 77 |
* @return true or false. |
| 78 |
* |
| 79 |
* @since 3.3.4 |
| 80 |
*/ |
| 81 |
public boolean isEnsureFontExistence() { |
| 82 |
return _ensureFontExistence; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Sets the flag so that FontNameConverter ensures the font exists on your OS. |
| 87 |
* |
| 88 |
* @param ensureFontExistence true or false. |
| 89 |
* |
| 90 |
* @since 3.3.4 |
| 91 |
*/ |
| 92 |
public void setEnsureFontExistence(boolean ensureFontExistence) { |
| 93 |
_ensureFontExistence = ensureFontExistence; |
| 94 |
} |
| 95 |
} |