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