/**
* ImageImporter.java
* Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.expeditee.importer;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.HashSet;
import org.expeditee.core.Colour;
import org.expeditee.core.Point;
import org.expeditee.core.bounds.AxisAlignedBoxBounds;
import org.expeditee.gio.DragAndDropManager;
import org.expeditee.gio.gesture.StandardGestureActions;
import org.expeditee.gui.DisplayController;
import org.expeditee.gui.FrameIO;
import org.expeditee.gui.FrameUtils;
import org.expeditee.gui.management.ResourceUtil;
import org.expeditee.items.Item;
import org.expeditee.items.Text;
import org.expeditee.items.XRayable;
public class ImageImporter implements FileImporter {
private static Collection validImageTypes = null;
public ImageImporter() {
super();
if (validImageTypes == null) {
validImageTypes = new HashSet();
validImageTypes.add("png");
validImageTypes.add("bmp");
validImageTypes.add("jpg");
validImageTypes.add("jpeg");
}
}
public Item importFile(File f, Point location, boolean attachToFreeItems) throws IOException {
if (location == null || f == null) {
return null;
}
String fullPath = f.getAbsolutePath();
int separator = fullPath.lastIndexOf('.');
if (separator < 0)
return null;
String suffix = fullPath.substring(separator + 1).toLowerCase();
if (!validImageTypes.contains(suffix)) {
return null;
}
Colour borderColor = null;
float thickness = 0;
String size = "";
Collection- enclosure = FrameUtils.getEnclosingLineEnds(location);
if (enclosure != null) {
for (Item i : enclosure) {
if (i.isLineEnd() && i.isEnclosed()) {
DisplayController.getCurrentFrame().removeAllItems(enclosure);
AxisAlignedBoxBounds rect = i.getEnclosedBox();
size = " " + Math.round(rect.getWidth());
location = new Point(rect.getMinX(), rect.getMinY());
thickness = i.getThickness();
borderColor = i.getColor();
break;
}
}
StandardGestureActions.deleteItems(enclosure, false);
}
Text source;
Path imagePath = Paths.get(fullPath);
Path imagePathRelitivized = ResourceUtil.relativiseImagePath(imagePath);
if (!isInternal(imagePathRelitivized)) {
Path destinationDirectoryPath = ResourceUtil.getImageSaveFileLocation(DisplayController.getCurrentFrame());
String extension = imagePathRelitivized.toString();
extension = extension.substring(extension.lastIndexOf('.'));
Path newFilePath = Files.createTempFile(destinationDirectoryPath, "", extension);
Files.copy(imagePathRelitivized, newFilePath, StandardCopyOption.REPLACE_EXISTING);
imagePathRelitivized = ResourceUtil.relativiseImagePath(newFilePath);
}
source = DragAndDropManager.importString("@i: " + imagePathRelitivized.toString() + size, location, attachToFreeItems);
// final Path imagesPath = Paths.get(FrameIO.IMAGES_PATH);
// if(imagePath.startsWith(imagesPath)) {
// source = DragAndDropManager.importString("@i: " + imagePath.getFileName() + size, location, attachToFreeItems);
// } else {
// source = DragAndDropManager.importString("@i: " + fullPath + size, location, attachToFreeItems);
// }
source.setThickness(thickness);
source.setBorderColor(borderColor);
StandardGestureActions.Refresh();
Collection extends XRayable> pictures = source.getEnclosures();
if (pictures.size() == 0)
return source;
return source.getEnclosures().iterator().next();
}
private boolean isInternal(Path p) {
Path expediteeHomePath = Paths.get(FrameIO.PARENT_FOLDER).toAbsolutePath();
return p.startsWith(expediteeHomePath);
}
}