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  
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  import java.io.File;
29  
30  import javax.swing.ImageIcon;
31  import javax.swing.JButton;
32  import javax.swing.JLabel;
33  import javax.swing.JOptionPane;
34  import javax.swing.JPanel;
35  import javax.swing.JScrollPane;
36  import javax.swing.JTable;
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.IO.LoadBackupCommand;
44  import ui.command.creation.ShowNewBackupCommand;
45  import ui.command.deletion.DeleteBackupCommand;
46  import ui.table.BackupTable;
47  import ui.table.TableSorter;
48  import ui.table.model.BackupTableModel;
49  import base.ConfigurationManager;
50  import base.InternetCafeManager;
51  import base.backup.Backup;
52  
53  @SuppressWarnings("serial") //$NON-NLS-1$
54  public class TabledBackupPanel extends JPanel {
55  
56  	private static final transient Logger logger = Logger
57  			.getLogger(TabledBackupPanel.class.getName());
58  
59  	private JTable backUpHistoryTable;
60  
61  	private JScrollPane backUpHistoryTableScrollPane;
62  
63  	private TableSorter backUpHistoryTableSorter;
64  
65  	private BackupTableModel backUpHistoryTableModel;
66  
67  	private JPanel backUpButtonPanel;
68  
69  	private JButton newBackUpButton;
70  
71  	private JButton restoreBackUpButton;
72  
73  	private JButton loadBackUpButton;
74  
75  	private JButton deleteBackUpButton;
76  
77  	public TabledBackupPanel() {
78  		initialize();
79  	}
80  
81  	protected void initialize() {
82  		TitledBorder titledBorder = new TitledBorder(Messages.getString("panel.backuppanel.backuphistory")); //$NON-NLS-1$
83  		this.setBorder(titledBorder);
84  		this.setLayout(new BorderLayout());
85  		this.add(getBackUpHistoryTableScrollPane(), BorderLayout.CENTER);
86  		this.add(getBackUpButtonPanel(), BorderLayout.EAST);
87  	}
88  
89  	/***
90  	 * @return Returns the backUpHistoryTable.
91  	 */
92  	protected JTable getBackUpHistoryTable() {
93  		if (backUpHistoryTable == null) {
94  			backUpHistoryTable = new BackupTable(getUserTableSorter());
95  			backUpHistoryTable
96  					.setPreferredScrollableViewportSize(new Dimension(500, 70));
97  			// We need to place it here to avoid a ciclyc call...
98  			backUpHistoryTableSorter.setTableHeader(backUpHistoryTable
99  					.getTableHeader());
100 			// Set up tool tips for column headers.
101 			backUpHistoryTable
102 					.getTableHeader()
103 					.setToolTipText(
104 							Messages.getString("tablesorter.header.tooltip")); //$NON-NLS-1$
105 		}
106 		return backUpHistoryTable;
107 	}
108 
109 	/***
110 	 * @return Returns the backUpHistoryTableScrollPane.
111 	 */
112 	protected JScrollPane getBackUpHistoryTableScrollPane() {
113 		if (backUpHistoryTableScrollPane == null) {
114 			backUpHistoryTableScrollPane = new JScrollPane(
115 					getBackUpHistoryTable());
116 		}
117 		return backUpHistoryTableScrollPane;
118 	}
119 
120 	/***
121 	 * @return Returns the backUpHistoryTableSorter.
122 	 */
123 	protected TableSorter getUserTableSorter() {
124 		if (backUpHistoryTableSorter == null) {
125 			backUpHistoryTableSorter = new TableSorter(
126 					getBackUpHistoryTableModel());
127 		}
128 		return backUpHistoryTableSorter;
129 	}
130 
131 	/***
132 	 * @return Returns the backUpHistoryTableModel.
133 	 */
134 	protected BackupTableModel getBackUpHistoryTableModel() {
135 		if (backUpHistoryTableModel == null) {
136 			backUpHistoryTableModel = new BackupTableModel();
137 		}
138 		return backUpHistoryTableModel;
139 	}
140 
141 	/***
142 	 * @return Returns the backUpButtonPanel.
143 	 */
144 	protected JPanel getBackUpButtonPanel() {
145 		if (backUpButtonPanel == null) {
146 			backUpButtonPanel = new JPanel();
147 			backUpButtonPanel.setLayout(new GridLayout(6, 1));
148 			backUpButtonPanel.add(new JLabel("")); //$NON-NLS-1$
149 			backUpButtonPanel.add(getNewBackUpButton());
150 			backUpButtonPanel.add(getLoadBackUpButton());
151 			backUpButtonPanel.add(getRestoreBackUpButton());
152 			backUpButtonPanel.add(getDeleteBackUpButton());
153 			backUpButtonPanel.add(new JLabel("")); //$NON-NLS-1$
154 		}
155 		return backUpButtonPanel;
156 	}
157 
158 	/***
159 	 * @return Returns the deleteBackUpButton.
160 	 */
161 	protected JButton getDeleteBackUpButton() {
162 		if (deleteBackUpButton == null) {
163 			deleteBackUpButton = new JButton(Messages.getString("button.delete")); //$NON-NLS-1$
164 			deleteBackUpButton.setIcon(new ImageIcon(this.getClass()
165 					.getResource("/icon/22x22/actions/edit-delete.png"))); //$NON-NLS-1$
166 
167 			deleteBackUpButton.addActionListener(new ActionListener() {
168 				public void actionPerformed(ActionEvent arg0) {
169 					logger.debug("actionPerformed deleteBackUpButton"); //$NON-NLS-1$
170 					if (getBackUpHistoryTable().getSelectedRow() != -1) {
171 						int backupId = Integer
172 								.parseInt(((TableSorter) getBackUpHistoryTable()
173 										.getModel()).getValueAt(
174 										getBackUpHistoryTable()
175 												.getSelectedRow(),
176 										BackupTableModel.BACKUP_ID_COLUMN)
177 										.toString());
178 						CommandExecutor.getInstance().executeCommand(
179 								new DeleteBackupCommand(backupId), false);
180 					} else
181 						JOptionPane
182 								.showMessageDialog(
183 										null,
184 										Messages.getString("panel.tabledbackuppanel.message1"), //$NON-NLS-1$
185 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
186 				}
187 			});
188 		}
189 		return deleteBackUpButton;
190 	}
191 
192 	/***
193 	 * @return Returns the loadBackUpButton.
194 	 */
195 	protected JButton getLoadBackUpButton() {
196 		if (loadBackUpButton == null) {
197 			loadBackUpButton = new JButton(Messages.getString("button.load")); //$NON-NLS-1$
198 			loadBackUpButton.setIcon(new ImageIcon(this.getClass().getResource(
199 					"/icon/22x22/actions/document-open.png"))); //$NON-NLS-1$
200 
201 			loadBackUpButton.addActionListener(new ActionListener() {
202 				public void actionPerformed(ActionEvent arg0) {
203 					logger.debug("actionPerformed loadBackUpButton"); //$NON-NLS-1$
204 					CommandExecutor.getInstance().executeCommand(
205 							new LoadBackupCommand(), false);
206 				}
207 			});
208 		}
209 		return loadBackUpButton;
210 	}
211 
212 	/***
213 	 * @return Returns the newBackUpButton.
214 	 */
215 	protected JButton getNewBackUpButton() {
216 		if (newBackUpButton == null) {
217 			newBackUpButton = new JButton(Messages.getString("button.new")); //$NON-NLS-1$
218 			newBackUpButton.setIcon(new ImageIcon(this.getClass().getResource(
219 					"/icon/22x22/actions/document-new.png"))); //$NON-NLS-1$
220 
221 			newBackUpButton.addActionListener(new ActionListener() {
222 				public void actionPerformed(ActionEvent arg0) {
223 					logger.debug("actionPerformed newBackUpButton"); //$NON-NLS-1$
224 					CommandExecutor.getInstance().executeCommand(
225 							new ShowNewBackupCommand(), false);
226 				}
227 			});
228 		}
229 		return newBackUpButton;
230 	}
231 
232 	/***
233 	 * @return Returns the restoreBackUpButton.
234 	 */
235 	protected JButton getRestoreBackUpButton() {
236 		if (restoreBackUpButton == null) {
237 			restoreBackUpButton = new JButton(Messages.getString("button.restore")); //$NON-NLS-1$
238 			restoreBackUpButton.setIcon(new ImageIcon(this.getClass()
239 					.getResource("/icon/22x22/actions/view-refresh.png"))); //$NON-NLS-1$
240 
241 			restoreBackUpButton.addActionListener(new ActionListener() {
242 				public void actionPerformed(ActionEvent arg0) {
243 					logger.debug("actionPerformed restoreBackUpButton"); //$NON-NLS-1$
244 					if (getBackUpHistoryTable().getSelectedRow() != -1) {
245 						Object[] options = { Messages.getString("TabledBackupPanel.19"), //$NON-NLS-1$
246 								Messages.getString("TabledBackupPanel.20") }; //$NON-NLS-1$
247 						int result = JOptionPane
248 								.showOptionDialog(
249 										null,
250 										Messages.getString("panel.tabledbackuppanel.message2"), //$NON-NLS-1$
251 										Messages.getString("panel.tabledbackuppanel.backuprestore"), //$NON-NLS-1$
252 										JOptionPane.YES_NO_OPTION,
253 										JOptionPane.QUESTION_MESSAGE, null,
254 										options, options[1]);
255 						if (result == JOptionPane.YES_OPTION) {
256 							int backupId = Integer
257 									.parseInt(((TableSorter) getBackUpHistoryTable()
258 											.getModel()).getValueAt(
259 											getBackUpHistoryTable()
260 													.getSelectedRow(),
261 											BackupTableModel.BACKUP_ID_COLUMN)
262 											.toString());
263 							Backup backup = InternetCafeManager.getInstance()
264 									.getBackupById(backupId);
265 							if (backup != null) {
266 								if (new File(backup.getDbLocationPath())
267 										.exists()) {
268 									ConfigurationManager.getInstance()
269 											.setDataBasePath(
270 													backup.getDbLocationPath());
271 									InternetCafeManager.getInstance().clear();
272 									InternetCafeManager.getInstance()
273 											.retrieve();
274 								} else
275 									JOptionPane
276 											.showMessageDialog(
277 													null,
278 													Messages.getString("panel.tabledbackuppanel.message3"), //$NON-NLS-1$
279 													Messages.getString("common.error"), //$NON-NLS-1$
280 													JOptionPane.ERROR_MESSAGE);
281 							} else
282 								logger.error(Messages.getString("BIG PROBLEM HERE!")); //$NON-NLS-1$
283 						}
284 					} else
285 						JOptionPane
286 								.showMessageDialog(
287 										null,
288 										Messages.getString("panel.tabledbackuppanel.message1"), //$NON-NLS-1$
289 										Messages.getString("common.warning"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
290 				}
291 			});
292 
293 		}
294 		return restoreBackUpButton;
295 	}
296 
297 }