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.Color;
24  
25  import javax.swing.JPanel;
26  import javax.swing.JScrollPane;
27  import javax.swing.JSplitPane;
28  import javax.swing.JTable;
29  import javax.swing.border.TitledBorder;
30  import javax.swing.table.TableColumn;
31  
32  import ui.Messages;
33  import ui.table.TableSorter;
34  import ui.table.model.ReducedClientWorkstationTableModel;
35  import ui.table.model.ReducedUserTableModel;
36  import base.InternetCafeManager;
37  import base.network.NetworkManager;
38  import base.network.Workstation;
39  import base.user.User;
40  
41  @SuppressWarnings("serial") //$NON-NLS-1$
42  public class SessionCreationPanel extends JPanel {
43  
44  	private JPanel contentPanel;
45  
46  	private JSplitPane contentSplitPanel;
47  
48  	private JPanel userPanel;
49  
50  	private JScrollPane userTableScrollPane;
51  
52  	private JTable userTable;
53  
54  	private TableSorter userTableSorter;
55  
56  	private ReducedUserTableModel userTableModel;
57  
58  	private JPanel workstationPanel;
59  
60  	private JTable workstationTable;
61  
62  	private TableSorter workstationTableSorter;
63  
64  	private JScrollPane workstationTableScrollPane;
65  
66  	private ReducedClientWorkstationTableModel workstationTableModel;
67  
68  	public SessionCreationPanel() {
69  		initialize();
70  	}
71  
72  	protected void initialize() {
73  		this.setLayout(new BorderLayout());
74  		this.add(getContentPanel(), BorderLayout.CENTER);
75  	}
76  
77  	/***
78  	 * @return Returns the contentPanel.
79  	 */
80  	protected JSplitPane getContentSplitPanel() {
81  		if (contentSplitPanel == null) {
82  			contentSplitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
83  					getUserPanel(), getWorkstationPanel());
84  			contentSplitPanel.setOneTouchExpandable(true);
85  			contentSplitPanel.setResizeWeight(.5D);
86  		}
87  		return contentSplitPanel;
88  	}
89  
90  	/***
91  	 * @return Returns the userPanel.
92  	 */
93  	protected JPanel getUserPanel() {
94  		if (userPanel == null) {
95  			userPanel = new JPanel();
96  			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.users")); //$NON-NLS-1$
97  			userPanel.setBorder(titledBorder);
98  			userPanel.setLayout(new BorderLayout());
99  			userPanel.add(getUserTableScrollPane(), BorderLayout.CENTER);
100 		}
101 		return userPanel;
102 	}
103 
104 	/***
105 	 * @return Returns the userTable.
106 	 */
107 	protected JTable getUserTable() {
108 		if (userTable == null) {
109 			userTable = new JTable(getUserTableSorter());
110 			TableColumn idColumn = userTable.getColumnModel().getColumn(
111 					ReducedUserTableModel.USER_ID_COLUMN);
112 			idColumn.setPreferredWidth(20);
113 			idColumn.setMaxWidth(50);
114 			// We need to place it here to avoid a ciclyc call...
115 			userTableSorter.setTableHeader(userTable.getTableHeader());
116 			// Set up tool tips for column headers.
117 			userTable
118 					.getTableHeader()
119 					.setToolTipText(
120 							Messages.getString("tablesorter.header.tooltip")); //$NON-NLS-1$
121 		}
122 		return userTable;
123 	}
124 
125 	/***
126 	 * @return Returns the userTableScrollPane.
127 	 */
128 	protected JScrollPane getUserTableScrollPane() {
129 		if (userTableScrollPane == null) {
130 			userTableScrollPane = new JScrollPane(getUserTable());
131 			userTableScrollPane
132 					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
133 
134 		}
135 		return userTableScrollPane;
136 	}
137 
138 	/***
139 	 * @return Returns the userTableSorter.
140 	 */
141 	protected TableSorter getUserTableSorter() {
142 		if (userTableSorter == null) {
143 			userTableSorter = new TableSorter(getUserTableModel());
144 		}
145 		return userTableSorter;
146 	}
147 
148 	/***
149 	 * @return Returns the userTableModel.
150 	 */
151 	protected ReducedUserTableModel getUserTableModel() {
152 		if (userTableModel == null) {
153 			userTableModel = new ReducedUserTableModel();
154 		}
155 		return userTableModel;
156 	}
157 
158 	/***
159 	 * @return Returns the workstationTableScrollPane.
160 	 */
161 	protected JScrollPane getWorkstationTableScrollPane() {
162 		if (workstationTableScrollPane == null) {
163 			workstationTableScrollPane = new JScrollPane(getWorkstationTable());
164 			workstationTableScrollPane
165 					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
166 		}
167 		return workstationTableScrollPane;
168 	}
169 
170 	/***
171 	 * @return Returns the workstationTableSorter.
172 	 */
173 	protected TableSorter getWorkstationTableSorter() {
174 		if (workstationTableSorter == null) {
175 			workstationTableSorter = new TableSorter(getWorkstationTableModel());
176 		}
177 		return workstationTableSorter;
178 	}
179 
180 	/***
181 	 * @return Returns the workstationTableModel.
182 	 */
183 	protected ReducedClientWorkstationTableModel getWorkstationTableModel() {
184 		if (workstationTableModel == null) {
185 			workstationTableModel = new ReducedClientWorkstationTableModel();
186 		}
187 		return workstationTableModel;
188 	}
189 
190 	/***
191 	 * @return Returns the workstationTable.
192 	 */
193 	protected JTable getWorkstationTable() {
194 		if (workstationTable == null) {
195 			workstationTable = new JTable(getWorkstationTableSorter());
196 			TableColumn idColumn = workstationTable.getColumnModel().getColumn(
197 					ReducedUserTableModel.USER_ID_COLUMN);
198 			idColumn.setPreferredWidth(20);
199 			idColumn.setMaxWidth(50);
200 			TableColumn inUseColumn = workstationTable
201 					.getColumnModel()
202 					.getColumn(
203 							ReducedClientWorkstationTableModel.WORKSTATION_IN_USE_COLUMN);
204 			inUseColumn.setPreferredWidth(50);
205 			inUseColumn.setMaxWidth(50);
206 			// We need to place it here to avoid a ciclyc call...
207 			workstationTableSorter.setTableHeader(workstationTable
208 					.getTableHeader());
209 			// Set up tool tips for column headers.
210 			workstationTable
211 					.getTableHeader()
212 					.setToolTipText(
213 							Messages.getString("panel.sessioncreationpanel.message1")); //$NON-NLS-1$
214 		}
215 		return workstationTable;
216 	}
217 
218 	/***
219 	 * @return Returns the workstationPanel.
220 	 */
221 	protected JPanel getWorkstationPanel() {
222 		if (workstationPanel == null) {
223 			workstationPanel = new JPanel();
224 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.workstations")); //$NON-NLS-1$
225 			workstationPanel.setBorder(titledBorder);
226 			workstationPanel.setLayout(new BorderLayout());
227 			workstationPanel.add(getWorkstationTableScrollPane(),
228 					BorderLayout.CENTER);
229 		}
230 		return workstationPanel;
231 	}
232 
233 	/***
234 	 * @return Returns the contentPanel.
235 	 */
236 	protected JPanel getContentPanel() {
237 		if (contentPanel == null) {
238 			contentPanel = new JPanel();
239 			contentPanel.setLayout(new BorderLayout());
240 			contentPanel.add(getContentSplitPanel(), BorderLayout.CENTER);
241 		}
242 		return contentPanel;
243 	}
244 
245 	public User getUser() {
246 		User result = null;
247 		if (getWorkstationTable().getSelectedRow() != -1) {
248 			int userId = Integer.parseInt(((TableSorter) getUserTable()
249 					.getModel()).getValueAt(getUserTable().getSelectedRow(),
250 					ReducedUserTableModel.USER_ID_COLUMN).toString());
251 			result = InternetCafeManager.getInstance().getUserById(userId);
252 		}
253 		return result;
254 	}
255 
256 	public Workstation getWorkstation() {
257 		Workstation result = null;
258 		if (getWorkstationTable().getSelectedRow() != -1) {
259 			int workstationId = Integer
260 					.parseInt(((TableSorter) getWorkstationTable().getModel())
261 							.getValueAt(
262 									getWorkstationTable().getSelectedRow(),
263 									ReducedClientWorkstationTableModel.WORKSTATION_ID_COLUMN)
264 							.toString());
265 			result = NetworkManager.getInstance().getNetwork().getClientById(
266 					workstationId);
267 		}
268 		return result;
269 	}
270 
271 	public void setUserError(boolean isError) {
272 		if (isError) {
273 			((TitledBorder) this.getUserPanel().getBorder())
274 					.setTitleColor(Color.RED);
275 		} else
276 			((TitledBorder) this.getUserPanel().getBorder())
277 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
278 		this.getUserPanel().updateUI();
279 	}
280 
281 	public void setWorkstationError(boolean isError) {
282 		if (isError) {
283 			((TitledBorder) this.getWorkstationPanel().getBorder())
284 					.setTitleColor(Color.RED);
285 		} else
286 			((TitledBorder) this.getWorkstationPanel().getBorder())
287 					.setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
288 		this.getWorkstationPanel().updateUI();
289 	}
290 }