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.JLabel;
31  import javax.swing.JOptionPane;
32  import javax.swing.JPanel;
33  import javax.swing.JScrollPane;
34  import javax.swing.JTable;
35  import javax.swing.border.EtchedBorder;
36  import javax.swing.border.TitledBorder;
37  
38  import org.apache.log4j.Logger;
39  
40  import ui.Messages;
41  import ui.command.CommandExecutor;
42  import ui.command.creation.ShowNewServiceDialogCommand;
43  import ui.command.deletion.DeleteServiceCommand;
44  import ui.command.information.InfoOnServiceCommand;
45  import ui.table.ServiceTable;
46  import ui.table.TableSorter;
47  import ui.table.model.ServiceTableModel;
48  import ui.table.model.SessionTableModel;
49  
50  @SuppressWarnings("serial") //$NON-NLS-1$
51  public class ICServicePanel extends JPanel {
52  
53  	private static final transient Logger logger = Logger
54  			.getLogger(ICServicePanel.class.getName());
55  
56  	private JPanel contentPanel;
57  
58  	private JPanel buttonPanel;
59  
60  	private JButton newServiceButton;
61  
62  	private JButton deleteServiceButton;
63  
64  	private JButton infoOnServiceButton;
65  
66  	private JTable serviceTable;
67  
68  	private TableSorter serviceTableSorter;
69  
70  	private JScrollPane serviceTableScrollPane;
71  
72  	private ServiceTableModel serviceTableModel;
73  
74  	public ICServicePanel() {
75  		initialize();
76  	}
77  
78  	protected void initialize() {
79  		this.setLayout(new BorderLayout());
80  		this.add(getContentPanel(), BorderLayout.CENTER);
81  		this.add(getButtonPanel(), BorderLayout.SOUTH);
82  	}
83  
84  	/***
85  	 * @return Returns the buttonPanel.
86  	 */
87  	protected JPanel getButtonPanel() {
88  		if (buttonPanel == null) {
89  			buttonPanel = new JPanel();
90  			buttonPanel.setBorder(new EtchedBorder());
91  			buttonPanel.setLayout(new GridLayout(1, 5));
92  			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
93  			buttonPanel.add(getNewServiceButton());
94  			buttonPanel.add(getInfoOnServiceButton());
95  			buttonPanel.add(getDeleteServiceButton());
96  			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
97  
98  		}
99  		return buttonPanel;
100 	}
101 
102 	/***
103 	 * @return Returns the contentPanel.
104 	 */
105 	protected JPanel getContentPanel() {
106 		if (contentPanel == null) {
107 			contentPanel = new JPanel();
108 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.services")); //$NON-NLS-1$
109 			contentPanel.setBorder(titledBorder);
110 			contentPanel.setLayout(new BorderLayout());
111 			contentPanel.add(getServiceTableScrollPane(), BorderLayout.CENTER);
112 		}
113 		return contentPanel;
114 	}
115 
116 	/***
117 	 * @return Returns the deleteServiceButton.
118 	 */
119 	protected JButton getDeleteServiceButton() {
120 		if (deleteServiceButton == null) {
121 			deleteServiceButton = new JButton(Messages.getString("button.delete")); //$NON-NLS-1$
122 			deleteServiceButton.setIcon(new ImageIcon(this.getClass()
123 					.getResource("/icon/16x16/actions/edit-delete.png"))); //$NON-NLS-1$
124 
125 			deleteServiceButton.addActionListener(new ActionListener() {
126 				public void actionPerformed(ActionEvent arg0) {
127 					logger.debug("actionPerformed deleteServiceButton"); //$NON-NLS-1$
128 					if (getServiceTable().getSelectedRow() != -1) {
129 						int serviceId = Integer
130 								.parseInt(((TableSorter) getServiceTable()
131 										.getModel()).getValueAt(
132 										getServiceTable().getSelectedRow(),
133 										SessionTableModel.SESSION_ID_COLUMN)
134 										.toString());
135 						CommandExecutor.getInstance().executeCommand(
136 								new DeleteServiceCommand(serviceId), false);
137 					} else
138 						JOptionPane
139 								.showMessageDialog(
140 										null,
141 										Messages.getString("panel.icservicepanel.message1"), //$NON-NLS-1$
142 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
143 				}
144 			});
145 		}
146 		return deleteServiceButton;
147 	}
148 
149 	/***
150 	 * @return Returns the infoOnServiceButton.
151 	 */
152 	protected JButton getInfoOnServiceButton() {
153 		if (infoOnServiceButton == null) {
154 			infoOnServiceButton = new JButton(Messages.getString("button.info")); //$NON-NLS-1$
155 			infoOnServiceButton.setIcon(new ImageIcon(this.getClass()
156 					.getResource("/icon/16x16/status/dialog-information.png"))); //$NON-NLS-1$
157 
158 			infoOnServiceButton.addActionListener(new ActionListener() {
159 				public void actionPerformed(ActionEvent arg0) {
160 					logger.debug("actionPerformed infoOnServiceButton"); //$NON-NLS-1$
161 					if (getServiceTable().getSelectedRow() != -1) {
162 						int serviceId = Integer
163 								.parseInt(((TableSorter) getServiceTable()
164 										.getModel()).getValueAt(
165 										getServiceTable().getSelectedRow(),
166 										SessionTableModel.SESSION_ID_COLUMN)
167 										.toString());
168 						CommandExecutor.getInstance().executeCommand(
169 								new InfoOnServiceCommand(serviceId), false);
170 					} else
171 						JOptionPane
172 								.showMessageDialog(
173 										null,
174 										Messages.getString("panel.icservicepanel.message1"), //$NON-NLS-1$
175 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
176 
177 				}
178 			});
179 		}
180 		return infoOnServiceButton;
181 	}
182 
183 	/***
184 	 * @return Returns the newServiceButton.
185 	 */
186 	protected JButton getNewServiceButton() {
187 		if (newServiceButton == null) {
188 			newServiceButton = new JButton(Messages.getString("button.new")); //$NON-NLS-1$
189 			newServiceButton
190 					.setIcon(new ImageIcon(
191 							this
192 									.getClass()
193 									.getResource(
194 											"/icon/16x16/apps/preferences-desktop-remote-desktop.png"))); //$NON-NLS-1$
195 
196 			newServiceButton.addActionListener(new ActionListener() {
197 				public void actionPerformed(ActionEvent arg0) {
198 					logger.debug("actionPerformed newServiceButton"); //$NON-NLS-1$
199 					CommandExecutor.getInstance().executeCommand(
200 							new ShowNewServiceDialogCommand(), false);
201 				}
202 			});
203 		}
204 		return newServiceButton;
205 	}
206 
207 	/***
208 	 * @return Returns the serviceTableScrollPane.
209 	 */
210 	protected JScrollPane getServiceTableScrollPane() {
211 		if (serviceTableScrollPane == null) {
212 			serviceTableScrollPane = new JScrollPane(getServiceTable());
213 		}
214 		return serviceTableScrollPane;
215 	}
216 
217 	/***
218 	 * @return Returns the serviceTableSorter.
219 	 */
220 	protected TableSorter getServiceTableSorter() {
221 		if (serviceTableSorter == null) {
222 			serviceTableSorter = new TableSorter(getServiceTableModel());
223 		}
224 		return serviceTableSorter;
225 	}
226 
227 	/***
228 	 * @return Returns the serviceTableModel.
229 	 */
230 	protected ServiceTableModel getServiceTableModel() {
231 		if (serviceTableModel == null) {
232 			serviceTableModel = new ServiceTableModel();
233 		}
234 		return serviceTableModel;
235 	}
236 
237 	/***
238 	 * @return Returns the serviceTable.
239 	 */
240 	protected JTable getServiceTable() {
241 		if (serviceTable == null) {
242 			serviceTable = new ServiceTable(getServiceTableSorter());
243 			serviceTable.setPreferredScrollableViewportSize(new Dimension(500,
244 					70));
245 			// We need to place it here to avoid a ciclyc call...
246 			serviceTableSorter.setTableHeader(serviceTable.getTableHeader());
247 			// Set up tool tips for column headers.
248 			serviceTable
249 					.getTableHeader()
250 					.setToolTipText(
251 							Messages.getString("tablesorter.header.tooltip")); //$NON-NLS-1$
252 		}
253 		return serviceTable;
254 	}
255 
256 }