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

Last change on this file since 253 was 253, checked in by ra33, 16 years ago
File size: 2.1 KB
Line 
1package org.expeditee.importer;
2
3import java.awt.Color;
4import java.awt.Point;
5import java.awt.Rectangle;
6import java.io.File;
7import java.io.IOException;
8import java.util.Collection;
9import java.util.HashSet;
10
11import org.expeditee.gui.DisplayIO;
12import org.expeditee.gui.FrameKeyboardActions;
13import org.expeditee.gui.FrameMouseActions;
14import org.expeditee.gui.FrameUtils;
15import org.expeditee.items.Item;
16import org.expeditee.items.Text;
17import org.expeditee.items.XRayable;
18
19public 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}
Note: See TracBrowser for help on using the repository browser.