1 package helloagain.kanday;
2
3 import java.awt.BorderLayout;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.KeyEvent;
6 import java.util.logging.Logger;
7 import javax.swing.AbstractAction;
8 import javax.swing.Icon;
9 import javax.swing.ImageIcon;
10 import javax.swing.JFrame;
11 import javax.swing.KeyStroke;
12
13 /***
14 * メニューバーとツールバーを持つ典型的なアプリケーションウィンドウ。
15 * <ul>
16 * <li>メニューバーを持つ(KandayMenuBar)
17 * <li>ツールバーを持つ(KandayToolBar)
18 * <li>ウィンドウがクローズされるとプロセスを終了する
19 * </ul>
20 * <p>
21 * Created: Wed May 26 11:42:36 2004
22 *
23 * @author <a href="mailto:torutk@02.246.ne.jp">TAKAHASHI,Toru</a>
24 * @version 1.0
25 */
26 public class KandayFrame extends JFrame {
27
28 /***
29 * 引数で指定されたPreferenceに基づきウィンドウを生成する。
30 * @param aPreferences ウィンドウの各種設定
31 */
32 public KandayFrame(KandayPreferences aPreferences) {
33 super(aPreferences.getFrameTitle());
34 logger.entering(MYCLASS, "KandayFrame", aPreferences);
35
36 preferences = aPreferences;
37
38 setBounds(
39 preferences.getFramePositionX(),
40 preferences.getFramePositionY(),
41 preferences.getFrameWidth(),
42 preferences.getFrameHeight()
43 );
44 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
45
46 menuBar = new KandayMenuBar(preferences);
47 logger.finest("Created KandayMenuBar");
48 toolBar = new KandayToolBar();
49 logger.finest("Created KandayToolBar");
50
51
52 Icon exitIcon = new ImageIcon(getClass().getResource("Stop16.gif"));
53 ExitAction exitAction =
54 new ExitAction(preferences.getMenuExit(), exitIcon);
55 menuBar.setExitAction(exitAction);
56 toolBar.add(exitAction);
57
58
59 Icon preferencesIcon =
60 new ImageIcon(getClass().getResource("Preferences16.gif"));
61 PreferencesAction preferencesAction = new PreferencesAction(
62 preferences.getMenuPreferences(), preferencesIcon
63 );
64 menuBar.setPreferencesAction(preferencesAction);
65 toolBar.add(preferencesAction);
66
67
68 Icon aboutIcon = new ImageIcon(getClass().getResource("About16.gif"));
69 AboutAction aboutAction =
70 new AboutAction(preferences.getMenuAbout(), aboutIcon);
71 menuBar.setAboutAction(aboutAction);
72 toolBar.add(aboutAction);
73
74
75 Icon splashIcon = new ImageIcon(getClass().getResource("splash.png"));
76 aboutDialog = new KandayAboutDialog(
77 this, "About Hello Again", splashIcon,
78 preferences.getAboutDescription(),
79 preferences.getAboutVersion(),
80 preferences.getAboutCopyright()
81 );
82
83 preferencesDialog = new KandayPreferencesDialog(this, preferences);
84
85 pane = new KandayPane();
86 setPaneProperties();
87
88 setJMenuBar(menuBar);
89 getContentPane().add(toolBar, BorderLayout.NORTH);
90 getContentPane().add(pane, BorderLayout.CENTER);
91 logger.exiting(MYCLASS, "KandayFrame");
92 }
93
94 /***
95 * 前景色/背景色が変更された場合に呼び出されるメソッド。
96 * 挨拶表示パネルの設定を更新して再描画する。
97 */
98 public void updatePreferences() {
99 setPaneProperties();
100 pane.repaint();
101 }
102
103 /***
104 * メッセージpaneの属性を設定するメソッド。
105 */
106 private void setPaneProperties() {
107 pane.setMessage(preferences.getMessage());
108 logger.finer("preferences' foreground=" +
109 preferences.getMessageForegroundColor());
110 logger.finer("preferences' background=" +
111 preferences.getMessageBackgroundColor());
112 pane.setForeground(preferences.getMessageForegroundColor());
113 pane.setBackground(preferences.getMessageBackgroundColor());
114 logger.finer("pane's foreground=" + pane.getForeground());
115 logger.finer("pane's background=" + pane.getBackground());
116 }
117
118 /***
119 * プログラムを終了するアクション。
120 * アクセラレータ・キーに Ctrl-Q を設定し、
121 * ニモニック・キーに X を設定している。
122 */
123 class ExitAction extends AbstractAction {
124 ExitAction(String aText) {
125 init(aText, null);
126 }
127 ExitAction(String aText, Icon anIcon) {
128 init(aText, anIcon);
129 }
130 final void init(String aText, Icon anIcon) {
131 putValue(NAME, aText);
132 putValue(SMALL_ICON, anIcon);
133 putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_X));
134 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(
135 KeyEvent.VK_Q, ActionEvent.CTRL_MASK)
136 );
137 putValue(SHORT_DESCRIPTION,
138 preferences.getMenuExitDescription()
139 );
140 }
141 public void actionPerformed(ActionEvent ev) {
142 logger.info("Exit program");
143 System.exit(0);
144 }
145 }
146
147 /***
148 * プログラムのバージョン情報を表示するアクション。
149 * ニモニック・キーに A を設定している。
150 */
151 class AboutAction extends AbstractAction {
152 AboutAction(String aText) {
153 init(aText, null);
154 }
155 AboutAction(String aText, Icon anIcon) {
156 init(aText, anIcon);
157 }
158 final void init(String aText, Icon anIcon) {
159 putValue(NAME, aText);
160 putValue(SMALL_ICON, anIcon);
161 putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
162 putValue(SHORT_DESCRIPTION,
163 preferences.getMenuAboutDescription()
164 );
165 }
166 public void actionPerformed(ActionEvent ev) {
167 logger.info("show about dialog");
168 aboutDialog.setVisible(true);
169 }
170 }
171
172 /***
173 * プログラムの設定を変更するアクション。
174 */
175 class PreferencesAction extends AbstractAction {
176 PreferencesAction(String aText) {
177 init(aText, null);
178 }
179 PreferencesAction(String aText, Icon anIcon) {
180 init(aText, anIcon);
181 }
182 final void init(String aText, Icon anIcon) {
183 putValue(NAME, aText);
184 putValue(SMALL_ICON, anIcon);
185 putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_E));
186 putValue(SHORT_DESCRIPTION,
187 preferences.getMenuPreferencesDescription()
188 );
189 }
190 public void actionPerformed(ActionEvent ev) {
191 logger.info("edit preferences");
192 preferencesDialog.setVisible(true);
193 }
194 }
195
196 private KandayMenuBar menuBar;
197 private KandayToolBar toolBar;
198 private KandayPane pane;
199 private KandayAboutDialog aboutDialog;
200 private KandayPreferences preferences;
201 private KandayPreferencesDialog preferencesDialog;
202
203 private static final String MYCLASS = KandayFrame.class.getName();
204 private static Logger logger = Logger.getLogger(MYCLASS);
205 }