4 * License : The MIT License
5 * Copyright(c) 2008 olyutorskii
8 package jp.sourceforge.jindolf;
11 import java.awt.font.FontRenderContext;
13 import jp.sourceforge.jovsonz.JsBoolean;
14 import jp.sourceforge.jovsonz.JsObject;
15 import jp.sourceforge.jovsonz.JsPair;
16 import jp.sourceforge.jovsonz.JsValue;
21 public class AppSetting{
23 private static final String NETCONFIG_FILE = "netconfig.json";
24 private static final String HASH_PROXY = "proxy";
26 private static final String TALKCONFIG_FILE = "talkconfig.json";
27 private static final String HASH_FONT = "font";
28 private static final String HASH_USEBODYICON = "useBodyIcon";
29 private static final String HASH_USEMONOTOMB = "useMonoTomb";
30 private static final String HASH_SIMPLEMODE = "isSimpleMode";
31 private static final String HASH_ALIGNBALOON = "alignBaloonWidth";
33 private OptionInfo optInfo;
35 private boolean useConfigPath;
36 private File configPath;
38 private FontInfo fontInfo = FontInfo.DEFAULT_FONTINFO;
40 private int frameWidth = 800;
41 private int frameHeight = 600;
42 private int frameXpos = Integer.MIN_VALUE;
43 private int frameYpos = Integer.MIN_VALUE;
45 private ProxyInfo proxyInfo = ProxyInfo.DEFAULT;
47 private DialogPref dialogPref = new DialogPref();
49 private JsValue loadedNetConfig;
50 private JsValue loadedTalkConfig;
61 * コマンドラインオプションからアプリ設定を展開する。
62 * @param optionInfo オプション情報
64 public void applyOptionInfo(OptionInfo optionInfo){
65 this.optInfo = optionInfo;
66 applyConfigPathSetting();
68 applyGeometrySetting();
75 private void applyConfigPathSetting(){
76 CmdOption opt = this.optInfo
77 .getExclusiveOption(CmdOption.OPT_CONFDIR,
78 CmdOption.OPT_NOCONF );
79 if(opt == CmdOption.OPT_NOCONF){
80 this.useConfigPath = false;
81 this.configPath = null;
82 }else if(opt == CmdOption.OPT_CONFDIR){
83 this.useConfigPath = true;
84 String path = this.optInfo.getStringArg(CmdOption.OPT_CONFDIR);
85 this.configPath = FileUtils.supplyFullPath(new File(path));
87 this.useConfigPath = true;
88 File path = ConfigFile.getImplicitConfigDirectory();
89 this.configPath = path;
98 private void applyFontSetting(){
99 String fontName = this.optInfo.getStringArg(CmdOption.OPT_INITFONT);
100 Boolean useAntiAlias =
101 this.optInfo.getBooleanArg(CmdOption.OPT_ANTIALIAS);
102 Boolean useFractional =
103 this.optInfo.getBooleanArg(CmdOption.OPT_FRACTIONAL);
105 if(fontName != null){
106 Font font = Font.decode(fontName);
107 this.fontInfo = this.fontInfo.deriveFont(font);
110 if(useAntiAlias != null){
111 FontRenderContext context = this.fontInfo.getFontRenderContext();
112 FontRenderContext newContext =
113 new FontRenderContext(context.getTransform(),
114 useAntiAlias.booleanValue(),
115 context.usesFractionalMetrics() );
116 this.fontInfo = this.fontInfo.deriveRenderContext(newContext);
119 if(useFractional != null){
120 FontRenderContext context = this.fontInfo.getFontRenderContext();
121 FontRenderContext newContext =
122 new FontRenderContext(context.getTransform(),
123 context.isAntiAliased(),
124 useFractional.booleanValue() );
125 this.fontInfo = this.fontInfo.deriveRenderContext(newContext);
134 private void applyGeometrySetting(){
137 ival = this.optInfo.initialFrameWidth();
138 if(ival != null) this.frameWidth = ival;
140 ival = this.optInfo.initialFrameHeight();
141 if(ival != null) this.frameHeight = ival;
143 ival = this.optInfo.initialFrameXpos();
144 if(ival != null) this.frameXpos = ival;
146 ival = this.optInfo.initialFrameYpos();
147 if(ival != null) this.frameYpos = ival;
154 * @return 設定格納ディレクトリ。
156 public File getConfigPath(){
157 return this.configPath;
162 * @param path 設定格納ディレクトリ
164 public void setConfigPath(File path){
165 this.configPath = path;
170 * 設定格納ディレクトリを使うか否かを返す。
173 public boolean useConfigPath(){
174 return this.useConfigPath;
178 * 設定格納ディレクトリを使うか否か設定する。
179 * @param need 使うならtrue
181 public void setUseConfigPath(boolean need){
182 this.useConfigPath = need;
190 public int initialFrameWidth(){
191 return this.frameWidth;
198 public int initialFrameHeight(){
199 return this.frameHeight;
204 * 特に指示されていなければInteger.MIN_VALUEを返す。
205 * @return 初期のフレーム位置のX座標
207 public int initialFrameXpos(){
208 return this.frameXpos;
213 * 特に指示されていなければInteger.MIN_VALUEを返す。
214 * @return 初期のフレーム位置のY座標
216 public int initialFrameYpos(){
217 return this.frameYpos;
224 public FontInfo getFontInfo(){
225 return this.fontInfo;
230 * @param fontInfo フォント設定
232 public void setFontInfo(FontInfo fontInfo){
233 this.fontInfo = fontInfo;
241 public ProxyInfo getProxyInfo(){
242 return this.proxyInfo;
247 * @param proxyInfo プロクシ設定。nullならプロクシなしと解釈される。
249 public void setProxyInfo(ProxyInfo proxyInfo){
250 if(proxyInfo == null) this.proxyInfo = ProxyInfo.DEFAULT;
251 else this.proxyInfo = proxyInfo;
259 public DialogPref getDialogPref(){
260 return this.dialogPref;
267 public void setDialogPref(DialogPref pref){
268 if(pref == null) this.dialogPref = new DialogPref();
269 else this.dialogPref = pref;
276 private void loadNetConfig(){
277 if( ! useConfigPath() ) return;
279 JsValue value = ConfigFile.loadJson(new File(NETCONFIG_FILE));
280 if(value == null) return;
281 this.loadedNetConfig = value;
283 if( ! (value instanceof JsObject) ) return;
284 JsObject root = (JsObject) value;
286 value = root.getValue(HASH_PROXY);
287 if( ! (value instanceof JsObject) ) return;
288 JsObject proxy = (JsObject) value;
290 ProxyInfo info = ProxyInfo.decodeJson(proxy);
300 private void loadTalkConfig(){
301 if( ! useConfigPath() ) return;
303 JsValue value = ConfigFile.loadJson(new File(TALKCONFIG_FILE));
304 if(value == null) return;
305 this.loadedTalkConfig = value;
307 if( ! (value instanceof JsObject) ) return;
308 JsObject root = (JsObject) value;
310 value = root.getValue(HASH_FONT);
311 if(value instanceof JsObject){
312 JsObject font = (JsObject) value;
313 FontInfo info = FontInfo.decodeJson(font);
318 DialogPref pref = new DialogPref();
320 value = root.getValue(HASH_USEBODYICON);
321 if(value instanceof JsBoolean){
322 boolValue = (JsBoolean) value;
323 pref.setBodyImageSetting(boolValue.booleanValue());
325 value = root.getValue(HASH_USEMONOTOMB);
326 if(value instanceof JsBoolean){
327 boolValue = (JsBoolean) value;
328 pref.setMonoImageSetting(boolValue.booleanValue());
330 value = root.getValue(HASH_SIMPLEMODE);
331 if(value instanceof JsBoolean){
332 boolValue = (JsBoolean) value;
333 pref.setSimpleMode(boolValue.booleanValue());
335 value = root.getValue(HASH_ALIGNBALOON);
336 if(value instanceof JsBoolean){
337 boolValue = (JsBoolean) value;
338 pref.setAlignBalooonWidthSetting(boolValue.booleanValue());
348 private void saveNetConfig(){
349 if( ! useConfigPath() ) return;
351 JsObject root = new JsObject();
352 JsObject proxy = ProxyInfo.buildJson(getProxyInfo());
353 root.putValue(HASH_PROXY, proxy);
355 if(this.loadedNetConfig != null){
356 if(this.loadedNetConfig.equals(root)) return;
359 ConfigFile.saveJson(new File(NETCONFIG_FILE), root);
367 private void saveTalkConfig(){
368 if( ! useConfigPath() ) return;
370 JsObject root = new JsObject();
372 JsObject font = FontInfo.buildJson(getFontInfo());
373 root.putValue(HASH_FONT, font);
375 DialogPref pref = getDialogPref();
377 new JsPair(HASH_USEBODYICON, pref.useBodyImage());
379 new JsPair(HASH_USEMONOTOMB, pref.useMonoImage());
381 new JsPair(HASH_SIMPLEMODE, pref.isSimpleMode());
383 new JsPair(HASH_ALIGNBALOON, pref.alignBaloonWidth());
384 root.putPair(useBodyIcon);
385 root.putPair(useMonoTomb);
386 root.putPair(isSimple);
387 root.putPair(alignBaloon);
389 if(this.loadedTalkConfig != null){
390 if(this.loadedTalkConfig.equals(root)) return;
393 ConfigFile.saveJson(new File(TALKCONFIG_FILE), root);
399 * 各種設定を設定格納ディレクトリからロードする。
401 public void loadConfig(){
408 * 各種設定を設定格納ディレクトリへセーブする。
410 public void saveConfig(){