1 | // |
---|
2 | // $Id: FilePanelTableImp.java 1399 2010-09-08 20:02:33Z etienne_sf $ |
---|
3 | // |
---|
4 | // jupload - A file upload applet. |
---|
5 | // Copyright 2007 The JUpload Team |
---|
6 | // |
---|
7 | // Created: ? |
---|
8 | // Creator: William JinHua Kwong |
---|
9 | // Last modified: $Date: 2010-09-08 17:02:33 -0300 (Qua, 08 Set 2010) $ |
---|
10 | // |
---|
11 | // This program is free software; you can redistribute it and/or modify it under |
---|
12 | // the terms of the GNU General Public License as published by the Free Software |
---|
13 | // Foundation; either version 2 of the License, or (at your option) any later |
---|
14 | // version. This program is distributed in the hope that it will be useful, but |
---|
15 | // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
16 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
17 | // details. You should have received a copy of the GNU General Public License |
---|
18 | // along with this program; if not, write to the Free Software Foundation, Inc., |
---|
19 | // 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
20 | package wjhk.jupload2.gui.filepanel; |
---|
21 | |
---|
22 | import java.awt.BorderLayout; |
---|
23 | import java.awt.Color; |
---|
24 | import java.awt.Component; |
---|
25 | import java.awt.Font; |
---|
26 | import java.awt.Point; |
---|
27 | import java.awt.event.ComponentEvent; |
---|
28 | import java.awt.event.ComponentListener; |
---|
29 | import java.io.File; |
---|
30 | |
---|
31 | import javax.swing.JPanel; |
---|
32 | import javax.swing.JScrollPane; |
---|
33 | import javax.swing.table.TableColumnModel; |
---|
34 | |
---|
35 | import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles; |
---|
36 | import wjhk.jupload2.filedata.FileData; |
---|
37 | import wjhk.jupload2.gui.JUploadPanel; |
---|
38 | import wjhk.jupload2.policies.UploadPolicy; |
---|
39 | |
---|
40 | /** |
---|
41 | * Implementation of the FilePanel : it creates the |
---|
42 | * {@link wjhk.jupload2.gui.filepanel.FilePanelJTable}, and handles the |
---|
43 | * necessary functionalities. |
---|
44 | * |
---|
45 | * @author William JinHua Kwong |
---|
46 | * @version $Revision: 1399 $ |
---|
47 | */ |
---|
48 | public class FilePanelTableImp extends JPanel implements FilePanel, |
---|
49 | ComponentListener { |
---|
50 | |
---|
51 | /** A generated serialVersionUID, to avoid warning during compilation */ |
---|
52 | private static final long serialVersionUID = -8273990467324350526L; |
---|
53 | private FilePanelJTable jtable; |
---|
54 | private FilePanelDataModel2 model; |
---|
55 | /** |
---|
56 | * The current policy, always useful. |
---|
57 | */ |
---|
58 | private UploadPolicy uploadPolicy = null; |
---|
59 | /** |
---|
60 | * The main panel of the applet. |
---|
61 | */ |
---|
62 | private JUploadPanel juploadPanel = null; |
---|
63 | |
---|
64 | ; |
---|
65 | |
---|
66 | /** |
---|
67 | * The view, which displays the view. |
---|
68 | */ |
---|
69 | private JScrollPane scrollPane = null; |
---|
70 | |
---|
71 | /** |
---|
72 | * Creates a new instance. |
---|
73 | * |
---|
74 | * @param juploadPanel The upload panel (parent). |
---|
75 | * @param uploadPolicy The upload policy to apply. |
---|
76 | */ |
---|
77 | public FilePanelTableImp(JUploadPanel juploadPanel, |
---|
78 | UploadPolicy uploadPolicy) { |
---|
79 | this.juploadPanel = juploadPanel; |
---|
80 | this.uploadPolicy = uploadPolicy; |
---|
81 | |
---|
82 | setLayout(new BorderLayout()); |
---|
83 | addMouseListener(juploadPanel.getMouseListener()); |
---|
84 | setTransferHandler(juploadPanel.getTransferHandler()); |
---|
85 | |
---|
86 | this.jtable = new FilePanelJTable(juploadPanel, uploadPolicy); |
---|
87 | |
---|
88 | this.model = new FilePanelDataModel2(uploadPolicy); |
---|
89 | this.jtable.setModel(this.model); |
---|
90 | |
---|
91 | this.scrollPane = new JScrollPane(this.jtable); |
---|
92 | add(this.scrollPane, BorderLayout.CENTER); |
---|
93 | this.scrollPane.addMouseListener(juploadPanel.getMouseListener()); |
---|
94 | |
---|
95 | // We must resize columns, when the size of the view changes. |
---|
96 | this.scrollPane.getViewport().addComponentListener(this); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * @see wjhk.jupload2.gui.filepanel.FilePanel#addFiles(java.io.File[],java.io.File) |
---|
101 | */ |
---|
102 | public final void addFiles(File[] f, File root) { |
---|
103 | if (null == f) { |
---|
104 | throw new java.lang.IllegalArgumentException( |
---|
105 | "FilePanelTableImpl: filesToUpload may not be null)"); |
---|
106 | } else { |
---|
107 | try { |
---|
108 | for (int i = 0; i < f.length; i++) { |
---|
109 | addDirectoryFiles(f[i], root); |
---|
110 | } |
---|
111 | } catch (JUploadExceptionStopAddingFiles e) { |
---|
112 | // The user want to stop here. Nothing else to do. |
---|
113 | this.uploadPolicy.displayWarn(getClass().getName() |
---|
114 | + ".addFiles() [" + e.getClass().getName() + "]: " |
---|
115 | + e.getMessage()); |
---|
116 | } |
---|
117 | } |
---|
118 | this.juploadPanel.updateButtonState(); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * This method allows a recursive calls through the file hierarchy. |
---|
123 | * |
---|
124 | * @param f The directory that contains the files to add |
---|
125 | * @param root The common root of all the added files. |
---|
126 | * @throws JUploadExceptionStopAddingFiles |
---|
127 | */ |
---|
128 | private final void addDirectoryFiles(File f, File root) |
---|
129 | throws JUploadExceptionStopAddingFiles { |
---|
130 | if (!f.isDirectory()) { |
---|
131 | addFileOnly(f, root); |
---|
132 | } else { |
---|
133 | File[] dirFiles = f.listFiles(); |
---|
134 | for (int i = 0; i < dirFiles.length; i++) { |
---|
135 | if (dirFiles[i].isDirectory()) { |
---|
136 | addDirectoryFiles(dirFiles[i], root); |
---|
137 | } else { |
---|
138 | addFileOnly(dirFiles[i], root); |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Adds a single file into the file list. |
---|
146 | * |
---|
147 | * @param f The file to add. |
---|
148 | * @param root The common root of all the added files. |
---|
149 | * @throws JUploadExceptionStopAddingFiles |
---|
150 | */ |
---|
151 | private final void addFileOnly(File f, File root) |
---|
152 | throws JUploadExceptionStopAddingFiles { |
---|
153 | // Make sure we don't select the same file twice. |
---|
154 | if (!this.model.contains(f)) { |
---|
155 | this.model.addFile(f, root); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * @see wjhk.jupload2.gui.filepanel.FilePanel#getFiles() |
---|
161 | */ |
---|
162 | public final FileData[] getFiles() { |
---|
163 | FileData[] files = new FileData[getFilesLength()]; |
---|
164 | for (int i = 0; i < files.length; i++) { |
---|
165 | files[i] = this.model.getFileDataAt(i); |
---|
166 | } |
---|
167 | return files; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * @see wjhk.jupload2.gui.filepanel.FilePanel#getFilesLength() |
---|
172 | */ |
---|
173 | public final int getFilesLength() { |
---|
174 | return this.jtable.getRowCount(); |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * @see wjhk.jupload2.gui.filepanel.FilePanel#removeSelected() |
---|
179 | */ |
---|
180 | public final void removeSelected() { |
---|
181 | int[] rows = this.jtable.getSelectedRows(); |
---|
182 | for (int i = rows.length - 1; 0 <= i; i--) { |
---|
183 | this.model.removeRow(rows[i]); |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * @see java.awt.Container#removeAll() |
---|
189 | */ |
---|
190 | @Override |
---|
191 | public final void removeAll() { |
---|
192 | for (int i = getFilesLength() - 1; 0 <= i; i--) { |
---|
193 | this.model.removeRow(i); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Removes all occurences of a file from the list. Each file should only |
---|
199 | * appear once here, but nobodody knows ! |
---|
200 | * |
---|
201 | * @param fileData The file to remove |
---|
202 | */ |
---|
203 | public final void remove(FileData fileData) { |
---|
204 | this.model.removeRow(fileData); |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Clear the current selection in the JTable. |
---|
209 | */ |
---|
210 | public final void clearSelection() { |
---|
211 | this.jtable.clearSelection(); |
---|
212 | } |
---|
213 | |
---|
214 | /** @see wjhk.jupload2.gui.filepanel.FilePanel#focusTable() */ |
---|
215 | public final void focusTable() { |
---|
216 | if (0 < this.jtable.getRowCount()) { |
---|
217 | this.jtable.requestFocus(); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /** @see wjhk.jupload2.gui.filepanel.FilePanel#getFileDataAt(Point) */ |
---|
222 | public FileData getFileDataAt(Point point) { |
---|
223 | int row = this.jtable.rowAtPoint(point); |
---|
224 | return this.model.getFileDataAt(row); |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * Return the component on which drop event can occur. Used by |
---|
229 | * {@link JUploadPanel}, when initializing the DropTarget. |
---|
230 | * |
---|
231 | * @return Component on which the drop event can occur. |
---|
232 | */ |
---|
233 | public Component getDropComponent() { |
---|
234 | return this; |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * Catches the <I>hidden</I> event on the JViewport. {@inheritDoc} |
---|
239 | */ |
---|
240 | public void componentHidden(ComponentEvent arg0) { |
---|
241 | // We don't care... |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * Catches the <I>moved</I> event on the JViewport. {@inheritDoc} |
---|
246 | */ |
---|
247 | public void componentMoved(ComponentEvent arg0) { |
---|
248 | // We don't care... |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * When the size of the file list (actually the JViewport) changes, we adapt |
---|
253 | * the size if the columns. {@inheritDoc} |
---|
254 | */ |
---|
255 | public void componentResized(ComponentEvent arg0) { |
---|
256 | // Is the width set? |
---|
257 | if (getWidth() > 0) { |
---|
258 | TableColumnModel colModel = this.jtable.getColumnModel(); |
---|
259 | for (int i = 0; i < this.model.getColumnCount(); i++) { |
---|
260 | colModel |
---|
261 | .getColumn(i) |
---|
262 | .setPreferredWidth( |
---|
263 | (this.model.getColumnSizePercentage(i) * this.scrollPane |
---|
264 | .getViewport().getWidth()) / 100); |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * Catches the <I>shown</I> event on the JViewport. {@inheritDoc} |
---|
271 | */ |
---|
272 | public void componentShown(ComponentEvent arg0) { |
---|
273 | // We don't care... |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * Set color of files list grid border. |
---|
278 | * @param color awt Color |
---|
279 | */ |
---|
280 | public void setGridBorderColor(Color color) { |
---|
281 | this.jtable.setGridColor(color); |
---|
282 | } |
---|
283 | |
---|
284 | /** |
---|
285 | * Set back color of table header |
---|
286 | * @param color awt Color |
---|
287 | */ |
---|
288 | public void setTableHeaderBackColor(Color color) { |
---|
289 | this.jtable.getTableHeader().setBackground(color); |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * Set table header text font |
---|
294 | * @param color awt Color |
---|
295 | */ |
---|
296 | public void setTableHeaderFont(Font font) { |
---|
297 | this.jtable.getTableHeader().setFont(font); |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * Set text color of table header |
---|
302 | * @param color awt Color |
---|
303 | */ |
---|
304 | public void setTableHeaderTextColor(Color color) { |
---|
305 | this.jtable.getTableHeader().setForeground(color); |
---|
306 | } |
---|
307 | } |
---|