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

Last change on this file since 1104 was 1104, checked in by bln4, 6 years ago

org.expeditee.gui.Popup ->

Fixed a bug in the Popup class that was causing a line to be drawn when Apollo was run. Now correctly does not try to draw when hidden.


org.expeditee.io.ItemSelection ->

When pasting a image from the clipboard, Expeditee imports the image into the images folder. This alternation makes it so the resulting @i uses the relative location rather than absolute.

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