ZipViewerPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.viewer.zipviewer |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.plugin.viewer.zipviewer;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.gui.FileProvider;
import org.xnap.gui.StatusBar;
import org.xnap.gui.action.OpenFileAction;
import org.xnap.gui.component.DirectoryChooser;
import org.xnap.gui.component.XNapMenuItem;
import org.xnap.gui.event.PopupListener;
import org.xnap.gui.menu.OpenFileWithMenu;
import org.xnap.util.FileHelper;
public class ZipViewerPanel extends JPanel implements FileProvider
{
//--- Constant(s) ---
//--- Data field(s) ---
private JTable jta;
private ZipFileTableModel zftm;
private JLabel jlTitle;
private ZipViewerPreferences prefs = ZipViewerPreferences.getInstance();
private static Logger logger = Logger.getLogger(ZipViewerPanel.class);
//--- Constructor(s) ---
public ZipViewerPanel()
{
super(new BorderLayout());
initialize();
}
private void initialize()
{
// label
jlTitle = new JLabel();
add(jlTitle, BorderLayout.NORTH);
// table
zftm = new ZipFileTableModel();
jta = zftm.createTable(prefs, "zipfile");
JPopupMenu popup = new JPopupMenu();
popup.add(new XNapMenuItem(new OpenFileAction(this)));
popup.add(new OpenFileWithMenu(this));
popup.addSeparator();
popup.add(new ExtractAction());
popup.add(new ExtractAllAction());
jta.addMouseListener(new PopupListener(popup));
JPanel jp = new JPanel(new BorderLayout());
jp.add(jta.getTableHeader(), BorderLayout.NORTH);
jp.add(jta, BorderLayout.CENTER);
add(jp, BorderLayout.CENTER);
}
//--- Method(s) ---
public void display(File f)
{
jlTitle.setText(XNap.tr("Entries of zip file {0}", f.getName()));
zftm.setZipFile(f);
}
private ZipEntry[] getSelectedZipEntries()
{
int rowC = jta.getSelectedRowCount();
int rows[] = jta.getSelectedRows();
if (rowC == 0) {
StatusBar.setText(XNap.tr("Please select something first"));
return new ZipEntry[0];
}
ZipEntry[] entries = new ZipEntry[rowC];
for (int i = 0; i < rowC; i++) {
entries[i] = zftm.get(rows[i]);
}
return entries;
}
public File[] getFiles()
{
ZipEntry[] entries = getSelectedZipEntries();
File[] files = new File[entries.length];
for (int i = 0; i < entries.length; i++) {
try {
files[i] = File.createTempFile
(stripDirsAndExt(entries[i].getName()),
"." + FileHelper.extension(entries[i].getName()));
InputStream in = zftm.getZipFile().getInputStream(entries[i]);
FileHelper.copy(in, new FileOutputStream(files[i]));
}
catch (IOException ie) {
logger.debug("Failed to create/copy tmp file for "
+ entries[i].getName(), ie);
return null;
}
}
return files;
}
private String stripDirsAndExt(String name)
{
return FileHelper.name(name.substring(name.lastIndexOf("/") + 1));
}
private class ExtractAction extends AbstractAction
{
private DirectoryChooser chooser = null;
public ExtractAction()
{
putValue(Action.NAME, XNap.tr("Extract") + "...");
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Extracts selected entries."));
}
public void actionPerformed(ActionEvent e)
{
ZipEntry[] entries = getSelectedZipEntries();
// lazy instantiation
if (chooser == null) {
chooser = new DirectoryChooser();
}
chooser.setSelectedDirectory
(new File(prefs.getExtractToDirectory()));
if (chooser.showChooseDialog(getParent() != null ? getParent()
: ZipViewerPanel.this)
== DirectoryChooser.APPROVE_OPTION) {
File dest = chooser.getSelectedDirectory();
prefs.setExtractToDirectory(dest.getAbsolutePath());
for (int i = 0; i < entries.length; i++) {
File target = new File(dest.getAbsolutePath()
+ File.separatorChar
+ entries[i].getName());
if (entries[i].isDirectory()) {
target.mkdirs();
}
else {
target.getParentFile().mkdirs();
try {
FileHelper.copy
(zftm.getZipFile().getInputStream(entries[i]),
new FileOutputStream(target));
}
catch (IOException ie) {
logger.debug("Could not extract "
+ entries[i].getName(), ie);
}
}
}
}
}
}
private class ExtractAllAction extends AbstractAction
{
private DirectoryChooser chooser = null;
public ExtractAllAction()
{
putValue(Action.NAME, XNap.tr("Extract All") + "...");
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Extracts all entries of the current file."));
}
public void actionPerformed(ActionEvent e)
{
// lazy instantiation
if (chooser == null) {
chooser = new DirectoryChooser();
}
chooser.setSelectedDirectory
(new File(prefs.getExtractToDirectory()));
if (chooser.showChooseDialog(getParent() != null ? getParent()
: ZipViewerPanel.this)
== DirectoryChooser.APPROVE_OPTION) {
File dest = chooser.getSelectedDirectory();
ZipFile zip = zftm.getZipFile();
Enumeration it = zip.entries();
while (it.hasMoreElements()) {
ZipEntry entry = (ZipEntry)it.nextElement();
File target = new File(dest.getAbsolutePath()
+ File.separatorChar
+ entry.getName());
if (entry.isDirectory()) {
logger.debug("creating directory: "
+ target.getAbsolutePath());
target.mkdirs();
}
else {
logger.debug("unzipping file: "
+ target.getAbsolutePath());
try {
InputStream in = zip.getInputStream(entry);
FileHelper.copy(in, new FileOutputStream(target));
}
catch (IOException ie) {
logger.debug("Could not unzip " + entry.getName(),
ie);
}
}
}
}
}
}
}
The table below shows all metrics for ZipViewerPanel.java.




