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 base.jdbs.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.JButton;
29 import javax.swing.JOptionPane;
30 import javax.swing.JPanel;
31 import javax.swing.JScrollPane;
32 import javax.swing.JTable;
33 import javax.swing.border.TitledBorder;
34 import javax.swing.table.TableModel;
35
36 import org.apache.log4j.Logger;
37
38 import ui.command.CommandExecutor;
39 import ui.table.TableSorter;
40 import ui.table.model.BackupTableModel;
41 import base.jdbs.ui.BackupDescriptorTableModel;
42 import base.jdbs.ui.action.IndexRepositoryActionListener;
43 import base.jdbs.ui.command.DeleteExpiredBackupCommand;
44 import base.jdbs.ui.command.NewBackupCommand;
45 import base.jdbs.ui.command.RestoreBackupArtifactCommand;
46
47 @SuppressWarnings("serial")
48 public class TabledRepositoryPanel extends JPanel {
49
50 private static final transient Logger logger = Logger.getLogger(TabledRepositoryPanel.class.getName());
51
52 /***The table where the local backups artifacts must be shown.**/
53 private JTable backupTable;
54 /***This is needed to scroll the backups artifacts displayed in the backupTable.**/
55 private JScrollPane backupTableScrollPane;
56 /***This is needed to sort the content of the backupTable.**/
57 private TableSorter backupTableSorter;
58 /***This is the model for the backupTable.**/
59 private TableModel backupTableModel;
60 /***The button panel is needed to offer some functions to the user.**/
61 private JPanel buttonPanel;
62 /***This is the panel where some statistics about the Repository usage must be shown.**/
63 private JPanel statsPanel;
64
65 private JButton indexRepositoryButton;
66 private JButton restoreBackupButton;
67 private JButton newBackupButton;
68 private JButton deleteExpiredBackupButton;
69
70
71 /***This is the default constructor.**/
72 public TabledRepositoryPanel(){
73 initialize();
74 }
75
76 /***
77 * This method initializes the panel configuring and setting its components.
78 */
79 protected void initialize(){
80 this.setBorder(new TitledBorder("Repository Panel"));
81 this.setLayout(new BorderLayout());
82 this.add(getStatsPanel(),BorderLayout.NORTH);
83 this.add(getBackupTableScrollPane(), BorderLayout.CENTER);
84 this.add(getButtonPanel(), BorderLayout.SOUTH);
85 }
86
87 /***
88 * @return Returns the backupTable.
89 */
90 protected JTable getBackupTable() {
91 if (this.backupTable == null) {
92 this.backupTable = new JTable(getBackupTableSorter());
93 this.backupTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
94
95 this.backupTableSorter.setTableHeader(backupTable.getTableHeader());
96
97 this.backupTable.getTableHeader().setToolTipText("Click to specify sorting; Control-Click to specify secondary sorting.");
98 }
99 return backupTable;
100 }
101
102 /***
103 * @return Returns the backupTableScrollPane.
104 */
105 protected JScrollPane getBackupTableScrollPane() {
106 if (this.backupTableScrollPane == null) {
107 this.backupTableScrollPane = new JScrollPane(getBackupTable());
108 }
109 return backupTableScrollPane;
110 }
111
112 /***
113 * @return Returns the backupTableSorter.
114 */
115 protected TableSorter getBackupTableSorter() {
116 if (this.backupTableSorter == null) {
117 this.backupTableSorter = new TableSorter(getBackupTableModel());
118 }
119 return backupTableSorter;
120 }
121
122 /***
123 * @return Returns the backupTableModel.
124 */
125 protected TableModel getBackupTableModel() {
126 if (this.backupTableModel == null) {
127 this.backupTableModel = new BackupDescriptorTableModel();
128 }
129 return backupTableModel;
130 }
131
132 /***
133 * @return the buttonPanel
134 */
135 protected JPanel getButtonPanel() {
136 if(this.buttonPanel == null){
137 this.buttonPanel = new JPanel();
138 this.buttonPanel.setLayout(new GridLayout(1,4));
139 this.buttonPanel.add(this.getIndexRepositoryButton());
140 this.buttonPanel.add(this.getDeleteExpiredBackupButton());
141 this.buttonPanel.add(this.getRestoreBackupButton());
142 this.buttonPanel.add(this.getNewBackupButton());
143
144 }
145 return buttonPanel;
146 }
147
148 /***
149 * @return the indexRepositoryButton
150 */
151 protected JButton getIndexRepositoryButton() {
152 if(this.indexRepositoryButton == null){
153 this.indexRepositoryButton = new JButton("Index Repository");
154 this.indexRepositoryButton.addActionListener(new IndexRepositoryActionListener());
155
156 }
157 return indexRepositoryButton;
158 }
159
160 /***
161 * @return Returns the restoreBackupButton.
162 */
163 protected JButton getRestoreBackupButton() {
164 if(this.restoreBackupButton == null){
165 this.restoreBackupButton = new JButton("Restore backup");
166 this.restoreBackupButton.addActionListener(new ActionListener() {
167 public void actionPerformed(ActionEvent arg0) {
168 logger.debug("actionPerformed restoreBackupButton");
169 if (getBackupTable().getSelectedRow() != -1) {
170 String guId = ((TableSorter) getBackupTable().getModel()).getValueAt(getBackupTable().getSelectedRow(),BackupTableModel.BACKUP_ID_COLUMN).toString();
171 CommandExecutor.getInstance().executeCommand(new RestoreBackupArtifactCommand(guId), false);
172 } else
173 JOptionPane
174 .showMessageDialog(
175 null,
176 "You must select a Backup before to try this action!",
177 "Warning", JOptionPane.WARNING_MESSAGE);
178 }
179 });
180 }
181 return restoreBackupButton;
182 }
183
184 /***
185 * @return Returns the statsPanel.
186 */
187 protected JPanel getStatsPanel() {
188 if(this.statsPanel == null){
189 this.statsPanel = new RepositoryStatisticPanel();
190 }
191 return statsPanel;
192 }
193
194 /***
195 * @return Returns the newBackupButton.
196 */
197 protected JButton getNewBackupButton() {
198 if(this.newBackupButton == null){
199 this.newBackupButton = new JButton("New Backup");
200 this.newBackupButton.addActionListener(new ActionListener(){
201
202 public void actionPerformed(ActionEvent arg0) {
203 CommandExecutor.getInstance().executeCommand(new NewBackupCommand(),false);
204 }});
205 }
206 return newBackupButton;
207 }
208
209 /***
210 * @return Returns the deleteExpiredBackupButton.
211 */
212 protected JButton getDeleteExpiredBackupButton() {
213 if(this.deleteExpiredBackupButton == null){
214 this.deleteExpiredBackupButton = new JButton("Delete Expired");
215 this.deleteExpiredBackupButton.addActionListener(new ActionListener(){
216
217 public void actionPerformed(ActionEvent arg0) {
218 CommandExecutor.getInstance().executeCommand(new DeleteExpiredBackupCommand(),false);
219 }});
220 }
221 return deleteExpiredBackupButton;
222 }
223
224 }