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 |
|
---|
19 | package org.expeditee.importer;
|
---|
20 |
|
---|
21 | import java.awt.Color;
|
---|
22 | import java.awt.Point;
|
---|
23 | import java.awt.Rectangle;
|
---|
24 | import java.io.File;
|
---|
25 | import java.io.IOException;
|
---|
26 | import java.util.Collection;
|
---|
27 | import java.util.HashSet;
|
---|
28 |
|
---|
29 | import org.expeditee.gui.DisplayIO;
|
---|
30 | import org.expeditee.gui.FrameKeyboardActions;
|
---|
31 | import org.expeditee.gui.FrameMouseActions;
|
---|
32 | import org.expeditee.gui.FrameUtils;
|
---|
33 | import org.expeditee.items.Item;
|
---|
34 | import org.expeditee.items.Text;
|
---|
35 | import org.expeditee.items.XRayable;
|
---|
36 |
|
---|
37 | public 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 | Color 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 | DisplayIO.getCurrentFrame().removeAllItems(enclosure);
|
---|
75 | Rectangle rect = i.getEnclosedRectangle();
|
---|
76 | size = " " + Math.round(rect.getWidth());
|
---|
77 | location = new Point(rect.x, rect.y);
|
---|
78 | thickness = i.getThickness();
|
---|
79 | borderColor = i.getColor();
|
---|
80 | break;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | FrameMouseActions.deleteItems(enclosure, false);
|
---|
84 | }
|
---|
85 |
|
---|
86 | Text source = FrameDNDTransferHandler.importString("@i: " + fullPath
|
---|
87 | + size, location);
|
---|
88 | source.setThickness(thickness);
|
---|
89 | source.setBorderColor(borderColor);
|
---|
90 |
|
---|
91 | FrameKeyboardActions.Refresh();
|
---|
92 | Collection<? extends XRayable> pictures = source.getEnclosures();
|
---|
93 | if (pictures.size() == 0)
|
---|
94 | return source;
|
---|
95 |
|
---|
96 | return source.getEnclosures().iterator().next();
|
---|
97 | }
|
---|
98 | }
|
---|