4 * License : The MIT License
5 * Copyright(c) 2008 olyutorskii
8 package jp.sfjp.jindolf.view;
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;
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;
35 * メニュー、ボタン、その他各種Actionを伴うイベントを生成する
38 public class ActionManager{
41 public static final String CMD_ACCOUNT = "ACCOUNT";
43 public static final String CMD_EXIT = "EXIT";
45 public static final String CMD_COPY = "COPY";
47 public static final String CMD_SHOWFIND = "SHOWFIND";
49 public static final String CMD_SEARCHNEXT = "SEARCHNEXT";
51 public static final String CMD_SEARCHPREV = "SEARCHPREV";
53 public static final String CMD_ALLPERIOD = "ALLPERIOD";
55 public static final String CMD_SHOWDIGEST = "DIGEST";
57 public static final String CMD_WEBVILL = "WEBVILL";
59 public static final String CMD_WEBCAST = "WEBCAST";
61 public static final String CMD_WEBWIKI = "WEBWIKI";
63 public static final String CMD_RELOAD = "RELOAD";
65 public static final String CMD_DAYSUMMARY = "DAYSUMMARY";
67 public static final String CMD_DAYEXPCSV = "DAYEXPCSV";
69 public static final String CMD_WEBDAY = "WEBDAY";
71 public static final String CMD_OPTION = "OPTION";
73 public static final String CMD_LANDF = "LANDF";
75 public static final String CMD_SHOWFILT = "SHOWFILT";
77 public static final String CMD_SHOWEDIT = "SHOWEDIT";
79 public static final String CMD_SHOWLOG = "SHOWLOG";
81 public static final String CMD_HELPDOC = "HELPDOC";
83 public static final String CMD_SHOWPORTAL = "SHOWPORTAL";
85 public static final String CMD_ABOUT = "ABOUT";
88 public static final String CMD_COPYTALK = "COPYTALK";
90 public static final String CMD_JUMPANCHOR = "JUMPANCHOR";
92 public static final String CMD_WEBTALK = "WEBTALK";
94 public static final String CMD_SWITCHORDER = "SWITCHORDER";
96 public static final String CMD_VILLAGELIST = "VILLAGELIST";
98 public static final String CMD_FONTSIZESEL = "FONTSIZESEL";
101 public static final Icon ICON_WWW = GUIUtils.getWWWIcon();
103 public static final Icon ICON_FIND;
105 public static final Icon ICON_SEARCH_PREV;
107 public static final Icon ICON_SEARCH_NEXT;
109 public static final Icon ICON_RELOAD;
111 public static final Icon ICON_FILTER;
113 public static final Icon ICON_EDITOR;
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");
125 ResourceManager.getButtonIcon("resources/image/tb_find.png");
128 ResourceManager.getButtonIcon("resources/image/tb_findprev.png");
131 ResourceManager.getButtonIcon("resources/image/tb_findnext.png");
134 ResourceManager.getButtonIcon("resources/image/tb_reload.png");
137 ResourceManager.getButtonIcon("resources/image/tb_filter.png");
140 ResourceManager.getButtonIcon("resources/image/tb_editor.png");
143 private final Set<AbstractButton> actionItems =
145 private final Map<String, JMenuItem> namedMenuItems =
147 private final Map<String, JButton> namedToolButtons =
150 private final JMenuBar menuBar;
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;
160 private final JMenu menuLook;
161 private final ButtonGroup landfGroup = new ButtonGroup();
162 private final Map<ButtonModel, String> landfMap =
165 private final JToolBar browseToolBar;
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);
179 this.menuLook = buildLookAndFeelMenu("ルック&フィール", KeyEvent.VK_L);
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, "村のダイジェストを表示...",
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);
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);
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);
222 registKeyAccelerator();
224 this.menuBar = buildMenuBar();
225 this.browseToolBar = buildBrowseToolBar();
227 appearVillageImpl(false);
228 appearPeriodImpl(false);
235 * @param label メニューラベル
236 * @param nemonic ニモニックキー
239 private JMenu buildMenu(String label, int nemonic){
240 JMenu result = new JMenu();
242 String keyText = label + "(" + KeyEvent.getKeyText(nemonic) + ")";
244 result.setText(keyText);
245 result.setMnemonic(nemonic);
252 * @param command アクションコマンド名
253 * @param label メニューラベル
254 * @param nemonic ニモニックキー
257 private JMenuItem buildMenuItem(String command,
260 JMenuItem result = new JMenuItem();
262 String keyText = label + "(" + KeyEvent.getKeyText(nemonic) + ")";
264 result.setActionCommand(command);
265 result.setText(keyText);
266 result.setMnemonic(nemonic);
268 this.actionItems.add(result);
269 this.namedMenuItems.put(command, result);
276 * @param command アクションコマンド名
277 * @param tooltip ツールチップ文字列
281 private JButton buildToolButton(String command,
284 JButton result = new JButton();
286 result.setIcon(icon);
287 result.setToolTipText(tooltip);
288 result.setMargin(new Insets(1, 1, 1, 1));
289 result.setActionCommand(command);
291 this.actionItems.add(result);
292 this.namedToolButtons.put(command, result);
298 * L&F 一覧メニューを作成する。
299 * @param label メニューラベル
300 * @param nemonic ニモニックキー
301 * @return L&F 一覧メニュー
303 private JMenu buildLookAndFeelMenu(String label, int nemonic){
304 JMenu result = buildMenu(label, nemonic);
306 LookAndFeel currentLookAndFeel = UIManager.getLookAndFeel();
307 String currentName = currentLookAndFeel.getClass().getName();
308 JMenuItem matchedButton = null;
310 UIManager.LookAndFeelInfo[] landfs =
311 UIManager.getInstalledLookAndFeels();
312 for(UIManager.LookAndFeelInfo lafInfo : landfs){
313 String name = lafInfo.getName();
314 String className = lafInfo.getClassName();
316 JRadioButtonMenuItem button = new JRadioButtonMenuItem(name);
317 button.setActionCommand(CMD_LANDF);
319 if(className.equals(currentName)) matchedButton = button;
321 this.actionItems.add(button);
322 this.landfGroup.add(button);
323 this.landfMap.put(button.getModel(), className);
328 if(matchedButton != null) matchedButton.setSelected(true);
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);
346 * アクションコマンド名からメニューアイテムを探す。
347 * @param command アクションコマンド名
350 private JMenuItem getMenuItem(String command){
351 JMenuItem result = this.namedMenuItems.get(command);
356 * アクションコマンド名からツールボタンを探す。
357 * @param command アクションコマンド名
360 private JButton getToolButton(String command){
361 JButton result = this.namedToolButtons.get(command);
366 * 現在メニューで選択中のL&Fのクラス名を返す。
367 * @return L&F クラス名
369 public String getSelectedLookAndFeel(){
370 ButtonModel selected = this.landfGroup.getSelection();
371 if(selected == null) return null;
372 String className = this.landfMap.get(selected);
377 * 全てのボタンにアクションリスナーを登録する。
378 * @param listener アクションリスナー
380 public void addActionListener(ActionListener listener){
381 for(AbstractButton button : this.actionItems){
382 button.addActionListener(listener);
391 private JMenuBar buildMenuBar(){
392 this.menuFile.add(getMenuItem(CMD_ACCOUNT));
393 this.menuFile.addSeparator();
394 this.menuFile.add(getMenuItem(CMD_EXIT));
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));
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));
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));
414 this.menuPreference.add(getMenuItem(CMD_OPTION));
415 this.menuPreference.addSeparator();
416 this.menuPreference.add(this.menuLook);
418 this.menuTool.add(getMenuItem(CMD_SHOWFILT));
419 this.menuTool.add(getMenuItem(CMD_SHOWEDIT));
420 this.menuTool.add(getMenuItem(CMD_SHOWLOG));
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));
427 JMenuBar bar = new JMenuBar();
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);
444 public JMenuBar getMenuBar(){
452 private JToolBar buildBrowseToolBar(){
453 JToolBar toolBar = new JToolBar();
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));
471 public JToolBar getBrowseToolBar(){
472 return this.browseToolBar;
476 * Periodが表示されているか通知を受ける。
477 * @param appear 表示されているときはtrue
479 private void appearPeriodImpl(boolean appear){
480 if(appear) appearVillageImpl(appear);
482 this.menuEdit.setEnabled(appear);
483 this.menuDay .setEnabled(appear);
485 getToolButton(CMD_RELOAD) .setEnabled(appear);
486 getToolButton(CMD_SHOWFIND) .setEnabled(appear);
487 getToolButton(CMD_SEARCHNEXT) .setEnabled(appear);
488 getToolButton(CMD_SEARCHPREV) .setEnabled(appear);
494 * Periodが表示されているか通知を受ける。
495 * @param appear 表示されているときはtrue
497 public void appearPeriod(boolean appear){
498 appearPeriodImpl(appear);
504 * @param appear 表示されているときはtrue
506 private void appearVillageImpl(boolean appear){
507 if( ! appear) appearPeriodImpl(appear);
509 this.menuVillage.setEnabled(appear);
516 * @param appear 表示されているときはtrue
518 public void appearVillage(boolean appear){
519 appearVillageImpl(appear);