OSDN Git Service

modify javadoc warnings.
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / view / ActionManager.java
1 /*
2  * action manager
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.view;
9
10 import java.awt.Insets;
11 import java.awt.event.ActionListener;
12 import java.awt.event.KeyEvent;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.Map;
16 import java.util.Set;
17 import javax.swing.AbstractButton;
18 import javax.swing.ButtonGroup;
19 import javax.swing.ButtonModel;
20 import javax.swing.Icon;
21 import javax.swing.JButton;
22 import javax.swing.JMenu;
23 import javax.swing.JMenuBar;
24 import javax.swing.JMenuItem;
25 import javax.swing.JRadioButtonMenuItem;
26 import javax.swing.JToolBar;
27 import javax.swing.KeyStroke;
28 import javax.swing.LookAndFeel;
29 import javax.swing.UIManager;
30 import jp.sfjp.jindolf.ResourceManager;
31 import jp.sfjp.jindolf.VerInfo;
32 import jp.sfjp.jindolf.util.GUIUtils;
33
34 /**
35  * メニュー、ボタン、その他各種Actionを伴うイベントを生成する
36  * コンポーネントの一括管理。
37  */
38 public class ActionManager{
39
40     /** アクション{@value}。 */
41     public static final String CMD_ACCOUNT    = "ACCOUNT";
42     /** アクション{@value}。 */
43     public static final String CMD_EXIT       = "EXIT";
44     /** アクション{@value}。 */
45     public static final String CMD_COPY       = "COPY";
46     /** アクション{@value}。 */
47     public static final String CMD_SHOWFIND   = "SHOWFIND";
48     /** アクション{@value}。 */
49     public static final String CMD_SEARCHNEXT = "SEARCHNEXT";
50     /** アクション{@value}。 */
51     public static final String CMD_SEARCHPREV = "SEARCHPREV";
52     /** アクション{@value}。 */
53     public static final String CMD_ALLPERIOD  = "ALLPERIOD";
54     /** アクション{@value}。 */
55     public static final String CMD_SHOWDIGEST = "DIGEST";
56     /** アクション{@value}。 */
57     public static final String CMD_WEBVILL    = "WEBVILL";
58     /** アクション{@value}。 */
59     public static final String CMD_WEBCAST    = "WEBCAST";
60     /** アクション{@value}。 */
61     public static final String CMD_WEBWIKI    = "WEBWIKI";
62     /** アクション{@value}。 */
63     public static final String CMD_RELOAD     = "RELOAD";
64     /** アクション{@value}。 */
65     public static final String CMD_DAYSUMMARY = "DAYSUMMARY";
66     /** アクション{@value}。 */
67     public static final String CMD_DAYEXPCSV  = "DAYEXPCSV";
68     /** アクション{@value}。 */
69     public static final String CMD_WEBDAY     = "WEBDAY";
70     /** アクション{@value}。 */
71     public static final String CMD_OPTION     = "OPTION";
72     /** アクション{@value}。 */
73     public static final String CMD_LANDF      = "LANDF";
74     /** アクション{@value}。 */
75     public static final String CMD_SHOWFILT   = "SHOWFILT";
76     /** アクション{@value}。 */
77     public static final String CMD_SHOWEDIT   = "SHOWEDIT";
78     /** アクション{@value}。 */
79     public static final String CMD_SHOWLOG    = "SHOWLOG";
80     /** アクション{@value}。 */
81     public static final String CMD_HELPDOC    = "HELPDOC";
82     /** アクション{@value}。 */
83     public static final String CMD_SHOWPORTAL = "SHOWPORTAL";
84     /** アクション{@value}。 */
85     public static final String CMD_ABOUT      = "ABOUT";
86
87     /** アクション{@value}。 */
88     public static final String CMD_COPYTALK    = "COPYTALK";
89     /** アクション{@value}。 */
90     public static final String CMD_JUMPANCHOR  = "JUMPANCHOR";
91     /** アクション{@value}。 */
92     public static final String CMD_WEBTALK     = "WEBTALK";
93     /** アクション{@value}。 */
94     public static final String CMD_SWITCHORDER = "SWITCHORDER";
95     /** アクション{@value}。 */
96     public static final String CMD_VILLAGELIST = "VILLAGELIST";
97     /** アクション{@value}。 */
98     public static final String CMD_FONTSIZESEL = "FONTSIZESEL";
99
100     /** WWWアイコン。 */
101     public static final Icon ICON_WWW = GUIUtils.getWWWIcon();
102     /** 検索アイコン。 */
103     public static final Icon ICON_FIND;
104     /** 前検索アイコン。 */
105     public static final Icon ICON_SEARCH_PREV;
106     /** 次検索アイコン。 */
107     public static final Icon ICON_SEARCH_NEXT;
108     /** リロードアイコン。 */
109     public static final Icon ICON_RELOAD;
110     /** フィルタアイコン。 */
111     public static final Icon ICON_FILTER;
112     /** 発言エディタアイコン。 */
113     public static final Icon ICON_EDITOR;
114
115     private static final KeyStroke KEY_F1 = KeyStroke.getKeyStroke("F1");
116     private static final KeyStroke KEY_F3 = KeyStroke.getKeyStroke("F3");
117     private static final KeyStroke KEY_SHIFT_F3 =
118             KeyStroke.getKeyStroke("shift F3");
119     private static final KeyStroke KEY_F5 = KeyStroke.getKeyStroke("F5");
120     private static final KeyStroke KEY_CTRL_F =
121             KeyStroke.getKeyStroke("ctrl F");
122
123     static{
124         ICON_FIND =
125             ResourceManager.getButtonIcon("resources/image/tb_find.png");
126
127         ICON_SEARCH_PREV =
128             ResourceManager.getButtonIcon("resources/image/tb_findprev.png");
129
130         ICON_SEARCH_NEXT =
131             ResourceManager.getButtonIcon("resources/image/tb_findnext.png");
132
133         ICON_RELOAD =
134             ResourceManager.getButtonIcon("resources/image/tb_reload.png");
135
136         ICON_FILTER =
137             ResourceManager.getButtonIcon("resources/image/tb_filter.png");
138
139         ICON_EDITOR =
140             ResourceManager.getButtonIcon("resources/image/tb_editor.png");
141     }
142
143     private final Set<AbstractButton> actionItems =
144             new HashSet<>();
145     private final Map<String, JMenuItem> namedMenuItems =
146             new HashMap<>();
147     private final Map<String, JButton> namedToolButtons =
148             new HashMap<>();
149
150     private final JMenuBar menuBar;
151
152     private final JMenu menuFile;
153     private final JMenu menuEdit;
154     private final JMenu menuVillage;
155     private final JMenu menuDay;
156     private final JMenu menuPreference;
157     private final JMenu menuTool;
158     private final JMenu menuHelp;
159
160     private final JMenu menuLook;
161     private final ButtonGroup landfGroup = new ButtonGroup();
162     private final Map<ButtonModel, String> landfMap =
163         new HashMap<>();
164
165     private final JToolBar browseToolBar;
166
167     /**
168      * コンストラクタ。
169      */
170     public ActionManager(){
171         this.menuFile       = buildMenu("Jindolf",  KeyEvent.VK_F);
172         this.menuEdit       = buildMenu("編集",     KeyEvent.VK_E);
173         this.menuVillage    = buildMenu("村",       KeyEvent.VK_V);
174         this.menuDay        = buildMenu("日",       KeyEvent.VK_D);
175         this.menuPreference = buildMenu("設定",     KeyEvent.VK_P);
176         this.menuTool       = buildMenu("ツール",   KeyEvent.VK_T);
177         this.menuHelp       = buildMenu("ヘルプ",   KeyEvent.VK_H);
178
179         this.menuLook = buildLookAndFeelMenu("ルック&フィール", KeyEvent.VK_L);
180
181         buildMenuItem(CMD_ACCOUNT, "アカウント管理", KeyEvent.VK_M);
182         buildMenuItem(CMD_EXIT, "終了", KeyEvent.VK_X);
183         buildMenuItem(CMD_COPY, "選択範囲をコピー", KeyEvent.VK_C);
184         buildMenuItem(CMD_SHOWFIND, "検索...", KeyEvent.VK_F);
185         buildMenuItem(CMD_SEARCHNEXT, "次候補", KeyEvent.VK_N);
186         buildMenuItem(CMD_SEARCHPREV, "前候補", KeyEvent.VK_P);
187         buildMenuItem(CMD_ALLPERIOD, "全日程の一括読み込み", KeyEvent.VK_R);
188         buildMenuItem(CMD_SHOWDIGEST, "村のダイジェストを表示...",
189                 KeyEvent.VK_D);
190         buildMenuItem(CMD_WEBVILL, "この村をブラウザで表示...", KeyEvent.VK_N);
191         buildMenuItem(CMD_WEBWIKI,
192                       "まとめサイトの村ページを表示...", KeyEvent.VK_M);
193         buildMenuItem(CMD_RELOAD, "この日を強制リロード", KeyEvent.VK_R);
194         buildMenuItem(CMD_DAYSUMMARY, "この日の発言を集計...", KeyEvent.VK_D);
195         buildMenuItem(CMD_DAYEXPCSV, "CSVへエクスポート...", KeyEvent.VK_C);
196         buildMenuItem(CMD_WEBDAY, "この日をブラウザで表示...", KeyEvent.VK_B);
197         buildMenuItem(CMD_OPTION, "オプション...", KeyEvent.VK_O);
198         buildMenuItem(CMD_SHOWFILT, "発言フィルタ", KeyEvent.VK_F);
199         buildMenuItem(CMD_SHOWEDIT, "発言エディタ", KeyEvent.VK_E);
200         buildMenuItem(CMD_SHOWLOG, "ログ表示", KeyEvent.VK_S);
201         buildMenuItem(CMD_HELPDOC, "ヘルプ表示", KeyEvent.VK_H);
202         buildMenuItem(CMD_SHOWPORTAL, "ポータルサイト...", KeyEvent.VK_P);
203         buildMenuItem(CMD_ABOUT, VerInfo.TITLE + "について...", KeyEvent.VK_A);
204
205         buildToolButton(CMD_RELOAD, "選択中の日を強制リロード", ICON_RELOAD);
206         buildToolButton(CMD_SHOWFIND,   "検索",     ICON_FIND);
207         buildToolButton(CMD_SEARCHPREV, "↑前候補", ICON_SEARCH_PREV);
208         buildToolButton(CMD_SEARCHNEXT, "↓次候補", ICON_SEARCH_NEXT);
209         buildToolButton(CMD_SHOWFILT, "発言フィルタ", ICON_FILTER);
210         buildToolButton(CMD_SHOWEDIT, "発言エディタ", ICON_EDITOR);
211
212         getMenuItem(CMD_SHOWPORTAL).setIcon(ICON_WWW);
213         getMenuItem(CMD_WEBVILL)   .setIcon(ICON_WWW);
214         getMenuItem(CMD_WEBWIKI)   .setIcon(ICON_WWW);
215         getMenuItem(CMD_WEBDAY)    .setIcon(ICON_WWW);
216         getMenuItem(CMD_SHOWFIND)  .setIcon(ICON_FIND);
217         getMenuItem(CMD_SEARCHPREV).setIcon(ICON_SEARCH_PREV);
218         getMenuItem(CMD_SEARCHNEXT).setIcon(ICON_SEARCH_NEXT);
219         getMenuItem(CMD_SHOWFILT)  .setIcon(ICON_FILTER);
220         getMenuItem(CMD_SHOWEDIT)  .setIcon(ICON_EDITOR);
221
222         registKeyAccelerator();
223
224         this.menuBar       = buildMenuBar();
225         this.browseToolBar = buildBrowseToolBar();
226
227         appearVillageImpl(false);
228         appearPeriodImpl(false);
229
230         return;
231     }
232
233     /**
234      * メニューを生成する。
235      * @param label メニューラベル
236      * @param nemonic ニモニックキー
237      * @return メニュー
238      */
239     private JMenu buildMenu(String label, int nemonic){
240         JMenu result = new JMenu();
241
242         String keyText = label + "(" + KeyEvent.getKeyText(nemonic) + ")";
243
244         result.setText(keyText);
245         result.setMnemonic(nemonic);
246
247         return result;
248     }
249
250     /**
251      * メニューアイテムを生成する。
252      * @param command アクションコマンド名
253      * @param label メニューラベル
254      * @param nemonic ニモニックキー
255      * @return メニューアイテム
256      */
257     private JMenuItem buildMenuItem(String command,
258                                       String label,
259                                       int nemonic ){
260         JMenuItem result = new JMenuItem();
261
262         String keyText = label + "(" + KeyEvent.getKeyText(nemonic) + ")";
263
264         result.setActionCommand(command);
265         result.setText(keyText);
266         result.setMnemonic(nemonic);
267
268         this.actionItems.add(result);
269         this.namedMenuItems.put(command, result);
270
271         return result;
272     }
273
274     /**
275      * ツールボタンを生成する。
276      * @param command アクションコマンド名
277      * @param tooltip ツールチップ文字列
278      * @param icon アイコン画像
279      * @return ツールボタン
280      */
281     private JButton buildToolButton(String command,
282                                       String tooltip,
283                                       Icon icon){
284         JButton result = new JButton();
285
286         result.setIcon(icon);
287         result.setToolTipText(tooltip);
288         result.setMargin(new Insets(1, 1, 1, 1));
289         result.setActionCommand(command);
290
291         this.actionItems.add(result);
292         this.namedToolButtons.put(command, result);
293
294         return result;
295     }
296
297     /**
298      * L&amp;F 一覧メニューを作成する。
299      * @param label メニューラベル
300      * @param nemonic ニモニックキー
301      * @return L&amp;F 一覧メニュー
302      */
303     private JMenu buildLookAndFeelMenu(String label, int nemonic){
304         JMenu result = buildMenu(label, nemonic);
305
306         LookAndFeel currentLookAndFeel = UIManager.getLookAndFeel();
307         String currentName = currentLookAndFeel.getClass().getName();
308         JMenuItem matchedButton = null;
309
310         UIManager.LookAndFeelInfo[] landfs =
311                 UIManager.getInstalledLookAndFeels();
312         for(UIManager.LookAndFeelInfo lafInfo : landfs){
313             String name      = lafInfo.getName();
314             String className = lafInfo.getClassName();
315
316             JRadioButtonMenuItem button = new JRadioButtonMenuItem(name);
317             button.setActionCommand(CMD_LANDF);
318
319             if(className.equals(currentName)) matchedButton = button;
320
321             this.actionItems.add(button);
322             this.landfGroup.add(button);
323             this.landfMap.put(button.getModel(), className);
324
325             result.add(button);
326         }
327
328         if(matchedButton != null) matchedButton.setSelected(true);
329
330         return result;
331     }
332
333     /**
334      * アクセラレータの設定。
335      */
336     private void registKeyAccelerator(){
337         getMenuItem(CMD_HELPDOC)    .setAccelerator(KEY_F1);
338         getMenuItem(CMD_SHOWFIND)   .setAccelerator(KEY_CTRL_F);
339         getMenuItem(CMD_SEARCHNEXT) .setAccelerator(KEY_F3);
340         getMenuItem(CMD_SEARCHPREV) .setAccelerator(KEY_SHIFT_F3);
341         getMenuItem(CMD_RELOAD)     .setAccelerator(KEY_F5);
342         return;
343     }
344
345     /**
346      * アクションコマンド名からメニューアイテムを探す。
347      * @param command アクションコマンド名
348      * @return メニューアイテム
349      */
350     private JMenuItem getMenuItem(String command){
351         JMenuItem result = this.namedMenuItems.get(command);
352         return result;
353     }
354
355     /**
356      * アクションコマンド名からツールボタンを探す。
357      * @param command アクションコマンド名
358      * @return ツールボタン
359      */
360     private JButton getToolButton(String command){
361         JButton result = this.namedToolButtons.get(command);
362         return result;
363     }
364
365     /**
366      * 現在メニューで選択中のL&amp;Fのクラス名を返す。
367      * @return L&amp;F クラス名
368      */
369     public String getSelectedLookAndFeel(){
370         ButtonModel selected = this.landfGroup.getSelection();
371         if(selected == null) return null;
372         String className = this.landfMap.get(selected);
373         return className;
374     }
375
376     /**
377      * 全てのボタンにアクションリスナーを登録する。
378      * @param listener アクションリスナー
379      */
380     public void addActionListener(ActionListener listener){
381         for(AbstractButton button : this.actionItems){
382             button.addActionListener(listener);
383         }
384         return;
385     }
386
387     /**
388      * メニューバーを生成する。
389      * @return メニューバー
390      */
391     private JMenuBar buildMenuBar(){
392         this.menuFile.add(getMenuItem(CMD_ACCOUNT));
393         this.menuFile.addSeparator();
394         this.menuFile.add(getMenuItem(CMD_EXIT));
395
396         this.menuEdit.add(getMenuItem(CMD_COPY));
397         this.menuEdit.addSeparator();
398         this.menuEdit.add(getMenuItem(CMD_SHOWFIND));
399         this.menuEdit.add(getMenuItem(CMD_SEARCHPREV));
400         this.menuEdit.add(getMenuItem(CMD_SEARCHNEXT));
401
402         this.menuVillage.add(getMenuItem(CMD_ALLPERIOD));
403         this.menuVillage.add(getMenuItem(CMD_SHOWDIGEST));
404         this.menuVillage.addSeparator();
405         this.menuVillage.add(getMenuItem(CMD_WEBVILL));
406         this.menuVillage.add(getMenuItem(CMD_WEBWIKI));
407
408         this.menuDay.add(getMenuItem(CMD_RELOAD));
409         this.menuDay.add(getMenuItem(CMD_DAYSUMMARY));
410         this.menuDay.add(getMenuItem(CMD_DAYEXPCSV));
411         this.menuDay.addSeparator();
412         this.menuDay.add(getMenuItem(CMD_WEBDAY));
413
414         this.menuPreference.add(getMenuItem(CMD_OPTION));
415         this.menuPreference.addSeparator();
416         this.menuPreference.add(this.menuLook);
417
418         this.menuTool.add(getMenuItem(CMD_SHOWFILT));
419         this.menuTool.add(getMenuItem(CMD_SHOWEDIT));
420         this.menuTool.add(getMenuItem(CMD_SHOWLOG));
421
422         this.menuHelp.add(getMenuItem(CMD_HELPDOC));
423         this.menuHelp.addSeparator();
424         this.menuHelp.add(getMenuItem(CMD_SHOWPORTAL));
425         this.menuHelp.add(getMenuItem(CMD_ABOUT));
426
427         JMenuBar bar = new JMenuBar();
428
429         bar.add(this.menuFile);
430         bar.add(this.menuEdit);
431         bar.add(this.menuVillage);
432         bar.add(this.menuDay);
433         bar.add(this.menuPreference);
434         bar.add(this.menuTool);
435         bar.add(this.menuHelp);
436
437         return bar;
438     }
439
440     /**
441      * メニューバーを取得する。
442      * @return メニューバー
443      */
444     public JMenuBar getMenuBar(){
445         return this.menuBar;
446     }
447
448     /**
449      * ブラウザ用ツールバーの生成を行う。
450      * @return ツールバー
451      */
452     private JToolBar buildBrowseToolBar(){
453         JToolBar toolBar = new JToolBar();
454
455         toolBar.add(getToolButton(CMD_RELOAD));
456         toolBar.addSeparator();
457         toolBar.add(getToolButton(CMD_SHOWFIND));
458         toolBar.add(getToolButton(CMD_SEARCHNEXT));
459         toolBar.add(getToolButton(CMD_SEARCHPREV));
460         toolBar.addSeparator();
461         toolBar.add(getToolButton(CMD_SHOWFILT));
462         toolBar.add(getToolButton(CMD_SHOWEDIT));
463
464         return toolBar;
465     }
466
467     /**
468      * ブラウザ用ツールバーを取得する。
469      * @return ツールバー
470      */
471     public JToolBar getBrowseToolBar(){
472         return this.browseToolBar;
473     }
474
475     /**
476      * Periodが表示されているか通知を受ける。
477      * @param appear 表示されているときはtrue
478      */
479     private void appearPeriodImpl(boolean appear){
480         if(appear) appearVillageImpl(appear);
481
482         this.menuEdit.setEnabled(appear);
483         this.menuDay .setEnabled(appear);
484
485         getToolButton(CMD_RELOAD)     .setEnabled(appear);
486         getToolButton(CMD_SHOWFIND)   .setEnabled(appear);
487         getToolButton(CMD_SEARCHNEXT) .setEnabled(appear);
488         getToolButton(CMD_SEARCHPREV) .setEnabled(appear);
489
490         return;
491     }
492
493     /**
494      * Periodが表示されているか通知を受ける。
495      * @param appear 表示されているときはtrue
496      */
497     public void appearPeriod(boolean appear){
498         appearPeriodImpl(appear);
499         return;
500     }
501
502     /**
503      * 村が表示されているか通知を受ける。
504      * @param appear 表示されているときはtrue
505      */
506     private void appearVillageImpl(boolean appear){
507         if( ! appear) appearPeriodImpl(appear);
508
509         this.menuVillage.setEnabled(appear);
510
511         return;
512     }
513
514     /**
515      * 村が表示されているか通知を受ける。
516      * @param appear 表示されているときはtrue
517      */
518     public void appearVillage(boolean appear){
519         appearVillageImpl(appear);
520         return;
521     }
522
523 }