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