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

Last change on this file since 964 was 964, checked in by bln4, 9 years ago

Made changes to what happens when pasting plan text. Because the code was using the method Frame.createNewText(String) it was being assigned a parent and actually getting added to the frame. However the goal was to attach it to the FreeItems list (cursor).

It now instead uses the constructor of Text to create the item and sets its font by the item template and its position by the mouse cursor.

File size: 8.1 KB
Line 
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
19package org.expeditee.io;
20
21
22import java.awt.Image;
23import java.awt.Toolkit;
24import java.awt.datatransfer.Clipboard;
25import java.awt.datatransfer.DataFlavor;
26import java.awt.datatransfer.Transferable;
27import java.awt.datatransfer.UnsupportedFlavorException;
28import java.awt.image.BufferedImage;
29import java.io.File;
30import java.io.IOException;
31import java.io.Serializable;
32import java.util.ArrayList;
33import java.util.Arrays;
34import java.util.List;
35
36import javax.imageio.ImageIO;
37
38import org.expeditee.gui.DisplayIO;
39import org.expeditee.gui.FrameGraphics;
40import org.expeditee.gui.FrameIO;
41import org.expeditee.gui.FrameMouseActions;
42import org.expeditee.gui.FreeItems;
43import org.expeditee.gui.MessageBay;
44import org.expeditee.items.Item;
45import org.expeditee.items.ItemUtils;
46import org.expeditee.items.Picture;
47import org.expeditee.items.StringUtils;
48import org.expeditee.items.Text;
49
50/**
51 * Allows item data (text) and metadata (position, shapes, etc) to be stored on the clipboard
52 *
53 * @author jts21
54 */
55public class ItemSelection implements Transferable {
56
57 /**
58 * Class used for storing data which can be used to reconstruct expeditee objects from the clipboard
59 * Stores data as a String containing .exp save data
60 */
61 public static final class ExpDataHandler implements Serializable {
62
63 private static final long serialVersionUID = 1L;
64
65 // whether this should be automatically picked up
66 public boolean autoPaste = true;
67
68 // exp save data
69 public String items;
70 }
71
72 public static final DataFlavor expDataFlavor = new DataFlavor(ExpDataHandler.class, "expDataHandler");
73
74 private static final int STRING = 0;
75 private static final int IMAGE = 1;
76 private static final int EXP_DATA = 2;
77
78 private static final DataFlavor[] flavors = {
79 DataFlavor.stringFlavor,
80 DataFlavor.imageFlavor,
81 expDataFlavor
82 };
83
84 private String data;
85 private Image image;
86 private ExpDataHandler expData;
87
88 public ItemSelection(String data, Image image, ExpDataHandler expData) {
89 this.data = data;
90 this.image = image;
91 this.expData = expData;
92 }
93
94 @Override
95 public DataFlavor[] getTransferDataFlavors() {
96 return (DataFlavor[])flavors.clone();
97 }
98
99 @Override
100 public boolean isDataFlavorSupported(DataFlavor flavor) {
101 for (int i = 0; i < flavors.length; i++) {
102 if (flavor.equals(flavors[i])) {
103 return true;
104 }
105 }
106 return false;
107 }
108
109 @Override
110 public Object getTransferData(DataFlavor flavor)
111 throws UnsupportedFlavorException, IOException {
112 if (flavor.equals(flavors[STRING])) {
113 return (Object)data;
114 } else if (flavor.equals(flavors[IMAGE])) {
115 return (Object)image;
116 } else if (flavor.equals(flavors[EXP_DATA])) {
117 return (Object)expData;
118 } else {
119 throw new UnsupportedFlavorException(flavor);
120 }
121 }
122
123 private static List<Item> getAllToCopy() {
124 List<Item> tmp = new ArrayList<Item>(FreeItems.getInstance());
125 List<Item> toCopy = new ArrayList<Item>(tmp);
126 for(Item i : tmp) {
127 for(Item c : i.getAllConnected()) {
128 if(! toCopy.contains(c)) {
129 toCopy.add(c);
130 FrameMouseActions.pickup(c);
131 }
132 }
133 }
134 return toCopy;
135 }
136
137 public static void cut() {
138
139 List<Item> toCopy = getAllToCopy();
140
141 copy(toCopy);
142
143 // remove the items attached to the cursor
144 DisplayIO.getCurrentFrame().removeAllItems(toCopy);
145 FreeItems.getInstance().clear();
146
147 FrameGraphics.refresh(false);
148 }
149
150 public static void copyClone() {
151
152 copy(ItemUtils.CopyItems(getAllToCopy()));
153
154 }
155
156 /**
157 * Copies whatever items are attached to the mouse to the clipboard
158 */
159 private static void copy(List<Item> items) {
160 if(items.size() <= 0) {
161 return;
162 }
163
164 StringBuilder clipboardText = new StringBuilder();
165 ExpDataHandler expData = new ExpDataHandler();
166 Image image = null;
167
168 // get plaintext
169 for(Item i : items) {
170 if(i instanceof Text) {
171 clipboardText.append(((Text)i).getText());
172 clipboardText.append("\n\n");
173 }
174 if(i instanceof Picture) {
175 // TODO: merge multiple images if necessary
176 image = ((Picture)i).getImage();
177 }
178 }
179
180 // get expeditee item data
181 ExpClipWriter ecw = new ExpClipWriter(FrameMouseActions.getX(), FrameMouseActions.getY());
182 try {
183 ecw.output(items);
184 } catch (IOException e1) {
185 e1.printStackTrace();
186 }
187 expData.items = ecw.getFileContents();
188 // System.out.println(expData.items);
189
190 // write out to clipboard
191 ItemSelection selection = new ItemSelection(clipboardText.toString(), image, expData);
192 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
193 }
194
195 /**
196 * Generates items from the clipboard data
197 * TODO: Enable pasting raw image data (would require saving the data as an image file and generating a Text item pointing to it)
198 */
199 public static void paste() {
200 if(FreeItems.itemsAttachedToCursor()) {
201 MessageBay.displayMessage("Drop any items being carried on the cursor, then try pasting again");
202 return;
203 }
204 String type = "";
205 FreeItems f = FreeItems.getInstance();
206 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
207 Transferable content = c.getContents(null);
208 try {
209 if(content.isDataFlavorSupported(ItemSelection.expDataFlavor)) { // Expeditee data
210 type = "Expeditee ";
211 ExpDataHandler expData = (ExpDataHandler)content.getTransferData(ItemSelection.expDataFlavor);
212 if(expData != null) {
213 List<Item> items = new ExpClipReader(FrameMouseActions.getX(), FrameMouseActions.getY()).read(expData.items);
214 // generate new IDs and pickup
215 FrameMouseActions.pickup(ItemUtils.CopyItems(items));
216 }
217 } else if(content.isDataFlavorSupported(DataFlavor.imageFlavor)) { // Image data
218 // System.out.println("IZ PIKTUR");
219 type = "Image ";
220 BufferedImage img = (BufferedImage)content.getTransferData(DataFlavor.imageFlavor);
221 int hashcode = Arrays.hashCode(img.getData().getPixels(0, 0, img.getWidth(), img.getHeight(), (int[])null));
222 File out = new File(FrameIO.IMAGES_PATH + Integer.toHexString(hashcode) + ".png");
223 out.mkdirs();
224 ImageIO.write(img, "png", out);
225 Text item = DisplayIO.getCurrentFrame().createNewText("@i: " + out.getPath());
226 f.add(item);
227 ExpClipReader.updateItems(f);
228 } else if(content.isDataFlavorSupported(DataFlavor.stringFlavor)) { // Plain text
229 type = "Plain Text ";
230 String clip = ((String) content.getTransferData(DataFlavor.stringFlavor));
231 // Covert the line separator char when pasting in
232 // windows (\r\n) or max (\r)
233 clip = StringUtils.convertNewLineChars(clip);
234 // blank line is an item separator
235 String[] items = clip.split("\n\n");
236 Item item, prevItem = null;
237 final int x = DisplayIO.getMouseX();
238 final int y = DisplayIO.getMouseY();
239 final Text template = DisplayIO.getCurrentFrame().getItemTemplate();
240 for(int i = 0; i < items.length; i++) {
241 // System.out.println(items[i]);
242 // System.out.println("Created item from string");
243// item = DisplayIO.getCurrentFrame().createNewText(items[i]);
244 item = new Text(items[i]);
245 ((Text)item).setFont(template.getFont());
246 item.setX(x);
247 if(prevItem != null){
248 item.setY(prevItem.getY() + prevItem.getBoundsHeight());
249 } else item.setY(y);
250 f.add(item);
251 prevItem = item;
252 }
253 } /* else if {
254 // Next handler
255 } */
256 } catch (Exception e) {
257 System.out.println("Failed to load " + type + "data");
258 e.printStackTrace();
259 }
260 }
261
262}
Note: See TracBrowser for help on using the repository browser.