-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathModuleClipboard.java
More file actions
210 lines (181 loc) · 7.44 KB
/
ModuleClipboard.java
File metadata and controls
210 lines (181 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.modsim.util;
import com.modsim.modules.BaseModule;
import com.modsim.modules.Link;
import com.modsim.modules.parts.Port;
import com.modsim.Main;
import com.modsim.simulator.PickableEntity;
import com.modsim.operations.CreateOperation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Created by Ali on 05/09/2015.
*/
public final class ModuleClipboard implements ClipboardOwner {
/**
* Whether the clipboard has any items on it
* @return True if the clipboard is isEmpty
*/
public boolean isEmpty() {
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
final Transferable contents = clipboard.getContents(null);
final boolean hasTransferableText = (contents != null)
&& contents.isDataFlavorSupported(DataFlavor.stringFlavor);
final boolean hasTransferableFiles = (contents != null)
&& contents.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
return !(hasTransferableText || hasTransferableFiles);
}
/**
* Copies the given entities to the clipboard in their current state. Only links and control points BETWEEN copied
* com.modsim.modules are stored. Caller should ensure no modifications are made to the original entities while this method
* is executing.
* @param entities The entities to copy
*/
public void copy(final List<PickableEntity> entities) {
// Pick out the com.modsim.modules from the generic entities list
final List<BaseModule> copiedRefs = new ArrayList<BaseModule>();
for (final PickableEntity e : entities) {
if (e.getType() == PickableEntity.MODULE) {
copiedRefs.add((BaseModule) e);
}
}
// Copy across to clipboard storage
doCopy(copiedRefs);
}
/**
* Copies the given entities to the clipboard in their current state. Only links
* and control points BETWEEN copied com.modsim.modules are stored. Caller
* should ensure no modifications are made to the original entities while this
* method is executing.
*
* @param selection Selection containing the entities to copy
*/
public void copy(final Selection selection) {
// Pick out the com.modsim.modules from the generic entities list
final List<BaseModule> copiedRefs = new ArrayList<BaseModule>();
for (final PickableEntity e : selection.internalSelection) {
if (e.getType() == PickableEntity.MODULE) {
copiedRefs.add((BaseModule) e);
}
}
// Copy across to clipboard storage
doCopy(copiedRefs);
}
/**
* Copies the stored com.modsim.modules into the simulation, generating the
* corresponding creation operations, and returns all created entities as a
* list.
*
* @return The newly created entities
* @throws Exception
*/
public List<PickableEntity> paste() throws Exception {
String xmlStr = getClipboardContents();
if (xmlStr != null) {
ResultData result = XMLReader.readString(xmlStr);
final List<BaseModule> modules = result.modules;
final List<Link> links = result.links;
final List<PickableEntity> output = new ArrayList<>(modules);
// Add to the simulation
for (final BaseModule m : modules) {
Main.opStack.pushOp(new CreateOperation(m));
}
for (final Link l : links) {
Main.opStack.pushOp(new CreateOperation(l));
// Need to return control points as well
output.addAll(l.path.ctrlPts);
}
return output;
}
throw new Exception("Clipboard contents empty or not valid.");
}
public List<Link> getAllLinks(final List<BaseModule> modules) {
List<Link> result = new ArrayList<Link>();
for (int i = 0; i < modules.size(); i++) {
final BaseModule m = modules.get(i);
for (int j = 0; j < m.ports.size(); j++) {
final Port port = m.ports.get(j);
// Check it's a link between two copied entities
if (port.link != null && port == port.link.src && modules.contains(port.link.src.owner)
&& modules.contains(port.link.targ.owner)) {
result.add(port.link);
}
}
}
return result;
}
/**
* Internal method: copies com.modsim.modules with their complete properties and
* shared links/control points
*
* @param src List of com.modsim.modules to copy
* @param destModules (out) list of copied com.modsim.modules
* @param destLinks (out) list of copied links
*/
protected void doCopy(final List<BaseModule> src) {
setClipboardContents(XMLWriter.writeString(src, getAllLinks(src)));
}
/**
* Empty implementation of the ClipboardOwner interface.
*/
@Override
public void lostOwnership(final Clipboard clipboard, final Transferable contents) {
// do nothing
}
/**
* Place a String on the clipboard, and make this class the owner of the
* Clipboard's contents.
*/
public void setClipboardContents(final String string) {
final StringSelection stringSelection = new StringSelection(string);
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, this);
}
/**
* Get the String residing on the clipboard.
*
* @return any text found on the Clipboard; if none found, returns an empty
* String.
*/
public String getClipboardContents() {
String result = null;
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
final Transferable contents = clipboard.getContents(null);
final boolean hasTransferableText = (contents != null)
&& contents.isDataFlavorSupported(DataFlavor.stringFlavor);
final boolean hasTransferableFiles = (contents != null)
&& contents.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
if (hasTransferableFiles) {
try {
@SuppressWarnings("unchecked")
List<File> files = (List<File>) contents.getTransferData(DataFlavor.javaFileListFlavor);
if (files.size() == 1) {
File file = files.get(0);
result = new String(Files.readAllBytes(file.toPath()));
}
} catch (UnsupportedFlavorException | IOException ex) {
System.out.println(ex);
ex.printStackTrace();
}
}
else if (hasTransferableText) {
try {
result = (String) contents.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException | IOException ex) {
System.out.println(ex);
ex.printStackTrace();
}
}
return result;
}
}