4 * License : The MIT License
5 * Copyright(c) 2008 olyutorskii
8 package jp.sourceforge.jindolf;
10 import java.awt.BorderLayout;
11 import java.awt.EventQueue;
12 import java.awt.Insets;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
16 import javax.swing.BorderFactory;
17 import javax.swing.ImageIcon;
18 import javax.swing.JButton;
19 import javax.swing.JComponent;
20 import javax.swing.JPanel;
21 import javax.swing.JScrollPane;
22 import javax.swing.JToolBar;
23 import javax.swing.JTree;
24 import javax.swing.border.Border;
25 import javax.swing.event.TreeSelectionEvent;
26 import javax.swing.event.TreeSelectionListener;
27 import javax.swing.tree.TreeModel;
28 import javax.swing.tree.TreePath;
29 import javax.swing.tree.TreeSelectionModel;
34 @SuppressWarnings("serial")
35 public class LandsTree
37 implements ActionListener, TreeSelectionListener{
39 private static final String TIP_ASCEND = "押すと降順に";
40 private static final String TIP_DESCEND = "押すと昇順に";
41 private static final String TIP_ORDER = "選択中の国の村一覧を読み込み直す";
42 private static final ImageIcon ICON_ASCEND;
43 private static final ImageIcon ICON_DESCEND;
47 url = Jindolf.getResource("resources/image/ascend.png");
48 ICON_ASCEND = new ImageIcon(url);
49 url = Jindolf.getResource("resources/image/descend.png");
50 ICON_DESCEND = new ImageIcon(url);
53 private final JButton orderButton = new JButton();
54 private final JButton reloadButton = new JButton();
55 private final JTree treeView = new JTree();
57 private boolean ascending = false;
67 this.orderButton.setIcon(ICON_DESCEND);
68 this.orderButton.setToolTipText(TIP_DESCEND);
69 this.orderButton.setMargin(new Insets(1, 1, 1, 1));
70 this.orderButton.setActionCommand(ActionManager.CMD_SWITCHORDER);
71 this.orderButton.addActionListener(this);
74 url = Jindolf.getResource("resources/image/reload.png");
75 this.reloadButton.setIcon(new ImageIcon(url));
76 this.reloadButton.setToolTipText(TIP_ORDER);
77 this.reloadButton.setMargin(new Insets(1, 1, 1, 1));
78 this.reloadButton.setActionCommand(ActionManager.CMD_VILLAGELIST);
80 TreeSelectionModel selModel = this.treeView.getSelectionModel();
81 selModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
82 this.treeView.addTreeSelectionListener(this);
90 private void design(){
91 BorderLayout layout = new BorderLayout();
94 JToolBar toolBar = new JToolBar();
95 toolBar.add(this.orderButton);
96 toolBar.add(this.reloadButton);
98 JComponent landSelector = createLandSelector();
100 add(toolBar, BorderLayout.NORTH);
101 add(landSelector, BorderLayout.CENTER);
107 * 国村選択ツリーコンポーネントを生成する。
108 * @return 国村選択ツリーコンポーネント
110 private JComponent createLandSelector(){
111 this.treeView.setRootVisible(false);
112 this.treeView.setCellRenderer(new VillageIconRenderer());
114 Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);
115 this.treeView.setBorder(border);
117 JScrollPane landSelector = new JScrollPane(this.treeView);
126 public JButton getReloadVillageListButton(){
127 return this.reloadButton;
134 public JTree getTreeView(){
135 return this.treeView;
142 public void expandLand(Land land){
143 TreeModel model = this.treeView.getModel();
144 Object root = model.getRoot();
145 Object[] path = {root, land};
146 TreePath treePath = new TreePath(path);
147 this.treeView.expandPath(treePath);
155 private LandsModel getLandsModel(){
156 TreeModel model = this.treeView.getModel();
157 if(model instanceof LandsModel){
158 return (LandsModel) model;
165 * @return 反転後が昇順ならtrue
167 private boolean toggleTreeOrder(){
168 this.ascending = ! this.ascending;
171 this.orderButton.setToolTipText(TIP_ASCEND);
172 this.orderButton.setIcon(ICON_ASCEND);
174 this.orderButton.setToolTipText(TIP_DESCEND);
175 this.orderButton.setIcon(ICON_DESCEND);
178 final TreePath lastPath = this.treeView.getSelectionPath();
180 LandsModel model = getLandsModel();
182 model.setAscending(this.ascending);
185 EventQueue.invokeLater(new Runnable(){
187 if(lastPath != null){
188 LandsTree.this.treeView.setSelectionPath(lastPath);
189 LandsTree.this.treeView.scrollPathToVisible(lastPath);
195 return this.ascending;
201 * @param event ボタン押下イベント {@inheritDoc}
204 public void actionPerformed(ActionEvent event){
205 String cmd = event.getActionCommand();
206 if(ActionManager.CMD_SWITCHORDER.equals(cmd)){
214 * ツリーリストで何らかの要素(国、村)がクリックされたときの処理。
215 * @param event イベント {@inheritDoc}
218 public void valueChanged(TreeSelectionEvent event){
219 TreePath path = event.getNewLeadSelectionPath();
221 this.reloadButton.setEnabled(false);
225 Object selObj = path.getLastPathComponent();
227 if( selObj instanceof Land ){
228 this.reloadButton.setEnabled(true);
230 this.reloadButton.setEnabled(false);
240 public void updateUI(){
242 if(this.treeView != null){
243 this.treeView.setCellRenderer(new VillageIconRenderer());