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.ShowNewNAddressDialogCommand;
41  import ui.command.deletion.DeleteNAddressCommand;
42  import ui.command.information.InfoOnNAddressCommand;
43  import ui.table.NAddressTable;
44  import ui.table.TableSorter;
45  import ui.table.model.NAddressTableModel;
46  import base.user.User;
47  
48  @SuppressWarnings("serial") //$NON-NLS-1$
49  public class TabledNAddressPanel extends JPanel {
50  
51  	private static final transient Logger logger = Logger
52  			.getLogger(TabledNAddressPanel.class.getName());
53  
54  	private JTable nAddressTable;
55  
56  	private TableSorter nAddressTableSorter;
57  
58  	private JScrollPane nAddressTableScrollPane;
59  
60  	private NAddressTableModel nAddressTableModel;
61  
62  	private JPanel nAddressButtonPanel;
63  
64  	private JButton newNAddressButton;
65  
66  	private JButton infoOnNAddressButton;
67  
68  	private JButton deleteNAddressButton;
69  
70  	private final User user;
71  
72  	public TabledNAddressPanel(User user) {
73  		this.user = user;
74  		initialize();
75  	}
76  
77  	protected void initialize() {
78  		this.setBorder(new TitledBorder(Messages.getString("common.normalmailaddresses"))); //$NON-NLS-1$
79  		this.setLayout(new BorderLayout());
80  		this.add(getNAddressTableScrollPane(), BorderLayout.CENTER);
81  		this.add(getNAddressButtonPanel(), BorderLayout.EAST);
82  	}
83  
84  	/***
85  	 * @return Returns the nAddressTable.
86  	 */
87  	protected JTable getNAddressTable() {
88  		if (nAddressTable == null) {
89  			nAddressTable = new NAddressTable(getNAddressTableSorter());
90  			nAddressTable.setPreferredScrollableViewportSize(new Dimension(500,
91  					70));
92  			// We need to place it here to avoid a ciclyc call...
93  			nAddressTableSorter.setTableHeader(nAddressTable.getTableHeader());
94  			// Set up tool tips for column headers.
95  			nAddressTable
96  					.getTableHeader()
97  					.setToolTipText(
98  							Messages.getString("tablesorter.header.tooltip")); //$NON-NLS-1$
99  		}
100 		return nAddressTable;
101 	}
102 
103 	/***
104 	 * @return Returns the nAddressTableScrollPane.
105 	 */
106 	protected JScrollPane getNAddressTableScrollPane() {
107 		if (nAddressTableScrollPane == null) {
108 			nAddressTableScrollPane = new JScrollPane(getNAddressTable());
109 		}
110 		return nAddressTableScrollPane;
111 	}
112 
113 	/***
114 	 * @return Returns the nAddressTableSorter.
115 	 */
116 	protected TableSorter getNAddressTableSorter() {
117 		if (nAddressTableSorter == null) {
118 			nAddressTableSorter = new TableSorter(getNAddressTableModel());
119 		}
120 		return nAddressTableSorter;
121 	}
122 
123 	/***
124 	 * @return Returns the nAddressTableModel.
125 	 */
126 	protected NAddressTableModel getNAddressTableModel() {
127 		if (nAddressTableModel == null) {
128 			nAddressTableModel = new NAddressTableModel(getUser());
129 		}
130 		return nAddressTableModel;
131 	}
132 
133 	/***
134 	 * @return Returns the nAddressButtonPanel.
135 	 */
136 	protected JPanel getNAddressButtonPanel() {
137 		if (nAddressButtonPanel == null) {
138 			nAddressButtonPanel = new JPanel();
139 			nAddressButtonPanel.setLayout(new GridLayout(3, 1));
140 			nAddressButtonPanel.add(getNewNAddressButton());
141 			nAddressButtonPanel.add(getDeleteNAddressButton());
142 			nAddressButtonPanel.add(getInfoOnNAddressButton());
143 		}
144 		return nAddressButtonPanel;
145 	}
146 
147 	/***
148 	 * @return Returns the deleteNAddressButton.
149 	 */
150 	protected JButton getDeleteNAddressButton() {
151 		if (deleteNAddressButton == null) {
152 			deleteNAddressButton = new JButton();
153 			deleteNAddressButton.setIcon(new ImageIcon(this.getClass()
154 					.getResource("/icon/16x16/actions/list-remove.png"))); //$NON-NLS-1$
155 
156 			deleteNAddressButton.addActionListener(new ActionListener() {
157 				public void actionPerformed(ActionEvent arg0) {
158 					logger.debug("actionPerformed deleteNAddressButton"); //$NON-NLS-1$
159 					if (getNAddressTable().getSelectedRow() != -1) {
160 						int nAddressId = Integer
161 								.parseInt(((TableSorter) getNAddressTable()
162 										.getModel()).getValueAt(
163 										getNAddressTable().getSelectedRow(),
164 										NAddressTableModel.NADDRESS_ID_COLUMN)
165 										.toString());
166 						CommandExecutor.getInstance().executeCommand(
167 								new DeleteNAddressCommand(getUser(),
168 										nAddressId, getNAddressTableModel()),
169 								false);
170 					} else
171 						JOptionPane
172 								.showMessageDialog(
173 										null,
174 										Messages.getString("panel.tablednaddresspanel.message1"), //$NON-NLS-1$
175 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
176 				}
177 			});
178 		}
179 		return deleteNAddressButton;
180 	}
181 
182 	/***
183 	 * @return Returns the infoOnNAddressButton.
184 	 */
185 	protected JButton getInfoOnNAddressButton() {
186 		if (infoOnNAddressButton == null) {
187 			infoOnNAddressButton = new JButton();
188 			infoOnNAddressButton.setIcon(new ImageIcon(this.getClass()
189 					.getResource("/icon/16x16/status/dialog-information.png"))); //$NON-NLS-1$
190 
191 			infoOnNAddressButton.addActionListener(new ActionListener() {
192 				public void actionPerformed(ActionEvent arg0) {
193 					logger.debug("actionPerformed infoOnNAddressButton"); //$NON-NLS-1$
194 					if (getNAddressTable().getSelectedRow() != -1) {
195 						int nAddressId = Integer
196 								.parseInt(((TableSorter) getNAddressTable()
197 										.getModel()).getValueAt(
198 										getNAddressTable().getSelectedRow(),
199 										NAddressTableModel.NADDRESS_ID_COLUMN)
200 										.toString());
201 						CommandExecutor.getInstance()
202 								.executeCommand(
203 										new InfoOnNAddressCommand(getUser(),
204 												nAddressId), false);
205 					} else
206 						JOptionPane
207 								.showMessageDialog(
208 										null,
209 										Messages.getString("panel.tablednaddresspanel.message1"), //$NON-NLS-1$
210 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
211 				}
212 			});
213 		}
214 		return infoOnNAddressButton;
215 	}
216 
217 	/***
218 	 * @return Returns the newNAddressButton.
219 	 */
220 	protected JButton getNewNAddressButton() {
221 		if (newNAddressButton == null) {
222 			newNAddressButton = new JButton();
223 			newNAddressButton.setIcon(new ImageIcon(this.getClass()
224 					.getResource("/icon/16x16/actions/list-add.png"))); //$NON-NLS-1$
225 
226 			newNAddressButton.addActionListener(new ActionListener() {
227 				public void actionPerformed(ActionEvent arg0) {
228 					logger.debug("actionPerformed newNAddressButton"); //$NON-NLS-1$
229 					ShowNewNAddressDialogCommand showNewNAddressDialogCommand = new ShowNewNAddressDialogCommand(
230 							getUser(), getNAddressTableModel());
231 					CommandExecutor.getInstance().executeCommand(
232 							showNewNAddressDialogCommand, false);
233 				}
234 			});
235 		}
236 		return newNAddressButton;
237 	}
238 
239 	/***
240 	 * @return Returns the user.
241 	 */
242 	protected User getUser() {
243 		return user;
244 	}
245 
246 }