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

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

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