OSDN Git Service

Merge branch 'master'
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / view / LandsTree.java
index 4f91e78..c5ad1e8 100644 (file)
@@ -8,10 +8,7 @@
 package jp.sfjp.jindolf.view;
 
 import java.awt.BorderLayout;
-import java.awt.EventQueue;
 import java.awt.Insets;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
 import javax.swing.BorderFactory;
 import javax.swing.Icon;
 import javax.swing.JButton;
@@ -22,7 +19,6 @@ import javax.swing.JToolBar;
 import javax.swing.JTree;
 import javax.swing.border.Border;
 import javax.swing.event.TreeSelectionEvent;
-import javax.swing.event.TreeSelectionListener;
 import javax.swing.tree.TreeModel;
 import javax.swing.tree.TreePath;
 import javax.swing.tree.TreeSelectionModel;
@@ -32,56 +28,93 @@ import jp.sfjp.jindolf.data.LandsTreeModel;
 
 /**
  * 国一覧Tree周辺コンポーネント群。
+ *
+ * <p>昇順/降順トグルボタン、村一覧リロードボタン、
+ * 人狼BBSサーバ国および村一覧ツリーから構成される。
  */
 @SuppressWarnings("serial")
-public class LandsTree
-        extends JPanel
-        implements ActionListener, TreeSelectionListener{
+public class LandsTree extends JPanel{
+
+    private static final String TIP_ASCEND =
+            "押すと降順に";
+    private static final String TIP_DESCEND =
+            "押すと昇順に";
+    private static final String TIP_ORDER =
+            "選択中の国の村一覧を読み込み直す";
+
+    private static final String RES_DIR = "resources/image/";
+    private static final String RES_ASCEND  = RES_DIR + "tb_ascend.png";
+    private static final String RES_DESCEND = RES_DIR + "tb_descend.png";
+    private static final String RES_RELOAD  = RES_DIR + "tb_reload.png";
 
-    private static final String TIP_ASCEND = "押すと降順に";
-    private static final String TIP_DESCEND = "押すと昇順に";
-    private static final String TIP_ORDER = "選択中の国の村一覧を読み込み直す";
     private static final Icon ICON_ASCEND;
     private static final Icon ICON_DESCEND;
+    private static final Icon ICON_RELOAD;
 
     static{
-        ICON_ASCEND =
-            ResourceManager.getButtonIcon("resources/image/tb_ascend.png");
-        ICON_DESCEND =
-            ResourceManager.getButtonIcon("resources/image/tb_descend.png");
+        ICON_ASCEND  = ResourceManager.getButtonIcon(RES_ASCEND);
+        ICON_DESCEND = ResourceManager.getButtonIcon(RES_DESCEND);
+        ICON_RELOAD  = ResourceManager.getButtonIcon(RES_RELOAD);
     }
 
-    private final JButton orderButton = new JButton();
-    private final JButton reloadButton = new JButton();
-    private final JTree treeView = new JTree();
+
+    private final JButton orderButton;
+    private final JButton reloadButton;
+    private final JTree treeView;
 
     private boolean ascending = false;
 
+
     /**
      * コンストラクタ。
      */
-    @SuppressWarnings("LeakingThisInConstructor")
     public LandsTree(){
         super();
 
+        this.orderButton = new JButton();
+        this.reloadButton = new JButton();
+        this.treeView = new JTree();
+
+        TreeSelectionModel selModel = this.treeView.getSelectionModel();
+        selModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
+
+        setupObserver();
+
+        decorateButtons();
         design();
 
+        return;
+    }
+
+
+    /**
+     * 各種監視Observerの登録。
+     */
+    private void setupObserver(){
+        this.orderButton.addActionListener((ev) -> {
+            toggleTreeOrder();
+        });
+
+        this.treeView.addTreeSelectionListener((ev) -> {
+            updateReloadButton(ev);
+        });
+
+        this.reloadButton.setActionCommand(ActionManager.CMD_VILLAGELIST);
+
+        return;
+    }
+
+    /**
+     * ボタン群を装飾する。
+     */
+    private void decorateButtons(){
         this.orderButton.setIcon(ICON_DESCEND);
         this.orderButton.setToolTipText(TIP_DESCEND);
         this.orderButton.setMargin(new Insets(1, 1, 1, 1));
-        this.orderButton.setActionCommand(ActionManager.CMD_SWITCHORDER);
-        this.orderButton.addActionListener(this);
 
-        Icon icon = ResourceManager
-                .getButtonIcon("resources/image/tb_reload.png");
-        this.reloadButton.setIcon(icon);
+        this.reloadButton.setIcon(ICON_RELOAD);
         this.reloadButton.setToolTipText(TIP_ORDER);
         this.reloadButton.setMargin(new Insets(1, 1, 1, 1));
-        this.reloadButton.setActionCommand(ActionManager.CMD_VILLAGELIST);
-
-        TreeSelectionModel selModel = this.treeView.getSelectionModel();
-        selModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
-        this.treeView.addTreeSelectionListener(this);
 
         return;
     }
@@ -94,6 +127,7 @@ public class LandsTree
         setLayout(layout);
 
         JToolBar toolBar = new JToolBar();
+        toolBar.setFloatable(false);
         toolBar.add(this.orderButton);
         toolBar.add(this.reloadButton);
 
@@ -107,6 +141,7 @@ public class LandsTree
 
     /**
      * 国村選択ツリーコンポーネントを生成する。
+     *
      * @return 国村選択ツリーコンポーネント
      */
     private JComponent createLandSelector(){
@@ -123,6 +158,7 @@ public class LandsTree
 
     /**
      * リロードボタンを返す。
+     *
      * @return リロードボタン
      */
     public JButton getReloadVillageListButton(){
@@ -131,6 +167,7 @@ public class LandsTree
 
     /**
      * 国村選択ツリービューを返す。
+     *
      * @return 国村選択ツリービュー
      */
     public JTree getTreeView(){
@@ -138,7 +175,10 @@ public class LandsTree
     }
 
     /**
-     * 指定した国を展開する。
+     * 指定した国をツリー展開する。
+     *
+     * <p>セクション一覧が国の下にツリー展開される。
+     *
      * @param land 国
      */
     public void expandLand(Land land){
@@ -152,6 +192,7 @@ public class LandsTree
 
     /**
      * 管理下のLandsTreeModelを返す。
+     *
      * @return LandsTreeModel
      */
     private LandsTreeModel getLandsModel(){
@@ -164,74 +205,61 @@ public class LandsTree
 
     /**
      * Tree表示順を反転させる。
+     *
+     * <p>昇順/降順ボタンも表示が切り替わる。
+     *
+     * <p>選択中のツリー要素があれば選択は保持される。
+     *
      * @return 反転後が昇順ならtrue
      */
     private boolean toggleTreeOrder(){
         this.ascending = ! this.ascending;
 
+        String newTip;
+        Icon newIcon;
         if(this.ascending){
-            this.orderButton.setToolTipText(TIP_ASCEND);
-            this.orderButton.setIcon(ICON_ASCEND);
+            newTip = TIP_ASCEND;
+            newIcon = ICON_ASCEND;
         }else{
-            this.orderButton.setToolTipText(TIP_DESCEND);
-            this.orderButton.setIcon(ICON_DESCEND);
+            newTip = TIP_DESCEND;
+            newIcon = ICON_DESCEND;
         }
+        this.orderButton.setToolTipText(newTip);
+        this.orderButton.setIcon(newIcon);
 
-        final TreePath lastPath = this.treeView.getSelectionPath();
+        TreePath lastPath = this.treeView.getSelectionPath();
 
         LandsTreeModel model = getLandsModel();
         if(model != null){
             model.setAscending(this.ascending);
         }
 
-        EventQueue.invokeLater(new Runnable(){
-            @Override
-            public void run(){
-                if(lastPath != null){
-                    LandsTree.this.treeView.setSelectionPath(lastPath);
-                    LandsTree.this.treeView.scrollPathToVisible(lastPath);
-                }
-                return;
-            }
-        });
+        if(lastPath != null){
+            this.treeView.setSelectionPath(lastPath);
+            this.treeView.scrollPathToVisible(lastPath);
+        }
 
         return this.ascending;
     }
 
     /**
-     * {@inheritDoc}
-     * ボタン押下処理。
-     * @param event ボタン押下イベント {@inheritDoc}
+     * ツリー選択状況によってリロードボタンの状態を変更する。
+     *
+     * <p>国がツリー選択された状況でのみリロードボタンは有効になる。
+     * その他の状況では無効に。
+     *
+     * @param event ツリー選択状況
      */
-    @Override
-    public void actionPerformed(ActionEvent event){
-        String cmd = event.getActionCommand();
-        if(ActionManager.CMD_SWITCHORDER.equals(cmd)){
-            toggleTreeOrder();
-        }
-        return;
-    }
+    private void updateReloadButton(TreeSelectionEvent event){
+        boolean reloadEnable = false;
 
-    /**
-     * {@inheritDoc}
-     * ツリーリストで何らかの要素(国、村)がクリックされたときの処理。
-     * @param event イベント {@inheritDoc}
-     */
-    @Override
-    public void valueChanged(TreeSelectionEvent event){
         TreePath path = event.getNewLeadSelectionPath();
-        if(path == null){
-            this.reloadButton.setEnabled(false);
-            return;
+        if(path != null){
+            Object selObj = path.getLastPathComponent();
+            if(selObj instanceof Land) reloadEnable = true;
         }
 
-        Object selObj = path.getLastPathComponent();
-
-        if( selObj instanceof Land ){
-            this.reloadButton.setEnabled(true);
-        }else{
-            this.reloadButton.setEnabled(false);
-        }
+        this.reloadButton.setEnabled(reloadEnable);
 
         return;
     }