OSDN Git Service

Maven3対応。
[jindolf/Jindolf.git] / src / main / java / jp / sourceforge / jindolf / AppSetting.java
1 /*
2  * application settings
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf;
9
10 import java.awt.Font;
11 import java.awt.font.FontRenderContext;
12 import java.io.File;
13 import jp.sourceforge.jovsonz.JsBoolean;
14 import jp.sourceforge.jovsonz.JsObject;
15 import jp.sourceforge.jovsonz.JsPair;
16 import jp.sourceforge.jovsonz.JsValue;
17
18 /**
19  * アプリケーションの各種設定。
20  */
21 public class AppSetting{
22
23     private static final String NETCONFIG_FILE = "netconfig.json";
24     private static final String HASH_PROXY = "proxy";
25
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";
32
33     private OptionInfo optInfo;
34
35     private boolean useConfigPath;
36     private File configPath;
37
38     private FontInfo fontInfo = FontInfo.DEFAULT_FONTINFO;
39
40     private int frameWidth  = 800;
41     private int frameHeight = 600;
42     private int frameXpos = Integer.MIN_VALUE;
43     private int frameYpos = Integer.MIN_VALUE;
44
45     private ProxyInfo proxyInfo = ProxyInfo.DEFAULT;
46
47     private DialogPref dialogPref = new DialogPref();
48
49     private JsValue loadedNetConfig;
50     private JsValue loadedTalkConfig;
51
52     /**
53      * コンストラクタ。
54      */
55     public AppSetting(){
56         super();
57         return;
58     }
59
60     /**
61      * コマンドラインオプションからアプリ設定を展開する。
62      * @param optionInfo オプション情報
63      */
64     public void applyOptionInfo(OptionInfo optionInfo){
65         this.optInfo = optionInfo;
66         applyConfigPathSetting();
67         applyFontSetting();
68         applyGeometrySetting();
69         return;
70     }
71
72     /**
73      * 設定格納ディレクトリ関係の設定。
74      */
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));
86         }else{
87             this.useConfigPath = true;
88             File path = ConfigFile.getImplicitConfigDirectory();
89             this.configPath = path;
90         }
91
92         return;
93     }
94
95     /**
96      * フォント関係の設定。
97      */
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);
104
105         if(fontName != null){
106             Font font = Font.decode(fontName);
107             this.fontInfo = this.fontInfo.deriveFont(font);
108         }
109
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);
117         }
118
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);
126         }
127
128         return;
129     }
130
131     /**
132      * ジオメトリ関係の設定。
133      */
134     private void applyGeometrySetting(){
135         Integer ival;
136
137         ival = this.optInfo.initialFrameWidth();
138         if(ival != null) this.frameWidth = ival;
139
140         ival = this.optInfo.initialFrameHeight();
141         if(ival != null) this.frameHeight = ival;
142
143         ival = this.optInfo.initialFrameXpos();
144         if(ival != null) this.frameXpos = ival;
145
146         ival = this.optInfo.initialFrameYpos();
147         if(ival != null) this.frameYpos = ival;
148
149         return;
150     }
151
152     /**
153      * 設定格納ディレクトリを返す。
154      * @return 設定格納ディレクトリ。
155      */
156     public File getConfigPath(){
157         return this.configPath;
158     }
159
160     /**
161      * 設定格納ディレクトリを設定する。
162      * @param path 設定格納ディレクトリ
163      */
164     public void setConfigPath(File path){
165         this.configPath = path;
166         return;
167     }
168
169     /**
170      * 設定格納ディレクトリを使うか否かを返す。
171      * @return 使うならtrue
172      */
173     public boolean useConfigPath(){
174         return this.useConfigPath;
175     }
176
177     /**
178      * 設定格納ディレクトリを使うか否か設定する。
179      * @param need 使うならtrue
180      */
181     public void setUseConfigPath(boolean need){
182         this.useConfigPath = need;
183         return;
184     }
185
186     /**
187      * 初期のフレーム幅を返す。
188      * @return 初期のフレーム幅
189      */
190     public int initialFrameWidth(){
191         return this.frameWidth;
192     }
193
194     /**
195      * 初期のフレーム高を返す。
196      * @return 初期のフレーム高
197      */
198     public int initialFrameHeight(){
199         return this.frameHeight;
200     }
201
202     /**
203      * 初期のフレーム位置のX座標を返す。
204      * 特に指示されていなければInteger.MIN_VALUEを返す。
205      * @return 初期のフレーム位置のX座標
206      */
207     public int initialFrameXpos(){
208         return this.frameXpos;
209     }
210
211     /**
212      * 初期のフレーム位置のY座標を返す。
213      * 特に指示されていなければInteger.MIN_VALUEを返す。
214      * @return 初期のフレーム位置のY座標
215      */
216     public int initialFrameYpos(){
217         return this.frameYpos;
218     }
219
220     /**
221      * フォント設定を返す。
222      * @return フォント設定
223      */
224     public FontInfo getFontInfo(){
225         return this.fontInfo;
226     }
227
228     /**
229      * フォント設定を更新する。
230      * @param fontInfo フォント設定
231      */
232     public void setFontInfo(FontInfo fontInfo){
233         this.fontInfo = fontInfo;
234         return;
235     }
236
237     /**
238      * プロクシ設定を返す。
239      * @return プロクシ設定
240      */
241     public ProxyInfo getProxyInfo(){
242         return this.proxyInfo;
243     }
244
245     /**
246      * プロクシ設定を更新する。
247      * @param proxyInfo プロクシ設定。nullならプロクシなしと解釈される。
248      */
249     public void setProxyInfo(ProxyInfo proxyInfo){
250         if(proxyInfo == null) this.proxyInfo = ProxyInfo.DEFAULT;
251         else                  this.proxyInfo = proxyInfo;
252         return;
253     }
254
255     /**
256      * 発言表示設定を返す。
257      * @return 表示設定
258      */
259     public DialogPref getDialogPref(){
260         return this.dialogPref;
261     }
262
263     /**
264      * 発言表示設定を返す。
265      * @param pref 表示設定
266      */
267     public void setDialogPref(DialogPref pref){
268         if(pref == null) this.dialogPref = new DialogPref();
269         else             this.dialogPref = pref;
270         return;
271     }
272
273     /**
274      * ネットワーク設定をロードする。
275      */
276     private void loadNetConfig(){
277         if( ! useConfigPath() ) return;
278
279         JsValue value = ConfigFile.loadJson(new File(NETCONFIG_FILE));
280         if(value == null) return;
281         this.loadedNetConfig = value;
282
283         if( ! (value instanceof JsObject) ) return;
284         JsObject root = (JsObject) value;
285
286         value = root.getValue(HASH_PROXY);
287         if( ! (value instanceof JsObject) ) return;
288         JsObject proxy = (JsObject) value;
289
290         ProxyInfo info = ProxyInfo.decodeJson(proxy);
291
292         setProxyInfo(info);
293
294         return;
295     }
296
297     /**
298      * 会話表示設定をロードする。
299      */
300     private void loadTalkConfig(){
301         if( ! useConfigPath() ) return;
302
303         JsValue value = ConfigFile.loadJson(new File(TALKCONFIG_FILE));
304         if(value == null) return;
305         this.loadedTalkConfig = value;
306
307         if( ! (value instanceof JsObject) ) return;
308         JsObject root = (JsObject) value;
309
310         value = root.getValue(HASH_FONT);
311         if(value instanceof JsObject){
312             JsObject font = (JsObject) value;
313             FontInfo info = FontInfo.decodeJson(font);
314             setFontInfo(info);
315             applyFontSetting();
316         }
317
318         DialogPref pref = new DialogPref();
319         JsBoolean boolValue;
320         value = root.getValue(HASH_USEBODYICON);
321         if(value instanceof JsBoolean){
322             boolValue = (JsBoolean) value;
323             pref.setBodyImageSetting(boolValue.booleanValue());
324         }
325         value = root.getValue(HASH_USEMONOTOMB);
326         if(value instanceof JsBoolean){
327             boolValue = (JsBoolean) value;
328             pref.setMonoImageSetting(boolValue.booleanValue());
329         }
330         value = root.getValue(HASH_SIMPLEMODE);
331         if(value instanceof JsBoolean){
332             boolValue = (JsBoolean) value;
333             pref.setSimpleMode(boolValue.booleanValue());
334         }
335         value = root.getValue(HASH_ALIGNBALOON);
336         if(value instanceof JsBoolean){
337             boolValue = (JsBoolean) value;
338             pref.setAlignBalooonWidthSetting(boolValue.booleanValue());
339         }
340         setDialogPref(pref);
341
342         return;
343     }
344
345     /**
346      * ネットワーク設定をセーブする。
347      */
348     private void saveNetConfig(){
349         if( ! useConfigPath() ) return;
350
351         JsObject root = new JsObject();
352         JsObject proxy = ProxyInfo.buildJson(getProxyInfo());
353         root.putValue(HASH_PROXY, proxy);
354
355         if(this.loadedNetConfig != null){
356             if(this.loadedNetConfig.equals(root)) return;
357         }
358
359         ConfigFile.saveJson(new File(NETCONFIG_FILE), root);
360
361         return;
362     }
363
364     /**
365      * 会話表示設定をセーブする。
366      */
367     private void saveTalkConfig(){
368         if( ! useConfigPath() ) return;
369
370         JsObject root = new JsObject();
371
372         JsObject font = FontInfo.buildJson(getFontInfo());
373         root.putValue(HASH_FONT, font);
374
375         DialogPref pref = getDialogPref();
376         JsPair useBodyIcon =
377                 new JsPair(HASH_USEBODYICON, pref.useBodyImage());
378         JsPair useMonoTomb =
379                 new JsPair(HASH_USEMONOTOMB, pref.useMonoImage());
380         JsPair isSimple =
381                 new JsPair(HASH_SIMPLEMODE, pref.isSimpleMode());
382         JsPair alignBaloon =
383                 new JsPair(HASH_ALIGNBALOON, pref.alignBaloonWidth());
384         root.putPair(useBodyIcon);
385         root.putPair(useMonoTomb);
386         root.putPair(isSimple);
387         root.putPair(alignBaloon);
388
389         if(this.loadedTalkConfig != null){
390             if(this.loadedTalkConfig.equals(root)) return;
391         }
392
393         ConfigFile.saveJson(new File(TALKCONFIG_FILE), root);
394
395         return;
396     }
397
398     /**
399      * 各種設定を設定格納ディレクトリからロードする。
400      */
401     public void loadConfig(){
402         loadNetConfig();
403         loadTalkConfig();
404         return;
405     }
406
407     /**
408      * 各種設定を設定格納ディレクトリへセーブする。
409      */
410     public void saveConfig(){
411         saveNetConfig();
412         saveTalkConfig();
413         return;
414     }
415
416 }