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.ShowNewSessionDialogCommand;
43 import ui.command.deletion.DeleteSessionCommand;
44 import ui.command.information.InfoOnSessionCommand;
45 import ui.table.SessionTable;
46 import ui.table.TableSorter;
47 import ui.table.model.SessionTableModel;
48
49 @SuppressWarnings("serial")
50 public class ICSessionPanel extends JPanel {
51
52 private static final transient Logger logger = Logger
53 .getLogger(ICSessionPanel.class.getName());
54
55 private JPanel sortPanel;
56
57 private JPanel sessionPanel;
58
59 private JPanel buttonPanel;
60
61 private JButton newSessionButton;
62
63 private JButton infoOnSessionButton;
64
65 private JButton deleteSessionButton;
66
67 private JTable sessionTable;
68
69 private JScrollPane sessionTableScrollPane;
70
71 private TableSorter sessionTableSorter;
72
73 private SessionTableModel sessionTableModel;
74
75 public ICSessionPanel() {
76 initialize();
77 }
78
79 protected void initialize() {
80 this.setName(Messages.getString("common.sessions"));
81 this.setLayout(new BorderLayout());
82 this.add(getSortPanel(), BorderLayout.NORTH);
83 this.add(getSessionPanel(), BorderLayout.CENTER);
84 this.add(getButtonPanel(), BorderLayout.SOUTH);
85 }
86
87 /***
88 * @return Returns the buttonPanel.
89 */
90 protected JPanel getButtonPanel() {
91 if (buttonPanel == null) {
92 buttonPanel = new JPanel();
93 buttonPanel.setLayout(new GridLayout(1, 5));
94 buttonPanel.setBorder(new EtchedBorder());
95 buttonPanel.add(new JLabel(""));
96 buttonPanel.add(getNewSessionButton());
97 buttonPanel.add(getInfoOnSessionButton());
98 buttonPanel.add(getDeleteSessionButton());
99 buttonPanel.add(new JLabel(""));
100 }
101 return buttonPanel;
102 }
103
104 /***
105 * @return Returns the sessionPanel.
106 */
107 protected JPanel getSessionPanel() {
108 if (sessionPanel == null) {
109 sessionPanel = new JPanel();
110 sessionPanel.setLayout(new BorderLayout());
111 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.sessions"));
112 sessionPanel.setBorder(titledBorder);
113 sessionPanel.add(getSessionTableScrollPane(), BorderLayout.CENTER);
114
115 }
116 return sessionPanel;
117 }
118
119 /***
120 * @return Returns the sortPanel.
121 */
122 protected JPanel getSortPanel() {
123 if (sortPanel == null) {
124 sortPanel = new SessionSearchPanel(getSessionTableModel());
125 }
126 return sortPanel;
127 }
128
129 /***
130 * @return Returns the sessionTableScrollPane.
131 */
132 protected JScrollPane getSessionTableScrollPane() {
133 if (sessionTableScrollPane == null) {
134 sessionTableScrollPane = new JScrollPane(getSessionTable());
135 }
136 return sessionTableScrollPane;
137 }
138
139 /***
140 * @return Returns the sessionTableSorter.
141 */
142 protected TableSorter getSessionTableSorter() {
143 if (sessionTableSorter == null) {
144 sessionTableSorter = new TableSorter(getSessionTableModel());
145 }
146 return sessionTableSorter;
147 }
148
149 /***
150 * @return Returns the sessionTableModel.
151 */
152 protected SessionTableModel getSessionTableModel() {
153 if (sessionTableModel == null) {
154 sessionTableModel = new SessionTableModel();
155 }
156 return sessionTableModel;
157 }
158
159 /***
160 * @return Returns the sessionTable.
161 */
162 protected JTable getSessionTable() {
163 if (sessionTable == null) {
164 sessionTable = new SessionTable(getSessionTableSorter());
165 sessionTable.setPreferredScrollableViewportSize(new Dimension(500,
166 70));
167
168 sessionTableSorter.setTableHeader(sessionTable.getTableHeader());
169
170 sessionTable
171 .getTableHeader()
172 .setToolTipText(
173 Messages.getString("tablesorte.header.tooltip"));
174 }
175 return sessionTable;
176 }
177
178 /***
179 * @return Returns the deleteSessionButton.
180 */
181 protected JButton getDeleteSessionButton() {
182 if (deleteSessionButton == null) {
183 deleteSessionButton = new JButton(Messages.getString("button.delete"));
184 deleteSessionButton.setIcon(new ImageIcon(this.getClass()
185 .getResource("/icon/16x16/actions/edit-delete.png")));
186
187 deleteSessionButton.addActionListener(new ActionListener() {
188 public void actionPerformed(ActionEvent arg0) {
189 logger.debug("actionPerformed deleteButton");
190 if (getSessionTable().getSelectedRow() != -1) {
191 int sessionId = Integer
192 .parseInt(((TableSorter) getSessionTable()
193 .getModel()).getValueAt(
194 getSessionTable().getSelectedRow(),
195 SessionTableModel.SESSION_ID_COLUMN)
196 .toString());
197 CommandExecutor.getInstance().executeCommand(
198 new DeleteSessionCommand(sessionId), false);
199 } else
200 JOptionPane
201 .showMessageDialog(
202 null,
203 Messages.getString("panel.icsessionpanel.message1"),
204 Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE);
205 }
206 });
207 }
208 return deleteSessionButton;
209 }
210
211 /***
212 * @return Returns the infoOnSessionButton.
213 */
214 protected JButton getInfoOnSessionButton() {
215 if (infoOnSessionButton == null) {
216 infoOnSessionButton = new JButton(Messages.getString("button.info"));
217 infoOnSessionButton.setIcon(new ImageIcon(this.getClass()
218 .getResource("/icon/16x16/status/dialog-information.png")));
219 infoOnSessionButton.addActionListener(new ActionListener() {
220 public void actionPerformed(ActionEvent arg0) {
221 logger.debug("actionPerformed infoButton");
222 if (getSessionTable().getSelectedRow() != -1) {
223 int sessionId = Integer
224 .parseInt(((TableSorter) getSessionTable()
225 .getModel()).getValueAt(
226 getSessionTable().getSelectedRow(),
227 SessionTableModel.SESSION_ID_COLUMN)
228 .toString());
229 CommandExecutor.getInstance().executeCommand(
230 new InfoOnSessionCommand(sessionId), false);
231 } else
232 JOptionPane
233 .showMessageDialog(
234 null,
235 Messages.getString("panel.icsessionpanel.message1"),
236 Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE);
237 }
238 });
239 }
240 return infoOnSessionButton;
241 }
242
243 /***
244 * @return Returns the newSessionButton.
245 */
246 protected JButton getNewSessionButton() {
247 if (newSessionButton == null) {
248 newSessionButton = new JButton(Messages.getString("button.new"));
249 newSessionButton.setIcon(new ImageIcon(this.getClass().getResource(
250 "/icon/16x16/actions/window-new.png")));
251 newSessionButton.addActionListener(new ActionListener() {
252 public void actionPerformed(ActionEvent arg0) {
253 CommandExecutor.getInstance().executeCommand(
254 new ShowNewSessionDialogCommand(), false);
255 logger.debug("actionPerformed newButton");
256 }
257 });
258
259 }
260 return newSessionButton;
261 }
262
263 }