/*
 * Autopsy Forensic Browser
 *
 * Copyright 2011 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.corecomponents;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import org.openide.nodes.Node;
import org.openide.util.lookup.ServiceProvider;
import org.sleuthkit.autopsy.corecomponentinterfaces.DataContentViewer;
import org.sleuthkit.datamodel.Content;
import org.sleuthkit.datamodel.File;
import org.sleuthkit.datamodel.FsContent;
import org.sleuthkit.datamodel.TskException;

/**
 * File content viewer capable of displaying some image formats.
 */
@ServiceProvider(service = DataContentViewer.class)
public class DataContentViewerPicture extends javax.swing.JPanel implements DataContentViewer {

    // for error handling
    private JPanel caller;
    private String className = this.getClass().toString();

    /** Creates new form DataContentViewerPicture */
    public DataContentViewerPicture() {
        initComponents();
        this.resetComponent();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        picViewerScrollPanel = new javax.swing.JScrollPane();
        picLabel = new javax.swing.JLabel();

        picLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        picLabel.setText(org.openide.util.NbBundle.getMessage(DataContentViewerPicture.class, "DataContentViewerPicture.picLabel.text")); // NOI18N
        picLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        picViewerScrollPanel.setViewportView(picLabel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(picViewerScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(picViewerScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel picLabel;
    private javax.swing.JScrollPane picViewerScrollPanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setNode(Node selectedNode) {
        // change the cursor to "waiting cursor" for this operation
        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        try {
            if (selectedNode != null) {
                try {
                    // read the byte of the image file
                    
                    Content content = selectedNode.getLookup().lookup(Content.class);
                    byte[] dataSource = content.read(0, content.getSize());

                    // create the input stream for the content
                    InputStream is = new ByteArrayInputStream(dataSource);

                    Image test = ImageIO.read(is); // create the image

                    this.picLabel.setIcon(new javax.swing.ImageIcon(test)); // show the file


                } catch (TskException ex) {
                    // TODO: maybe make errors bubble
                    Logger.getLogger(this.className).log(Level.WARNING, "Error while trying to display the picture content.", ex);
                } catch (Exception ex) {
                    Logger.getLogger(this.className).log(Level.WARNING, "Error while trying to display the picture content.", ex);
                }
            }
        } finally {
            this.setCursor(null);
        }
    }

    @Override
    public String getTitle() {
        return "Picture View";
    }
    
    @Override
    public String getToolTip() {
        return "Displays supported image files.";
    }

    @Override
    public DataContentViewer getInstance() {
        return new DataContentViewerPicture();
    }

    @Override
    public void resetComponent() {
        this.picLabel.setText("");
        //this.picLabel.setIcon(null);
    }

    @Override
    public boolean isSupported(Node node) {
        if (node != null) {
            // Note: only supports JPG, GIF, and PNG for now
            /*return node.getDisplayName().toLowerCase().endsWith(".jpg")
                    || node.getDisplayName().toLowerCase().endsWith(".jpeg")
                    || node.getDisplayName().toLowerCase().endsWith(".jpe")
                    || node.getDisplayName().toLowerCase().endsWith(".jfif")
                    || node.getDisplayName().toLowerCase().endsWith(".gif")
                    || node.getDisplayName().toLowerCase().endsWith(".bmp")
                    || //node.getDisplayName().toLowerCase().endsWith(".tif") ||
                    //node.getDisplayName().toLowerCase().endsWith(".tiff") ||
                    //node.getDisplayName().toLowerCase().endsWith(".tga") ||
            node.getDisplayName().toLowerCase().endsWith(".png");*/
            File file = node.getLookup().lookup(File.class);

            if (file != null) {
                return file.getSize() > 0
                        && (file.getName().toLowerCase().endsWith(".jpg")
                        || file.getName().toLowerCase().endsWith(".jpeg")
                        || file.getName().toLowerCase().endsWith(".jpe")
                        || file.getName().toLowerCase().endsWith(".jfif")
                        || file.getName().toLowerCase().endsWith(".gif")
                        || file.getName().toLowerCase().endsWith(".bmp")
                        || //node.getName().toLowerCase().endsWith(".tif") ||
                        //node.getName().toLowerCase().endsWith(".tiff") ||
                        //node.getName().toLowerCase().endsWith(".tga") ||
                        file.getName().toLowerCase().endsWith(".png"));
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    @Override
    public boolean isPreferred(Node node, boolean isSupported) {
        return isSupported;
    }

    @Override
    public Component getComponent() {
        return this;
    }
}

@@ -4164,20 +4164,21 @@
   Image 
-test
+image
  = Image
@@ -4211,16 +4211,58 @@
  image%0A%0A
+                    if (image!= null)%0A    
         
@@ -4325,12 +4325,13 @@
 con(
-test
+image
 )); 

Fix null ptr in picture viewer which lead to numerous annoying dialogs