/**
* pdfImporter.java
* Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.expeditee.importer;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.expeditee.core.Colour;
import org.expeditee.core.Image;
import org.expeditee.core.Point;
import org.expeditee.gio.DragAndDropManager;
import org.expeditee.gio.EcosystemManager;
import org.expeditee.gio.GraphicsManager;
import org.expeditee.gui.DisplayController;
import org.expeditee.gui.Frame;
import org.expeditee.gui.FrameCreator;
import org.expeditee.gui.FrameGraphics;
import org.expeditee.gui.FrameIO;
import org.expeditee.gui.MessageBay;
import org.expeditee.items.Item;
import org.expeditee.items.Text;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class pdfImporter implements FileImporter {
public Item importFile(final File f, Point location) throws IOException {
if (location == null || f == null) {
return null;
}
final int x = 0;
final int y = 60;
final int width = EcosystemManager.getGraphicsManager().getWindowSize().width;
System.out.println(width);
final String name = FrameIO.ConvertToValidFramesetName(f.getName().substring(0,f.getName().lastIndexOf('.'))).toLowerCase();
//check if the file is a pdf
if(!f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf('.')+1).toLowerCase().equals("pdf"))
{
return null;
}
final Text link = DragAndDropManager.importString(name, location);
link.setLink(name+"1");
final Frame frameset;
try
{
frameset = FrameIO.CreateNewFrameset(name);
}
catch(Exception e)
{
MessageBay.displayMessage("Frameset \"" + name + "\" already exists, creating a link to it");
return link;//if the frameset exists just make a link to the existing frameset
}
final String framesetPath = frameset.getFramesetPath();
System.out.println("PATH = " + framesetPath);
new Thread() {
public void run() {
try {
MessageBay.displayMessage("Importing " + f.getName() + "...");
//load a pdf from a byte buffer
RandomAccessFile raf = new RandomAccessFile(f, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
int pages = pdffile.getNumPages();
Frame currentFrame=frameset;
Frame _currentFrame=null;
Text nextButton=null, prevButton=null;
final int spacing=((Text)FrameCreator.createButton("Next", null, null, 10, 10)).getBoundsWidth() + 30;
//make images from the pdf pages, write frames with those images
for(int i=1; i<=pages; i++)
{
//remove the title from the frame
/* if(FrameDNDTransferHandler.modifier!=1 &&
currentFrame!=null &&
currentFrame.getTitleItem()!=null)
((Text)currentFrame.getTitleItem()).delete(); */
//get the pdf page
PDFPage page = pdffile.getPage(i);
//get the width and height for the page
int w=(int)page.getBBox().getWidth(), h=(int)page.getBBox().getHeight();
final double res=1080;
if(whs)
{
w*=ws;
h*=ws;
}
else
{
w*=hs;
h*=hs;
}
}
//generate the image
Image img = Image.createImage(w, h, page);
//Create a buffered image to store the image in
Image bimg = Image.createImage(img.getWidth(), img.getHeight(), false);
GraphicsManager g = EcosystemManager.getGraphicsManager();
//Paint the image onto the buffered image
g.pushDrawingSurface(bimg);
g.drawImage(img, new Point(0, 0));
g.popDrawingSurface();
//save it as a file
File out = new File(framesetPath+i+".png");
bimg.writeToDisk("png", out);
//generate a frame with that image
System.out.println(width);
System.out.println("@i: "+framesetPath+i+".png "+width);
currentFrame.addText(x, y, "@i: "+framesetPath+i+".png "+width, null);
if(i>1)
{
//put a next button on the previous frame (points to current frame)
nextButton=(Text)FrameCreator.createButton("Next", null, null, 10, 10);
nextButton.setID(_currentFrame.getNextItemID());
nextButton.addAction("next");
_currentFrame.addItem(nextButton);
//put a previous button on the current frame (points to previous frame)
prevButton=(Text)FrameCreator.createButton("Prev", null, null, (i