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.awt.BorderLayout;
23  import java.awt.event.ActionEvent;
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.util.Enumeration;
29  import java.util.zip.ZipEntry;
30  import java.util.zip.ZipFile;
31  
32  import javax.swing.AbstractAction;
33  import javax.swing.Action;
34  import javax.swing.JLabel;
35  import javax.swing.JPanel;
36  import javax.swing.JPopupMenu;
37  import javax.swing.JTable;
38  
39  import org.apache.log4j.Logger;
40  import org.xnap.XNap;
41  import org.xnap.gui.FileProvider;
42  import org.xnap.gui.StatusBar;
43  import org.xnap.gui.action.OpenFileAction;
44  import org.xnap.gui.component.DirectoryChooser;
45  import org.xnap.gui.component.XNapMenuItem;
46  import org.xnap.gui.event.PopupListener;
47  import org.xnap.gui.menu.OpenFileWithMenu;
48  import org.xnap.util.FileHelper;
49  
50  public class ZipViewerPanel extends JPanel implements FileProvider
51  {
52  	//--- Constant(s) ---
53  	
54  	//--- Data field(s) ---
55  	
56  	private JTable jta;
57  	private ZipFileTableModel zftm;
58  	private JLabel jlTitle;
59  	private ZipViewerPreferences prefs = ZipViewerPreferences.getInstance();
60  
61      private static Logger logger = Logger.getLogger(ZipViewerPanel.class);
62  	
63  	//--- Constructor(s) ---
64  	
65      public ZipViewerPanel()
66  	{
67  		super(new BorderLayout());
68  		initialize();
69  	}
70  
71  	private void initialize()
72  	{
73  		// label
74  		jlTitle = new JLabel();
75  		add(jlTitle, BorderLayout.NORTH);
76  
77  		// table
78  		zftm = new ZipFileTableModel();
79  		jta = zftm.createTable(prefs, "zipfile");
80  
81  		JPopupMenu popup = new JPopupMenu();
82  		popup.add(new XNapMenuItem(new OpenFileAction(this)));
83  		popup.add(new OpenFileWithMenu(this));
84  		popup.addSeparator();
85  		popup.add(new ExtractAction());
86  		popup.add(new ExtractAllAction());
87  		jta.addMouseListener(new PopupListener(popup));
88  		
89  		JPanel jp = new JPanel(new BorderLayout());
90  		jp.add(jta.getTableHeader(), BorderLayout.NORTH);
91  		jp.add(jta, BorderLayout.CENTER);
92  		
93  		add(jp, BorderLayout.CENTER);
94  	}
95  
96      //--- Method(s) ---
97  
98  	public void display(File f)
99  	{
100 		jlTitle.setText(XNap.tr("Entries of zip file {0}", f.getName()));
101 		zftm.setZipFile(f);
102 	}
103 
104 	private ZipEntry[] getSelectedZipEntries()
105 	{
106 		int rowC = jta.getSelectedRowCount();
107 		int rows[] = jta.getSelectedRows();
108 		
109 		if (rowC == 0) {
110 			StatusBar.setText(XNap.tr("Please select something first"));
111 			return new ZipEntry[0];
112 		}
113 
114 		ZipEntry[] entries = new ZipEntry[rowC];
115 
116 		for (int i = 0; i < rowC; i++) {
117 			entries[i] = zftm.get(rows[i]);
118 		}
119 		return entries;
120 	}
121 
122 	public File[] getFiles()
123 	{
124 		ZipEntry[] entries = getSelectedZipEntries();
125 		File[] files = new File[entries.length];
126 		
127 		for (int i = 0; i < entries.length; i++) {
128 			try {
129 				files[i] = File.createTempFile
130 					(stripDirsAndExt(entries[i].getName()),
131 					 "." + FileHelper.extension(entries[i].getName()));
132 				InputStream in = zftm.getZipFile().getInputStream(entries[i]);
133 				FileHelper.copy(in, new FileOutputStream(files[i]));
134 			}
135 			catch (IOException ie) {
136 				logger.debug("Failed to create/copy tmp file for " 
137 							 + entries[i].getName(), ie);
138 				return null;
139 			}
140 		}
141 		return files;
142 	}
143 
144 	private String stripDirsAndExt(String name)
145 	{
146 		return FileHelper.name(name.substring(name.lastIndexOf("/") + 1));
147 	}
148 
149 	private class ExtractAction extends AbstractAction
150 	{
151 		private DirectoryChooser chooser = null;
152 
153 		public ExtractAction()
154 		{
155 			putValue(Action.NAME, XNap.tr("Extract") + "...");
156 			putValue(Action.SHORT_DESCRIPTION,
157 					 XNap.tr("Extracts selected entries."));
158 		}
159 
160 		public void actionPerformed(ActionEvent e)
161 		{
162 			ZipEntry[] entries = getSelectedZipEntries();
163 
164 			// lazy instantiation
165 			if (chooser == null) {
166 				chooser = new DirectoryChooser();
167 			}
168 
169 			chooser.setSelectedDirectory
170 				(new File(prefs.getExtractToDirectory()));
171 
172 			if (chooser.showChooseDialog(getParent() != null ? getParent()
173 										 : ZipViewerPanel.this)
174 				== DirectoryChooser.APPROVE_OPTION) {
175 				File dest = chooser.getSelectedDirectory();
176 				prefs.setExtractToDirectory(dest.getAbsolutePath());
177 				
178 				for (int i = 0; i < entries.length; i++) {
179 					File target = new File(dest.getAbsolutePath()
180 										   + File.separatorChar 
181 										   + entries[i].getName());
182 					if (entries[i].isDirectory()) {
183 						target.mkdirs();
184 					}
185 					else {
186 						target.getParentFile().mkdirs();
187 						try {
188 							FileHelper.copy
189 								(zftm.getZipFile().getInputStream(entries[i]),
190 								 new FileOutputStream(target));
191 						}
192 						catch (IOException ie) {
193 							logger.debug("Could not extract "
194 										 + entries[i].getName(), ie);
195 						}
196 					}
197 				}
198 			}
199 		}
200 	}
201 
202 	private class ExtractAllAction extends AbstractAction
203 	{
204 		private DirectoryChooser chooser = null;
205 
206 		public ExtractAllAction()
207 		{
208 			putValue(Action.NAME, XNap.tr("Extract All") + "...");
209 			putValue(Action.SHORT_DESCRIPTION,
210 					 XNap.tr("Extracts all entries of the current file."));
211 		}
212 
213 		public void actionPerformed(ActionEvent e)
214 		{
215 			// lazy instantiation
216 			if (chooser == null) {
217 				chooser = new DirectoryChooser();
218 			}
219 			
220 			chooser.setSelectedDirectory
221 				(new File(prefs.getExtractToDirectory()));
222 
223 			if (chooser.showChooseDialog(getParent() != null ? getParent()
224 										 : ZipViewerPanel.this)
225 				== DirectoryChooser.APPROVE_OPTION) {
226 				File dest = chooser.getSelectedDirectory();
227 
228 				ZipFile zip = zftm.getZipFile();
229 
230 				Enumeration it = zip.entries();
231 				
232 				while (it.hasMoreElements()) {
233 					ZipEntry entry = (ZipEntry)it.nextElement();
234 					File target = new File(dest.getAbsolutePath()
235 										   + File.separatorChar 
236 										   + entry.getName());
237 					
238 					if (entry.isDirectory()) {
239 						logger.debug("creating directory: "
240 									 + target.getAbsolutePath());
241 						target.mkdirs();
242 					}
243 					else {
244 						logger.debug("unzipping file: "
245 									 + target.getAbsolutePath());
246 						try {
247 							InputStream in = zip.getInputStream(entry);
248 							FileHelper.copy(in, new FileOutputStream(target));
249 						}
250 						catch (IOException ie) {
251 							logger.debug("Could not unzip " + entry.getName(),
252 										 ie);
253 						}
254 					}
255 				}
256 			}
257 		}
258 	}
259 }