1 /* JChessBoard -- a chess game
2  * Copyright (C) 2000-2004 Claus Divossen <claus.divossen@gmx.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 /* $Id: InfoPanel.java,v 1.11 2004/12/24 23:56:43 cdivossen Exp $ */
20 package jchessboard;
21 
22 import java.awt.Component;
23 import java.awt.Font;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.MouseAdapter;
27 import java.awt.event.MouseEvent;
28 
29 import javax.swing.JMenuItem;
30 import javax.swing.JOptionPane;
31 import javax.swing.JPopupMenu;
32 import javax.swing.JScrollPane;
33 import javax.swing.JTable;
34 import javax.swing.event.TableModelEvent;
35 
36 /**
37  * @author cd
38  *
39  * To change the template for this generated type comment go to
40  * Window>Preferences>Java>Code Generation>Code and Comments
41  */
42 
43 public class InfoPanel extends JScrollPane {
44 	 private PGN.STR str = new PGN.STR();
45 	 private TagTableModel tableModel;
46 	 private JTable jTable;
47 	 private JPopupMenu tagTableContextMenu;
48 
49 	 class TagTableModel extends javax.swing.table.AbstractTableModel {
getColumnCount()50 		 public int getColumnCount() {
51 			 return 2;
52 		 }
getRowCount()53 		 public int getRowCount() {
54 			 return str.size();
55 		 }
getColumnName(int col)56 		 public String getColumnName(int col) {
57 			 if (col == 0)
58 				 return "Tag";
59 			 if (col == 1)
60 				 return "Value";
61 			 return null;
62 		 }
getValueAt(int row, int col)63 		 public Object getValueAt(int row, int col) {
64 			 if (col == 0)
65 				 return str.tags()[row];
66 			 else if (col == 1)
67 				 return str.getTag(str.tags()[row]);
68 			 else
69 				 return null;
70 		 }
isCellEditable(int row, int column)71 		 public boolean isCellEditable(int row, int column) {
72 			 return column == 1;
73 		 }
setValueAt(Object value, int row, int column)74 		 public void setValueAt(Object value, int row, int column) {
75 			 if (column == 1 && value instanceof String) {
76 				 str.setTag(str.tags()[row], (String) value);
77 			 }
78 			// TODO: Update gameTable to show the new value.
79 		 }
80 	 }
81 
updateFields()82 	 private void updateFields() {
83 		 if (str == null)
84 			 return;
85 		 tableModel.fireTableChanged(new TableModelEvent(tableModel));
86 	 }
87 
setSTR(PGN.STR str)88 	 public void setSTR(PGN.STR str) {
89 		 this.str = str;
90 		 updateFields();
91 	 }
92 
getSTR()93 	 public PGN.STR getSTR() {
94 		 return str;
95 	 }
96 
InfoPanel()97 	 public InfoPanel() {
98 		 super();
99 		 tableModel = new TagTableModel();
100 		 jTable = new JTable(tableModel);
101 		 //            jTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
102 		 setViewportView(jTable);
103 		 jTable.setFont(new Font("SansSerrif", Font.PLAIN, 10));
104 		 jTable.setRowSelectionAllowed(false);
105 		 tagTableContextMenu = new JPopupMenu();
106 		 JMenuItem removeTagItem = new JMenuItem("Remove tag");
107 		 removeTagItem.addActionListener(new ActionListener() {
108 			 public void actionPerformed(ActionEvent e) {
109 				 str.removeTag(
110 					 (String) tableModel.getValueAt(
111 						 jTable.getSelectedRow(),
112 						 0));
113 				 tableModel.fireTableChanged(
114 					 new TableModelEvent(tableModel));
115 			 }
116 		 });
117 		 tagTableContextMenu.add(removeTagItem);
118 
119 		 JMenuItem addTagItem = new JMenuItem("Add tag...");
120 		 addTagItem.addActionListener(new ActionListener() {
121 			 public void actionPerformed(ActionEvent e) {
122 				 String tagName =
123 					 JOptionPane.showInputDialog(
124 						 null,
125 						 "Tag name:",
126 						 "Add tag",
127 						 JOptionPane.QUESTION_MESSAGE);
128 				 if (tagName != null
129 					 && !tagName.equals("")
130 					 && !str.hasTag(tagName)) {
131 					 str.setTag(tagName, "");
132 					 tableModel.fireTableChanged(
133 						 new TableModelEvent(tableModel));
134 				 }
135 			 }
136 		 });
137 		 tagTableContextMenu.add(addTagItem);
138 
139 		 jTable.addMouseListener(new MouseAdapter() {
140 			 public void mousePressed(MouseEvent e) {
141 				 if (e.isPopupTrigger()) {
142 					 tagTableContextMenu.show(
143 						 (Component) e.getSource(),
144 						 e.getX(),
145 						 e.getY());
146 				 }
147 			 }
148 			 public void mouseReleased(MouseEvent e) {
149 				 if (e.isPopupTrigger()) {
150 					 tagTableContextMenu.show(
151 						 (Component) e.getSource(),
152 						 e.getX(),
153 						 e.getY());
154 				 }
155 			 }
156 		 });
157 	 }
158  }