View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package ui.panel;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Dimension;
24  import java.awt.GridLayout;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  
28  import javax.swing.ImageIcon;
29  import javax.swing.JButton;
30  import javax.swing.JOptionPane;
31  import javax.swing.JPanel;
32  import javax.swing.JScrollPane;
33  import javax.swing.JTable;
34  import javax.swing.border.TitledBorder;
35  
36  import org.apache.log4j.Logger;
37  
38  import ui.Messages;
39  import ui.command.CommandExecutor;
40  import ui.command.creation.ShowNewPhoneNumberDialogCommand;
41  import ui.command.deletion.DeletePhoneNumberCommand;
42  import ui.command.information.InfoOnPhoneNumberCommand;
43  import ui.table.PhoneNumberTable;
44  import ui.table.TableSorter;
45  import ui.table.model.PhoneNumberTableModel;
46  import base.user.User;
47  
48  @SuppressWarnings("serial") //$NON-NLS-1$
49  public class TabledPhoneNumberPanel extends JPanel {
50  
51  	private static final transient Logger logger = Logger
52  			.getLogger(TabledPhoneNumberPanel.class.getName());
53  
54  	private JTable phoneNumberTable;
55  
56  	private TableSorter phoneNumberTableSorter;
57  
58  	private JScrollPane phoneNumberTableScrollPane;
59  
60  	private PhoneNumberTableModel phoneNumberTableModel;
61  
62  	private JPanel phoneNumberButtonPanel;
63  
64  	private JButton newPhoneNumberButton;
65  
66  	private JButton infoOnPhoneNumberButton;
67  
68  	private JButton deletePhoneNumberButton;
69  
70  	private final User user;
71  
72  	public TabledPhoneNumberPanel(User user) {
73  		this.user = user;
74  		initialize();
75  	}
76  
77  	protected void initialize() {
78  		this.setBorder(new TitledBorder(Messages.getString("common.phonenumbers"))); //$NON-NLS-1$
79  		this.setLayout(new BorderLayout());
80  		this.add(getPhoneNumberTableScrollPane(), BorderLayout.CENTER);
81  		this.add(getPhoneNumberButtonPanel(), BorderLayout.EAST);
82  	}
83  
84  	/***
85  	 * @return Returns the phoneNumberTable.
86  	 */
87  	protected JTable getPhoneNumberTable() {
88  		if (phoneNumberTable == null) {
89  			phoneNumberTable = new PhoneNumberTable(getPhoneNumberTableSorter());
90  			phoneNumberTable.setPreferredScrollableViewportSize(new Dimension(
91  					500, 70));
92  			// We need to place it here to avoid a ciclyc call...
93  			phoneNumberTableSorter.setTableHeader(phoneNumberTable
94  					.getTableHeader());
95  			// Set up tool tips for column headers.
96  			phoneNumberTable
97  					.getTableHeader()
98  					.setToolTipText(
99  							Messages.getString("tablesorter.header.tooltip")); //$NON-NLS-1$
100 		}
101 		return phoneNumberTable;
102 	}
103 
104 	/***
105 	 * @return Returns the phoneNumberTableScrollPane.
106 	 */
107 	protected JScrollPane getPhoneNumberTableScrollPane() {
108 		if (phoneNumberTableScrollPane == null) {
109 			phoneNumberTableScrollPane = new JScrollPane(getPhoneNumberTable());
110 		}
111 		return phoneNumberTableScrollPane;
112 	}
113 
114 	/***
115 	 * @return Returns the phoneNumberTableSorter.
116 	 */
117 	protected TableSorter getPhoneNumberTableSorter() {
118 		if (phoneNumberTableSorter == null) {
119 			phoneNumberTableSorter = new TableSorter(getPhoneNumberTableModel());
120 		}
121 		return phoneNumberTableSorter;
122 	}
123 
124 	/***
125 	 * @return Returns the phoneNumberTableModel.
126 	 */
127 	protected PhoneNumberTableModel getPhoneNumberTableModel() {
128 		if (phoneNumberTableModel == null) {
129 			phoneNumberTableModel = new PhoneNumberTableModel(getUser());
130 		}
131 		return phoneNumberTableModel;
132 	}
133 
134 	/***
135 	 * @return Returns the phoneNumberButtonPanel.
136 	 */
137 	protected JPanel getPhoneNumberButtonPanel() {
138 		if (phoneNumberButtonPanel == null) {
139 			phoneNumberButtonPanel = new JPanel();
140 			phoneNumberButtonPanel.setLayout(new GridLayout(3, 1));
141 			phoneNumberButtonPanel.add(getNewPhoneNumberButton());
142 			phoneNumberButtonPanel.add(getDeletePhoneNumberButton());
143 			phoneNumberButtonPanel.add(getInfoOnPhoneNumberButton());
144 		}
145 		return phoneNumberButtonPanel;
146 	}
147 
148 	/***
149 	 * @return Returns the deletePhoneNumberButton.
150 	 */
151 	protected JButton getDeletePhoneNumberButton() {
152 		if (deletePhoneNumberButton == null) {
153 			deletePhoneNumberButton = new JButton();
154 			deletePhoneNumberButton.setIcon(new ImageIcon(this.getClass()
155 					.getResource("/icon/16x16/actions/list-remove.png"))); //$NON-NLS-1$
156 
157 			deletePhoneNumberButton.addActionListener(new ActionListener() {
158 				public void actionPerformed(ActionEvent arg0) {
159 					logger.debug("actionPerformed deletePhoneNumberButton"); //$NON-NLS-1$
160 					if (getPhoneNumberTable().getSelectedRow() != -1) {
161 						int phoneNumberId = Integer
162 								.parseInt(((TableSorter) getPhoneNumberTable()
163 										.getModel())
164 										.getValueAt(
165 												getPhoneNumberTable()
166 														.getSelectedRow(),
167 												PhoneNumberTableModel.PHONENUMBER_ID_COLUMN)
168 										.toString());
169 						CommandExecutor.getInstance().executeCommand(
170 								new DeletePhoneNumberCommand(getUser(),
171 										phoneNumberId,
172 										getPhoneNumberTableModel()), false);
173 					} else
174 						JOptionPane
175 								.showMessageDialog(
176 										null,
177 										Messages.getString("panel.tabledphonenumberpanel.message1"), //$NON-NLS-1$
178 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
179 				}
180 			});
181 
182 		}
183 		return deletePhoneNumberButton;
184 	}
185 
186 	/***
187 	 * @return Returns the infoOnPhoneNumberButton.
188 	 */
189 	protected JButton getInfoOnPhoneNumberButton() {
190 		if (infoOnPhoneNumberButton == null) {
191 			infoOnPhoneNumberButton = new JButton();
192 			infoOnPhoneNumberButton.setIcon(new ImageIcon(this.getClass()
193 					.getResource("/icon/16x16/status/dialog-information.png"))); //$NON-NLS-1$
194 
195 			infoOnPhoneNumberButton.addActionListener(new ActionListener() {
196 				public void actionPerformed(ActionEvent arg0) {
197 					logger.debug("actionPerformed infoOnPhoneNumberButton"); //$NON-NLS-1$
198 					if (getPhoneNumberTable().getSelectedRow() != -1) {
199 						int phoneNumberId = Integer
200 								.parseInt(((TableSorter) getPhoneNumberTable()
201 										.getModel())
202 										.getValueAt(
203 												getPhoneNumberTable()
204 														.getSelectedRow(),
205 												PhoneNumberTableModel.PHONENUMBER_ID_COLUMN)
206 										.toString());
207 						CommandExecutor.getInstance().executeCommand(
208 								new InfoOnPhoneNumberCommand(getUser(),
209 										phoneNumberId), false);
210 					} else
211 						JOptionPane
212 								.showMessageDialog(
213 										null,
214 										Messages.getString("panel.tabledphonenumberpanel.message1"), //$NON-NLS-1$
215 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
216 				}
217 			});
218 		}
219 		return infoOnPhoneNumberButton;
220 	}
221 
222 	/***
223 	 * @return Returns the newPhoneNumberButton.
224 	 */
225 	protected JButton getNewPhoneNumberButton() {
226 		if (newPhoneNumberButton == null) {
227 			newPhoneNumberButton = new JButton();
228 			newPhoneNumberButton.setIcon(new ImageIcon(this.getClass()
229 					.getResource("/icon/16x16/actions/list-add.png"))); //$NON-NLS-1$
230 
231 			newPhoneNumberButton.addActionListener(new ActionListener() {
232 				public void actionPerformed(ActionEvent arg0) {
233 					logger.debug("actionPerformed newPhoneNumberButton"); //$NON-NLS-1$
234 					ShowNewPhoneNumberDialogCommand showNewPhoneNumberDialogCommand = new ShowNewPhoneNumberDialogCommand(
235 							getUser(), getPhoneNumberTableModel());
236 					CommandExecutor.getInstance().executeCommand(
237 							showNewPhoneNumberDialogCommand, false);
238 				}
239 			});
240 		}
241 		return newPhoneNumberButton;
242 	}
243 
244 	/***
245 	 * @return Returns the user.
246 	 */
247 	protected User getUser() {
248 		return user;
249 	}
250 
251 }