View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.viewer.zipviewer;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Enumeration;
26  import java.util.zip.ZipEntry;
27  import java.util.zip.ZipFile;
28  
29  import org.apache.log4j.Logger;
30  import org.xnap.XNap;
31  import org.xnap.gui.StatusBar;
32  import org.xnap.gui.table.AbstractColumnTableModel;
33  import org.xnap.gui.table.Column;
34  import org.xnap.gui.table.FilesizeCellRenderer;
35  import org.xnap.gui.table.StringCellRenderer;
36  import org.xnap.gui.table.TimeCellRenderer;
37  
38  public class ZipFileTableModel extends AbstractColumnTableModel
39  {
40  	//--- Constant(s) ---
41  
42  	public static final int FILE_NAME = 0;
43  	public static final int SIZE = 1;
44  	public static final int COMPRESSED_SIZE = 2;
45  	public static final int RATIO = 3;
46  	public static final int TIME = 4;
47  	public static final int CRC = 5;       
48  
49  	//--- Data field(s) ---
50  	
51  	protected Column columns[] = new Column[] {
52  		new Column("filename", XNap.tr("Filename"), String.class,
53  				   new StringCellRenderer()),
54  		new Column("size", XNap.tr("Size"), Long.class, 
55  				   new FilesizeCellRenderer()),
56  		new Column("compressedsize", XNap.tr("Compressed Size"), Long.class, 
57  				   new FilesizeCellRenderer()),
58  		new Column("ratio", XNap.tr("Compression Ratio"), String.class),
59  		new Column("modified", XNap.tr("Modified"), Long.class, 
60  				   new TimeCellRenderer()),
61  		new Column("crc", XNap.tr("CRC"), String.class)
62      };
63  
64  	ArrayList data = new ArrayList();
65  	ZipFile zipFile;
66  
67      private static Logger logger = Logger.getLogger(ZipFileTableModel.class);
68  	
69  	//--- Constructor(s) ---
70  
71      public ZipFileTableModel()
72  	{
73  		addColumns(columns);
74  	}
75  
76      //--- Method(s) ---
77  
78  	public void setZipFile(File file)
79  	{
80  		clear();
81  
82  		try {
83  			zipFile = new ZipFile(file);
84  		}
85  		catch (IOException ie) {
86  			logger.debug("Error opening zip file", ie);
87  			StatusBar.setText(ie.getLocalizedMessage());
88  			return;
89  		}
90  		
91  		Enumeration entries = zipFile.entries();
92  		while (entries.hasMoreElements()) {
93  			data.add((ZipEntry)entries.nextElement());
94  		}
95  		fireTableRowsInserted(0, data.size() - 1);
96  	}
97  
98  	public ZipFile getZipFile()
99  	{
100 		return  zipFile;
101 	}
102 
103 	public ZipEntry get(int row)
104 	{
105 		return (ZipEntry)data.get(mapToIndex(row));
106 	}
107 
108 	public void clear()
109 	{
110 		int k = data.size() - 1;
111 		data.clear();
112 		fireTableRowsDeleted(0, k);
113 	}
114 
115 	public int getRowCount()
116 	{
117 		return data.size();
118 	}
119 	
120 	public String getTableName()
121 	{
122 		return XNap.tr("Zip File Table");
123 	}
124 
125 	public Object get(int row, int column)
126 	{
127 		if (row >= data.size()) 
128 			return null;
129 
130 		ZipEntry entry = (ZipEntry)data.get(row);
131 
132 		switch (column) {
133 			case FILE_NAME:
134 				return entry.getName();
135 		case SIZE:
136 			return new Long(entry.getSize());
137 		case COMPRESSED_SIZE:
138 			return new Long(entry.getCompressedSize());
139 		case RATIO:
140 			if (entry.getSize() > 0) {
141 				return ((100 * (entry.getSize() - entry.getCompressedSize())) 
142 						/ entry.getSize()) + "%";
143 			}
144 			return "0 %";
145 		case TIME:
146 			return new Long(entry.getTime());
147 		case CRC:
148 			return entry.getCrc() + "";
149 		default:
150 			return null;
151 		}
152 	}
153 }