OSDN Git Service

スタートアップ処理の改善
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / view / WindowManager.java
1 /*
2  * window manager
3  *
4  * License : The MIT License
5  * Copyright(c) 2012 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.view;
9
10 import java.awt.Component;
11 import java.awt.Container;
12 import java.awt.Frame;
13 import java.awt.Window;
14 import java.util.LinkedList;
15 import java.util.List;
16 import javax.swing.JComponent;
17 import javax.swing.JMenu;
18 import javax.swing.JPopupMenu;
19 import jp.sfjp.jindolf.VerInfo;
20 import jp.sfjp.jindolf.editor.TalkPreview;
21 import jp.sfjp.jindolf.log.LogFrame;
22 import jp.sfjp.jindolf.summary.DaySummary;
23 import jp.sfjp.jindolf.summary.VillageDigest;
24
25 /**
26  * ウィンドウ群の管理を行う。
27  */
28 public class WindowManager {
29
30     private static final String TITLE_FILTER =
31             getFrameTitle("発言フィルタ");
32     private static final String TITLE_LOGGER =
33             getFrameTitle("ログ表示");
34     private static final String TITLE_EDITOR =
35             getFrameTitle("発言エディタ");
36     private static final String TITLE_OPTION =
37             getFrameTitle("オプション設定");
38     private static final String TITLE_FIND =
39             getFrameTitle("発言検索");
40     private static final String TITLE_ACCOUNT =
41             getFrameTitle("アカウント管理");
42     private static final String TITLE_DIGEST =
43             getFrameTitle("村のダイジェスト");
44     private static final String TITLE_DAYSUMMARY =
45             getFrameTitle("発言集計");
46     private static final String TITLE_HELP =
47             getFrameTitle("ヘルプ");
48
49     private static final Frame NULLPARENT = null;
50
51
52     private FilterPanel filterPanel;
53     private LogFrame logFrame;
54     private TalkPreview talkPreview;
55     private OptionPanel optionPanel;
56     private FindPanel findPanel;
57     private AccountPanel accountPanel;
58     private VillageDigest villageDigest;
59     private DaySummary daySummary;
60     private HelpFrame helpFrame;
61     private TopFrame topFrame;
62
63     private final List<Window> windowSet = new LinkedList<Window>();
64
65
66     /**
67      * コンストラクタ。
68      */
69     public WindowManager(){
70         super();
71         return;
72     }
73
74
75     /**
76      * ウィンドウタイトルに前置詞をつける。
77      * @param text 元タイトル
78      * @return タイトル文字列
79      */
80     private static String getFrameTitle(String text){
81         String result = VerInfo.getFrameTitle(text);
82         return result;
83     }
84
85
86     /**
87      * 発言フィルタウィンドウを生成する。
88      * @return 発言フィルタウィンドウ
89      */
90     protected FilterPanel createFilterPanel(){
91         FilterPanel result;
92
93         result = new FilterPanel(NULLPARENT);
94         result.setTitle(TITLE_FILTER);
95         result.pack();
96         result.setVisible(false);
97
98         this.windowSet.add(result);
99
100         return result;
101     }
102
103     /**
104      * 発言フィルタウィンドウを返す。
105      * @return 発言フィルタウィンドウ
106      */
107     public FilterPanel getFilterPanel(){
108         if(this.filterPanel == null){
109             this.filterPanel = createFilterPanel();
110         }
111         return this.filterPanel;
112     }
113
114     /**
115      * ログウィンドウを生成する。
116      * @return ログウィンドウ
117      */
118     protected LogFrame createLogFrame(){
119         LogFrame result;
120
121         result = new LogFrame(NULLPARENT);
122         result.setTitle(TITLE_LOGGER);
123         result.pack();
124         result.setSize(600, 500);
125         result.setLocationByPlatform(true);
126         result.setVisible(false);
127
128         this.windowSet.add(result);
129
130         return result;
131     }
132
133     /**
134      * ログウィンドウを返す。
135      * @return ログウィンドウ
136      */
137     public LogFrame getLogFrame(){
138         if(this.logFrame == null){
139             this.logFrame = createLogFrame();
140         }
141         return this.logFrame;
142     }
143
144     /**
145      * 発言エディタウィンドウを生成する。
146      * @return 発言エディタウィンドウ
147      */
148     protected TalkPreview createTalkPreview(){
149         TalkPreview result;
150
151         result = new TalkPreview();
152         result.setTitle(TITLE_EDITOR);
153         result.pack();
154         result.setSize(700, 500);
155         result.setVisible(false);
156
157         this.windowSet.add(result);
158
159         return result;
160     }
161
162     /**
163      * 発言エディタウィンドウを返す。
164      * @return 発言エディタウィンドウ
165      */
166     public TalkPreview getTalkPreview(){
167         if(this.talkPreview == null){
168             this.talkPreview = createTalkPreview();
169         }
170         return this.talkPreview;
171     }
172
173     /**
174      * オプション設定ウィンドウを生成する。
175      * @return オプション設定ウィンドウ
176      */
177     protected OptionPanel createOptionPanel(){
178         OptionPanel result;
179
180         result = new OptionPanel(NULLPARENT);
181         result.setTitle(TITLE_OPTION);
182         result.pack();
183         result.setSize(450, 500);
184         result.setVisible(false);
185
186         this.windowSet.add(result);
187
188         return result;
189     }
190
191     /**
192      * オプション設定ウィンドウを返す。
193      * @return オプション設定ウィンドウ
194      */
195     public OptionPanel getOptionPanel(){
196         if(this.optionPanel == null){
197             this.optionPanel = createOptionPanel();
198         }
199         return this.optionPanel;
200     }
201
202     /**
203      * 検索ウィンドウを生成する。
204      * @return 検索ウィンドウ
205      */
206     protected FindPanel createFindPanel(){
207         FindPanel result;
208
209         result = new FindPanel(NULLPARENT);
210         result.setTitle(TITLE_FIND);
211         result.pack();
212         result.setVisible(false);
213
214         this.windowSet.add(result);
215
216         return result;
217     }
218
219     /**
220      * 検索ウィンドウを返す。
221      * @return 検索ウィンドウ
222      */
223     public FindPanel getFindPanel(){
224         if(this.findPanel == null){
225             this.findPanel = createFindPanel();
226         }
227         return this.findPanel;
228     }
229
230     /**
231      * ログインウィンドウを生成する。
232      * @return ログインウィンドウ
233      */
234     protected AccountPanel createAccountPanel(){
235         AccountPanel result;
236
237         result = new AccountPanel(NULLPARENT);
238         result.setTitle(TITLE_ACCOUNT);
239         result.pack();
240         result.setVisible(false);
241
242         this.windowSet.add(result);
243
244         return result;
245     }
246
247     /**
248      * ログインウィンドウを返す。
249      * @return ログインウィンドウ
250      */
251     public AccountPanel getAccountPanel(){
252         if(this.accountPanel == null){
253             this.accountPanel = createAccountPanel();
254         }
255         return this.accountPanel;
256     }
257
258     /**
259      * 村ダイジェストウィンドウを生成する。
260      * @return 村ダイジェストウィンドウ
261      */
262     protected VillageDigest createVillageDigest(){
263         VillageDigest result;
264
265         result = new VillageDigest(NULLPARENT);
266         result.setTitle(TITLE_DIGEST);
267         result.pack();
268         result.setSize(600, 550);
269         result.setVisible(false);
270
271         this.windowSet.add(result);
272
273         return result;
274     }
275
276     /**
277      * 村ダイジェストウィンドウを返す。
278      * @return 村ダイジェストウィンドウ
279      */
280     public VillageDigest getVillageDigest(){
281         if(this.villageDigest == null){
282             this.villageDigest = createVillageDigest();
283         }
284         return this.villageDigest;
285     }
286
287     /**
288      * 発言集計ウィンドウを生成する。
289      * @return 発言集計ウィンドウ
290      */
291     protected DaySummary createDaySummary(){
292         DaySummary result;
293
294         result = new DaySummary(NULLPARENT);
295         result.setTitle(TITLE_DAYSUMMARY);
296         result.pack();
297         result.setSize(400, 500);
298         result.setVisible(false);
299
300         this.windowSet.add(result);
301
302         return result;
303     }
304
305     /**
306      * 発言集計ウィンドウを返す。
307      * @return 発言集計ウィンドウ
308      */
309     public DaySummary getDaySummary(){
310         if(this.daySummary == null){
311             this.daySummary = createDaySummary();
312         }
313         return this.daySummary;
314     }
315
316     /**
317      * ヘルプウィンドウを生成する。
318      * @return ヘルプウィンドウ
319      */
320     protected HelpFrame createHelpFrame(){
321         HelpFrame result;
322
323         result = new HelpFrame();
324         result.setTitle(TITLE_HELP);
325         result.pack();
326         result.setSize(450, 450);
327         result.setVisible(false);
328
329         this.windowSet.add(result);
330
331         return result;
332     }
333
334     /**
335      * ヘルプウィンドウを返す。
336      * @return ヘルプウィンドウ
337      */
338     public HelpFrame getHelpFrame(){
339         if(this.helpFrame == null){
340             this.helpFrame = createHelpFrame();
341         }
342         return this.helpFrame;
343     }
344
345     /**
346      * トップフレームを生成する。
347      * @return トップフレーム
348      */
349     protected TopFrame createTopFrame(){
350         TopFrame result = new TopFrame();
351         result.setVisible(false);
352         this.windowSet.add(result);
353         return result;
354     }
355
356     /**
357      * トップフレームを返す。
358      * @return トップフレーム
359      */
360     public TopFrame getTopFrame(){
361         if(this.topFrame == null){
362             this.topFrame = createTopFrame();
363         }
364         return this.topFrame;
365     }
366
367     /**
368      * 管理下にある全ウィンドウのLookAndFeelを更新する。
369      * 必要に応じて再パッキングが行われる。
370      */
371     public void changeAllWindowUI(){
372         for(Window window : this.windowSet){
373             updateTreeUI(window);
374         }
375
376         if(this.filterPanel  != null) this.filterPanel.pack();
377         if(this.findPanel    != null) this.findPanel.pack();
378         if(this.accountPanel != null) this.accountPanel.pack();
379
380         return;
381     }
382
383     /**
384      * 再帰的に下層コンポーネントのLaFを更新する。
385      * <p>{@link javax.swing.SwingUtilities#updateComponentTreeUI(Component)}
386      * がポップアップメニューのLaF更新を正しく行わないSun製JREのバグ
387      * [BugID:6299213]
388      * を回避するために作られた。
389      * @param comp 開始コンポーネント
390      * @see <a href="http://bugs.sun.com/view_bug.do?bug_id=6299213">
391      * BugID:6299213
392      * </a>
393      */
394     public static void updateTreeUI(Component comp) {
395         updateTreeUI(comp, true);
396         return;
397     }
398
399     /**
400      * 再帰的に下層コンポーネントのLaFを更新する。
401      * @param comp 開始コンポーネント
402      * @param isRoot このコンポーネントが最上位か否か指定する。
403      * trueが指定された場合、LaF更新作業の後に再レイアウトを促す。
404      * パフォーマンスの観点から、ポップアップ以外の下層コンポーネントには
405      * 必要のない限りfalse指定を推奨。
406      */
407     public static void updateTreeUI(Component comp, boolean isRoot) {
408         if(comp instanceof JComponent){
409             JComponent jcomp = (JComponent)comp;
410             jcomp.updateUI();
411
412             JPopupMenu popup = jcomp.getComponentPopupMenu();
413             if(popup != null){
414                 updateTreeUI(popup, true);
415             }
416         }
417
418         if(comp instanceof JMenu){
419             JMenu menu = (JMenu)comp;
420             for(Component child : menu.getMenuComponents()){
421                 updateTreeUI(child, false);
422             }
423         }else if(comp instanceof Container){
424             Container cont = (Container)comp;
425             for(Component child : cont.getComponents()){
426                 updateTreeUI(child, false);
427             }
428         }
429
430         if(isRoot){
431             comp.invalidate();
432             comp.validate();
433             comp.repaint();
434         }
435
436         return;
437     }
438
439 }