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