package edu.drexel.tm.cs338.textacular;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

public class PreviewPanel extends JPanel {
	private Image image;
	
	private ImageIcon icon;
	
	private JLabel label;
	
	public PreviewPanel() {
		setBackground(Color.RED);
		setPreferredSize(new Dimension(300, 600));
		
		icon = new ImageIcon();
		
		label = new JLabel();
		label.setVerticalAlignment(JLabel.TOP);
		add(new JScrollPane(label));
	}
	
	public void refresh() {
		if (loadPdf()) {
			icon.setImage(image);
			label.setIcon(icon);
			repaint();
			revalidate();
		}
	}
	
	protected boolean loadPdf() {
		String filename = "res/output/output.pdf";
		File f = new File(filename);
		if (f.exists() && !f.isDirectory()) {
			try {
				RandomAccessFile raf = new RandomAccessFile(f, "r");
				FileChannel fc = raf.getChannel();
				ByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
				PDFFile pdfFile = new PDFFile(buf);
				
				int nPages = pdfFile.getNumPages();
				System.out.println(nPages);
				PDFPage page = pdfFile.getPage(1);
				Rectangle2D r2d = page.getBBox();
				
				double w = r2d.getWidth();
				double h = r2d.getHeight();
				w /= 72.0;
				h /= 72.0;
				int res = Toolkit.getDefaultToolkit().getScreenResolution();
				w *= res;
				h *= res;
				
				image = page.getImage((int) w, (int) h, r2d, null, true, true);
				
				return true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.out.printf("%s does not exist.", f.getAbsolutePath());
		}
		
		return false;
	}
}

@@ -88,34 +88,8 @@
 on;%0A
-import java.awt.Graphics;%0A
 impo
@@ -107,16 +107,16 @@
 .Image;%0A
+
 import j
@@ -667,11 +667,13 @@
 lor.
-RED
+WHITE
 );%0A%09
@@ -708,16 +708,16 @@
 ion(
-3
+5
 00, 
-600
+768
 ));%0A
@@ -824,31 +824,14 @@
 add(
-new JScrollPane(
 label)
-)
 ;%0A%09%7D
@@ -1518,24 +1518,24 @@
 getWidth();%0A
+
 %09%09%09%09double h
@@ -1558,131 +1558,8 @@
 ();%0A
-%09%09%09%09w /= 72.0;%0A%09%09%09%09h /= 72.0;%0A%09%09%09%09int res = Toolkit.getDefaultToolkit().getScreenResolution();%0A%09%09%09%09w *= res;%0A%09%09%09%09h *= res;%0A
 %09%09%09%09

Update preview panel preferred size and remove image height and width modifications