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 base.jdbs.ui;
22  
23  import java.util.Observable;
24  import java.util.Observer;
25  
26  import javax.swing.table.AbstractTableModel;
27  
28  import org.apache.log4j.Logger;
29  
30  import base.jdbs.Backup;
31  
32  @SuppressWarnings("serial")
33  public class FileTableModel extends AbstractTableModel implements Observer {
34  	
35  	private static final transient Logger logger = Logger.getLogger(AbstractTableModel.class.getName());
36  
37  	private String[] columnNames = {"Absolute Path","File Name","Type","Size (Kb)"};
38  
39  	public static final int FILE_ABSOLUTE_PATH_COLUMN = 0;
40  
41  	public static final int FILE_NAME_COLUMN = 1;
42  
43  	public static final int FILE_TYPE_COLUMN = 2;
44  
45  	public static final int FILE_SIZE_COLUMN = 3;
46  
47  	private final Backup backup;
48  	
49  
50  	public FileTableModel(Backup backup) {
51  		this.backup = backup;
52  		this.backup.addObserver(this);
53  	}
54  
55  	public int getColumnCount() {
56  		return columnNames.length;
57  	}
58  
59  	public int getRowCount() {
60  		return backup.getFileDescriptor().length;
61  	}
62  
63  	public String getColumnName(int column) {
64  		return columnNames[column];
65  	}
66  
67  	public Object getValueAt(int row, int column) {
68  		if (row < 0 || column < 0)
69  			return null;
70  		Object result = null;
71  		switch (column) {
72  		case FILE_ABSOLUTE_PATH_COLUMN:
73  			result = backup.getFileDescriptor()[row].getFile().getAbsolutePath();
74  			break;
75  		case FILE_NAME_COLUMN:
76  			result = backup.getFileDescriptor()[row].getFile().getName();
77  			break;
78  		case FILE_TYPE_COLUMN:
79  			String name = backup.getFileDescriptor()[row].getFile().getName();
80  			if(name.lastIndexOf(".") > 0)
81  				result = name.substring(name.lastIndexOf("."),name.length()).toUpperCase();
82  			else result = "";
83  			break;
84  		case FILE_SIZE_COLUMN:
85  			result = backup.getFileDescriptor()[row].getSize();
86  			break;
87  		}
88  		return result;
89  	}
90  
91  	@SuppressWarnings("unchecked")
92  	public Class getColumnClass(int column) {
93  		Class result = null;
94  		switch (column) {
95  		case FILE_ABSOLUTE_PATH_COLUMN:
96  			result = String.class;
97  			break;
98  		case FILE_NAME_COLUMN:
99  			result = String.class;
100 			break;
101 		case FILE_TYPE_COLUMN:
102 			result = String.class;
103 			break;
104 		case FILE_SIZE_COLUMN:
105 			result = Long.class;
106 			break;
107 		}
108 		return result;
109 	}
110 
111 	public void update(Observable arg0, Object arg1) {
112 		this.fireTableDataChanged();
113 	}
114 
115 }