source: trunk/src/org/expeditee/gio/DragAndDropManager.java@ 1097

Last change on this file since 1097 was 1097, checked in by davidb, 6 years ago

Newly structured files from Corey's work on logic/graphics separation

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