OSDN Git Service

add a little program 'chartool'
[sawarabi-fonts/sawarabi-fonts.git] / chartool / src / jp / sourceforge / sawarabifonts / chartool / GuiController.java
1 /*
2  * Copyright (C) 2010, mshio <mshio@users.sourceforge.jp>
3  *
4  * This program is free software: you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  * ---
17  * Require JDK 1.5 (or later)
18  */
19 package jp.sourceforge.sawarabifonts.chartool;
20
21 import java.util.logging.Logger;
22
23 public class GuiController {
24         private final Main main;
25         private GlyphViewer glyphViewer = null;
26         private SearchPanel searchPanel = null;
27         private PropertyPanel propertyPanel = null;
28
29         public GuiController(Main main) {
30                 this.main = main;
31         }
32
33         public void setGlyphViewer(GlyphViewer g) {
34                 this.glyphViewer = g;
35         }
36
37         public void setPropertyPanel(PropertyPanel p) {
38                 this.propertyPanel = p;
39         }
40
41         public void setSearchPanel(SearchPanel s) {
42                 this.searchPanel = s;
43         }
44
45         private boolean isHexChar(char ch) {
46                 return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') ||
47                                 (ch >= 'A' && ch <= 'F');
48         }
49
50         private boolean foundByCode(String searchText, int pos) {
51                 char ch = searchText.charAt(pos);
52                 int len = searchText.length();
53                 if (isHexChar(ch)) {
54                         boolean four = false;
55                         int p = pos;
56                         for (;;) {
57                                 four = p - pos >= 3;
58                                 if (p - pos == 4) {
59                                         if (showGlyphByCode(searchText, pos, pos + 5) || 
60                                                         (four && showGlyphByCode(searchText, pos, pos + 4))) {
61                                                 return true;
62                                         }
63                                 } else if (p + 1 < len && isHexChar(searchText.charAt(p + 1))) {
64                                         p++;
65                                 } else {
66                                         break;
67                                 }
68                         }
69                         int b = pos;
70                         for (;;) {
71                                 four = p - b >= 3;
72                                 if (p - b == 4) {
73                                         if (showGlyphByCode(searchText, b, b + 5) ||
74                                                         (four && showGlyphByCode(searchText, b + 1, b + 5))) {
75                                                 return true;
76                                         }
77                                 } else if (b - 1 >= 0 && isHexChar(searchText.charAt(b - 1))) {
78                                         b--; 
79                                 } else {
80                                         if (four) { return showGlyphByCode(searchText, b, b + 4); }
81                                         break;
82                                 }
83                         }
84                 }
85                 return false;
86         }
87
88         public void onSearch(String searchText, int pos, String selected) {
89                 if (searchText.length() == 0) { return; }
90                 if (pos >= searchText.length()) { pos = 0; }
91
92                 if (! foundByCode(searchText, pos)) {
93                         String s = searchText.substring(pos);
94                         if (s.length() == 0) { s = searchText; }
95                         try {
96                                 showGlyph(main.searchChar(s));
97                                 boolean h = Character.isHighSurrogate(s.charAt(0));
98                                 searchPanel.setCaretPosition(pos, h ? 2 : 1);
99                         } catch (InvalidSearchStringException e) {
100                                 e.printStackTrace();
101                                 Logger.getLogger(Main.LOGNAME).warning(e.getMessage());
102                         }
103                 }
104         }
105
106         private boolean showGlyphByCode(String text, int pos0, int pos1) {
107                 boolean ret = false;
108                 int c = Integer.parseInt(text.substring(pos0, pos1), 16);
109                 try {
110                         CharProperty p = main.searchChar(c);
111                         int len = p.character.length();
112                         if ((len >= 2 && Character.isHighSurrogate(p.character.charAt(0)) &&
113                                         Character.isLowSurrogate(p.character.charAt(1))) || len <= 1) {
114                                 showGlyph(main.searchChar(c));
115                                 searchPanel.setCaretPosition(pos0, pos1 - pos0);
116                                 ret = true;
117                         }
118                 } catch (InvalidSearchStringException e) {
119                         e.printStackTrace();
120                         Logger.getLogger(Main.LOGNAME).warning(e.getMessage());
121                 }
122                 return ret;
123         }
124
125         private void showGlyph(CharProperty cp) {
126                 if (cp != null) {
127                         glyphViewer.setText(cp.character);
128                         propertyPanel.setCode(cp.code);
129                 }
130         }
131 }