OSDN Git Service

srcディレクトリとdocディレクトリを作成
[xdf/git-repos.git] / src / xdf-swing / src / main / java / jp / ac / aiit / xdf / component / swing / typeconvert / FontConverter.java
1 package jp.ac.aiit.xdf.component.swing.typeconvert;
2
3 import java.awt.Font;
4 import java.util.regex.Matcher;
5 import java.util.regex.Pattern;
6
7 import jp.ac.aiit.xdf.core.typeconvert.TypeConverter;
8
9 /**
10  * フォント指定文字列からjava.awt.Fontのインスタンスへの変換を行う
11  * 
12  * @author Takagi Pin.Yuan
13  *
14  */
15 public class FontConverter implements TypeConverter<Font> {
16         private static final Pattern APPLIABLE_PATTERN = Pattern.compile("([^\\,]*)(\\,([1-9][0-9]*)pt)?(\\,(italic|bold|normal))?");
17         
18         @Override
19         public Font apply(String target) {
20                 Matcher m = APPLIABLE_PATTERN.matcher(target);
21                 
22                 if( m.matches() ) {
23                         // System.out.println("DebugInfo:1<" + m.group(1) + "> 3<" + m.group(3) + "> 3<" + m.group(5));
24                         String name = m.group(1);
25                         int size = Integer.valueOf(m.group(3));
26                         int style = asStyle( m.group(5) );
27                         
28                         return new Font(name, style, size);
29                 } else {
30                         return null;
31                 }
32         }
33
34         @Override
35         public boolean isAppliable(String target) {
36                 return APPLIABLE_PATTERN.matcher(target).matches();
37         }
38         
39         private int asStyle(String style) {
40                 if( style == null ) {
41                         return Font.PLAIN;
42                 } else if( style.equals("italic") ) {
43                         return Font.ITALIC;
44                 } else if( style.equals("bold") ) {
45                         return Font.BOLD;
46                 } else if( style.equals("normal") ) {
47                         return Font.PLAIN;
48                 } else {
49                         return Font.PLAIN;
50                 }
51         }
52 }