/*
 * 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.Image;
import java.lang.ref.SoftReference;
import java.util.concurrent.ExecutionException;
import javax.swing.ImageIcon;
import javax.swing.SwingWorker;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.sleuthkit.autopsy.coreutils.ImageUtils;
import org.sleuthkit.datamodel.Content;

/**
 * Node that wraps around original node and adds the bitmap icon representing
 * the picture
 */
class ThumbnailViewNode extends FilterNode {

    private static final Image waitingIcon = new ImageIcon(ImageUtils.class.getResource("/org/sleuthkit/autopsy/images/Throbber_allbackgrounds_cyanblue.gif")).getImage();

    private SoftReference<Image> iconCache = null;
    private int iconSize = ImageUtils.ICON_SIZE_MEDIUM;
   
    private SwingWorker<Image, Object> swingWorker;

    /**
     * the constructor
     */
    ThumbnailViewNode(Node arg, int iconSize) {
        super(arg, Children.LEAF);
        this.iconSize = iconSize;
    }

    @Override
    public String getDisplayName() {
        if (super.getDisplayName().length() > 15) {
            return super.getDisplayName().substring(0, 15).concat("...");
        } else {
            return super.getDisplayName();
        }
    }

    @Override
     public Image getIcon(int type) {
        Image icon = null;

        if (iconCache != null) {
            icon = iconCache.get();
        }

        if (icon != null) {
            return icon;
        } else {
            final Content content = this.getLookup().lookup(Content.class);
            if (content == null) {
                return ImageUtils.getDefaultIcon();
            }
            if (swingWorker == null || swingWorker.isDone()) {
                swingWorker = new SwingWorker<Image, Object>() {
                    final private ProgressHandle progressHandle = ProgressHandleFactory.createHandle("generating thumbnail for video file " + content.getName());

                    @Override
                    protected Image doInBackground() throws Exception {
                        progressHandle.start();
                        return ImageUtils.getIcon(content, iconSize);
                    }

                    @Override
                    protected void done() {
                        super.done();
                        try {
                            iconCache = new SoftReference<>(super.get());
                            progressHandle.finish();
                            fireIconChange();
                        } catch (InterruptedException | ExecutionException ex) {
                            Exceptions.printStackTrace(ex);
                        }
                        swingWorker = null;
                    }
                };
                swingWorker.execute();
            }

            return waitingIcon;
        }
    }

    public void setIconSize(int iconSize) {
        this.iconSize = iconSize;
        iconCache = null;
        swingWorker = null;
    }
}

@@ -745,16 +745,76 @@
 .Image;%0A
+import java.awt.Toolkit;%0Aimport java.awt.event.ActionEvent;%0A
 import j
@@ -841,16 +841,16 @@
 erence;%0A
-
 import j
@@ -893,38 +893,8 @@
 on;%0A
-import javax.swing.ImageIcon;%0A
 impo
@@ -921,16 +921,42 @@
 Worker;%0A
+import javax.swing.Timer;%0A
 import o
@@ -1403,22 +1403,22 @@
     
+static 
 private
- static
  fin
@@ -1444,32 +1444,65 @@
 n = 
-new ImageIcon(ImageUtils
+Toolkit.getDefaultToolkit().createImage(ThumbnailViewNode
 .cla
@@ -1551,57 +1551,29 @@
 ges/
-Throbber_allbackgrounds_cyanblue.gif%22)).getImage(
+working_spinner.gif%22)
 );%0A%0A
@@ -1683,12 +1683,9 @@
 UM;%0A
-   
 %0A
+
     
@@ -1731,16 +1731,41 @@
 gWorker;
+%0A    private Timer timer;
 %0A%0A    /*
@@ -2185,25 +2185,24 @@
   @Override%0A
- 
     public I
@@ -2222,24 +2222,24 @@
 int type) %7B%0A
+
         Imag
@@ -3426,24 +3426,195 @@
 onChange();%0A
+                            if (timer != null) %7B%0A                                timer.stop();%0A                                timer = null;%0A                            %7D%0A
             
@@ -3898,32 +3898,227 @@
 ;%0A            %7D%0A
+            if (timer == null) %7B%0A                timer = new Timer(100, (ActionEvent e) -%3E %7B%0A                    fireIconChange();%0A                %7D);%0A                timer.start();%0A            %7D
 %0A            ret

fix animation of spinner by using timer to force repaint.