1 | package org.expeditee.importer;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Point;
|
---|
5 | import java.awt.Rectangle;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.HashSet;
|
---|
10 |
|
---|
11 | import org.expeditee.gui.DisplayIO;
|
---|
12 | import org.expeditee.gui.FrameKeyboardActions;
|
---|
13 | import org.expeditee.gui.FrameMouseActions;
|
---|
14 | import org.expeditee.gui.FrameUtils;
|
---|
15 | import org.expeditee.items.Item;
|
---|
16 | import org.expeditee.items.Text;
|
---|
17 | import org.expeditee.items.XRayable;
|
---|
18 |
|
---|
19 | public class ImageImporter implements FileImporter {
|
---|
20 |
|
---|
21 | private static Collection<String> validImageTypes = null;
|
---|
22 |
|
---|
23 | public ImageImporter() {
|
---|
24 | super();
|
---|
25 | if (validImageTypes == null) {
|
---|
26 | validImageTypes = new HashSet<String>();
|
---|
27 | validImageTypes.add("png");
|
---|
28 | validImageTypes.add("bmp");
|
---|
29 | validImageTypes.add("jpg");
|
---|
30 | validImageTypes.add("jpeg");
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public Item importFile(File f, Point location) throws IOException {
|
---|
35 |
|
---|
36 | if (location == null || f == null) {
|
---|
37 | return null;
|
---|
38 | }
|
---|
39 | String fullPath = f.getAbsolutePath();
|
---|
40 | int separator = fullPath.lastIndexOf('.');
|
---|
41 | if (separator < 0)
|
---|
42 | return null;
|
---|
43 | String suffix = fullPath.substring(separator + 1).toLowerCase();
|
---|
44 |
|
---|
45 | if (!validImageTypes.contains(suffix)) {
|
---|
46 | return null;
|
---|
47 | }
|
---|
48 |
|
---|
49 | Color borderColor = null;
|
---|
50 | float thickness = 0;
|
---|
51 | String size = "";
|
---|
52 | Collection<Item> enclosure = FrameUtils.getEnclosingLineEnds(location);
|
---|
53 | if (enclosure != null) {
|
---|
54 | for (Item i : enclosure) {
|
---|
55 | if (i.isLineEnd() && i.isEnclosed()) {
|
---|
56 | DisplayIO.getCurrentFrame().removeAllItems(enclosure);
|
---|
57 | Rectangle rect = i.getEnclosedRectangle();
|
---|
58 | size = " " + Math.round(rect.getWidth());
|
---|
59 | location = new Point(rect.x, rect.y);
|
---|
60 | thickness = i.getThickness();
|
---|
61 | borderColor = i.getColor();
|
---|
62 | break;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | FrameMouseActions.deleteItems(enclosure, false);
|
---|
66 | }
|
---|
67 |
|
---|
68 | Text source = FrameDNDTransferHandler.importString("@i: " + fullPath
|
---|
69 | + size, location);
|
---|
70 | source.setThickness(thickness);
|
---|
71 | source.setBorderColor(borderColor);
|
---|
72 |
|
---|
73 | FrameKeyboardActions.Refresh();
|
---|
74 | Collection<? extends XRayable> pictures = source.getEnclosures();
|
---|
75 | if (pictures.size() == 0)
|
---|
76 | return source;
|
---|
77 |
|
---|
78 | return source.getEnclosures().iterator().next();
|
---|
79 | }
|
---|
80 | }
|
---|