1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.ui.element;
|
5
|
|
6
|
import java.awt.Point;
|
7
|
import java.io.IOException;
|
8
|
import java.util.Set;
|
9
|
import java.util.regex.Matcher;
|
10
|
import java.util.regex.Pattern;
|
11
|
|
12
|
import javax.xml.parsers.ParserConfigurationException;
|
13
|
|
14
|
import org.apache.commons.lang.StringUtils;
|
15
|
import org.eclipse.jface.dialogs.MessageDialog;
|
16
|
import org.eclipse.jface.layout.GridDataFactory;
|
17
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
18
|
import org.eclipse.jface.viewers.CellEditor.LayoutData;
|
19
|
import org.eclipse.nebula.widgets.richtext.RichTextEditor;
|
20
|
import org.eclipse.nebula.widgets.richtext.RichTextEditorConfiguration;
|
21
|
import org.eclipse.nebula.widgets.richtext.toolbar.ToolbarButton;
|
22
|
import org.eclipse.swt.SWT;
|
23
|
import org.eclipse.swt.events.KeyAdapter;
|
24
|
import org.eclipse.swt.events.KeyEvent;
|
25
|
import org.eclipse.swt.events.ModifyEvent;
|
26
|
import org.eclipse.swt.events.ModifyListener;
|
27
|
import org.eclipse.swt.events.SelectionAdapter;
|
28
|
import org.eclipse.swt.events.SelectionEvent;
|
29
|
import org.eclipse.swt.graphics.Color;
|
30
|
import org.eclipse.swt.layout.GridLayout;
|
31
|
import org.eclipse.swt.layout.RowLayout;
|
32
|
import org.eclipse.swt.widgets.Button;
|
33
|
import org.eclipse.swt.widgets.Composite;
|
34
|
import org.eclipse.swt.widgets.Control;
|
35
|
import org.eclipse.swt.widgets.Label;
|
36
|
import org.eclipse.swt.widgets.Listener;
|
37
|
import org.eclipse.swt.widgets.Text;
|
38
|
import org.eclipse.ui.dialogs.SelectionDialog;
|
39
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
40
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
41
|
|
42
|
|
43
|
|
44
|
|
45
|
|
46
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
47
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
48
|
import eu.etaxonomy.cdm.model.common.IIntextReferenceTarget;
|
49
|
import eu.etaxonomy.cdm.model.common.IntextReference;
|
50
|
import eu.etaxonomy.cdm.model.common.Language;
|
51
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
52
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
53
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
54
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
55
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
56
|
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
|
57
|
|
58
|
/**
|
59
|
* @author n.hoffmann
|
60
|
* @version $Id: $
|
61
|
*/
|
62
|
public class RichTextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
|
63
|
ISelectable {
|
64
|
|
65
|
|
66
|
protected RichTextEditor text;
|
67
|
|
68
|
public void setLanguageString(LanguageString languageString) {
|
69
|
if (languageString == null){
|
70
|
this.languageString = LanguageString.NewInstance("", Language.DEFAULT());
|
71
|
}
|
72
|
this.languageString = languageString;
|
73
|
|
74
|
|
75
|
}
|
76
|
|
77
|
private LanguageString languageString ;
|
78
|
|
79
|
EntitySelectionElement<Reference> selection_reference;
|
80
|
|
81
|
private Label label;
|
82
|
|
83
|
private final boolean isMultiLine;
|
84
|
|
85
|
public static final int MAX_HEIGHT = 0;
|
86
|
public static final int SINGLE = -1;
|
87
|
|
88
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, boolean isMultiLine) {
|
89
|
super(formFactory, parentElement);
|
90
|
this.isMultiLine = isMultiLine;
|
91
|
}
|
92
|
|
93
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
94
|
LanguageString initialLanguageString, Integer textHeight, int style) {
|
95
|
this(formFactory, parentElement, labelString, initialLanguageString, textHeight, null, true, style);
|
96
|
}
|
97
|
|
98
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
99
|
LanguageString initialText, Integer textHeight, boolean isMultiLine, int style) {
|
100
|
this(formFactory, parentElement, labelString, initialText, textHeight, null, isMultiLine, style);
|
101
|
}
|
102
|
|
103
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
104
|
LanguageString initialText, Integer textHeight, Integer textLimit, int style) {
|
105
|
this(formFactory, parentElement, labelString, initialText, textHeight, textLimit, false, style);
|
106
|
}
|
107
|
|
108
|
protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
109
|
LanguageString initialLanguageString, Integer textHeight, Integer textLimit, boolean isMultiLine, int style) {
|
110
|
super(formFactory, parentElement);
|
111
|
|
112
|
this.isMultiLine = isMultiLine;
|
113
|
this.languageString = initialLanguageString;
|
114
|
|
115
|
initLabel(formFactory, labelString, isMultiLine, getLayoutComposite());
|
116
|
if (initialLanguageString != null){
|
117
|
initText( initialLanguageString.getText(), textHeight, textLimit, isMultiLine, style, getLayoutComposite());
|
118
|
}else{
|
119
|
initText( "", textHeight, textLimit, isMultiLine, style, getLayoutComposite());
|
120
|
|
121
|
}
|
122
|
formFactory.addPropertyChangeListener(this);
|
123
|
}
|
124
|
|
125
|
protected void initText(String initialText, Integer textHeight, Integer textLimit,
|
126
|
boolean isMultiLine, int style, Composite layoutComposite) {
|
127
|
int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
|
128
|
|
129
|
int combinedStyle = style | SWT.BORDER | scrollStyle;
|
130
|
|
131
|
// SWT.PASSWORD does not work when SWT.WRAP is set.
|
132
|
if (style != SWT.PASSWORD) {
|
133
|
combinedStyle = combinedStyle | SWT.WRAP;
|
134
|
}
|
135
|
|
136
|
text = formFactory.createRichTextEditor(layoutComposite, "", combinedStyle);
|
137
|
String browsertype = text.getEditorConfiguration().getBrowser().getBrowserType();
|
138
|
|
139
|
|
140
|
addControl(text);
|
141
|
|
142
|
// text.setWO
|
143
|
|
144
|
// if (textHeight == null) {
|
145
|
// text.addKeyListener(new KeyAdapter() {
|
146
|
// @Override
|
147
|
// public void keyPressed(KeyEvent e) {
|
148
|
// if (e.character == SWT.CR) {
|
149
|
// // Don't accept carriage returns as input when in single
|
150
|
// // line mode
|
151
|
// e.doit = false;
|
152
|
// } else if (e.character == SWT.TAB) {
|
153
|
// // traverse is not working for wrapped text widgets so
|
154
|
// // we reintroduce it here
|
155
|
// e.doit = false;
|
156
|
// text.traverse(SWT.TRAVERSE_TAB_NEXT);
|
157
|
// }
|
158
|
// }
|
159
|
// });
|
160
|
// }
|
161
|
|
162
|
TableWrapData layoutData;
|
163
|
if(isMultiLine){
|
164
|
layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
|
165
|
}
|
166
|
else{
|
167
|
layoutData = LayoutConstants.FILL();
|
168
|
}
|
169
|
if (textHeight != null && textHeight > 0) {
|
170
|
(layoutData).heightHint = textHeight;
|
171
|
}
|
172
|
|
173
|
text.setLayoutData(layoutData);
|
174
|
|
175
|
text.addModifyListener(this);
|
176
|
|
177
|
|
178
|
setText();
|
179
|
}
|
180
|
|
181
|
//
|
182
|
|
183
|
protected void initLabel(CdmFormFactory formFactory, String labelString, boolean isMultiLine, Composite layoutComposite) {
|
184
|
if (labelString != null) {
|
185
|
label = formFactory.createLabel(layoutComposite, CdmUtils.Nz(labelString), SWT.NULL);
|
186
|
addControl(label);
|
187
|
if(isMultiLine){
|
188
|
label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
189
|
((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
|
190
|
}
|
191
|
else{
|
192
|
label.setLayoutData(LayoutConstants.LEFT());
|
193
|
((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
|
194
|
}
|
195
|
}
|
196
|
}
|
197
|
|
198
|
|
199
|
|
200
|
/** {@inheritDoc} */
|
201
|
@Override
|
202
|
public void setBackground(Color color) {
|
203
|
if (label != null) {
|
204
|
label.setBackground(color);
|
205
|
}
|
206
|
}
|
207
|
|
208
|
@Override
|
209
|
public void setSelected(boolean selected) {
|
210
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
211
|
}
|
212
|
|
213
|
@Override
|
214
|
public void setIrrelevant(boolean irrelevant) {
|
215
|
// TODO Auto-generated method stub
|
216
|
|
217
|
}
|
218
|
|
219
|
@Override
|
220
|
public void setEnabled(boolean enabled) {
|
221
|
// TODO Auto-generated method stub
|
222
|
|
223
|
}
|
224
|
|
225
|
@Override
|
226
|
public boolean isEnabled() {
|
227
|
// TODO Auto-generated method stub
|
228
|
return false;
|
229
|
}
|
230
|
|
231
|
@Override
|
232
|
public void modifyText(ModifyEvent e) {
|
233
|
if(languageString == null){
|
234
|
languageString = LanguageString.NewInstance("", CdmStore.getDefaultLanguage());
|
235
|
}
|
236
|
|
237
|
languageString.setText(getText());
|
238
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
239
|
}
|
240
|
|
241
|
public LanguageString getLanguageString(){
|
242
|
return languageString;
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Set the text of this composites text composite
|
247
|
*
|
248
|
* @param string
|
249
|
* a {@link java.lang.String} object.
|
250
|
*/
|
251
|
|
252
|
public void setText() {
|
253
|
|
254
|
|
255
|
String convertedString = "";
|
256
|
|
257
|
if (languageString != null){
|
258
|
Set<IntextReference> inrefs = this.languageString.getIntextReferences();
|
259
|
convertedString = languageString.getText();
|
260
|
String inLineString = null;
|
261
|
String innerText = null;
|
262
|
String htmlString = null;
|
263
|
boolean converted = true;
|
264
|
if (!languageString.getText().contains("<cdm")){
|
265
|
text.setText(languageString.getText());
|
266
|
return;
|
267
|
}
|
268
|
if (inrefs == null || inrefs.size() == 0){
|
269
|
text.setText(CdmUtils.Nz(languageString.getText()));
|
270
|
}else{
|
271
|
for (IntextReference inref: inrefs){
|
272
|
|
273
|
if (inref.getTarget() == null){
|
274
|
break;
|
275
|
}
|
276
|
|
277
|
inLineString = findInlineString(convertedString, inref.toInlineString(".*"));
|
278
|
innerText = findInText(inLineString, "(<cdm:.+>)(.+)(</cdm:.+>)");
|
279
|
if (innerText != null){
|
280
|
htmlString = createHtml(inref, innerText);
|
281
|
}else{
|
282
|
inLineString =findInlineString(convertedString, "(<cdm:.+/>)");
|
283
|
htmlString = createHtml(inref, innerText);
|
284
|
}
|
285
|
if (inLineString != null){
|
286
|
convertedString = convertedString.replace(inLineString, htmlString);
|
287
|
System.out.println("new text after converting "+convertedString + " - " + languageString.getText());
|
288
|
}else{
|
289
|
converted = false;
|
290
|
}
|
291
|
|
292
|
}
|
293
|
text.setText(convertedString);
|
294
|
if(!converted){
|
295
|
System.err.println("There is a problem with the language string and an intextrefernce.");
|
296
|
}
|
297
|
|
298
|
}
|
299
|
}
|
300
|
|
301
|
// Listener[] listeners = text.getListeners(SWT.Modify);
|
302
|
//
|
303
|
// text.removeModifyListener(this);
|
304
|
// text.addModifyListener(this);
|
305
|
// for (Listener listener : listeners) {
|
306
|
// text.removeListener(SWT.Modify, listener);
|
307
|
// }
|
308
|
//
|
309
|
//
|
310
|
//
|
311
|
// for (Listener listener : listeners) {
|
312
|
// text.addListener(SWT.Modify, listener);
|
313
|
// }
|
314
|
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* Set the text of this composites text composite
|
319
|
*
|
320
|
* @param string
|
321
|
* a {@link java.lang.String} object.
|
322
|
*/
|
323
|
public String getText() {
|
324
|
|
325
|
|
326
|
String convertedString = "";
|
327
|
|
328
|
if (this.languageString != null){
|
329
|
Set<IntextReference> inrefs = this.languageString.getIntextReferences();
|
330
|
String inLineString = null;
|
331
|
String innerText = null;
|
332
|
String htmlString = null;
|
333
|
convertedString = text.getText();
|
334
|
System.out.println("before converting html to markup: "+convertedString);
|
335
|
for (IntextReference inref: inrefs){
|
336
|
if (inref.getTarget() == null){
|
337
|
break;
|
338
|
}
|
339
|
|
340
|
htmlString = findHtmlString(text.getText(), inref);
|
341
|
System.out.println("html"+htmlString);
|
342
|
if (htmlString != null){
|
343
|
innerText = findInText(htmlString, "(.+>)(.*)(</span>)");
|
344
|
inLineString = createCdmMarkUp(inref, innerText);
|
345
|
|
346
|
convertedString = convertedString.replace(htmlString, inLineString);
|
347
|
|
348
|
}else{
|
349
|
convertedString = text.getText();
|
350
|
}
|
351
|
}
|
352
|
}
|
353
|
return convertedString;
|
354
|
|
355
|
|
356
|
|
357
|
|
358
|
}
|
359
|
|
360
|
|
361
|
private String findHtmlString(String htmlText, IntextReference inRef) {
|
362
|
String htmlString = createHtml(inRef, ".*");
|
363
|
Pattern pattern = Pattern.compile(htmlString);
|
364
|
Matcher matcher = pattern.matcher(htmlText);
|
365
|
if (matcher.find())
|
366
|
{
|
367
|
String result = matcher.group();
|
368
|
return result;
|
369
|
}
|
370
|
return null;
|
371
|
|
372
|
|
373
|
}
|
374
|
|
375
|
private String createCdmMarkUp(IntextReference inRef, String innerText){
|
376
|
return inRef.toInlineString(innerText);
|
377
|
|
378
|
}
|
379
|
|
380
|
private static final String CDM_PREFIX = "cdm:";
|
381
|
private String createHtml(IntextReference inRef, String innerString){
|
382
|
/*
|
383
|
*
|
384
|
* <span
|
385
|
class="cdm:IntextReference"
|
386
|
data-cdm-referenced-type="reference"
|
387
|
data-cdm-referenced-uuid="38567a90-bcae-4f76-b148-21fcae649a85'"
|
388
|
data-cdm-intext-uuid="'d427802f-ea22-4d10-b90b-fa90aa0092ed">
|
389
|
innerString
|
390
|
</span>
|
391
|
|
392
|
*/
|
393
|
String type = " data-cdm-referenced-type=\"" + inRef.myClass().tag() + "\"";
|
394
|
IIntextReferenceTarget entity = inRef.getTarget();
|
395
|
String attributes = " data-cdm-referenced-uuid=\"" + entity.getUuid() + "\" data-cdm-intext-uuid=\"" + inRef.getUuid() + "\"" ;
|
396
|
String result;
|
397
|
if (StringUtils.isNotEmpty(innerString)){
|
398
|
result = "<span \n class=\"cdm:IntextReference\"" + type + attributes + ">" + innerString + "</" + "span" + ">";
|
399
|
}else{
|
400
|
result ="<span \n class=\"cdm:IntextReference\"" + type + attributes + "/>";
|
401
|
}
|
402
|
return result;
|
403
|
|
404
|
|
405
|
}
|
406
|
|
407
|
private String findInlineString(String text, String inlineString){
|
408
|
if (text == null){
|
409
|
return null;
|
410
|
}
|
411
|
Pattern pattern = Pattern.compile(inlineString);
|
412
|
Matcher matcher = pattern.matcher(text);
|
413
|
String result = null;
|
414
|
if (matcher.find()){
|
415
|
result = matcher.group();
|
416
|
System.out.println(result);
|
417
|
for (int i = 0; i<matcher.groupCount(); i++){
|
418
|
if (i == 0){
|
419
|
result= matcher.group(i);
|
420
|
}
|
421
|
System.out.println(matcher.group(i));
|
422
|
}
|
423
|
|
424
|
|
425
|
|
426
|
}
|
427
|
return result;
|
428
|
}
|
429
|
|
430
|
private String findInText(String text, String inlineString){
|
431
|
if (text == null){
|
432
|
return null;
|
433
|
}
|
434
|
|
435
|
Pattern pattern = Pattern.compile(inlineString);
|
436
|
Matcher matcher = pattern.matcher(text);
|
437
|
if (matcher.find())
|
438
|
{
|
439
|
String result = matcher.group(1);
|
440
|
return matcher.group(2);
|
441
|
}
|
442
|
return null;
|
443
|
}
|
444
|
|
445
|
|
446
|
|
447
|
|
448
|
}
|
449
|
|
450
|
|
451
|
|
452
|
|
453
|
|
454
|
|
455
|
|
456
|
|