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 |
|
---|
18 | public class ImageImporter implements FileImporter {
|
---|
19 |
|
---|
20 | private static Collection<String> validImageTypes = null;
|
---|
21 |
|
---|
22 | public ImageImporter() {
|
---|
23 | super();
|
---|
24 | if (validImageTypes == null) {
|
---|
25 | validImageTypes = new HashSet<String>();
|
---|
26 | validImageTypes.add("png");
|
---|
27 | validImageTypes.add("bmp");
|
---|
28 | validImageTypes.add("jpg");
|
---|
29 | validImageTypes.add("jpeg");
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | public boolean importFile(File f, Point location) throws IOException {
|
---|
34 |
|
---|
35 | if (location == null || f == null) {
|
---|
36 | return false;
|
---|
37 | }
|
---|
38 | String fullPath = f.getAbsolutePath();
|
---|
39 | int separator = fullPath.lastIndexOf('.');
|
---|
40 | if (separator < 0)
|
---|
41 | return false;
|
---|
42 | String suffix = fullPath.substring(separator + 1).toLowerCase();
|
---|
43 |
|
---|
44 | if (!validImageTypes.contains(suffix)) {
|
---|
45 | return false;
|
---|
46 | }
|
---|
47 |
|
---|
48 | Color borderColor = null;
|
---|
49 | float thickness = 0;
|
---|
50 | String size = "";
|
---|
51 | Collection<Item> enclosure = FrameUtils.getEnclosingLineEnds(location);
|
---|
52 | if (enclosure != null) {
|
---|
53 | for (Item i : enclosure) {
|
---|
54 | if (i.isLineEnd() && i.isEnclosed()) {
|
---|
55 | DisplayIO.getCurrentFrame().removeAllItems(enclosure);
|
---|
56 | Rectangle rect = i.getEnclosedRectangle();
|
---|
57 | size = " " + Math.round(rect.getWidth());
|
---|
58 | location = new Point(rect.x, rect.y);
|
---|
59 | thickness = i.getThickness();
|
---|
60 | borderColor = i.getColor();
|
---|
61 | break;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | FrameMouseActions.deleteItems(enclosure, false);
|
---|
65 | }
|
---|
66 |
|
---|
67 | Text source = FrameDNDTransferHandler
|
---|
68 | .importString("@i: " + fullPath + size, location);
|
---|
69 | source.setThickness(thickness);
|
---|
70 | source.setBorderColor(borderColor);
|
---|
71 |
|
---|
72 | FrameKeyboardActions.Refresh();
|
---|
73 |
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 | }
|
---|