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

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

Copying images in Expeditee now duplicates the associated file on the filesystem.
Also changed USER_NAME back to USER.NAME on David's direction.

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