Search in sources :

Example 1 with FileTreeModel

use of com.codename1.components.FileTreeModel in project CodenameOne by codenameone.

the class CodenameOneImplementation method openGallery.

/**
 * Opens the device gallery
 * The method returns immediately and the response will be sent asynchronously
 * to the given ActionListener Object
 *
 * use this in the actionPerformed to retrieve the file path
 * String path = (String) evt.getSource();
 *
 * @param response a callback Object to retrieve the file path
 * @param type one of the following GALLERY_IMAGE, GALLERY_VIDEO, GALLERY_ALL
 * @throws RuntimeException if this feature failed or unsupported on the platform
 */
public void openGallery(final ActionListener response, int type) {
    final Dialog d = new Dialog("Select a picture");
    d.setLayout(new BorderLayout());
    FileTreeModel model = new FileTreeModel(true);
    if (type == Display.GALLERY_IMAGE) {
        model.addExtensionFilter("jpg");
        model.addExtensionFilter("png");
    } else if (type == Display.GALLERY_VIDEO) {
        model.addExtensionFilter("mp4");
        model.addExtensionFilter("3pg");
        model.addExtensionFilter("avi");
        model.addExtensionFilter("mov");
    } else if (type == Display.GALLERY_ALL) {
        model.addExtensionFilter("jpg");
        model.addExtensionFilter("png");
        model.addExtensionFilter("mp4");
        model.addExtensionFilter("3pg");
        model.addExtensionFilter("avi");
        model.addExtensionFilter("mov");
    }
    FileTree t = new FileTree(model) {

        protected Button createNodeComponent(final Object node, int depth) {
            if (node == null || !getModel().isLeaf(node)) {
                return super.createNodeComponent(node, depth);
            }
            Hashtable t = (Hashtable) Storage.getInstance().readObject("thumbnails");
            if (t == null) {
                t = new Hashtable();
            }
            final Hashtable thumbs = t;
            final Button b = super.createNodeComponent(node, depth);
            b.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    response.actionPerformed(new ActionEvent(node, ActionEvent.Type.Other));
                    d.dispose();
                }
            });
            final ImageIO imageio = ImageIO.getImageIO();
            if (imageio != null) {
                Display.getInstance().scheduleBackgroundTask(new Runnable() {

                    public void run() {
                        byte[] data = (byte[]) thumbs.get(node);
                        if (data == null) {
                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                            try {
                                imageio.save(FileSystemStorage.getInstance().openInputStream((String) node), out, ImageIO.FORMAT_JPEG, b.getIcon().getWidth(), b.getIcon().getHeight(), 1);
                                data = out.toByteArray();
                                thumbs.put(node, data);
                                Storage.getInstance().writeObject("thumbnails", thumbs);
                            } catch (IOException ex) {
                                Log.e(ex);
                            }
                        }
                        Image im = Image.createImage(data, 0, data.length);
                        b.setIcon(im);
                    }
                });
            }
            return b;
        }
    };
    d.addComponent(BorderLayout.CENTER, t);
    d.placeButtonCommands(new Command[] { new Command("Cancel") });
    Command c = d.showAtPosition(2, 2, 2, 2, true);
    if (c != null) {
        response.actionPerformed(null);
    }
}
Also used : Hashtable(java.util.Hashtable) ActionEvent(com.codename1.ui.events.ActionEvent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageIO(com.codename1.ui.util.ImageIO) FileTreeModel(com.codename1.components.FileTreeModel) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) FileTree(com.codename1.components.FileTree)

Aggregations

FileTree (com.codename1.components.FileTree)1 FileTreeModel (com.codename1.components.FileTreeModel)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 ImageIO (com.codename1.ui.util.ImageIO)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Hashtable (java.util.Hashtable)1