/
ClipBoardUtil.groovy
152 lines (136 loc) · 6.44 KB
/
ClipBoardUtil.groovy
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
package freeplaneGTD
import org.freeplane.core.util.TextUtils
import org.freeplane.features.format.FormattedDate
import org.freeplane.features.format.FormattedNumber
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.Transferable
import java.awt.datatransfer.UnsupportedFlavorException
class ClipBoardUtil {
static class MyTransferable implements Transferable {
private static final DataFlavor[] supportedFlavors = [
new DataFlavor("text/freeplane-nodes; class=java.lang.String"),
new DataFlavor('text/html; class=java.lang.String'),
new DataFlavor('text/plain; class=java.lang.String'),
]
private final String freeplaneData;
private final String plainData;
private final String htmlData;
public MyTransferable(String freeplaneData, String plainData, String htmlData) {
this.freeplaneData = freeplaneData;
this.plainData = plainData;
this.htmlData = htmlData;
}
public DataFlavor[] getTransferDataFlavors() {
return supportedFlavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
for (DataFlavor supportedFlavor : supportedFlavors) {
if (supportedFlavor == flavor) {
return true;
}
}
return false;
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (flavor.equals(supportedFlavors[0])) {
return freeplaneData;
}
if (flavor.equals(supportedFlavors[1])) {
return htmlData
}
if (flavor.equals(supportedFlavors[2])) {
return plainData
}
throw new UnsupportedFlavorException(flavor);
}
}
static Transferable createTransferable(Map content, GTDMapReader reader, boolean showNotes) {
return new MyTransferable(
extractFreeplane(content, reader),
extractText(content),
extractHtml(content,showNotes));
}
static String contextsToList(String context) {
String[] list = context.split(',')
return ' @' + list.join(' @')
}
static String extractFreeplane(Map content, GTDMapReader reader) {
Tag body = new Tag('node', [TEXT: TextUtils.getText('freeplaneGTD_view_' + content['type'])])
content['groups'].each {
Tag curItem = body.addChild('node', [TEXT: it['title']])
it['items'].each {
Tag node = curItem.addChild('node', [TEXT: it['action']])
node.addChild('icon', [BUILTIN: reader.iconNextAction])
it['done'] ? node.addChild('icon', [BUILTIN: reader.iconDone]) : false
it['context'] ? node.addChild('attribute', [NAME: 'Where', VALUE: it['context']]) : false
if (it['when']) {
Tag attr = node.addChild('attribute', [
NAME : 'When',
VALUE: it['when']])
if (it['when'] instanceof FormattedDate) {
attr.addProperty('OBJECT', 'org.freeplane.features.format.FormattedDate|' + it['when'] + '|yyyy-MM-dd')
}
}
it['who'] ? node.addChild('attribute', [NAME: 'Who', VALUE: it['who']]) : false
if (it['priority']) {
Tag attr = node.addChild('attribute', [NAME : 'Priority',
VALUE: it['priority']
])
if (it['priority'] instanceof FormattedNumber) {
attr.addProperty('OBJECT', 'org.freeplane.features.format.FormattedNumber|' + it['priority'])
}
}
it['details'] ? node.addChild('richcontent', [TYPE: 'DETAILS']).addPreformatted((String) it['details']) : false
it['notes'] ? node.addChild('richcontent', [TYPE: 'NOTE']).addPreformatted((String) it['notes']) : false
}
}
return body.toString()
}
static String extractText(Map content) {
def list = []
list << TextUtils.getText('freeplaneGTD_view_' + content['type'])
content['groups'].each {
list << '\t' + it['title']
it['items'].each {
list << '\t\t*' + (it['priority'] ? ' #' + it['priority'] : '') + ' ' + it['action'] +
(it['who'] ? ' [' + it['who'] + ']' : '') +
(it['when'] ? ' {' + it['when'] + '}' : '') +
(it['project'] ? ' for ' + it['project'] : '') +
(it['context'] ? contextsToList((String) it['context']) : '')
}
}
return list.join('\n');
}
static String extractHtml(Map content, boolean showNotes) {
Tag body = new Tag('body')
body.addContent('h1', TextUtils.getText('freeplaneGTD_view_' + content['type']))
content['groups'].each {
body.addContent('h2', it['title'])
Tag curItem = body.addChild('ul')
it['items'].each {
Tag wrap = curItem.addChild('li')
if (it['done']) wrap.params = [style: 'text-decoration: line-through']
if (it['priority']) {
wrap = wrap.addContent('span', it['priority'] + ' ', [style: 'color:red'])
}
wrap.addContent(it['action'] +
(it['who'] ? ' [' + it['who'] + ']' : '') +
(it['when'] ? ' {' + it['when'] + '}' : '') +
(it['project'] ? ' for ' + it['project'] : '') +
(it['context'] ? contextsToList((String) it['context']) : '')
)
if (showNotes && (it['details'] || it['notes'])) {
Tag tag = new Tag('div',)
if (it['details']) {
tag.addChild('div', [style: 'background-color: rgb(240,250,240);font-size:10pt']).addPreformatted((String) it['details'])
}
if (it['notes']) {
tag.addChild('div', [style: 'background-color: rgb(250,250,240);font-size:10pt']).addPreformatted((String) it['notes'])
}
wrap.addContent(tag)
}
}
}
return body.toString()
}
}