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

Last change on this file since 1447 was 1447, checked in by bnemhaus, 5 years ago

When dnd'ing a or copy/pasting a image that is external to expeditee (not a sub file of expedite.home) then that image is imported into the expeditee file system with a randomly generated name. A users FolderSettings are used to determine location to import image too.

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