1 package helloagain.kanday;
2
3 import java.awt.Color;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8 import java.util.prefs.Preferences;
9
10 /***
11 * HelloAgain Desktopプログラムにおける設定可能パラメータを
12 * 保持する。パラメータ値は、リソースバンドルから読み込み、
13 * 変更値は、プリファレンスAPIによって永続化する。
14 * <p>
15 * リソースバンドルは、helloagain.kandayパッケージの中に、
16 * KandayResourceという名称で保持する。具体的には以下のどちらかの
17 * 方法でパラメータ値を指定することが可能。
18 * <ol>
19 * <li>helloagain.kanday.KandayResource クラスを設ける
20 * <li>helloagain/kanday/KandayResource.properties ファイルを設ける
21 * </ol>
22 * <p>
23 * プリファレンスAPIによって永続化する属性については、
24 * {@link #save} を参照。
25 * <p>
26 * Created: Wed May 26 12:05:42 2004
27 *
28 * @author <a href="mailto:torutk@02.246.ne.jp">TAKAHASHI,Toru</a>
29 * @version 1.0
30 */
31 public class KandayPreferences {
32
33 /***
34 * コンストラクタ。
35 * リソースバンドルからパラメータ値を取り出し各フィールドに代入。
36 * その後、ユーザ・プリファレンスの値でフィールドを更新。
37 * TODO: リソースが存在しないと以下の例外がスローされてしまう
38 * @throws java.util.MissingResourceException
39 */
40 public KandayPreferences() {
41 logger.entering(MYCLASS, "KandayPreferences");
42
43 resource = ResourceBundle.getBundle("helloagain.kanday.KandayResource");
44 frameTitle = readString(KEY_FRAME_TITLE, "KANDAY");
45 frameWidth = readInt(KEY_FRAME_WIDTH, 320);
46 frameHeight = readInt(KEY_FRAME_HEIGHT, 200);
47 framePositionX = readInt(KEY_FRAME_POSITION_X, 352);
48 framePositionY = readInt(KEY_FRAME_POSITION_Y, 284);
49 message = readString(KEY_KANDAY_MESSAGE, "Hello world, again!");
50 menuFile = readString(KEY_MENU_FILE, "File");
51 menuHelp = readString(KEY_MENU_HELP, "Help");
52 menuExit = readString(KEY_MENU_EXIT, "Exit");
53 menuExitDescriptoin = readString(KEY_MENU_EXIT_DESCRIPTION,
54 "Exit this program");
55 menuAbout = readString(KEY_MENU_ABOUT, "About");
56 menuAboutDescription = readString(KEY_MENU_ABOUT_DESCRIPTION,
57 "About this program");
58 menuPreferences = readString(KEY_MENU_PREFERENCES, "Preferences");
59 menuPreferencesDescription = readString(
60 KEY_MENU_PREFERENCES_DESCRIPTION, "Edit preferences"
61 );
62 messageBackgroundColor =
63 readColor(KEY_MESSAGE_BACKGROUND_COLOR, Color.LIGHT_GRAY);
64 messageForegroundColor =
65 readColor(KEY_MESSAGE_FOREGROUND_COLOR, Color.DARK_GRAY);
66 aboutDescription = readString(
67 KEY_ABOUT_DESCRIPTION,
68 "Desktop example program by 'Hello Again' project"
69 );
70 aboutVersion = readString(KEY_ABOUT_VERSION, "Version: Souchong") +
71 "(" + getClass().getPackage().getImplementationVersion() +")";
72
73 aboutCopyright = readString(KEY_ABOUT_COPYRIGHT, "(C) TAKAHASHI,Toru");
74 preferencesDialogTitle = readString(
75 KEY_PREFERENCES_DIALOG_TITLE, "Preferences"
76 );
77 preferencesDialogFglabelText = readString(
78 KEY_PREFERENCES_DIALOG_FGLABEL_TEXT, "Foreground Color:"
79 );
80 preferencesDialogBglabelText = readString(
81 KEY_PREFERENCES_DIALOG_BGLABEL_TEXT, "Background Color:"
82 );
83 preferencesDialogChangebuttonText = readString(
84 KEY_PREFERENCES_DIALOG_CHANGEBUTTON_TEXT, "Change"
85 );
86 preferencesDialogApplybuttonText = readString(
87 KEY_PREFERENCES_DIALOG_APPLYBUTTON_TEXT, "Apply"
88 );
89 preferencesDialogCancelbuttonText = readString(
90 KEY_PREFERENCES_DIALOG_CANCELBUTTON_TEXT, "Cancel"
91 );
92 preferencesDialogFgcolorchooserTitle = readString(
93 KEY_PREFERENCES_DIALOG_FGCOLORCHOOSER_TITLE,
94 "Select Foreground Color"
95 );
96 preferencesDialogBgcolorchooserTitle = readString(
97 KEY_PREFERENCES_DIALOG_BGCOLORCHOOSER_TITLE,
98 "Select Background Color"
99 );
100 logger.config("values from resources:" + toString());
101
102 load();
103 logger.config("values from user preferences:" + toString());
104 logger.exiting(MYCLASS, "KandayPreferences");
105 }
106
107 public String toString() {
108 return "helloagain.kanday.KandayPreferences[frameTitle=" + frameTitle +
109 ",frameWidth=" + frameWidth + ",frameHeight=" + frameHeight +
110 ",framePositionX=" + framePositionX + ",framePositionY=" +
111 framePositionY + ",message=" + message + "]";
112 }
113
114 /***
115 * リソース中から指定したキーに対応する値をint型で取り出す。
116 * @param aKey リソースから値を取り出す際に指定するキー
117 * @param aDefault 指定したキーに対応する値がリソース中に見つからない
118 * 場合に戻り値として返却する値
119 * @return キーに対応する値
120 */
121 private final int readInt(String aKey, int aDefault) {
122 if (logger.isLoggable(Level.FINER)) {
123 logger.entering(MYCLASS, "readInt",
124 new Object[]{aKey, new Integer(aDefault)}
125 );
126 }
127 int value = 0;
128 try {
129 String param = resource.getString(aKey);
130 value = Integer.parseInt(param);
131 } catch (MissingResourceException e) {
132 logger.fine("readInt catching exception:" + e);
133 value = aDefault;
134 } catch (NumberFormatException e) {
135 logger.fine("readInt catching exception:" + e);
136 value = aDefault;
137 }
138 if (logger.isLoggable(Level.FINER)) {
139 logger.exiting(MYCLASS, "readInt", new Integer(value));
140 }
141 return value;
142 }
143
144 /***
145 * リソース中から指定したキーに対応する値をString型で取り出す。
146 * @param aKey リソースから値を取り出す際に指定するキー
147 * @param aDefault 指定したキーに対応する値がリソース中に見つからない
148 * 場合に戻り値として返却する値
149 * @return キーに対応する値
150 */
151 private final String readString(String aKey, String aDefault) {
152 if (logger.isLoggable(Level.FINER)) {
153 logger.entering(MYCLASS, "readString",
154 new Object[] {aKey, aDefault}
155 );
156 }
157 String param = null;
158 try {
159 param = resource.getString(aKey);
160 } catch (MissingResourceException e) {
161 param = aDefault;
162 }
163 if (logger.isLoggable(Level.FINER)) {
164 logger.exiting(MYCLASS, "readString", param);
165 }
166 return param;
167 }
168
169 /***
170 * リソース中から指定したキーに対応する値をColor型で取り出す。
171 * リソース中では、16進の文字列 0xRRGGBBで表記する。
172 *
173 * @param aKey リソースから値を取り出す際に指定するキー
174 * @param aDefault 指定したキーに対応する値がリソース中に見つからない
175 * 場合に戻り値として返却する値
176 * @return キーに対応する値
177 */
178 private final Color readColor(String aKey, Color aDefault) {
179 if (logger.isLoggable(Level.FINER)) {
180 logger.entering(MYCLASS, "readColor",
181 new Object[] {aKey, aDefault}
182 );
183 }
184 Color param = null;
185 try {
186 param = Color.decode(resource.getString(aKey));
187 } catch (MissingResourceException e) {
188 param = aDefault;
189 logger.warning("not found from resources for key:" + aKey);
190 } catch (NumberFormatException e) {
191 param = aDefault;
192 logger.warning("parsing error from resources for key:" + aKey);
193 }
194 if (logger.isLoggable(Level.FINER)) {
195 logger.exiting(MYCLASS, "readColor", param);
196 }
197 return param;
198 }
199
200 /***
201 * ウィンドウのタイトルバーに表示する文字列を返却する。
202 * @return ウィンドウのタイトルバーに表示する文字列
203 */
204 public String getFrameTitle() {
205 return frameTitle;
206 }
207
208 /***
209 * ウィンドウの大きさ(幅)を返却する。
210 * @return ウィンドウの幅[ピクセル]
211 */
212 public int getFrameWidth() {
213 return frameWidth;
214 }
215
216 /***
217 * ウィンドウの大きさ(幅)を設定する。
218 * @param aWidth ウィンドウの幅[ピクセル]
219 */
220 public void setFrameWidth(int aWidth) {
221 frameWidth = aWidth;
222 }
223
224 /***
225 * ウィンドウの大きさ(高さ)を返却する。
226 * @return ウィンドウの高さ[ピクセル]
227 */
228 public int getFrameHeight() {
229 return frameHeight;
230 }
231
232 /***
233 * ウィンドウの大きさ(高さ)を設定する。
234 * @param aHeight ウィンドウの高さ[ピクセル]
235 */
236 public void setFrameHeight(int aHeight) {
237 frameHeight = aHeight;
238 }
239
240 /***
241 * ウィンドウの位置(X座標)を返却する。
242 * @return ウィンドウのX座標[ピクセル]
243 */
244 public int getFramePositionX() {
245 return framePositionX;
246 }
247
248 /***
249 * ウィンドウの位置(X座標)を設定する。
250 * @param aX ウィンドウのX座標[ピクセル]
251 */
252 public void setFramePositionX(int aX) {
253 framePositionX = aX;
254 }
255
256 /***
257 * ウィンドウの位置(Y座標)を返却する。
258 * @return ウィンドウのY座標[ピクセル]
259 */
260 public int getFramePositionY() {
261 return framePositionY;
262 }
263
264 /***
265 * ウィンドウの位置(Y座標)を設定する。
266 * @param aY ウィンドウのY座標[ピクセル]
267 */
268 public void setFramePositionY(int aY) {
269 framePositionY = aY;
270 }
271
272 /***
273 * ウィンドウに表示するメッセージ文字列を返却する。
274 * @return ウィンドウに表示するメッセージ
275 */
276 public String getMessage() {
277 return message;
278 }
279
280 /***
281 * Fileメニューに表示する文字列を返却する。
282 * @return Fileメニューの文字列
283 */
284 public String getMenuFile() {
285 return menuFile;
286 }
287 /***
288 * Helpメニューに表示する文字列を返却する。
289 * @return Helpメニューの文字列
290 */
291 public String getMenuHelp() {
292 return menuHelp;
293 }
294 /***
295 * Exitメニュー項目に表示する文字列を返却する。
296 * @return Exitメニュー項目の文字列
297 */
298 public String getMenuExit() {
299 return menuExit;
300 }
301 /***
302 * Exitメニュー項目に対応するアクションの説明文字列を返却する。
303 * ツールチップの文字列に使用される。
304 * @return Exitアクションの説明文字列
305 */
306 public String getMenuExitDescription() {
307 return menuExitDescriptoin;
308 }
309
310 /***
311 * Aboutメニュー項目に表示する文字列を返却する。
312 * @return Aboutメニュー項目の文字列
313 */
314 public String getMenuAbout() {
315 return menuAbout;
316 }
317 /***
318 * Aboutメニュー項目に対応するアクションの説明文字列を返却する。
319 * ツールチップの文字列に使用される。
320 * @return Aboutアクションの説明文字列
321 */
322 public String getMenuAboutDescription() {
323 return menuAboutDescription;
324 }
325 /***
326 * Preferencesメニュー項目に表示する文字列を返却する。
327 * @return Preferencesメニュー項目の文字列
328 */
329 public String getMenuPreferences() {
330 return menuPreferences;
331 }
332 /***
333 * Preferencesメニュー項目に対応するアクションの説明文字列を返却する。
334 * ツールチップの文字列に使用される。
335 * @return Preferencesアクションの説明文字列
336 */
337 public String getMenuPreferencesDescription() {
338 return menuPreferencesDescription;
339 }
340 /***
341 * Aboutダイアログに表示するプログラムの説明文を返却する。
342 * @return Aboutダイアログのプログラムの説明文
343 */
344 public String getAboutDescription() {
345 return aboutDescription;
346 }
347 /***
348 * Aboutダイアログに表示するプログラムのバージョン文字列を返却する。
349 * @return Aboutダイアログのバージョン文字列
350 */
351 public String getAboutVersion() {
352 return aboutVersion;
353 }
354 /***
355 * Aboutダイアログに表示する著作権表示文を返却する。
356 * @return Aboutダイアログの著作権の文字列
357 */
358 public String getAboutCopyright() {
359 return aboutCopyright;
360 }
361 /***
362 * メッセージ表示パネルの背景色を返却する。
363 * @return メッセージ表示パネルの背景色
364 */
365 public Color getMessageBackgroundColor() {
366 return messageBackgroundColor;
367 }
368 /***
369 * メッセージ表示パネルの背景色を設定する。
370 * @param aColor メッセージ表示パネルの背景色
371 */
372 public void setMessageBackgroundColor(Color aColor) {
373 messageBackgroundColor = aColor;
374 }
375
376 /***
377 * メッセージ表示パネルの前景色を返却する。
378 * @return メッセージ表示パネルの前景色
379 */
380 public Color getMessageForegroundColor() {
381 return messageForegroundColor;
382 }
383 /***
384 * 設定ダイアログのタイトル文字列を返却する。
385 * @return 設定ダイアログのタイトル文字列
386 */
387 public String getPreferencesDialogTitle() {
388 return preferencesDialogTitle;
389 }
390 /***
391 * 設定ダイアログの前景色ラベル文字列を返却する。
392 * @return 設定ダイアログの前景色ラベル文字列
393 */
394 public String getPreferencesDialogFglabelText() {
395 return preferencesDialogFglabelText;
396 }
397 /***
398 * 前景色選択ダイアログのタイトル文字列を返却する。
399 * @return 前景色選択ダイアログのタイトル文字列
400 */
401 public String getPreferencesDialogFgcolorchooserTitle() {
402 return preferencesDialogFgcolorchooserTitle;
403 }
404 /***
405 * 背景色選択ダイアログのタイトル文字列を返却する。
406 * @return 背景色選択ダイアログのタイトル文字列
407 */
408 public String getPreferencesDialogBgcolorchooserTitle() {
409 return preferencesDialogBgcolorchooserTitle;
410 }
411 /***
412 * 設定ダイアログの背景色ラベル文字列を返却する。
413 * @return 設定ダイアログの背景色ラベル文字列
414 */
415 public String getPreferencesDialogBglabelText() {
416 return preferencesDialogBglabelText;
417 }
418 /***
419 * 設定ダイアログの色変更ボタン文字列を返却する。
420 * @return 設定ダイアログの変更ボタン文字列
421 */
422 public String getPreferencesDialogChangebuttonText() {
423 return preferencesDialogChangebuttonText;
424 }
425 /***
426 * 設定ダイアログの適用ボタン文字列を返却する。
427 * @return 設定ダイアログの適用ボタン文字列
428 */
429 public String getPreferencesDialogApplybuttonText() {
430 return preferencesDialogApplybuttonText;
431 }
432 /***
433 * 設定ダイアログの取消ボタン文字列を返却する。
434 * @return 設定ダイアログの取消ボタン文字列
435 */
436 public String getPreferencesDialogCancelbuttonText() {
437 return preferencesDialogCancelbuttonText;
438 }
439
440 /***
441 * メッセージ表示パネルの前景色を設定する。
442 * @param aColor メッセージ表示パネルの前景色
443 */
444 public void setMessageForegroundColor(Color aColor) {
445 messageForegroundColor = aColor;
446 }
447
448 /***
449 * 属性の値を、Preferences APIを用いてユーザ・プリファレンスとして
450 * バッキングストアに保存する。
451 * 現在対象の属性
452 * <ul>
453 * <li>frameWidth
454 * <li>frameHeight
455 * <li>framePositionX
456 * <li>framePositionY
457 * <li>messageForegroundColor
458 * <li>messageBackgroundColor
459 * </ul>
460 */
461 public void save() {
462 logger.entering(MYCLASS, "save");
463 Preferences prefs = Preferences.userNodeForPackage(getClass());
464 prefs.putInt(KEY_FRAME_WIDTH, frameWidth);
465 prefs.putInt(KEY_FRAME_HEIGHT, frameHeight);
466 prefs.putInt(KEY_FRAME_POSITION_X, framePositionX);
467 prefs.putInt(KEY_FRAME_POSITION_Y, framePositionY);
468 prefs.putInt(
469 KEY_MESSAGE_FOREGROUND_COLOR, messageForegroundColor.getRGB()
470 );
471 prefs.putInt(
472 KEY_MESSAGE_BACKGROUND_COLOR, messageBackgroundColor.getRGB()
473 );
474 if (logger.isLoggable(Level.CONFIG)) {
475 logger.config(
476 "Save to user preferences:[frameWidth=" + frameWidth +
477 ",frameHeight=" + frameHeight + ",framePositionX=" +
478 framePositionX + ",framePositionY=" + framePositionY +
479 ",messageForegroundColor=" + messageForegroundColor.getRGB() +
480 ",messageBackgroundColor=" + messageBackgroundColor.getRGB() +
481 "]"
482 );
483 }
484 logger.exiting(MYCLASS, "save");
485 }
486
487 /***
488 * 属性の値を、Preferences APIを用いてユーザ・プリファレンスとして
489 * バッキングストアから取得する。
490 */
491 public void load() {
492 logger.entering(MYCLASS, "load");
493 Preferences prefs = Preferences.userNodeForPackage(getClass());
494 frameWidth = prefs.getInt(KEY_FRAME_WIDTH, frameWidth);
495 frameHeight = prefs.getInt(KEY_FRAME_HEIGHT, frameHeight);
496 framePositionX = prefs.getInt(KEY_FRAME_POSITION_X, framePositionX);
497 framePositionY = prefs.getInt(KEY_FRAME_POSITION_Y, framePositionY);
498 int fgcolorInt = prefs.getInt(
499 KEY_MESSAGE_FOREGROUND_COLOR, messageForegroundColor.getRGB()
500 );
501 messageForegroundColor = new Color(fgcolorInt);
502 int bgcolorInt = prefs.getInt(
503 KEY_MESSAGE_BACKGROUND_COLOR, messageBackgroundColor.getRGB()
504 );
505 messageBackgroundColor = new Color(bgcolorInt);
506 logger.exiting(MYCLASS, "load");
507 }
508
509 private static final String KEY_FRAME_TITLE = "frame.title";
510 private static final String KEY_FRAME_WIDTH = "frame.width";
511 private static final String KEY_FRAME_HEIGHT = "frame.height";
512 private static final String KEY_FRAME_POSITION_X = "frame.position_x";
513 private static final String KEY_FRAME_POSITION_Y = "frame.position_y";
514 private static final String KEY_KANDAY_MESSAGE = "kanday.message";
515 private static final String KEY_MENU_FILE = "menu.file";
516 private static final String KEY_MENU_HELP = "menu.help";
517 private static final String KEY_MENU_EXIT = "menu.exit";
518 private static final String KEY_MENU_EXIT_DESCRIPTION =
519 "menu.exit.description";
520 private static final String KEY_MENU_ABOUT = "menu.about";
521 private static final String KEY_MENU_ABOUT_DESCRIPTION =
522 "menu.about.description";
523 private static final String KEY_MENU_PREFERENCES = "menu.preferences";
524 private static final String KEY_MENU_PREFERENCES_DESCRIPTION =
525 "menu.preferences.description";
526 private static final String KEY_ABOUT_DESCRIPTION = "about.description";
527 private static final String KEY_ABOUT_VERSION = "about.version";
528 private static final String KEY_ABOUT_COPYRIGHT = "about.copyright";
529 private static final String KEY_MESSAGE_FOREGROUND_COLOR =
530 "message.foreground.color";
531 private static final String KEY_MESSAGE_BACKGROUND_COLOR =
532 "message.background.color";
533 private static final String KEY_PREFERENCES_DIALOG_TITLE =
534 "preferences.dialog.title";
535 private static final String KEY_PREFERENCES_DIALOG_FGLABEL_TEXT =
536 "preferences.dialog.fglabel.text";
537 private static final String KEY_PREFERENCES_DIALOG_BGLABEL_TEXT =
538 "preferences.dialog.bglabel.text";
539 private static final String KEY_PREFERENCES_DIALOG_CHANGEBUTTON_TEXT =
540 "preferences.dialog.changebutton.text";
541 private static final String KEY_PREFERENCES_DIALOG_APPLYBUTTON_TEXT =
542 "preferences.dialog.applybutton.text";
543 private static final String KEY_PREFERENCES_DIALOG_CANCELBUTTON_TEXT =
544 "preferences.dialog.cancelbutton.text";
545 private static final String KEY_PREFERENCES_DIALOG_FGCOLORCHOOSER_TITLE =
546 "preferences.dialog.fgcolorchooser.title";
547 private static final String KEY_PREFERENCES_DIALOG_BGCOLORCHOOSER_TITLE =
548 "preferences.dialog.bgcolorchooser.title";
549
550 private ResourceBundle resource;
551
552 private String frameTitle;
553 private int frameWidth;
554 private int frameHeight;
555 private int framePositionX;
556 private int framePositionY;
557 private String message;
558 private String menuFile;
559 private String menuHelp;
560 private String menuExit;
561 private String menuExitDescriptoin;
562 private String menuAbout;
563 private String menuAboutDescription;
564 private String menuPreferences;
565 private String menuPreferencesDescription;
566 private String aboutDescription;
567 private String aboutVersion;
568 private String aboutCopyright;
569 private Color messageForegroundColor;
570 private Color messageBackgroundColor;
571 private String preferencesDialogTitle;
572 private String preferencesDialogFglabelText;
573 private String preferencesDialogBglabelText;
574 private String preferencesDialogChangebuttonText;
575 private String preferencesDialogApplybuttonText;
576 private String preferencesDialogCancelbuttonText;
577 private String preferencesDialogFgcolorchooserTitle;
578 private String preferencesDialogBgcolorchooserTitle;
579
580
581 private static String MYCLASS = KandayPreferences.class.getName();
582 private static Logger logger = Logger.getLogger(MYCLASS);
583 }