OSDN Git Service

add a little program 'chartool'
[sawarabi-fonts/sawarabi-fonts.git] / chartool / src / jp / sourceforge / sawarabifonts / chartool / PropertyPanel.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.FontMetrics;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.awt.event.ComponentAdapter;
25 import java.awt.event.ComponentEvent;
26 import java.util.logging.Logger;
27
28 import javax.swing.JButton;
29 import javax.swing.JLabel;
30 import javax.swing.JPanel;
31
32 public class PropertyPanel extends JPanel {
33         private static final long serialVersionUID = -762290847824180418L;
34
35         private final static int MARGIN_LEFT = 5;
36         private final static int MARGIN_TOP = 5;
37         private final int labelWidth;
38         private final JButton[] buttons;
39         private final JLabel[] properties;
40
41         public PropertyPanel(GuiController controller) {
42                 setLayout(null);
43                 FontMetrics fm = getFontMetrics(getFont());
44                 String[] t = new String[] { "Code: ", "Name: " };
45                 buttons = new JButton[t.length];
46                 properties = new JLabel[t.length];
47                 labelWidth = setPropertyLabels(t, fm);
48                 int maxWidth = setPropertyButtons(t, fm, labelWidth);
49                 setPropertyLabels(t, fm, maxWidth);
50                 addComponentListener(new ComponentAdapter() {
51                         @Override public void componentResized(ComponentEvent e) {
52                                 onResized();
53                         }
54                 });
55
56                 controller.setPropertyPanel(this);
57         }
58
59         public void setCode(String code) {
60                 properties[0].setText(code);
61         }
62
63         private void setPropertyLabels(String[] text, FontMetrics fm, int x) {
64                 int y = MARGIN_TOP;
65                 int h = fm.getHeight();
66                 for (int i = 0; i < text.length; i++) {
67                         properties[i] = new JLabel();
68                         add(properties[i]);
69                         properties[i].setLocation(x, y);
70                         y += h;
71                 }
72         }
73
74         private int setPropertyLabels(String[] text, FontMetrics fm) {
75                 int max = 0;
76                 int y = MARGIN_TOP;
77                 int h = fm.getHeight();
78                 for (String t : text) {
79                         JLabel n = new JLabel(t);
80                         n = new JLabel(t);
81                         add(n);
82                         n.setLocation(MARGIN_LEFT, y);
83                         int w = fm.stringWidth(t);
84                         n.setSize(w, h);
85                         y += h;
86                         if (max < w) max = w;
87                 }
88                 return max + MARGIN_LEFT;
89         }
90
91         private int setPropertyButtons(String[] text, FontMetrics fm, int x) {
92                 int w = fm.stringWidth("S");
93                 int h = fm.getHeight();
94                 int y = MARGIN_TOP;
95
96                 for (int i = 0; i < text.length; i++) {
97                         JButton b = new JButton("S");
98                         b.setFocusable(false);
99                         final int id = i;
100                         b.addActionListener(new ActionListener() {
101                                 public void actionPerformed(ActionEvent e) {
102                                         String t = properties[id].getText();
103                                         ConfigScript s = ConfigScript.getInstance();
104                                         try {
105                                                 s.execScript(id, t, e.getModifiers());
106                                         } catch (Exception ex) {
107                                                 ex.printStackTrace();
108                                                 Logger.getLogger(Main.LOGNAME).warning(ex.getMessage());
109                                         }
110                                 }
111                         });
112                         add(b);
113                         b.setLocation(x, y);
114                         b.setSize(w + 10, h);
115                         y += h;
116                         buttons[i] = b;
117                 }
118                 return w;
119         }
120
121         private void onResized() {
122                 int y = MARGIN_TOP;
123                 int h = getFontMetrics(getFont()).getHeight();
124                 int w = getWidth() - labelWidth - buttons[0].getWidth();
125                 for (int i = 0; i < properties.length; i++) {
126                         properties[i].setLocation(labelWidth, y);
127                         properties[i].setSize(w, h);
128                         buttons[i].setLocation(labelWidth + w, y);
129                         y += h;
130                 }
131         }
132
133 }