/**
 * 4/12/2005 19:05
 * Miguel de Benito Delgado <nonick AT 8027 DOT org>
 *
 * This little app demonstrates how to load an image from a zip file.
 *
 * This method was intended to be used as a means for reducing download
 * time of resources but, for certain applications, zipping only gave a 5%
 * file size decrease.
 *
 * Nevertheless, using a zip to store resources has its advantages, namely
 * that you get access to named files. No more need to store file pointers
 * sometime during the resource build process. One could simply pack everything
 * into a zip file and let JarInflater do the work of providing us with
 * InputStreams to be used by MediaManager.
 *
 * As to application's text data, the gain would double: not only would they be
 * compressed, but we could, using a readline()-like function, parse the strings
 * quite easily, again without having to care about offsets or string sizes.
 *
 * Of course, I might be wrong... ;)
 */

import com.nttdocomo.util.JarInflater;
import com.nttdocomo.ui.*;
import javax.microedition.io.Connector;
import java.io.InputStream;

/**
 *
 */
public class InflaterTest extends IApplication implements ComponentListener
{
	Panel dialog;
	Button buttonOk;
	ImageLabel picture;

	/**
	 * Application's entry point.
	 */
	public void start()
	{
		try {
			/// Open the zip archive
			InputStream is = Connector.openInputStream("resource:///img.zip");
			JarInflater reader = new JarInflater(is);
			is.close();
			/// and decompress a file in it.
			is = reader.getInputStream("img.gif");

			/// Now use the image as usual.
			MediaImage img = MediaManager.getImage(is);
			img.use();
			is.close();
			picture = new ImageLabel(img.getImage());

			/// Finally build the panel
			dialog = new Panel();
			buttonOk = new Button("Close");
			HTMLLayout layout = new HTMLLayout();
			dialog.setLayoutManager(layout);

			dialog.setTitle("Zipped!");

			layout.begin(HTMLLayout.CENTER);

			dialog.add(picture);

			layout.br();

			dialog.add(buttonOk);

			layout.end();

			/// Set things up
			buttonOk.requestFocus();
			Display.setCurrent(dialog);
			dialog.setComponentListener(this);
		}
		// Not very sofisticated error handling...
		catch (Exception e) {
			System.out.println("ooops: " + e.toString());
			terminate();
		}
	}

	/**
	 * Just close the application if we are requested to.
	 */
	public void componentAction(Component source, int type, int param)
	{
		if (source == buttonOk && type == BUTTON_PRESSED) {
			System.out.println("Bye!");
			terminate();
		}
	}

}

