1 | package org.expeditee.gio;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.net.URI;
|
---|
6 | import java.net.URISyntaxException;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.StringTokenizer;
|
---|
10 |
|
---|
11 | import org.expeditee.core.Point;
|
---|
12 | import org.expeditee.gui.DisplayController;
|
---|
13 | import org.expeditee.gui.FreeItems;
|
---|
14 | import org.expeditee.importer.FileImporter;
|
---|
15 | import org.expeditee.importer.FilePathImporter;
|
---|
16 | import org.expeditee.importer.ImageImporter;
|
---|
17 | import org.expeditee.importer.TextImporter;
|
---|
18 | import org.expeditee.importer.pdfImporter;
|
---|
19 | import org.expeditee.items.Item;
|
---|
20 | import org.expeditee.items.Text;
|
---|
21 |
|
---|
22 | /** TODO: Comment. cts16 */
|
---|
23 | public abstract class DragAndDropManager {
|
---|
24 |
|
---|
25 | /** The list of custom file importers registered with the DnD manager. */
|
---|
26 | private List<FileImporter> _customFileImporters = new LinkedList<FileImporter>();
|
---|
27 |
|
---|
28 | /** The list of standard file importers registered with the DnD manager. */
|
---|
29 | private List<FileImporter> _standardFileImporters = new LinkedList<FileImporter>();
|
---|
30 |
|
---|
31 | /** Standard constructor. */
|
---|
32 | protected DragAndDropManager()
|
---|
33 | {
|
---|
34 | // Set up the standard file importers
|
---|
35 | _standardFileImporters.add(new ImageImporter());
|
---|
36 | _standardFileImporters.add(new pdfImporter());
|
---|
37 | _standardFileImporters.add(new TextImporter());
|
---|
38 | _standardFileImporters.add(new FilePathImporter()); // Filepath importer as last resort
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Adds a custom importer. If an importer competes with another importer for
|
---|
43 | * handling the same file types, the importer that was added first will have
|
---|
44 | * first serve.
|
---|
45 | *
|
---|
46 | * @param importer
|
---|
47 | * The importer to add. Must not be null
|
---|
48 | *
|
---|
49 | * @throws NullPointerException
|
---|
50 | * if importer is null.
|
---|
51 | */
|
---|
52 | public void addCustomFileImporter(FileImporter importer)
|
---|
53 | {
|
---|
54 | if (importer == null) throw new NullPointerException("importer");
|
---|
55 |
|
---|
56 | if (!_customFileImporters.contains(importer)) {
|
---|
57 | _customFileImporters.add(importer);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Removes a custom importer.
|
---|
63 | *
|
---|
64 | * @param importer
|
---|
65 | * The importer to remove.
|
---|
66 | *
|
---|
67 | * @throws NullPointerException
|
---|
68 | * if importer is null.
|
---|
69 | */
|
---|
70 | public void removeCustomFileImporter(FileImporter importer)
|
---|
71 | {
|
---|
72 | if (importer == null) throw new NullPointerException("importer");
|
---|
73 |
|
---|
74 | _customFileImporters.remove(importer);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Imports a string into expeditee's current frame
|
---|
79 | *
|
---|
80 | * @param text
|
---|
81 | * The text content.
|
---|
82 | * @param expediteeDropPoint
|
---|
83 | * The location in the current expeditee frame of where to drop
|
---|
84 | * the text item.
|
---|
85 | * @param attachToFreeItems TODO
|
---|
86 | */
|
---|
87 | public static Text importString(String text, Point expediteeDropPoint, boolean attachToFreeItems)
|
---|
88 | {
|
---|
89 | assert (DisplayController.getCurrentFrame() != null);
|
---|
90 | assert (text != null && text.length() > 0);
|
---|
91 |
|
---|
92 | Text importedTextItem = new Text(DisplayController.getCurrentFrame().getNextItemID(), text);
|
---|
93 | importedTextItem.setPosition(expediteeDropPoint);
|
---|
94 |
|
---|
95 | if (attachToFreeItems) {
|
---|
96 | FreeItems.getInstance().add(importedTextItem);
|
---|
97 |
|
---|
98 | } else {
|
---|
99 | DisplayController.getCurrentFrame().addItem(importedTextItem);
|
---|
100 | }
|
---|
101 | DisplayController.requestRefresh(true);
|
---|
102 |
|
---|
103 | return importedTextItem;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Imports a list of files into Expeditee's current frame.
|
---|
107 | * @param attachToFreeItems TODO*/
|
---|
108 | public void importFileList(List<? extends File> files, Point expediteeDropPoint, boolean attachToFreeItems) throws IOException {
|
---|
109 | Point currentPoint = new Point(expediteeDropPoint);
|
---|
110 |
|
---|
111 | // import files one by one
|
---|
112 | for (File fileToImport : files) {
|
---|
113 |
|
---|
114 | Item lastItem = importFile(fileToImport, currentPoint, attachToFreeItems);
|
---|
115 |
|
---|
116 | if (lastItem == null) {
|
---|
117 | currentPoint.setY(currentPoint.getY() + 30);
|
---|
118 | } else {
|
---|
119 | currentPoint.setY(currentPoint.getY() + lastItem.getBoundsHeight());
|
---|
120 | }
|
---|
121 | // of the item that was created
|
---|
122 |
|
---|
123 | // TODO: Better placement strategy
|
---|
124 | // if (currentPoint.y > (Browser._theBrowser.getHeight() - 20))
|
---|
125 | // currentPoint.y = Browser._theBrowser.getHeight() - 20;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Imports a file into Expeditee. Any custom importers registered get first attempt,
|
---|
131 | * followed by the standard importers.
|
---|
132 | *
|
---|
133 | * @param f
|
---|
134 | * The file to import.
|
---|
135 | * @param expediteeDropPoint
|
---|
136 | * @param attachToFreeItems If true, instead of adding to generated item at {@code expediteeDropPoint}, add to free items.
|
---|
137 | *
|
---|
138 | * @throws IOException
|
---|
139 | */
|
---|
140 | public Item importFile(File f, Point expediteeDropPoint, boolean attachToFreeItems) throws IOException
|
---|
141 | {
|
---|
142 | assert (f != null);
|
---|
143 |
|
---|
144 | // Check for custom importers first. They get preference to standard
|
---|
145 | // importing routines...
|
---|
146 | Item lastCreatedItem;
|
---|
147 | if (null == (lastCreatedItem = performFileImport(_customFileImporters, f, expediteeDropPoint, attachToFreeItems))) {
|
---|
148 | // Standard file importing
|
---|
149 | lastCreatedItem = performFileImport(_standardFileImporters, f, expediteeDropPoint, attachToFreeItems);
|
---|
150 | }
|
---|
151 |
|
---|
152 | return lastCreatedItem;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Imports a single file into Expeditee's current frame. Importers are given the opportunity to
|
---|
157 | * interpret the file in order of their addition to the list.
|
---|
158 | * @param attachToFreeItems If true, instead of adding to generated item at {@code expediteeDropPoint}, add to free items.
|
---|
159 | */
|
---|
160 | private Item performFileImport(List<FileImporter> importers, File f, Point expediteeDropPoint, boolean attachToFreeItems) throws IOException
|
---|
161 | {
|
---|
162 | for (FileImporter fi : importers) {
|
---|
163 | Item lastCreated = fi.importFile(f, expediteeDropPoint, attachToFreeItems);
|
---|
164 |
|
---|
165 | if (lastCreated != null) return lastCreated;
|
---|
166 | }
|
---|
167 |
|
---|
168 | return null;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Converts a string formatted as a list of URIs into a list of Files.
|
---|
173 | *
|
---|
174 | * Code adopted from SUN - java BUG ID 4899516 workaround for KDE/GNOME
|
---|
175 | * Desktops
|
---|
176 | *
|
---|
177 | * @param uriListString
|
---|
178 | * Formatted according to RFC 2483
|
---|
179 | *
|
---|
180 | * @return The list of FILES in the uriListString. Never null.
|
---|
181 | */
|
---|
182 | protected List<File> textURIListToFileList(String uriListString)
|
---|
183 | {
|
---|
184 | List<File> fileList = new LinkedList<File>();
|
---|
185 |
|
---|
186 | for (StringTokenizer st = new StringTokenizer(uriListString, "\r\n"); st.hasMoreTokens();) {
|
---|
187 |
|
---|
188 | String s = st.nextToken();
|
---|
189 |
|
---|
190 | if (s.startsWith("#")) {
|
---|
191 | // the line is a comment (as per the RFC 2483)
|
---|
192 | continue;
|
---|
193 | }
|
---|
194 |
|
---|
195 | try {
|
---|
196 |
|
---|
197 | URI uri = new URI(s);
|
---|
198 | File file = new File(uri);
|
---|
199 | fileList.add(file);
|
---|
200 |
|
---|
201 | } catch (URISyntaxException e) {
|
---|
202 | // malformed URI
|
---|
203 | } catch (IllegalArgumentException e) {
|
---|
204 | // the URI is not a valid 'file:' URI
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | return fileList;
|
---|
209 | }
|
---|
210 | }
|
---|