1 | /**
|
---|
2 | * TextImporter.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 |
|
---|
19 | package org.expeditee.importer;
|
---|
20 |
|
---|
21 | import java.io.BufferedReader;
|
---|
22 | import java.io.File;
|
---|
23 | import java.io.FileReader;
|
---|
24 | import java.io.IOException;
|
---|
25 |
|
---|
26 | import org.expeditee.core.Colour;
|
---|
27 | import org.expeditee.core.Point;
|
---|
28 | import org.expeditee.gio.DragAndDropManager;
|
---|
29 | import org.expeditee.gui.DisplayController;
|
---|
30 | import org.expeditee.gui.FrameCreator;
|
---|
31 | import org.expeditee.gui.FrameGraphics;
|
---|
32 | import org.expeditee.gui.MessageBay;
|
---|
33 | import org.expeditee.items.Item;
|
---|
34 | import org.expeditee.items.Text;
|
---|
35 |
|
---|
36 | public class TextImporter implements FileImporter {
|
---|
37 |
|
---|
38 | public TextImporter() {
|
---|
39 | super();
|
---|
40 | }
|
---|
41 |
|
---|
42 | public Item importFile(final File f, Point location, boolean attachToFreeItems) throws IOException {
|
---|
43 | if (location == null || f == null) {
|
---|
44 | return null;
|
---|
45 | }
|
---|
46 | final String fullPath = f.getAbsolutePath();
|
---|
47 |
|
---|
48 | final Text source = DragAndDropManager.importString(f.getPath(), location, false);
|
---|
49 |
|
---|
50 | // Create a frameCreator to write the text
|
---|
51 | final FrameCreator frames = new FrameCreator(f.getName());
|
---|
52 |
|
---|
53 | new Thread() {
|
---|
54 | public void run() {
|
---|
55 | try {
|
---|
56 | // Open a file stream to the file
|
---|
57 | BufferedReader br = new BufferedReader(new FileReader(fullPath));
|
---|
58 |
|
---|
59 | MessageBay.displayMessage("Importing " + f.getName() + "...");
|
---|
60 |
|
---|
61 | // Read in the text
|
---|
62 | String nextLine;
|
---|
63 | while ((nextLine = br.readLine()) != null) {
|
---|
64 | frames.addText(nextLine, null, null, null, false);
|
---|
65 | }
|
---|
66 |
|
---|
67 | frames.save();
|
---|
68 | source.setLink(frames.getName());
|
---|
69 | MessageBay.displayMessage(f.getName() + " import complete", Colour.GREEN);
|
---|
70 | DisplayController.requestRefresh(true);
|
---|
71 | } catch (Exception e) {
|
---|
72 | e.printStackTrace();
|
---|
73 | MessageBay.errorMessage(e.getMessage());
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }.start();
|
---|
77 | DisplayController.requestRefresh(true);
|
---|
78 | return source;
|
---|
79 | }
|
---|
80 | }
|
---|