source: trunk/src/org/expeditee/importer/ImageImporter.java@ 1109

Last change on this file since 1109 was 1109, checked in by bln4, 6 years ago

org.expeditee.importer.ImageImporter ->

Now when dragging and dropping a image that is contained within Expeditee.home's images directory, the resulting @i is generated to use a relative address.

File size: 3.3 KB
Line 
1/**
2 * ImageImporter.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.nio.file.Path;
24import java.nio.file.Paths;
25import java.util.Collection;
26import java.util.HashSet;
27
28import org.expeditee.core.Colour;
29import org.expeditee.core.Point;
30import org.expeditee.core.bounds.AxisAlignedBoxBounds;
31import org.expeditee.gio.DragAndDropManager;
32import org.expeditee.gio.gesture.StandardGestureActions;
33import org.expeditee.gui.DisplayController;
34import org.expeditee.gui.FrameIO;
35import org.expeditee.gui.FrameUtils;
36import org.expeditee.items.Item;
37import org.expeditee.items.Text;
38import org.expeditee.items.XRayable;
39
40public class ImageImporter implements FileImporter {
41
42 private static Collection<String> validImageTypes = null;
43
44 public ImageImporter() {
45 super();
46 if (validImageTypes == null) {
47 validImageTypes = new HashSet<String>();
48 validImageTypes.add("png");
49 validImageTypes.add("bmp");
50 validImageTypes.add("jpg");
51 validImageTypes.add("jpeg");
52 }
53 }
54
55 public Item importFile(File f, Point location) throws IOException {
56
57 if (location == null || f == null) {
58 return null;
59 }
60 String fullPath = f.getAbsolutePath();
61 int separator = fullPath.lastIndexOf('.');
62 if (separator < 0)
63 return null;
64 String suffix = fullPath.substring(separator + 1).toLowerCase();
65
66 if (!validImageTypes.contains(suffix)) {
67 return null;
68 }
69
70 Colour borderColor = null;
71 float thickness = 0;
72 String size = "";
73 Collection<Item> enclosure = FrameUtils.getEnclosingLineEnds(location);
74 if (enclosure != null) {
75 for (Item i : enclosure) {
76 if (i.isLineEnd() && i.isEnclosed()) {
77 DisplayController.getCurrentFrame().removeAllItems(enclosure);
78 AxisAlignedBoxBounds rect = i.getEnclosedBox();
79 size = " " + Math.round(rect.getWidth());
80 location = new Point(rect.getMinX(), rect.getMinY());
81 thickness = i.getThickness();
82 borderColor = i.getColor();
83 break;
84 }
85 }
86 StandardGestureActions.deleteItems(enclosure, false);
87 }
88 Text source;
89 final Path imagePath = Paths.get(fullPath);
90 final Path imagesPath = Paths.get(FrameIO.IMAGES_PATH);
91 if(imagePath.startsWith(imagesPath))
92 source = DragAndDropManager.importString("@i: " + imagePath.getFileName() + size, location);
93 else source = DragAndDropManager.importString("@i: " + fullPath + size, location);
94 source.setThickness(thickness);
95 source.setBorderColor(borderColor);
96
97 StandardGestureActions.Refresh();
98 Collection<? extends XRayable> pictures = source.getEnclosures();
99 if (pictures.size() == 0)
100 return source;
101
102 return source.getEnclosures().iterator().next();
103 }
104}
Note: See TracBrowser for help on using the repository browser.