package fi.csc.microarray.client.visualisation.methods;

import java.io.InputStream;

import javax.swing.JComponent;
import javax.swing.JPanel;

import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
import org.icepdf.ri.common.views.DocumentViewControllerImpl;

import fi.csc.microarray.client.visualisation.Visualisation;
import fi.csc.microarray.client.visualisation.VisualisationFrame;
import fi.csc.microarray.databeans.DataBean;
import fi.csc.microarray.databeans.DataBean.DataNotAvailableHandling;
import fi.csc.microarray.databeans.DataManager.StorageMethod;
import fi.csc.microarray.exception.MicroarrayException;

public class PDFViewer extends Visualisation {

	public void initialise(VisualisationFrame frame) throws Exception {
		super.initialise(frame);
	}

	@Override
	public boolean canVisualise(DataBean bean) throws MicroarrayException {
		return bean.isContentTypeCompatitible("application/pdf");
		
	}

	@Override
	public JComponent getVisualisation(DataBean data) throws Exception {

		// build a controller
		SwingController controller = new SwingController();

		// Build a SwingViewFactory configured with the controller
		SwingViewBuilder factory = new SwingViewBuilder(controller);

		// Use the factory to build a JPanel that is pre-configured
		//with a complete, active Viewer UI.
		JPanel viewerComponentPanel = factory.buildViewerPanel();

		// Open a PDF document to view
		InputStream in = data.getContentStream(DataNotAvailableHandling.NULL_ON_NA);
		if (in != null) {
			controller.openDocument(in, data.getName(), data.getContentLocation(StorageMethod.LOCAL_FILE_METHODS).getUrl().toString());

			// Set view mode
			controller.setPageViewMode(DocumentViewControllerImpl.ONE_COLUMN_VIEW, true);
		}
		
		return viewerComponentPanel;
	}
}

@@ -65,27 +65,20 @@
 java.io.
-InputStream
+File
 ;%0A%0Aimpor
@@ -225,70 +225,8 @@
 der;
-%0Aimport org.icepdf.ri.common.views.DocumentViewControllerImpl;
 %0A%0Aim
@@ -399,140 +399,8 @@
 an;%0A
-import fi.csc.microarray.databeans.DataBean.DataNotAvailableHandling;%0Aimport fi.csc.microarray.databeans.DataManager.StorageMethod;%0A
 impo
@@ -1194,25 +1194,108 @@
 werPanel();%0A
-%0A
+%09%09%0A//the pdf reader can't read file over http but says the file is corrupted%0A//%09%09%0A//
 %09%09// Open a 
@@ -1315,16 +1315,18 @@
 to view%0A
+//
 %09%09InputS
@@ -1396,16 +1396,18 @@
 ON_NA);%0A
+//
 %09%09if (in
@@ -1418,16 +1418,24 @@
 null) %7B%0A
+//%09%09%09%0A//
 %09%09%09contr
@@ -1553,17 +1553,24 @@
 ing());%0A
-%0A
+//%09%09%09%0A//
 %09%09%09// Se
@@ -1581,16 +1581,18 @@
 ew mode%0A
+//
 %09%09%09contr
@@ -1668,15 +1668,153 @@
 e);%0A
+//
 %09%09%7D%0A%09%09%0A
+%09%09//use local file for now%0A%09%09File file = application.getDataManager().getLocalFile(data);%0A%09%09controller.openDocument(file.getPath());%0A%09%09%0A
 %09%09re

Create a local copy for the pdf viewer.