source: 3thparty/jupload/src/main/java/wjhk/jupload2/gui/filepanel/FilePanelDataModel2.java @ 3951

Revision 3951, 12.6 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1709 - Adicao de codigo fonte java do componente jupload

Line 
1//
2// $Id: FilePanelDataModel2.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: 2006-04-21
8// Creator: etienne_sf
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
21package wjhk.jupload2.gui.filepanel;
22
23import java.io.File;
24import java.util.Collections;
25import java.util.Date;
26import java.util.Iterator;
27import java.util.Vector;
28
29import javax.swing.table.AbstractTableModel;
30
31import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles;
32import wjhk.jupload2.filedata.FileData;
33import wjhk.jupload2.policies.UploadPolicy;
34
35/**
36 * This class replaces FilePanelDataModel. The data for each row is now
37 * contained in an instance of FileData (or one of its subclasses, like
38 * {@link wjhk.jupload2.filedata.PictureFileData}). This allow easy add of new
39 * functionalites, during upload, by adding attributes or methods to these
40 * classes, or create new ones. <BR>
41 * Some ides of improvements :
42 * <UL>
43 * <LI>Compression of picture before Upload (see
44 * {@link wjhk.jupload2.filedata.PictureFileData})
45 * <LI>Could be XML validation before sending to the server
46 * <LI>Up to your imagination...
47 * </UL>
48 */
49class FilePanelDataModel2 extends AbstractTableModel {
50
51    /** A generated serialVersionUID, to avoid warning during compilation */
52    private static final long serialVersionUID = 1473262424494858913L;
53
54    /**
55     * The default colum indices of the columns, as displayed by the applet.
56     * Index of the column "Filename"
57     */
58    public final static int COLINDEX_NAME = 0;
59
60    /**
61     * The default colum indices of the columns, as displayed by the applet.
62     * Index of the column "Filename"
63     */
64    public final static int COLINDEX_SIZE = 1;
65
66    /**
67     * The default colum indices of the columns, as displayed by the applet.
68     * Index of the column "Filesize"
69     */
70    public final static int COLINDEX_DIRECTORY = 2;
71
72    /**
73     * The default colum indices of the columns, as displayed by the applet.
74     * Index of the column "Last modified"
75     */
76    public final static int COLINDEX_MODIFIED = 3;
77
78    /**
79     * The uploadPolicy contains all current parameter, including the
80     * FileDataParam
81     */
82    private UploadPolicy uploadPolicy = null;
83
84    /**
85     * The column names, as displayed on the applet. They are not real final
86     * values, as they are translated: we need to have an uploadPolicy for this
87     * translation, and the uploadPolicy is 'given' to the constructor.
88     */
89    private String COL_NAME = null;
90
91    private String COL_SIZE = null;
92
93    private String COL_DIRECTORY = null;
94
95    private String COL_MODIFIED = null;
96
97    protected String[] columnNames = null;
98
99    /**
100     * This array indicates, for each column, the percentage of the available
101     * width it should use. It's initialized in the constructor of this class.
102     *
103     * @see #getColumnSize(int)
104     */
105    protected int[] columnSizePercentage = null;
106
107    protected Class<?> columnClasses[] = null;
108
109    /**
110     * This Vector contains all FileData.
111     */
112    private Vector<FileData> rows = new Vector<FileData>();
113
114    /**
115     * @param uploadPolicy
116     */
117    public FilePanelDataModel2(UploadPolicy uploadPolicy) {
118        // Property initialization is done ... for each property. Nothing to do
119        // here.
120        super();
121        //
122        this.uploadPolicy = uploadPolicy;
123
124        // Initialization for column name, type and size.
125        this.COL_NAME = uploadPolicy.getLocalizedString("colName");
126        this.COL_SIZE = uploadPolicy.getLocalizedString("colSize");
127        this.COL_DIRECTORY = uploadPolicy.getLocalizedString("colDirectory");
128        this.COL_MODIFIED = uploadPolicy.getLocalizedString("colModified");
129
130        this.columnNames = new String[] {
131                this.COL_NAME, this.COL_SIZE, this.COL_DIRECTORY,
132                this.COL_MODIFIED
133        };
134
135        // Initial size (before percentage) was: 150, 75, 199, 130, 75
136        //
137        this.columnSizePercentage = new int[] {
138                30, 13, 37, 20
139        };
140        // Check that the sum of the previous values is actually 100%
141        int total = 0;
142        for (int i = 0; i < this.columnSizePercentage.length; i += 1) {
143            total += this.columnSizePercentage[i];
144        }
145        if (total != 100) {
146            throw new java.lang.AssertionError("Total sum of '"
147                    + this.getClass().getName()
148                    + ".columnSizePercentage' should be 100% (but was " + total
149                    + ")");
150        }
151
152        this.columnClasses = new Class[] {
153                String.class, Long.class, String.class, Date.class,
154                Boolean.class
155        };
156    }
157
158    /**
159     * Does this table contain this file ?
160     *
161     * @param file : the file that could be contained...
162     * @return true if the table contains this file.
163     */
164    public boolean contains(File file) {
165        boolean found = false;
166
167        synchronized (this.rows) {
168            Iterator<FileData> i = this.rows.iterator();
169            while (i.hasNext()) {
170                if (file.equals(i.next().getFile())) {
171                    found = true;
172                    break;
173                }
174            }
175        }// synchronized
176
177        return found;
178    }
179
180    /**
181     * Add a file to the panel (at the end of the list)
182     *
183     * @param file
184     * @param root
185     * @throws JUploadExceptionStopAddingFiles
186     */
187    public void addFile(File file, File root)
188            throws JUploadExceptionStopAddingFiles {
189        synchronized (this.rows) {
190            if (contains(file)) {
191                this.uploadPolicy.displayWarn("File " + file.getName()
192                        + " already exists");
193            } else {
194                // We first call the upload policy, to get :
195                // - The correct fileData instance (for instance the
196                // PictureUploadPolicy returns a PictureFileData)
197                // - The reference to this newly FileData, or null if an error
198                // occurs (for instance: invalid file content, according to the
199                // current upload policy, or non allowed file extension).
200                FileData df = this.uploadPolicy.createFileData(file, root);
201                if (df != null) {
202                    // The file is Ok, let's add it.
203                    this.rows.add(df);
204                    fireTableDataChanged();
205                }
206            }
207        }
208    }
209
210    /**
211     * Ask for the file contained at specified row number.
212     *
213     * @param row The row number
214     * @return The return instance of File.
215     */
216    public File getFileAt(int row) {
217        synchronized (this.rows) {
218            FileData fileData = this.rows.get(row);
219            return (fileData == null) ? null : fileData.getFile();
220        }
221    }
222
223    /**
224     * Ask for the file contained at specified row number.
225     *
226     * @param row The row number
227     * @return The return instance of File.
228     */
229    public FileData getFileDataAt(int row) {
230        int size = -1;
231        if (row >= 0) {
232            try {
233                synchronized (this.rows) {
234                    size = this.rows.size();
235                    return (row < size) ? this.rows.get(row) : null;
236                }
237            } catch (ArrayIndexOutOfBoundsException e) {
238                // Nothing to do. It seems that it can occurs when upload is
239                // very
240                // fast (for instance: small files to localhost).
241                this.uploadPolicy.displayWarn(e.getClass().getName()
242                        + " in FilePanelDataModel2.getFileDataAt(" + row + ")");
243            }
244        }
245        return null;
246    }
247
248    /**
249     * Remove a specified row.
250     *
251     * @param row The row to remove.
252     */
253    public synchronized void removeRow(int row) {
254        synchronized (this.rows) {
255            this.rows.remove(row);
256            fireTableDataChanged();
257        }
258    }
259
260    /**
261     * Removes fileData from the current list. There should be only one.
262     *
263     * @param fileData
264     */
265    public synchronized void removeRow(FileData fileData) {
266        boolean found = false;
267
268        synchronized (this.rows) {
269            Iterator<FileData> i = this.rows.iterator();
270            FileData item;
271            while (i.hasNext()) {
272                item = i.next();
273                if (item.getFile().equals(fileData.getFile())) {
274                    this.rows.removeElement(item);
275                    found = true;
276                    break;
277                }
278            }
279        }// synchronized(rows)
280
281        if (found) {
282            fireTableDataChanged();
283        }
284    }
285
286    /** @see javax.swing.table.TableModel#getColumnCount() */
287    public int getColumnCount() {
288        return this.columnNames.length;
289    }
290
291    /** @see javax.swing.table.TableModel#getRowCount() */
292    public int getRowCount() {
293        synchronized (this.rows) {
294            return this.rows.size();
295        }
296    }
297
298    /**
299     * Always return false here : no editable cell.
300     *
301     * @see javax.swing.table.TableModel#isCellEditable(int, int)
302     */
303    @Override
304    public boolean isCellEditable(int arg0, int arg1) {
305        // No editable columns.
306        return false;
307    }
308
309    /**
310     * Sort the rows, according to one column.
311     *
312     * @param col The index of the column to sort
313     * @param ascending true if ascending, false if descending.
314     */
315    public void sortColumn(int col, boolean ascending) {
316        synchronized (this.rows) {
317            Collections.sort(this.rows, new ColumnComparator(col, ascending));
318        }
319        fireTableDataChanged();
320    }
321
322    /**
323     * Return true if this column can be sorted.
324     *
325     * @param col The index of the column which can sortable or not.
326     * @return true if the column can be sorted. false otherwise.
327     */
328    public boolean isSortable(int col) {
329        return (Boolean.class != getColumnClass(col));
330    }
331
332    /**
333     * @see javax.swing.table.TableModel#getColumnClass(int)
334     */
335    @SuppressWarnings("unchecked")
336    @Override
337    public Class getColumnClass(int arg0) {
338        return this.columnClasses[arg0];
339    }
340
341    /**
342     * @see javax.swing.table.TableModel#getValueAt(int, int)
343     */
344    public Object getValueAt(int row, int col) {
345        FileData fileData = getFileDataAt(row);
346        if (fileData != null) {
347            String colName = getColumnName(col);
348            // Don't know if it will be useful, but the switch below allows the
349            // column to be in any order.
350            if (colName.equals(this.COL_NAME)) {
351                return fileData.getFileName();
352            } else if (colName.equals(this.COL_SIZE)) {
353                return Long.valueOf(fileData.getFileLength());
354            } else if (colName.equals(this.COL_DIRECTORY)) {
355                return fileData.getDirectory();
356            } else if (colName.equals(this.COL_MODIFIED)) {
357                return fileData.getLastModified();
358            } else {
359                this.uploadPolicy.displayErr("Unknown column in "
360                        + this.getClass().getName() + ": " + colName);
361            }
362        }
363        return null;
364    }
365
366    /**
367     * This method doesn't do anything : no changeable values.
368     *
369     * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
370     */
371    @Override
372    public void setValueAt(Object arg0, int arg1, int arg2) {
373        this.uploadPolicy.displayWarn(this.getClass().getName()
374                + ".setValueAt: no action");
375    }
376
377    /**
378     * @see javax.swing.table.TableModel#getColumnName(int)
379     */
380    @Override
381    public String getColumnName(int arg0) {
382        return this.columnNames[arg0];
383    }
384
385    /**
386     * Retrieves the default colum percentage size of a column, that is: its
387     * percentage of the available width.
388     *
389     * @param col The index of the column to query.
390     * @return the default size of the requested column.
391     */
392    public int getColumnSizePercentage(int col) {
393        return this.columnSizePercentage[col];
394    }
395}
Note: See TracBrowser for help on using the repository browser.