OSDN Git Service

add a little program 'chartool'
[sawarabi-fonts/sawarabi-fonts.git] / chartool / src / jp / sourceforge / sawarabifonts / chartool / GlyphViewer.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.awt.Dimension;
22 import java.awt.Font;
23 import java.awt.Toolkit;
24 import java.awt.datatransfer.Clipboard;
25 import java.awt.datatransfer.StringSelection;
26 import java.awt.event.ActionEvent;
27 import java.io.IOException;
28 import java.util.logging.Logger;
29
30 import javax.swing.JMenu;
31 import javax.swing.JPopupMenu;
32 import javax.swing.JTextField;
33 import javax.swing.border.CompoundBorder;
34 import javax.swing.border.EmptyBorder;
35 import javax.swing.border.EtchedBorder;
36 import javax.swing.event.CaretEvent;
37 import javax.swing.event.CaretListener;
38 import javax.swing.text.TextAction;
39
40 public class GlyphViewer extends JTextField {
41         private static final long serialVersionUID = 5811308266106026080L;
42
43         public GlyphViewer(GuiController controller) {
44                 Font f = new Font(getFont().getFontName(), Font.PLAIN, 64);
45                 setFont(f);
46                 setHorizontalAlignment(JTextField.CENTER);
47                 setEditable(false);
48                 EmptyBorder m = new EmptyBorder(10, 10, 10, 10);
49                 setBorder(new CompoundBorder(new EtchedBorder(), m));
50
51                 controller.setGlyphViewer(this);
52                 setPreferredSize(new Dimension(100, 100));
53
54                 final JPopupMenu menu = new GlyphViewerMenu();
55                 addCaretListener(new CaretListener() {
56                         public void caretUpdate(CaretEvent e) {
57                                 String s = getSelectedText();
58                                 if (s != null && s.length() > 0) {
59                                         menu.show(GlyphViewer.this, 70, 70);
60                                 }
61                         }
62                 });
63         }
64
65         private void copyChar() {
66                 Clipboard b = Toolkit.getDefaultToolkit().getSystemClipboard();
67                 StringSelection s = new StringSelection(getText());
68                 b.setContents(s, s);
69         }
70
71         private void changeFont(String nm) {
72                 Font f = getFont();
73                 int sz = f.getSize();
74                 int st = f.getStyle();
75                 setFont(new Font(nm, st, sz));
76         }
77
78         class GlyphViewerMenu extends JPopupMenu {
79                 private static final long serialVersionUID = 847563481396381931L;
80
81                 GlyphViewerMenu() {
82                         add(new TextAction("Copy") {
83                                 private static final long serialVersionUID = -4087404094754292233L;
84                                 public void actionPerformed(ActionEvent e) { copyChar(); }
85                         });
86                         JMenu fontmenu = new JMenu("font");
87                         setFontMenu(fontmenu, "Dialog");
88                         try {
89                                 addCustomFontMenu(fontmenu);
90                         } catch (IOException e) {
91                                 e.printStackTrace();
92                                 Logger.getLogger(Main.LOGNAME).warning(e.getMessage());
93                         }
94                         add(fontmenu);
95                 }
96
97                 private void addCustomFontMenu(JMenu menu) throws IOException {
98                         ConfigScript s = ConfigScript.getInstance();
99                         if (! s.isLoaded()) { s.load(); }
100                         String[] fnt = s.getFontNames();
101                         if (fnt != null) {
102                                 for (String nm: fnt) setFontMenu(menu, nm);
103                         } else {
104                                 String msg = "configuration of fonts is not found.";
105                                 System.err.println(msg);
106                                 Logger.getLogger(Main.LOGNAME).warning(msg);
107                         }
108                 }
109
110                 private void setFontMenu(JMenu menu, final String nm) {
111                         menu.add(new TextAction(nm) {
112                                 private static final long serialVersionUID = 7621704223879655193L;
113                                 public void actionPerformed(ActionEvent e) { changeFont(nm); }
114                         });
115                 }
116         }
117 }