source: trunk/src/org/expeditee/io/ItemSelection.java

Last change on this file was 1446, checked in by bnemhaus, 5 years ago

org.expeditee.gui.managment.ResourceUtil.newImageWithName(Image, String) now handles the generation of new images when copy/pasting images that do not have a file (for example, from a web page or paint program)

File size: 7.1 KB
RevLine 
[919]1/**
2 * ItemSelection.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
[504]19package org.expeditee.io;
20
21import java.io.IOException;
22import java.io.Serializable;
[740]23import java.util.ArrayList;
[504]24import java.util.List;
25
[1102]26import org.expeditee.core.Image;
27import org.expeditee.gio.ClipboardManager.ClipboardData;
28import org.expeditee.gio.EcosystemManager;
29import org.expeditee.gio.gesture.StandardGestureActions;
30import org.expeditee.gui.DisplayController;
[504]31import org.expeditee.gui.FreeItems;
32import org.expeditee.gui.MessageBay;
[1446]33import org.expeditee.gui.management.ResourceUtil;
[504]34import org.expeditee.items.Item;
35import org.expeditee.items.ItemUtils;
36import org.expeditee.items.Picture;
37import org.expeditee.items.StringUtils;
38import org.expeditee.items.Text;
39
40/**
41 * Allows item data (text) and metadata (position, shapes, etc) to be stored on the clipboard
42 *
43 * @author jts21
44 */
[1102]45public class ItemSelection {
46 // TODO : Tidy commented code once tested. cts16
[504]47 /**
48 * Class used for storing data which can be used to reconstruct expeditee objects from the clipboard
49 * Stores data as a String containing .exp save data
50 */
51 public static final class ExpDataHandler implements Serializable {
52
53 private static final long serialVersionUID = 1L;
[738]54
55 // whether this should be automatically picked up
56 public boolean autoPaste = true;
[504]57
58 // exp save data
59 public String items;
[1102]60 public Image image;
61 public String text;
[504]62 }
63
[740]64 private static List<Item> getAllToCopy() {
65 List<Item> tmp = new ArrayList<Item>(FreeItems.getInstance());
66 List<Item> toCopy = new ArrayList<Item>(tmp);
67 for(Item i : tmp) {
68 for(Item c : i.getAllConnected()) {
69 if(! toCopy.contains(c)) {
70 toCopy.add(c);
[1102]71 StandardGestureActions.pickup(c);
[740]72 }
73 }
74 }
75 return toCopy;
76 }
[504]77
[740]78 public static void cut() {
[504]79
[740]80 List<Item> toCopy = getAllToCopy();
[504]81
[740]82 copy(toCopy);
83
[504]84 // remove the items attached to the cursor
[1102]85 DisplayController.getCurrentFrame().removeAllItems(toCopy);
[740]86 FreeItems.getInstance().clear();
[504]87
[1102]88 DisplayController.requestRefresh(false);
[504]89 }
90
[740]91 public static void copyClone() {
[504]92
[740]93 copy(ItemUtils.CopyItems(getAllToCopy()));
[504]94
95 }
96
97 /**
98 * Copies whatever items are attached to the mouse to the clipboard
99 */
100 private static void copy(List<Item> items) {
101 if(items.size() <= 0) {
102 return;
103 }
[1102]104
[504]105 StringBuilder clipboardText = new StringBuilder();
106 ExpDataHandler expData = new ExpDataHandler();
107 Image image = null;
108
109 // get plaintext
110 for(Item i : items) {
111 if(i instanceof Text) {
112 clipboardText.append(((Text)i).getText());
113 clipboardText.append("\n\n");
114 }
115 if(i instanceof Picture) {
116 // TODO: merge multiple images if necessary
117 image = ((Picture)i).getImage();
118 }
119 }
120
121 // get expeditee item data
[1102]122 ExpClipWriter ecw = new ExpClipWriter(EcosystemManager.getInputManager().getCursorPosition());
[504]123 try {
124 ecw.output(items);
125 } catch (IOException e1) {
126 e1.printStackTrace();
127 }
128 expData.items = ecw.getFileContents();
[1102]129 expData.image = image;
130 expData.text = clipboardText.toString();
131
132 // Format the data for the clipboard
133 ClipboardData clipboardData = new ClipboardData(expData, expData.image, expData.text);
134
[504]135 // System.out.println(expData.items);
[1102]136
[504]137 // write out to clipboard
[1102]138 //ItemSelection selection = new ItemSelection(clipboardText.toString(), image, expData);
139 //Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
140 EcosystemManager.getClipboardManager().set(clipboardData);
[504]141 }
142
143 /**
144 * Generates items from the clipboard data
145 * TODO: Enable pasting raw image data (would require saving the data as an image file and generating a Text item pointing to it)
[1102]146 * TODO: Above TODO seems to be done? cts16
[504]147 */
148 public static void paste() {
[1102]149 if(FreeItems.hasItemsAttachedToCursor()) {
[504]150 MessageBay.displayMessage("Drop any items being carried on the cursor, then try pasting again");
151 return;
152 }
153 String type = "";
154 FreeItems f = FreeItems.getInstance();
[1102]155 //Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
156 //Transferable content = c.getContents(null);
157 ClipboardData content = EcosystemManager.getClipboardManager().get();
158
[504]159 try {
[1102]160 if (content.data instanceof ExpDataHandler) { // Expeditee data
[504]161 type = "Expeditee ";
[1102]162 ExpDataHandler expData = (ExpDataHandler) content.data;
[504]163 if(expData != null) {
[1102]164 List<Item> items = new ExpClipReader(EcosystemManager.getInputManager().getCursorPosition()).read(expData.items);
[504]165 // generate new IDs and pickup
[1102]166 StandardGestureActions.pickup(ItemUtils.CopyItems(items));
[504]167 }
[1102]168 } else if (content.imageRepresentation != null) { // Image data
[504]169 // System.out.println("IZ PIKTUR");
170 type = "Image ";
[1102]171 Image img = content.imageRepresentation;
172 //int hashcode = Arrays.hashCode(img.getData().getPixels(0, 0, img.getWidth(), img.getHeight(), (int[])null));
173 int hashcode = img.hashCode();
[1446]174 String name = Integer.toHexString(hashcode) + ".png";
175 Text item = ResourceUtil.newImageWithName(img, name);
[504]176 f.add(item);
177 ExpClipReader.updateItems(f);
[1102]178 } else if (content.stringRepresentation != null) { // Plain text
[504]179 type = "Plain Text ";
[1102]180 String clip = content.stringRepresentation;
[504]181 // Covert the line separator char when pasting in
182 // windows (\r\n) or max (\r)
183 clip = StringUtils.convertNewLineChars(clip);
184 // blank line is an item separator
185 String[] items = clip.split("\n\n");
186 Item item, prevItem = null;
[1102]187 final int x = DisplayController.getMouseX();
188 final int y = DisplayController.getMouseY();
189 final Text template = DisplayController.getCurrentFrame().getItemTemplate();
[504]190 for(int i = 0; i < items.length; i++) {
[652]191 // System.out.println(items[i]);
[504]192 // System.out.println("Created item from string");
[964]193// item = DisplayIO.getCurrentFrame().createNewText(items[i]);
194 item = new Text(items[i]);
195 ((Text)item).setFont(template.getFont());
196 item.setX(x);
[504]197 if(prevItem != null){
198 item.setY(prevItem.getY() + prevItem.getBoundsHeight());
[964]199 } else item.setY(y);
[504]200 f.add(item);
201 prevItem = item;
202 }
[1436]203 } else if (content.fileRepresentation != null) { // Files
204 EcosystemManager.getDragAndDropManager().importFileList(content.fileRepresentation, DisplayController.getMousePosition(), true);
[1437]205 ExpClipReader.updateItems(FreeItems.getInstance());
[504]206 } /* else if {
207 // Next handler
208 } */
209 } catch (Exception e) {
210 System.out.println("Failed to load " + type + "data");
211 e.printStackTrace();
212 }
213 }
214
215}
Note: See TracBrowser for help on using the repository browser.