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

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

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

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