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 base.jdbs.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 import java.util.Vector;
30
31 import javax.swing.ImageIcon;
32 import javax.swing.JButton;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JScrollPane;
36 import javax.swing.JTable;
37 import javax.swing.border.TitledBorder;
38 import javax.swing.table.TableModel;
39
40 import org.apache.log4j.Logger;
41
42 import ui.table.TableSorter;
43 import base.jdbs.Backup;
44 import base.jdbs.ui.FileTableModel;
45 import base.util.FileUtil;
46
47 /***
48 * @author skunk
49 *
50 */
51 @SuppressWarnings("serial")
52 public class TabledFilePanel extends JPanel {
53
54 private static final transient Logger logger = Logger
55 .getLogger(TabledFilePanel.class.getName());
56
57 private JTable fileTable;
58
59 private JScrollPane fileTableScrollPane;
60
61 private TableSorter fileTableSorter;
62
63 private TableModel fileTableModel;
64
65 private JPanel statPanel;
66
67 private JPanel buttonPanel;
68
69 private JButton addFileButton;
70
71 private JButton removeFileButton;
72
73 private final Backup backup;
74
75 public TabledFilePanel(Backup backup) {
76 this.backup = backup;
77 initialize();
78 }
79
80 protected void initialize() {
81 this.setBorder(new TitledBorder("Backup Files"));
82 this.setLayout(new BorderLayout());
83 this.add(this.getFileTableScrollPane(), BorderLayout.CENTER);
84 this.add(this.getButtonPanel(), BorderLayout.EAST);
85 this.add(this.getStatPanel(), BorderLayout.SOUTH);
86 }
87
88 /***
89 * @return Returns the fileTable.
90 */
91 protected JTable getFileTable() {
92 if (fileTable == null) {
93 fileTable = new JTable(getFileTableSorter());
94 fileTable
95 .setPreferredScrollableViewportSize(new Dimension(500, 70));
96
97 fileTableSorter.setTableHeader(fileTable.getTableHeader());
98
99 fileTable
100 .getTableHeader()
101 .setToolTipText(
102 "Click to specify sorting; Control-Click to specify secondary sorting.");
103 }
104 return fileTable;
105 }
106
107 /***
108 * @return Returns the fileTableScrollPane.
109 */
110 protected JScrollPane getFileTableScrollPane() {
111 if (fileTableScrollPane == null) {
112 fileTableScrollPane = new JScrollPane(getFileTable());
113 }
114 return fileTableScrollPane;
115 }
116
117 /***
118 * @return Returns the fileTableSorter.
119 */
120 protected TableSorter getFileTableSorter() {
121 if (fileTableSorter == null) {
122 fileTableSorter = new TableSorter(getFileTableModel());
123 }
124 return fileTableSorter;
125 }
126
127 /***
128 * @return Returns the fileTableModel.
129 */
130 protected TableModel getFileTableModel() {
131 if (fileTableModel == null) {
132 fileTableModel = new FileTableModel(backup);
133 }
134 return fileTableModel;
135 }
136
137 /***
138 * @return Returns the buttonPanel.
139 */
140 protected JPanel getButtonPanel() {
141 if (this.buttonPanel == null) {
142 this.buttonPanel = new JPanel();
143 this.buttonPanel.setLayout(new GridLayout(4, 1));
144 this.buttonPanel.add(new JLabel());
145 this.buttonPanel.add(this.getAddFileButton());
146 this.buttonPanel.add(this.getRemoveFileButton());
147 this.buttonPanel.add(new JLabel());
148 }
149 return buttonPanel;
150 }
151
152 /***
153 * @return Returns the addFileButton.
154 */
155 protected JButton getAddFileButton() {
156 if (this.addFileButton == null) {
157 this.addFileButton = new JButton();
158 this.addFileButton
159 .setToolTipText("Adds one or more files to the the backup...");
160 this.addFileButton.setIcon(new ImageIcon(this.getClass()
161 .getResource("/icon/16x16/actions/list-add.png")));
162
163 this.addFileButton.addActionListener(new ActionListener() {
164
165 public void actionPerformed(ActionEvent arg0) {
166 Vector<File> fileSelection = new Vector<File>();
167 javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
168 chooser
169 .setDialogTitle("Select the files and the folders that must be backupped...");
170 chooser
171 .setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
172 chooser.setApproveButtonText("Select");
173 chooser.setMultiSelectionEnabled(true);
174
175 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION) {
176 File[] tempFileSelection = chooser.getSelectedFiles();
177 for (int i = 0; i < tempFileSelection.length; i++) {
178 File[] allFileAndFolder = FileUtil
179 .allFileContent(tempFileSelection[i]);
180 for (int k = 0; k < allFileAndFolder.length; k++)
181 fileSelection.add(allFileAndFolder[k]);
182 }
183 }
184 backup.addAllFile(fileSelection.toArray(new File[0]));
185 }
186 });
187 }
188 return addFileButton;
189 }
190
191 /***
192 * @return Returns the removeFileButton.
193 */
194 protected JButton getRemoveFileButton() {
195 if (this.removeFileButton == null) {
196 this.removeFileButton = new JButton();
197 this.removeFileButton
198 .setToolTipText("Removes one or more files from the the backup...");
199 this.removeFileButton.setIcon(new ImageIcon(this.getClass()
200 .getResource("/icon/16x16/actions/list-remove.png")));
201
202 }
203 return removeFileButton;
204 }
205
206 /***
207 * @return Returns the statPanel.
208 */
209 protected JPanel getStatPanel() {
210 if (this.statPanel == null) {
211 this.statPanel = new FileStatisticPanel(backup);
212 }
213 return statPanel;
214 }
215 }