/**
* ItemSelection.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.io;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.expeditee.core.Image;
import org.expeditee.gio.ClipboardManager.ClipboardData;
import org.expeditee.gio.EcosystemManager;
import org.expeditee.gio.gesture.StandardGestureActions;
import org.expeditee.gui.DisplayController;
import org.expeditee.gui.FrameGraphics;
import org.expeditee.gui.FrameIO;
import org.expeditee.gui.FreeItems;
import org.expeditee.gui.MessageBay;
import org.expeditee.items.Item;
import org.expeditee.items.ItemUtils;
import org.expeditee.items.Picture;
import org.expeditee.items.StringUtils;
import org.expeditee.items.Text;
/**
* Allows item data (text) and metadata (position, shapes, etc) to be stored on the clipboard
*
* @author jts21
*/
public class ItemSelection {
// TODO : Tidy commented code once tested. cts16
/**
* Class used for storing data which can be used to reconstruct expeditee objects from the clipboard
* Stores data as a String containing .exp save data
*/
public static final class ExpDataHandler implements Serializable {
private static final long serialVersionUID = 1L;
// whether this should be automatically picked up
public boolean autoPaste = true;
// exp save data
public String items;
public Image image;
public String text;
}
private static List- getAllToCopy() {
List
- tmp = new ArrayList
- (FreeItems.getInstance());
List
- toCopy = new ArrayList
- (tmp);
for(Item i : tmp) {
for(Item c : i.getAllConnected()) {
if(! toCopy.contains(c)) {
toCopy.add(c);
StandardGestureActions.pickup(c);
}
}
}
return toCopy;
}
public static void cut() {
List
- toCopy = getAllToCopy();
copy(toCopy);
// remove the items attached to the cursor
DisplayController.getCurrentFrame().removeAllItems(toCopy);
FreeItems.getInstance().clear();
DisplayController.requestRefresh(false);
}
public static void copyClone() {
copy(ItemUtils.CopyItems(getAllToCopy()));
}
/**
* Copies whatever items are attached to the mouse to the clipboard
*/
private static void copy(List
- items) {
if(items.size() <= 0) {
return;
}
StringBuilder clipboardText = new StringBuilder();
ExpDataHandler expData = new ExpDataHandler();
Image image = null;
// get plaintext
for(Item i : items) {
if(i instanceof Text) {
clipboardText.append(((Text)i).getText());
clipboardText.append("\n\n");
}
if(i instanceof Picture) {
// TODO: merge multiple images if necessary
image = ((Picture)i).getImage();
}
}
// get expeditee item data
ExpClipWriter ecw = new ExpClipWriter(EcosystemManager.getInputManager().getCursorPosition());
try {
ecw.output(items);
} catch (IOException e1) {
e1.printStackTrace();
}
expData.items = ecw.getFileContents();
expData.image = image;
expData.text = clipboardText.toString();
// Format the data for the clipboard
ClipboardData clipboardData = new ClipboardData(expData, expData.image, expData.text);
// System.out.println(expData.items);
// write out to clipboard
//ItemSelection selection = new ItemSelection(clipboardText.toString(), image, expData);
//Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
EcosystemManager.getClipboardManager().set(clipboardData);
}
/**
* Generates items from the clipboard data
* TODO: Enable pasting raw image data (would require saving the data as an image file and generating a Text item pointing to it)
* TODO: Above TODO seems to be done? cts16
*/
public static void paste() {
if(FreeItems.hasItemsAttachedToCursor()) {
MessageBay.displayMessage("Drop any items being carried on the cursor, then try pasting again");
return;
}
String type = "";
FreeItems f = FreeItems.getInstance();
//Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
//Transferable content = c.getContents(null);
ClipboardData content = EcosystemManager.getClipboardManager().get();
try {
if (content.data instanceof ExpDataHandler) { // Expeditee data
type = "Expeditee ";
ExpDataHandler expData = (ExpDataHandler) content.data;
if(expData != null) {
List
- items = new ExpClipReader(EcosystemManager.getInputManager().getCursorPosition()).read(expData.items);
// generate new IDs and pickup
StandardGestureActions.pickup(ItemUtils.CopyItems(items));
}
} else if (content.imageRepresentation != null) { // Image data
// System.out.println("IZ PIKTUR");
type = "Image ";
Image img = content.imageRepresentation;
//int hashcode = Arrays.hashCode(img.getData().getPixels(0, 0, img.getWidth(), img.getHeight(), (int[])null));
int hashcode = img.hashCode();
File out = new File(FrameIO.IMAGES_PATH + Integer.toHexString(hashcode) + ".png");
out.mkdirs();
img.writeToDisk("png", out);
Text item = DisplayController.getCurrentFrame().createNewText("@i: " + out.getPath());
f.add(item);
ExpClipReader.updateItems(f);
} else if (content.stringRepresentation != null) { // Plain text
type = "Plain Text ";
String clip = content.stringRepresentation;
// Covert the line separator char when pasting in
// windows (\r\n) or max (\r)
clip = StringUtils.convertNewLineChars(clip);
// blank line is an item separator
String[] items = clip.split("\n\n");
Item item, prevItem = null;
final int x = DisplayController.getMouseX();
final int y = DisplayController.getMouseY();
final Text template = DisplayController.getCurrentFrame().getItemTemplate();
for(int i = 0; i < items.length; i++) {
// System.out.println(items[i]);
// System.out.println("Created item from string");
// item = DisplayIO.getCurrentFrame().createNewText(items[i]);
item = new Text(items[i]);
((Text)item).setFont(template.getFont());
item.setX(x);
if(prevItem != null){
item.setY(prevItem.getY() + prevItem.getBoundsHeight());
} else item.setY(y);
f.add(item);
prevItem = item;
}
} /* else if {
// Next handler
} */
} catch (Exception e) {
System.out.println("Failed to load " + type + "data");
e.printStackTrace();
}
}
}