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