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