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 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.io.Serializable;
|
---|
23 | import java.util.ArrayList;
|
---|
24 | import java.util.List;
|
---|
25 |
|
---|
26 | import org.expeditee.core.Image;
|
---|
27 | import org.expeditee.gio.ClipboardManager.ClipboardData;
|
---|
28 | import org.expeditee.gio.EcosystemManager;
|
---|
29 | import org.expeditee.gio.gesture.StandardGestureActions;
|
---|
30 | import org.expeditee.gui.DisplayController;
|
---|
31 | import org.expeditee.gui.FreeItems;
|
---|
32 | import org.expeditee.gui.MessageBay;
|
---|
33 | import org.expeditee.gui.management.ResourceUtil;
|
---|
34 | import org.expeditee.items.Item;
|
---|
35 | import org.expeditee.items.ItemUtils;
|
---|
36 | import org.expeditee.items.Picture;
|
---|
37 | import org.expeditee.items.StringUtils;
|
---|
38 | import 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 | */
|
---|
45 | public class ItemSelection {
|
---|
46 | // TODO : Tidy commented code once tested. cts16
|
---|
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;
|
---|
54 |
|
---|
55 | // whether this should be automatically picked up
|
---|
56 | public boolean autoPaste = true;
|
---|
57 |
|
---|
58 | // exp save data
|
---|
59 | public String items;
|
---|
60 | public Image image;
|
---|
61 | public String text;
|
---|
62 | }
|
---|
63 |
|
---|
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);
|
---|
71 | StandardGestureActions.pickup(c);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | return toCopy;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public static void cut() {
|
---|
79 |
|
---|
80 | List<Item> toCopy = getAllToCopy();
|
---|
81 |
|
---|
82 | copy(toCopy);
|
---|
83 |
|
---|
84 | // remove the items attached to the cursor
|
---|
85 | DisplayController.getCurrentFrame().removeAllItems(toCopy);
|
---|
86 | FreeItems.getInstance().clear();
|
---|
87 |
|
---|
88 | DisplayController.requestRefresh(false);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public static void copyClone() {
|
---|
92 |
|
---|
93 | copy(ItemUtils.CopyItems(getAllToCopy()));
|
---|
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 | }
|
---|
104 |
|
---|
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
|
---|
122 | ExpClipWriter ecw = new ExpClipWriter(EcosystemManager.getInputManager().getCursorPosition());
|
---|
123 | try {
|
---|
124 | ecw.output(items);
|
---|
125 | } catch (IOException e1) {
|
---|
126 | e1.printStackTrace();
|
---|
127 | }
|
---|
128 | expData.items = ecw.getFileContents();
|
---|
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 |
|
---|
135 | // System.out.println(expData.items);
|
---|
136 |
|
---|
137 | // write out to clipboard
|
---|
138 | //ItemSelection selection = new ItemSelection(clipboardText.toString(), image, expData);
|
---|
139 | //Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
|
---|
140 | EcosystemManager.getClipboardManager().set(clipboardData);
|
---|
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)
|
---|
146 | * TODO: Above TODO seems to be done? cts16
|
---|
147 | */
|
---|
148 | public static void paste() {
|
---|
149 | if(FreeItems.hasItemsAttachedToCursor()) {
|
---|
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();
|
---|
155 | //Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
|
---|
156 | //Transferable content = c.getContents(null);
|
---|
157 | ClipboardData content = EcosystemManager.getClipboardManager().get();
|
---|
158 |
|
---|
159 | try {
|
---|
160 | if (content.data instanceof ExpDataHandler) { // Expeditee data
|
---|
161 | type = "Expeditee ";
|
---|
162 | ExpDataHandler expData = (ExpDataHandler) content.data;
|
---|
163 | if(expData != null) {
|
---|
164 | List<Item> items = new ExpClipReader(EcosystemManager.getInputManager().getCursorPosition()).read(expData.items);
|
---|
165 | // generate new IDs and pickup
|
---|
166 | StandardGestureActions.pickup(ItemUtils.CopyItems(items));
|
---|
167 | }
|
---|
168 | } else if (content.imageRepresentation != null) { // Image data
|
---|
169 | // System.out.println("IZ PIKTUR");
|
---|
170 | type = "Image ";
|
---|
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();
|
---|
174 | String name = Integer.toHexString(hashcode) + ".png";
|
---|
175 | Text item = ResourceUtil.newImageWithName(img, name);
|
---|
176 | f.add(item);
|
---|
177 | ExpClipReader.updateItems(f);
|
---|
178 | } else if (content.stringRepresentation != null) { // Plain text
|
---|
179 | type = "Plain Text ";
|
---|
180 | String clip = content.stringRepresentation;
|
---|
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;
|
---|
187 | final int x = DisplayController.getMouseX();
|
---|
188 | final int y = DisplayController.getMouseY();
|
---|
189 | final Text template = DisplayController.getCurrentFrame().getItemTemplate();
|
---|
190 | for(int i = 0; i < items.length; i++) {
|
---|
191 | // System.out.println(items[i]);
|
---|
192 | // System.out.println("Created item from string");
|
---|
193 | // item = DisplayIO.getCurrentFrame().createNewText(items[i]);
|
---|
194 | item = new Text(items[i]);
|
---|
195 | ((Text)item).setFont(template.getFont());
|
---|
196 | item.setX(x);
|
---|
197 | if(prevItem != null){
|
---|
198 | item.setY(prevItem.getY() + prevItem.getBoundsHeight());
|
---|
199 | } else item.setY(y);
|
---|
200 | f.add(item);
|
---|
201 | prevItem = item;
|
---|
202 | }
|
---|
203 | } else if (content.fileRepresentation != null) { // Files
|
---|
204 | EcosystemManager.getDragAndDropManager().importFileList(content.fileRepresentation, DisplayController.getMousePosition(), true);
|
---|
205 | ExpClipReader.updateItems(FreeItems.getInstance());
|
---|
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 | }
|
---|