source: trunk/src/org/expeditee/importer/pdfImporter.java@ 1242

Last change on this file since 1242 was 1242, checked in by bln4, 5 years ago

Support for new regime in the form of new fields and conditional setting of all paths fields.

Settings are now able to generate their own representation. This allows for the user to explicitly inspect the default values.

When profiles are created, an optional parameter may now be provided. If not null, the new map parameter can contain default values for settings to apply to this profile. This allows for the creation of profiles to that have (for example), their username set to an explicit value. Multiuser mode uses this functionality for usernames and key values among other things.

Frames can now be asked were they are located on the file system. Furthermore, frame indirection is now a thing. Rather than containing data to display, an exp file can contain a line in the format of "REDIRECT:<path>" to go looking for that data. Frames are able to return both their logical (their exp file) and real (the file actually containing the data) paths.

Frames can now store data.

Further fixes to how edits from other users are loaded in.

File size: 6.5 KB
Line 
1/**
2 * pdfImporter.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.importer;
20
21import java.io.File;
22import java.io.IOException;
23import java.io.RandomAccessFile;
24import java.nio.ByteBuffer;
25import java.nio.channels.FileChannel;
26
27import org.expeditee.core.Colour;
28import org.expeditee.core.Image;
29import org.expeditee.core.Point;
30import org.expeditee.gio.DragAndDropManager;
31import org.expeditee.gio.EcosystemManager;
32import org.expeditee.gio.GraphicsManager;
33import org.expeditee.gui.DisplayController;
34import org.expeditee.gui.Frame;
35import org.expeditee.gui.FrameCreator;
36import org.expeditee.gui.FrameGraphics;
37import org.expeditee.gui.FrameIO;
38import org.expeditee.gui.MessageBay;
39import org.expeditee.items.Item;
40import org.expeditee.items.Text;
41
42import com.sun.pdfview.PDFFile;
43import com.sun.pdfview.PDFPage;
44
45public class pdfImporter implements FileImporter {
46
47 public Item importFile(final File f, Point location) throws IOException {
48 if (location == null || f == null) {
49 return null;
50 }
51
52 final int x = 0;
53 final int y = 60;
54 final int width = EcosystemManager.getGraphicsManager().getWindowSize().width;
55 System.out.println(width);
56
57 final String name = FrameIO.ConvertToValidFramesetName(f.getName().substring(0,f.getName().lastIndexOf('.'))).toLowerCase();
58
59 //check if the file is a pdf
60 if(!f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf('.')+1).toLowerCase().equals("pdf"))
61 {
62 return null;
63 }
64
65 final Text link = DragAndDropManager.importString(name, location);
66 link.setLink(name+"1");
67
68 final Frame frameset;
69 try
70 {
71 frameset = FrameIO.CreateNewFrameset(name);
72 }
73 catch(Exception e)
74 {
75 MessageBay.displayMessage("Frameset \"" + name + "\" already exists, creating a link to it");
76 return link;//if the frameset exists just make a link to the existing frameset
77 }
78
79 final String framesetPath = frameset.getFramesetPath();
80
81 System.out.println("PATH = " + framesetPath);
82
83 new Thread() {
84 public void run() {
85 try {
86 MessageBay.displayMessage("Importing " + f.getName() + "...");
87 //load a pdf from a byte buffer
88 RandomAccessFile raf = new RandomAccessFile(f, "r");
89 FileChannel channel = raf.getChannel();
90 ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
91 PDFFile pdffile = new PDFFile(buf);
92 int pages = pdffile.getNumPages();
93
94 Frame currentFrame=frameset;
95 Frame _currentFrame=null;
96 Text nextButton=null, prevButton=null;
97 final int spacing=((Text)FrameCreator.createButton("Next", null, null, 10, 10)).getBoundsWidth() + 30;
98
99 //make images from the pdf pages, write frames with those images
100 for(int i=1; i<=pages; i++)
101 {
102 //remove the title from the frame
103 /* if(FrameDNDTransferHandler.modifier!=1 &&
104 currentFrame!=null &&
105 currentFrame.getTitleItem()!=null)
106 ((Text)currentFrame.getTitleItem()).delete(); */
107 //get the pdf page
108 PDFPage page = pdffile.getPage(i);
109 //get the width and height for the page
110 int w=(int)page.getBBox().getWidth(), h=(int)page.getBBox().getHeight();
111 final double res=1080;
112 if(w<res || h<res)
113 {
114 double ws=res/w;
115 double hs=res/h;
116 if(ws>hs)
117 {
118 w*=ws;
119 h*=ws;
120 }
121 else
122 {
123 w*=hs;
124 h*=hs;
125 }
126 }
127 //generate the image
128 Image img = Image.createImage(w, h, page);
129 //Create a buffered image to store the image in
130 Image bimg = Image.createImage(img.getWidth(), img.getHeight(), false);
131 GraphicsManager g = EcosystemManager.getGraphicsManager();
132 //Paint the image onto the buffered image
133 g.pushDrawingSurface(bimg);
134 g.drawImage(img, new Point(0, 0));
135 g.popDrawingSurface();
136 //save it as a file
137 File out = new File(framesetPath+i+".png");
138 bimg.writeToDisk("png", out);
139 //generate a frame with that image
140 System.out.println(width);
141 System.out.println("@i: "+framesetPath+i+".png "+width);
142 currentFrame.addText(x, y, "@i: "+framesetPath+i+".png "+width, null);
143 if(i>1)
144 {
145 //put a next button on the previous frame (points to current frame)
146 nextButton=(Text)FrameCreator.createButton("Next", null, null, 10, 10);
147 nextButton.setID(_currentFrame.getNextItemID());
148 nextButton.addAction("next");
149 _currentFrame.addItem(nextButton);
150 //put a previous button on the current frame (points to previous frame)
151 prevButton=(Text)FrameCreator.createButton("Prev", null, null, (i<pages)?spacing:10, 10);
152 prevButton.setID(currentFrame.getNextItemID());
153 prevButton.addAction("previous");
154 currentFrame.addItem(prevButton);
155 }
156 else
157 {
158 prevButton=(Text)FrameCreator.createButton("Home", null, null, (i<pages)?spacing:10, 10);
159 prevButton.setID(currentFrame.getNextItemID());
160 prevButton.addAction("GotoHome");
161 currentFrame.addItem(prevButton);
162 }
163 FrameIO.SaveFrame(_currentFrame,true);
164 _currentFrame=currentFrame;
165 if(i<pages) currentFrame = FrameIO.CreateFrame(frameset.getFramesetName(), name, null);
166 }
167 prevButton=(Text)FrameCreator.createButton("Home", null, null, spacing, 10);
168 prevButton.setID(currentFrame.getNextItemID());
169 prevButton.addAction("gotohome");
170 currentFrame.addItem(prevButton);
171 FrameIO.SaveFrame(currentFrame,true);
172 MessageBay.displayMessage(f.getName() + " import complete", Colour.GREEN);
173 DisplayController.requestRefresh(true);
174 } catch (Exception e) {
175 e.printStackTrace();
176 MessageBay.errorMessage(e.getMessage());
177 }
178 }
179 }.start();
180 DisplayController.requestRefresh(true);
181 //return source;
182 return link;
183 }
184}
Note: See TracBrowser for help on using the repository browser.