OSDN Git Service

srcディレクトリとdocディレクトリを作成
[xdf/git-repos.git] / src / xdf-swing / src / main / java / jp / ac / aiit / xdf / component / swing / typeconvert / FontConverter.java
diff --git a/src/xdf-swing/src/main/java/jp/ac/aiit/xdf/component/swing/typeconvert/FontConverter.java b/src/xdf-swing/src/main/java/jp/ac/aiit/xdf/component/swing/typeconvert/FontConverter.java
new file mode 100644 (file)
index 0000000..da642b1
--- /dev/null
@@ -0,0 +1,52 @@
+package jp.ac.aiit.xdf.component.swing.typeconvert;
+
+import java.awt.Font;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import jp.ac.aiit.xdf.core.typeconvert.TypeConverter;
+
+/**
+ * フォント指定文字列からjava.awt.Fontのインスタンスへの変換を行う
+ * 
+ * @author Takagi Pin.Yuan
+ *
+ */
+public class FontConverter implements TypeConverter<Font> {
+       private static final Pattern APPLIABLE_PATTERN = Pattern.compile("([^\\,]*)(\\,([1-9][0-9]*)pt)?(\\,(italic|bold|normal))?");
+       
+       @Override
+       public Font apply(String target) {
+               Matcher m = APPLIABLE_PATTERN.matcher(target);
+               
+               if( m.matches() ) {
+                       // System.out.println("DebugInfo:1<" + m.group(1) + "> 3<" + m.group(3) + "> 3<" + m.group(5));
+                       String name = m.group(1);
+                       int size = Integer.valueOf(m.group(3));
+                       int style = asStyle( m.group(5) );
+                       
+                       return new Font(name, style, size);
+               } else {
+                       return null;
+               }
+       }
+
+       @Override
+       public boolean isAppliable(String target) {
+               return APPLIABLE_PATTERN.matcher(target).matches();
+       }
+       
+       private int asStyle(String style) {
+               if( style == null ) {
+                       return Font.PLAIN;
+               } else if( style.equals("italic") ) {
+                       return Font.ITALIC;
+               } else if( style.equals("bold") ) {
+                       return Font.BOLD;
+               } else if( style.equals("normal") ) {
+                       return Font.PLAIN;
+               } else {
+                       return Font.PLAIN;
+               }
+       }
+}