package ca.ubc.cs.commandrecommender.usagedata.gathering.screenshot;

import java.awt.AWTException;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

import ca.ubc.cs.commandrecommender.usagedata.gathering.UsageDataCaptureActivator;

public class TimedScreenCapture implements ActionListener{
	
	private static final int DELAY = 10000; // ten seconds in milliseconds
	private static final String IMG_NAME = "screenshot";
	private static final String IMG_FORMAT = ".png";
	
	private Timer timer;
	private int imgCounter;
	private UsageDataCaptureActivator udca;
	
	public TimedScreenCapture(UsageDataCaptureActivator udca) throws AWTException {
		this.timer = new Timer(DELAY, this);
		timer.start();
		imgCounter = 0;
		this.udca = udca;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				Display display = PlatformUI.getWorkbench().getDisplay();
				Composite shell = display.getActiveShell();
				if (shell == null) {
					return;
				} else {
					takeScreenshot(display, shell);
				}
			}

			public void takeScreenshot(Display display, Composite shell) {
				if (shell.getParent() == null) {
					GC gc = new GC(display);
					final Image screenshot = new Image(display, shell.getBounds());
					gc.copyArea(screenshot, shell.getBounds().x, shell.getBounds().y);
					gc.dispose();
					
					String fileName = IMG_NAME + imgCounter + IMG_FORMAT;
					imgCounter++;
					String imgFilePath = udca.getSettings().getScreenCapFilePath(fileName);
					// TODO printing file path for testing only
					System.out.println(imgFilePath);
					ImageLoader imgLoader = new ImageLoader();
					imgLoader.data = new ImageData[] {screenshot.getImageData()};
					imgLoader.save(imgFilePath, SWT.IMAGE_PNG);

					screenshot.dispose();
				} else {
					takeScreenshot(display, shell.getParent());
				}
			}
		});	
	}
}

@@ -1332,24 +1332,88 @@
 iveShell();%0A
+%09%09%09%09// if the Eclipse window is not active, skip the screenshot%0A
 %09%09%09%09if (shel
@@ -1421,24 +1421,24 @@
  == null) %7B%0A
-
 %09%09%09%09%09return;
@@ -1562,24 +1562,212 @@
 te shell) %7B%0A
+%09%09%09%09// if this is the main Eclipse workbench (i.e., not a child of the Eclipse window,%0A%09%09%09%09// so parent == the display which is null), then take the screenshot using the shell coordinates%0A
 %09%09%09%09if (shel
@@ -1787,24 +1787,52 @@
  == null) %7B%0A
+%09%09%09%09%09// take the screenshot%0A
 %09%09%09%09%09GC gc =
@@ -2015,16 +2015,44 @@
 ;%0A%09%09%09%09%09%0A
+%09%09%09%09%09// save the screenshot%0A
 %09%09%09%09%09Str
@@ -2202,95 +2202,8 @@
 e);%0A
-%09%09%09%09%09// TODO printing file path for testing only%0A%09%09%09%09%09System.out.println(imgFilePath);%0A
 %09%09%09%09
@@ -2386,24 +2386,24 @@
 .dispose();%0A
-
 %09%09%09%09%7D else %7B
@@ -2395,32 +2395,233 @@
 );%0A%09%09%09%09%7D else %7B%0A
+%09%09%09%09%09// if this is a child of the main Eclipse shell, pass its parent shell back to takeScreenshot%0A%09%09%09%09%09// recursion should be minimal as there will only ever be a small number of Eclipse windows open%0A
 %09%09%09%09%09takeScreens

Add inline comments for TimedScreenCapture methods