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
21 package ui.panel;
22
23 import java.awt.BorderLayout;
24 import java.awt.Dimension;
25 import java.awt.GridLayout;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28
29 import javax.swing.ImageIcon;
30 import javax.swing.JButton;
31 import javax.swing.JLabel;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.JScrollPane;
35 import javax.swing.JTable;
36 import javax.swing.border.EtchedBorder;
37 import javax.swing.border.TitledBorder;
38
39 import org.apache.log4j.Logger;
40
41 import ui.Messages;
42 import ui.command.CommandExecutor;
43 import ui.command.creation.ShowNewServerWorkstationDialogCommand;
44 import ui.command.deletion.DeleteServerWorkstationCommand;
45 import ui.command.information.InfoOnServerWorkstationCommand;
46 import ui.table.ServerWorkstationTable;
47 import ui.table.TableSorter;
48 import ui.table.model.ClientWorkstationTableModel;
49 import ui.table.model.ServerWorkstationTableModel;
50
51 /***
52 * @author skunk
53 *
54 */
55 @SuppressWarnings("serial")
56 public class TabledServerWorkstationPanel extends JPanel {
57
58 private static final transient Logger logger = Logger
59 .getLogger(TabledServerWorkstationPanel.class.getName());
60
61 private JTable workstationTable;
62
63 private TableSorter workstationTableSorter;
64
65 private JScrollPane workstationTableScrollPane;
66
67 private ServerWorkstationTableModel workstationTableModel;
68
69 private JPanel buttonPanel;
70
71 private JButton newServerWorkstationButton;
72
73 private JButton infoOnServerWorkstationButton;
74
75 private JButton deleteServerWorkstationButton;
76
77 public TabledServerWorkstationPanel() {
78 initialize();
79 }
80
81 protected void initialize() {
82 this.setLayout(new BorderLayout());
83 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.serverworkstations"));
84 this.setBorder(titledBorder);
85 this.add(getWorkstationTableScrollPane(), BorderLayout.CENTER);
86 this.add(getButtonPanel(), BorderLayout.EAST);
87 }
88
89 /***
90 * @return Returns the buttonPanel.
91 */
92 protected JPanel getButtonPanel() {
93 if (buttonPanel == null) {
94 buttonPanel = new JPanel();
95 buttonPanel.setLayout(new GridLayout(5, 1));
96 buttonPanel.setBorder(new EtchedBorder());
97 buttonPanel.add(new JLabel(""));
98 buttonPanel.add(getNewServerWorkstationButton());
99 buttonPanel.add(getInfoOnServerWorkstationButton());
100 buttonPanel.add(getDeleteServerWorkstationButton());
101 buttonPanel.add(new JLabel(""));
102 }
103 return buttonPanel;
104 }
105
106 /***
107 * @return Returns the workstationTableScrollPane.
108 */
109 protected JScrollPane getWorkstationTableScrollPane() {
110 if (workstationTableScrollPane == null) {
111 workstationTableScrollPane = new JScrollPane(getWorkstationTable());
112 }
113 return workstationTableScrollPane;
114 }
115
116 /***
117 * @return Returns the workstationTableSorter.
118 */
119 protected TableSorter getWorkstationTableSorter() {
120 if (workstationTableSorter == null) {
121 workstationTableSorter = new TableSorter(getWorkstationTableModel());
122 }
123 return workstationTableSorter;
124 }
125
126 /***
127 * @return Returns the workstationTableModel.
128 */
129 protected ServerWorkstationTableModel getWorkstationTableModel() {
130 if (workstationTableModel == null) {
131 workstationTableModel = new ServerWorkstationTableModel();
132 }
133 return workstationTableModel;
134 }
135
136 /***
137 * @return Returns the workstationTable.
138 */
139 protected JTable getWorkstationTable() {
140 if (workstationTable == null) {
141 workstationTable = new ServerWorkstationTable(
142 getWorkstationTableSorter());
143 workstationTable.setPreferredScrollableViewportSize(new Dimension(
144 500, 70));
145
146 workstationTableSorter.setTableHeader(workstationTable
147 .getTableHeader());
148
149 workstationTable
150 .getTableHeader()
151 .setToolTipText(
152 Messages.getString("tablesorter.header.tooltip"));
153 }
154 return workstationTable;
155 }
156
157 /***
158 * @return Returns the deleteServerWorkstationButton.
159 */
160 protected JButton getDeleteServerWorkstationButton() {
161 if (deleteServerWorkstationButton == null) {
162 deleteServerWorkstationButton = new JButton(
163 Messages.getString("button.delete"));
164 deleteServerWorkstationButton.setIcon(new ImageIcon(this.getClass()
165 .getResource("/icon/22x22/actions/edit-delete.png")));
166
167 deleteServerWorkstationButton
168 .addActionListener(new ActionListener() {
169 public void actionPerformed(ActionEvent arg0) {
170 logger
171 .debug("actionPerformed deleteServerWorkstationButton");
172 if (getWorkstationTable().getSelectedRow() != -1) {
173 int workstationId = Integer
174 .parseInt(((TableSorter) getWorkstationTable()
175 .getModel())
176 .getValueAt(
177 getWorkstationTable()
178 .getSelectedRow(),
179 ClientWorkstationTableModel.WORKSTATION_ID_COLUMN)
180 .toString());
181 CommandExecutor.getInstance().executeCommand(
182 new DeleteServerWorkstationCommand(
183 workstationId), false);
184 } else
185 JOptionPane
186 .showMessageDialog(
187 null,
188 Messages.getString("panel.tabledserverworkstationpanel.message1"),
189 Messages.getString("common.warning"),
190 JOptionPane.WARNING_MESSAGE);
191 }
192 });
193 }
194 return deleteServerWorkstationButton;
195 }
196
197 /***
198 * @return Returns the infoOnServerWorkstationButton.
199 */
200 protected JButton getInfoOnServerWorkstationButton() {
201 if (infoOnServerWorkstationButton == null) {
202 infoOnServerWorkstationButton = new JButton(
203 Messages.getString("button.info"));
204 infoOnServerWorkstationButton.setIcon(new ImageIcon(this.getClass()
205 .getResource("/icon/22x22/status/dialog-information.png")));
206
207 infoOnServerWorkstationButton
208 .addActionListener(new ActionListener() {
209 public void actionPerformed(ActionEvent arg0) {
210 logger
211 .debug("actionPerformed infoOnServerWorkstationButton");
212 if (getWorkstationTable().getSelectedRow() != -1) {
213 int workstationId = Integer
214 .parseInt(((TableSorter) getWorkstationTable()
215 .getModel())
216 .getValueAt(
217 getWorkstationTable()
218 .getSelectedRow(),
219 ClientWorkstationTableModel.WORKSTATION_ID_COLUMN)
220 .toString());
221 CommandExecutor.getInstance().executeCommand(
222 new InfoOnServerWorkstationCommand(
223 workstationId), false);
224 } else
225 JOptionPane
226 .showMessageDialog(
227 null,
228 Messages.getString("panel.tabledserverworkstationpanel.message1"),
229 Messages.getString("common.warning"),
230 JOptionPane.WARNING_MESSAGE);
231
232 }
233 });
234 }
235 return infoOnServerWorkstationButton;
236 }
237
238 /***
239 * @return Returns the newServerWorkstationButton.
240 */
241 protected JButton getNewServerWorkstationButton() {
242 if (newServerWorkstationButton == null) {
243 newServerWorkstationButton = new JButton(Messages.getString("button.new"));
244 newServerWorkstationButton.setIcon(new ImageIcon(this.getClass()
245 .getResource("/icon/22x22/places/network-server.png")));
246
247 newServerWorkstationButton.addActionListener(new ActionListener() {
248 public void actionPerformed(ActionEvent arg0) {
249 logger.debug("actionPerformed newServerWorkstationButton");
250 CommandExecutor.getInstance().executeCommand(
251 new ShowNewServerWorkstationDialogCommand(), false);
252 }
253 });
254 }
255 return newServerWorkstationButton;
256 }
257 }