/*
 * ZipSuite.java  $Revision: 1.4 $  $Date: 2007/05/12 06:11:12 $
 *
 * Copyright (c) Meldcraft, Inc. 2006
 */
package com.meldcraft.zipsuite;

import java.io.File;
import java.net.URL;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.swing.JTable;

import com.meldcraft.application.container.ApplicationManager;

/**
 * @version $Revision: 1.4 $ $Date: 2007/05/12 06:11:12 $
 * @author Rob Richards
 */
public class ZipSuite
{
    private File    mFile;
    private String  mSelectedItem;

    public void viewSelectedImage() throws Exception
    {
        String item = getSelectedItem();
        if (item != null)
        {
            String app = "com.meldcraft.imageviewer.ImageViewer";
            ApplicationManager.openApp(app, new String[]{item});
        }
    }

    public void openFile(File file)
    {
        mFile = file;
    }

    public String zipItemSelected(JTable table) throws Exception
    {
        boolean image = false;
        String jarstr = null;

        if (table != null)
        {
            int row = table.getSelectedRow();
            if (row >= 0)
            {
                String fpath = (String)table.getValueAt(row, 5);
                if (fpath == null)
                {
                    fpath = (String)table.getValueAt(row, 0);
                }
                else
                {
                    fpath += (String)table.getValueAt(row, 0);
                }
                URL zipURL = mFile.toURI().toURL();
                jarstr = "jar:" + zipURL + "!/" + fpath;

                String suffix = jarstr.replaceAll(".*\\.", "");
                if ((suffix != null) && (suffix.length() > 0))
                {
                    Iterator x = ImageIO.getImageReadersBySuffix(suffix);
                    image = ((x != null) && x.hasNext());
                }
            }
        }

        if (!image)
        {
            jarstr = null;
        }
        setSelectedItem(jarstr);
        return jarstr;
    }

    public synchronized String getSelectedItem()
    {
        return mSelectedItem;
    }

    protected synchronized void setSelectedItem(String item)
    {
        mSelectedItem = item;
    }
}



